3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
33 class CRM_Report_BAO_ReportInstance
extends CRM_Report_DAO_ReportInstance
{
36 * Takes an associative array and creates an instance object.
38 * the function extract all the params it needs to initialize the create a
39 * instance object. the params array could contain additional unused name/value
42 * @param array $params
43 * (reference ) an assoc array of name/value pairs.
45 * @return CRM_Report_DAO_ReportInstance
47 public static function add(&$params) {
48 $instance = new CRM_Report_DAO_ReportInstance();
53 $instanceID = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('instance_id', $params));
55 // convert roles array to string
56 if (isset($params['grouprole']) && is_array($params['grouprole'])) {
57 $grouprole_array = array();
58 foreach ($params['grouprole'] as $key => $value) {
59 $grouprole_array[$value] = $value;
61 $params['grouprole'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
62 array_keys($grouprole_array)
66 if (!$instanceID ||
!isset($params['id'])) {
67 $params['is_reserved'] = CRM_Utils_Array
::value('is_reserved', $params, FALSE);
68 $params['domain_id'] = CRM_Utils_Array
::value('domain_id', $params, CRM_Core_Config
::domainID());
69 // CRM-17256 set created_id on report creation.
70 $params['created_id'] = isset($params['created_id']) ?
$params['created_id'] : CRM_Core_Session
::getLoggedInContactID();
74 CRM_Utils_Hook
::pre('edit', 'ReportInstance', $instanceID, $params);
77 CRM_Utils_Hook
::pre('create', 'ReportInstance', NULL, $params);
80 $instance = new CRM_Report_DAO_ReportInstance();
81 $instance->copyValues($params);
83 if (CRM_Core_Config
::singleton()->userFramework
== 'Joomla') {
84 $instance->permission
= 'null';
87 // explicitly set to null if params value is empty
88 if (!$instanceID && empty($params['grouprole'])) {
89 $instance->grouprole
= 'null';
93 $instance->id
= $instanceID;
97 if ($reportID = CRM_Utils_Array
::value('report_id', $params)) {
98 $instance->report_id
= $reportID;
100 elseif ($instanceID) {
101 $instance->report_id
= CRM_Report_Utils_Report
::getValueFromUrl($instanceID);
104 // just take it from current url
105 $instance->report_id
= CRM_Report_Utils_Report
::getValueFromUrl();
112 CRM_Utils_Hook
::pre('edit', 'ReportInstance', $instance->id
, $instance);
115 CRM_Utils_Hook
::pre('create', 'ReportInstance', $instance->id
, $instance);
122 * takes an associative array and creates a instance object and does any related work like permissioning, adding to dashboard etc.
124 * This function is invoked from within the web form layer and also from the api layer
126 * @param array $params
127 * (reference ) an assoc array of name/value pairs.
129 * @return CRM_Report_BAO_ReportInstance
131 public static function &create(&$params) {
132 if (isset($params['report_header'])) {
133 $params['header'] = CRM_Utils_Array
::value('report_header', $params);
135 if (isset($params['report_footer'])) {
136 $params['footer'] = CRM_Utils_Array
::value('report_footer', $params);
139 // build navigation parameters
140 if (!empty($params['is_navigation'])) {
141 if (!array_key_exists('navigation', $params)) {
142 $params['navigation'] = array();
144 $navigationParams =& $params['navigation'];
146 $navigationParams['permission'] = array();
147 $navigationParams['label'] = $params['title'];
148 $navigationParams['name'] = $params['title'];
150 $navigationParams['current_parent_id'] = CRM_Utils_Array
::value('parent_id', $navigationParams);
151 $navigationParams['parent_id'] = CRM_Utils_Array
::value('parent_id', $params);
152 $navigationParams['is_active'] = 1;
154 if ($permission = CRM_Utils_Array
::value('permission', $params)) {
155 $navigationParams['permission'][] = $permission;
158 // unset the navigation related elements, not used in report form values
159 unset($params['parent_id']);
160 unset($params['is_navigation']);
163 $viewMode = !empty($params['view_mode']) ?
$params['view_mode'] : FALSE;
165 // Do not save to the DB - it's saved in the url.
166 unset($params['view_mode']);
170 $dashletParams = array();
171 if (!empty($params['addToDashboard'])) {
172 $dashletParams = array(
173 'label' => $params['title'],
176 if ($permission = CRM_Utils_Array
::value('permission', $params)) {
177 $dashletParams['permission'][] = $permission;
181 $transaction = new CRM_Core_Transaction();
183 $instance = self
::add($params);
184 if (is_a($instance, 'CRM_Core_Error')) {
185 $transaction->rollback();
189 // add / update navigation as required
190 if (!empty($navigationParams)) {
191 if (empty($params['id']) && empty($params['instance_id']) && !empty($navigationParams['id'])) {
192 unset($navigationParams['id']);
194 $navigationParams['url'] = "civicrm/report/instance/{$instance->id}" . ($viewMode == 'view' ?
'?reset=1&force=1' : '?reset=1&output=criteria');
195 $navigation = CRM_Core_BAO_Navigation
::add($navigationParams);
197 if (!empty($navigationParams['is_active'])) {
198 //set the navigation id in report instance table
199 CRM_Core_DAO
::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id
, 'navigation_id', $navigation->id
);
202 // has been removed from the navigation bar
203 CRM_Core_DAO
::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id
, 'navigation_id', 'NULL');
206 CRM_Core_BAO_Navigation
::resetNavigation();
210 if (!empty($dashletParams)) {
213 if (!empty($params['charts'])) {
215 $chart = "&charts=" . $params['charts'];
218 if (!empty($params['row_count'])) {
219 $limitResult = '&rowCount=' . $params['row_count'];
221 $dashletParams['name'] = "report/{$instance->id}";
222 $dashletParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1§ion={$section}&snippet=5{$chart}&context=dashlet" . $limitResult;
223 $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}?reset=1§ion={$section}&snippet=5{$chart}&context=dashletFullscreen" . $limitResult;
224 $dashletParams['instanceURL'] = "civicrm/report/instance/{$instance->id}";
225 CRM_Core_BAO_Dashboard
::addDashlet($dashletParams);
227 $transaction->commit();
233 * Delete the instance of the Report.
238 * $results no of deleted Instance on success, false otherwise
240 public static function del($id = NULL) {
241 $dao = new CRM_Report_DAO_ReportInstance();
243 return $dao->delete();
249 * @param array $params
250 * @param array $defaults
252 * @return CRM_Report_DAO_ReportInstance|null
254 public static function retrieve($params, &$defaults) {
255 $instance = new CRM_Report_DAO_ReportInstance();
256 $instance->copyValues($params);
258 if ($instance->find(TRUE)) {
259 CRM_Core_DAO
::storeValues($instance, $defaults);