Merge pull request #4627 from colemanw/docblocks
[civicrm-core.git] / CRM / Report / BAO / ReportInstance.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
06b69b18 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
06b69b18 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27*/
28
29/**
30 *
31 * @package CRM
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
0b25329b 36class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance {
6a488035 37
6ea7a229
DS
38 /**
39 * takes an associative array and creates an instance object
40 *
41 * the function extract all the params it needs to initialize the create a
42 * instance object. the params array could contain additional unused name/value
43 * pairs
44 *
45 * @param array $params (reference ) an assoc array of name/value pairs
46 *
c490a46a 47 * @return CRM_Report_DAO_ReportInstance object
6ea7a229
DS
48 * @access public
49 * @static
50 */
51 static function add(&$params) {
0b25329b 52 $instance = new CRM_Report_DAO_ReportInstance();
6ea7a229
DS
53 if (empty($params)) {
54 return NULL;
55 }
56
edb1d052 57 $instanceID = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('instance_id', $params));
6ea7a229
DS
58
59 // convert roles array to string
60 if (isset($params['grouprole']) && is_array($params['grouprole'])) {
61 $grouprole_array = array();
62 foreach ($params['grouprole'] as $key => $value) {
63 $grouprole_array[$value] = $value;
64 }
65 $params['grouprole'] = implode(CRM_Core_DAO::VALUE_SEPARATOR,
66 array_keys($grouprole_array)
67 );
68 }
69
08f06889 70 if (!$instanceID || !isset($params['id'])) {
0bbbbae1 71 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
edb1d052 72 $params['domain_id'] = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
6ea7a229
DS
73 }
74
75 if ($instanceID) {
76 CRM_Utils_Hook::pre('edit', 'ReportInstance', $instanceID, $params);
77 }
78 else {
79 CRM_Utils_Hook::pre('create', 'ReportInstance', NULL, $params);
80 }
81
0b25329b 82 $instance = new CRM_Report_DAO_ReportInstance();
6ea7a229
DS
83 $instance->copyValues($params);
84
edb1d052 85 if (CRM_Core_Config::singleton()->userFramework == 'Joomla') {
6ea7a229
DS
86 $instance->permission = 'null';
87 }
88
89 // explicitly set to null if params value is empty
0bbbbae1 90 if (!$instanceID && empty($params['grouprole'])) {
6ea7a229
DS
91 $instance->grouprole = 'null';
92 }
93
94 if ($instanceID) {
95 $instance->id = $instanceID;
96 }
97
a8d9a461
TO
98 if (! $instanceID) {
99 if ($reportID = CRM_Utils_Array::value('report_id', $params)) {
100 $instance->report_id = $reportID;
101 } else if ($instanceID) {
102 $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl($instanceID);
103 } else {
104 // just take it from current url
105 $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl();
106 }
6ea7a229 107 }
a8d9a461 108
6ea7a229 109 $instance->save();
2efcf0c2 110
6ea7a229
DS
111 if ($instanceID) {
112 CRM_Utils_Hook::pre('edit', 'ReportInstance', $instance->id, $instance);
113 }
114 else {
115 CRM_Utils_Hook::pre('create', 'ReportInstance', $instance->id, $instance);
116 }
117 return $instance;
118 }
119
120 /**
c490a46a 121 * create instance
6ea7a229
DS
122 * takes an associative array and creates a instance object and does any related work like permissioning, adding to dashboard etc.
123 *
124 * This function is invoked from within the web form layer and also from the api layer
125 *
126 * @param array $params (reference ) an assoc array of name/value pairs
127 *
c490a46a 128 * @return CRM_Report_BAO_ReportInstance object
6ea7a229
DS
129 * @access public
130 * @static
131 */
132 static function &create(&$params) {
0bbbbae1
TO
133 if (isset($params['report_header'])) {
134 $params['header'] = CRM_Utils_Array::value('report_header',$params);
135 }
136 if (isset($params['report_footer'])) {
137 $params['footer'] = CRM_Utils_Array::value('report_footer',$params);
138 }
6ea7a229
DS
139
140 // build navigation parameters
a7488080 141 if (!empty($params['is_navigation'])) {
6ea7a229
DS
142 if (!array_key_exists('navigation', $params)) {
143 $params['navigation'] = array();
144 }
145 $navigationParams =& $params['navigation'];
146
147 $navigationParams['permission'] = array();
148 $navigationParams['label'] = $params['title'];
149 $navigationParams['name'] = $params['title'];
150
151 $navigationParams['current_parent_id'] = CRM_Utils_Array::value('parent_id', $navigationParams);
152 $navigationParams['parent_id'] = CRM_Utils_Array::value('parent_id', $params);
153 $navigationParams['is_active'] = 1;
154
155 if ($permission = CRM_Utils_Array::value('permission', $params)) {
156 $navigationParams['permission'][] = $permission;
157 }
158
159 // unset the navigation related elements, not used in report form values
160 unset($params['parent_id']);
161 unset($params['is_navigation']);
162 }
163
164 // add to dashboard
165 $dashletParams = array();
a7488080 166 if (!empty($params['addToDashboard'])) {
6ea7a229
DS
167 $dashletParams = array(
168 'label' => $params['title'],
169 'is_active' => 1,
170 );
171 if ($permission = CRM_Utils_Array::value('permission', $params)) {
172 $dashletParams['permission'][] = $permission;
173 }
174 }
175
176 $transaction = new CRM_Core_Transaction();
177
178 $instance = self::add($params);
179 if (is_a($instance, 'CRM_Core_Error')) {
180 $transaction->rollback();
181 return $instance;
182 }
183
184 // add / update navigation as required
185 if (!empty($navigationParams)) {
8cc574cf 186 if (empty($params['id']) && empty($params['instance_id']) && !empty($navigationParams['id'])) {
6ea7a229
DS
187 unset($navigationParams['id']);
188 }
f37ea1c8 189 $navigationParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1";
6ea7a229
DS
190 $navigation = CRM_Core_BAO_Navigation::add($navigationParams);
191
a7488080 192 if (!empty($navigationParams['is_active'])) {
6ea7a229 193 //set the navigation id in report instance table
0b25329b 194 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', $navigation->id);
6ea7a229
DS
195 }
196 else {
197 // has been removed from the navigation bar
0b25329b 198 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', 'NULL');
6ea7a229
DS
199 }
200 //reset navigation
201 CRM_Core_BAO_Navigation::resetNavigation();
202 }
203
204 // add to dashlet
205 if (!empty($dashletParams)) {
206 $section = 2;
207 $chart = '';
a7488080 208 if (!empty($params['charts'])) {
6ea7a229
DS
209 $section = 1;
210 $chart = "&charts=" . $params['charts'];
211 }
dbb4a0f9
PN
212 $limitResult = NULL;
213 if (CRM_Utils_Array::value('row_count', $params)) {
214 $limitResult = '&rowCount=' . $params['row_count'];
215 }
db049bcd 216 $dashletParams['name'] = "report/{$instance->id}";
f687ec36
DJ
217 $dashletParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}&snippet=5{$chart}&context=dashlet" . $limitResult;
218 $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}&snippet=5{$chart}&context=dashletFullscreen" . $limitResult;
6ea7a229
DS
219 $dashletParams['instanceURL'] = "civicrm/report/instance/{$instance->id}";
220 CRM_Core_BAO_Dashboard::addDashlet($dashletParams);
221 }
222 $transaction->commit();
223
224 return $instance;
225 }
226
6a488035
TO
227 /**
228 * Delete the instance of the Report
229 *
dd244018 230 * @param null $id
6a488035 231 *
dd244018 232 * @return mixed $results no of deleted Instance on success, false otherwise@access public
6a488035 233 */
0b25329b
DS
234 static function del($id = NULL) {
235 $dao = new CRM_Report_DAO_ReportInstance();
6a488035
TO
236 $dao->id = $id;
237 return $dao->delete();
238 }
239
74cf4551 240 /**
c490a46a 241 * @param array $params
74cf4551
EM
242 * @param $defaults
243 *
244 * @return CRM_Report_DAO_ReportInstance|null
245 */
6a488035 246 static function retrieve($params, &$defaults) {
0b25329b 247 $instance = new CRM_Report_DAO_ReportInstance();
6a488035
TO
248 $instance->copyValues($params);
249
250 if ($instance->find(TRUE)) {
251 CRM_Core_DAO::storeValues($instance, $defaults);
252 $instance->free();
253 return $instance;
254 }
255 return NULL;
256 }
257}