Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 | 32 | */ |
0b25329b | 33 | class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance { |
6a488035 | 34 | |
6ea7a229 | 35 | /** |
fe482240 | 36 | * Takes an associative array and creates an instance object. |
6ea7a229 DS |
37 | * |
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 | |
40 | * pairs | |
41 | * | |
7e06c9f5 TO |
42 | * @param array $params |
43 | * (reference ) an assoc array of name/value pairs. | |
6ea7a229 | 44 | * |
16b10e64 | 45 | * @return CRM_Report_DAO_ReportInstance |
6ea7a229 | 46 | */ |
00be9182 | 47 | public static function add(&$params) { |
0b25329b | 48 | $instance = new CRM_Report_DAO_ReportInstance(); |
6ea7a229 DS |
49 | if (empty($params)) { |
50 | return NULL; | |
51 | } | |
52 | ||
edb1d052 | 53 | $instanceID = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('instance_id', $params)); |
6ea7a229 DS |
54 | |
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; | |
60 | } | |
61 | $params['grouprole'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
62 | array_keys($grouprole_array) | |
63 | ); | |
64 | } | |
65 | ||
08f06889 | 66 | if (!$instanceID || !isset($params['id'])) { |
0bbbbae1 | 67 | $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE); |
edb1d052 | 68 | $params['domain_id'] = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID()); |
aaf5f726 | 69 | // CRM-17256 set created_id on report creation. |
70 | $params['created_id'] = isset($params['created_id']) ? $params['created_id'] : CRM_Core_Session::getLoggedInContactID(); | |
6ea7a229 DS |
71 | } |
72 | ||
73 | if ($instanceID) { | |
74 | CRM_Utils_Hook::pre('edit', 'ReportInstance', $instanceID, $params); | |
75 | } | |
76 | else { | |
77 | CRM_Utils_Hook::pre('create', 'ReportInstance', NULL, $params); | |
78 | } | |
79 | ||
0b25329b | 80 | $instance = new CRM_Report_DAO_ReportInstance(); |
6ea7a229 DS |
81 | $instance->copyValues($params); |
82 | ||
edb1d052 | 83 | if (CRM_Core_Config::singleton()->userFramework == 'Joomla') { |
6ea7a229 DS |
84 | $instance->permission = 'null'; |
85 | } | |
86 | ||
87 | // explicitly set to null if params value is empty | |
0bbbbae1 | 88 | if (!$instanceID && empty($params['grouprole'])) { |
6ea7a229 DS |
89 | $instance->grouprole = 'null'; |
90 | } | |
91 | ||
92 | if ($instanceID) { | |
93 | $instance->id = $instanceID; | |
94 | } | |
95 | ||
353ffa53 | 96 | if (!$instanceID) { |
a8d9a461 TO |
97 | if ($reportID = CRM_Utils_Array::value('report_id', $params)) { |
98 | $instance->report_id = $reportID; | |
0db6c3e1 | 99 | } |
4c9b6178 | 100 | elseif ($instanceID) { |
a8d9a461 | 101 | $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl($instanceID); |
0db6c3e1 TO |
102 | } |
103 | else { | |
a8d9a461 TO |
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 | /** | |
fe482240 | 121 | * Create instance. |
182f5081 | 122 | * |
6ea7a229 DS |
123 | * takes an associative array and creates a instance object and does any related work like permissioning, adding to dashboard etc. |
124 | * | |
125 | * This function is invoked from within the web form layer and also from the api layer | |
126 | * | |
7e06c9f5 TO |
127 | * @param array $params |
128 | * (reference ) an assoc array of name/value pairs. | |
6ea7a229 | 129 | * |
16b10e64 | 130 | * @return CRM_Report_BAO_ReportInstance |
6ea7a229 | 131 | */ |
00be9182 | 132 | public static function &create(&$params) { |
0bbbbae1 | 133 | if (isset($params['report_header'])) { |
353ffa53 | 134 | $params['header'] = CRM_Utils_Array::value('report_header', $params); |
0bbbbae1 TO |
135 | } |
136 | if (isset($params['report_footer'])) { | |
353ffa53 | 137 | $params['footer'] = CRM_Utils_Array::value('report_footer', $params); |
0bbbbae1 | 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']; | |
353ffa53 | 149 | $navigationParams['name'] = $params['title']; |
6ea7a229 DS |
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 | ||
e6e7e540 | 164 | $viewMode = !empty($params['view_mode']) ? $params['view_mode'] : FALSE; |
165 | if ($viewMode) { | |
166 | // Do not save to the DB - it's saved in the url. | |
167 | unset($params['view_mode']); | |
168 | } | |
169 | ||
6ea7a229 DS |
170 | // add to dashboard |
171 | $dashletParams = array(); | |
a7488080 | 172 | if (!empty($params['addToDashboard'])) { |
6ea7a229 DS |
173 | $dashletParams = array( |
174 | 'label' => $params['title'], | |
175 | 'is_active' => 1, | |
176 | ); | |
177 | if ($permission = CRM_Utils_Array::value('permission', $params)) { | |
178 | $dashletParams['permission'][] = $permission; | |
179 | } | |
180 | } | |
181 | ||
182 | $transaction = new CRM_Core_Transaction(); | |
183 | ||
184 | $instance = self::add($params); | |
185 | if (is_a($instance, 'CRM_Core_Error')) { | |
186 | $transaction->rollback(); | |
187 | return $instance; | |
188 | } | |
189 | ||
190 | // add / update navigation as required | |
191 | if (!empty($navigationParams)) { | |
8cc574cf | 192 | if (empty($params['id']) && empty($params['instance_id']) && !empty($navigationParams['id'])) { |
6ea7a229 DS |
193 | unset($navigationParams['id']); |
194 | } | |
e6e7e540 | 195 | $navigationParams['url'] = "civicrm/report/instance/{$instance->id}" . ($viewMode == 'view' ? '?reset=1&force=1' : '?reset=1&output=criteria'); |
6ea7a229 DS |
196 | $navigation = CRM_Core_BAO_Navigation::add($navigationParams); |
197 | ||
a7488080 | 198 | if (!empty($navigationParams['is_active'])) { |
6ea7a229 | 199 | //set the navigation id in report instance table |
0b25329b | 200 | CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', $navigation->id); |
6ea7a229 DS |
201 | } |
202 | else { | |
203 | // has been removed from the navigation bar | |
0b25329b | 204 | CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', 'NULL'); |
6ea7a229 DS |
205 | } |
206 | //reset navigation | |
207 | CRM_Core_BAO_Navigation::resetNavigation(); | |
208 | } | |
209 | ||
210 | // add to dashlet | |
211 | if (!empty($dashletParams)) { | |
212 | $section = 2; | |
353ffa53 | 213 | $chart = ''; |
a7488080 | 214 | if (!empty($params['charts'])) { |
6ea7a229 | 215 | $section = 1; |
353ffa53 | 216 | $chart = "&charts=" . $params['charts']; |
6ea7a229 | 217 | } |
dbb4a0f9 | 218 | $limitResult = NULL; |
b53cbfbc | 219 | if (!empty($params['row_count'])) { |
dbb4a0f9 PN |
220 | $limitResult = '&rowCount=' . $params['row_count']; |
221 | } | |
db049bcd | 222 | $dashletParams['name'] = "report/{$instance->id}"; |
f687ec36 DJ |
223 | $dashletParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1§ion={$section}&snippet=5{$chart}&context=dashlet" . $limitResult; |
224 | $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}?reset=1§ion={$section}&snippet=5{$chart}&context=dashletFullscreen" . $limitResult; | |
6ea7a229 DS |
225 | $dashletParams['instanceURL'] = "civicrm/report/instance/{$instance->id}"; |
226 | CRM_Core_BAO_Dashboard::addDashlet($dashletParams); | |
227 | } | |
228 | $transaction->commit(); | |
229 | ||
230 | return $instance; | |
231 | } | |
232 | ||
6a488035 | 233 | /** |
fe482240 | 234 | * Delete the instance of the Report. |
6a488035 | 235 | * |
100fef9d | 236 | * @param int $id |
6a488035 | 237 | * |
72b3a70c CW |
238 | * @return mixed |
239 | * $results no of deleted Instance on success, false otherwise | |
6a488035 | 240 | */ |
00be9182 | 241 | public static function del($id = NULL) { |
e3c612db | 242 | $navId = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $id, 'navigation_id', 'id'); |
0b25329b | 243 | $dao = new CRM_Report_DAO_ReportInstance(); |
6a488035 | 244 | $dao->id = $id; |
e3c612db | 245 | $result = $dao->delete(); |
246 | ||
247 | // Delete navigation if exists. | |
248 | if ($navId) { | |
249 | CRM_Core_BAO_Navigation::processDelete($navId); | |
250 | CRM_Core_BAO_Navigation::resetNavigation(); | |
251 | } | |
252 | return $result; | |
6a488035 TO |
253 | } |
254 | ||
74cf4551 | 255 | /** |
98f1f7a1 | 256 | * Retrieve instance. |
257 | * | |
c490a46a | 258 | * @param array $params |
98f1f7a1 | 259 | * @param array $defaults |
74cf4551 EM |
260 | * |
261 | * @return CRM_Report_DAO_ReportInstance|null | |
262 | */ | |
00be9182 | 263 | public static function retrieve($params, &$defaults) { |
0b25329b | 264 | $instance = new CRM_Report_DAO_ReportInstance(); |
6a488035 TO |
265 | $instance->copyValues($params); |
266 | ||
267 | if ($instance->find(TRUE)) { | |
268 | CRM_Core_DAO::storeValues($instance, $defaults); | |
269 | $instance->free(); | |
270 | return $instance; | |
271 | } | |
272 | return NULL; | |
273 | } | |
96025800 | 274 | |
0f8c6e58 | 275 | /** |
276 | * Check if report is private. | |
277 | * | |
278 | * @param int $instance_id | |
279 | * | |
280 | * @return bool | |
281 | */ | |
282 | public static function reportIsPrivate($instance_id) { | |
283 | $owner_id = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instance_id, 'owner_id', 'id'); | |
284 | if ($owner_id) { | |
285 | return TRUE; | |
286 | } | |
287 | return FALSE; | |
288 | } | |
289 | ||
290 | /** | |
291 | * Check if the logged in user is the owner. | |
292 | * | |
293 | * @param int $instance_id | |
294 | * | |
295 | * @return TRUE if contact owns the report, FALSE if not | |
296 | */ | |
297 | public static function contactIsOwner($instance_id) { | |
298 | $session = CRM_Core_Session::singleton(); | |
299 | $contact_id = $session->get('userID'); | |
300 | $owner_id = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instance_id, 'owner_id', 'id'); | |
301 | if ($contact_id === $owner_id) { | |
302 | return TRUE; | |
303 | } | |
304 | return FALSE; | |
305 | } | |
306 | ||
307 | /** | |
308 | * Check if the logged in contact can administer the report. | |
309 | * | |
310 | * @param int $instance_id | |
311 | * | |
312 | * @return bool | |
313 | * True if contact can edit the private report, FALSE if not. | |
314 | */ | |
315 | public static function contactCanAdministerReport($instance_id) { | |
316 | if (self::reportIsPrivate($instance_id)) { | |
317 | if (self::contactIsOwner($instance_id) || CRM_Core_Permission::check('access all private reports')) { | |
318 | return TRUE; | |
319 | } | |
320 | } | |
321 | elseif (CRM_Core_Permission::check('administer Reports')) { | |
322 | return TRUE; | |
323 | } | |
324 | return FALSE; | |
325 | } | |
326 | ||
e3c612db | 327 | /** |
328 | * Delete a report instance wrapped in handling for the form layer. | |
329 | * | |
330 | * @param int $instanceId | |
331 | * @param string $bounceTo | |
332 | * Url to redirect the browser to on fail. | |
333 | * @param string $successRedirect | |
334 | */ | |
335 | public static function doFormDelete($instanceId, $bounceTo = 'civicrm/report/list?reset=1', $successRedirect = NULL) { | |
336 | if (!CRM_Core_Permission::check('administer Reports')) { | |
337 | $statusMessage = ts('You do not have permission to Delete Report.'); | |
338 | CRM_Core_Error::statusBounce($statusMessage, $bounceTo); | |
339 | } | |
340 | ||
341 | CRM_Report_BAO_ReportInstance::del($instanceId); | |
342 | ||
343 | CRM_Core_Session::setStatus(ts('Selected report has been deleted.'), ts('Deleted'), 'success'); | |
344 | if ($successRedirect) { | |
345 | CRM_Utils_System::redirect(CRM_Utils_System::url($successRedirect)); | |
346 | } | |
347 | } | |
348 | ||
44543184 | 349 | /** |
350 | * Get the metadata of actions available for this entity. | |
1a7356e7 | 351 | * |
352 | * The thinking here is to describe the various actions on the BAO and then functions | |
353 | * can add a mix of actions from different BAO as appropriate. The crm.SearchForm.js code | |
354 | * transforms the 'confirm_mesage' into a message that needs to be confirmed. | |
355 | * confirm_refresh_fields need to be reviewed & potentially updated at the confirm stage. | |
356 | * | |
357 | * Ideas not yet implemented: | |
358 | * - supports_modal task can be loaded in a popup, theoretically worked, not attempted. | |
359 | * - class and or icon - per option icons or classes (I added these in addTaskMenu::addTaskMenu | |
360 | * but I didn't have the right classes). ie adding 'class' => 'crm-i fa-print' to print / class looked | |
361 | * wrong, but at the php level it worked https://github.com/civicrm/civicrm-core/pull/8529#issuecomment-227639091 | |
362 | * - general script-add. | |
44543184 | 363 | */ |
364 | public static function getActionMetadata() { | |
365 | $actions = array( | |
e5227b8d | 366 | 'report_instance.save' => array('title' => ts('Save')), |
1a7356e7 | 367 | 'report_instance.copy' => array( |
368 | 'title' => ts('Save a Copy'), | |
369 | 'data' => array( | |
370 | 'is_confirm' => TRUE, | |
371 | 'confirm_title' => ts('Save a copy...'), | |
372 | 'confirm_refresh_fields' => json_encode(array( | |
373 | 'title' => array('selector' => '.crm-report-instanceForm-form-block-title', 'prepend' => ts('(Copy) ')), | |
374 | 'description' => array('selector' => '.crm-report-instanceForm-form-block-description', 'prepend' => ''), | |
375 | 'parent_id' => array('selector' => '.crm-report-instanceForm-form-block-parent_id', 'prepend' => ''), | |
376 | )), | |
377 | ), | |
378 | ), | |
379 | 'report_instance.print' => array('title' => ts('Print Report')), | |
44543184 | 380 | 'report_instance.pdf' => array('title' => ts('Print to PDF')), |
381 | 'report_instance.csv' => array('title' => ts('Export as CSV')), | |
382 | ); | |
383 | if (CRM_Core_Permission::check('administer Reports')) { | |
384 | $actions['report_instance.delete'] = array( | |
385 | 'title' => ts('Delete report'), | |
1a7356e7 | 386 | 'data' => array( |
387 | 'is_confirm' => TRUE, | |
388 | 'confirm_message' => ts('Are you sure you want delete this report? This action cannot be undone.'), | |
389 | ), | |
44543184 | 390 | ); |
391 | } | |
392 | return $actions; | |
393 | } | |
394 | ||
6a488035 | 395 | } |