Merge pull request #22080 from seamuslee001/dev_drupal_169
[civicrm-core.git] / CRM / Report / BAO / ReportInstance.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance implements Civi\Test\HookInterface {
18
19 /**
20 * Takes an associative array and creates an instance object.
21 *
22 * the function extract all the params it needs to initialize the create a
23 * instance object. the params array could contain additional unused name/value
24 * pairs
25 *
26 * @param array $params
27 * (reference ) an assoc array of name/value pairs.
28 *
29 * @return CRM_Report_DAO_ReportInstance
30 */
31 public static function add(&$params) {
32 if (empty($params)) {
33 return NULL;
34 }
35
36 $instanceID = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('instance_id', $params));
37
38 // convert roles array to string
39 if (isset($params['grouprole']) && is_array($params['grouprole'])) {
40 $grouprole_array = [];
41 foreach ($params['grouprole'] as $key => $value) {
42 $grouprole_array[$value] = $value;
43 }
44 $params['grouprole'] = implode(CRM_Core_DAO::VALUE_SEPARATOR,
45 array_keys($grouprole_array)
46 );
47 }
48
49 if (!$instanceID || !isset($params['id'])) {
50 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
51 $params['domain_id'] = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
52 // CRM-17256 set created_id on report creation.
53 $params['created_id'] = $params['created_id'] ?? CRM_Core_Session::getLoggedInContactID();
54 }
55
56 if ($instanceID) {
57 CRM_Utils_Hook::pre('edit', 'ReportInstance', $instanceID, $params);
58 }
59 else {
60 CRM_Utils_Hook::pre('create', 'ReportInstance', NULL, $params);
61 }
62
63 $instance = new CRM_Report_DAO_ReportInstance();
64 $instance->copyValues($params);
65
66 if (CRM_Core_Config::singleton()->userFramework == 'Joomla') {
67 $instance->permission = 'null';
68 }
69
70 // explicitly set to null if params value is empty
71 if (!$instanceID && empty($params['grouprole'])) {
72 $instance->grouprole = 'null';
73 }
74
75 if ($instanceID) {
76 $instance->id = $instanceID;
77 }
78
79 if (!$instanceID) {
80 if ($reportID = CRM_Utils_Array::value('report_id', $params)) {
81 $instance->report_id = $reportID;
82 }
83 elseif ($instanceID) {
84 $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl($instanceID);
85 }
86 else {
87 // just take it from current url
88 $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl();
89 }
90 }
91
92 $instance->save();
93
94 if ($instanceID) {
95 CRM_Utils_Hook::post('edit', 'ReportInstance', $instance->id, $instance);
96 }
97 else {
98 CRM_Utils_Hook::post('create', 'ReportInstance', $instance->id, $instance);
99 }
100 return $instance;
101 }
102
103 /**
104 * Create instance.
105 *
106 * takes an associative array and creates a instance object and does any related work like permissioning, adding to dashboard etc.
107 *
108 * This function is invoked from within the web form layer and also from the api layer
109 *
110 * @param array $params
111 * (reference ) an assoc array of name/value pairs.
112 *
113 * @return CRM_Report_BAO_ReportInstance
114 */
115 public static function &create(&$params) {
116 if (isset($params['report_header'])) {
117 $params['header'] = $params['report_header'];
118 }
119 if (isset($params['report_footer'])) {
120 $params['footer'] = $params['report_footer'];
121 }
122
123 // build navigation parameters
124 if (!empty($params['is_navigation'])) {
125 if (!array_key_exists('navigation', $params)) {
126 $params['navigation'] = [];
127 }
128 $navigationParams =& $params['navigation'];
129
130 $navigationParams['permission'] = [];
131 $navigationParams['label'] = $params['title'];
132 $navigationParams['name'] = $params['title'];
133
134 $navigationParams['current_parent_id'] = $navigationParams['parent_id'] ?? NULL;
135 $navigationParams['parent_id'] = $params['parent_id'] ?? NULL;
136 $navigationParams['is_active'] = 1;
137
138 if ($permission = CRM_Utils_Array::value('permission', $params)) {
139 $navigationParams['permission'][] = $permission;
140 }
141
142 // unset the navigation related elements, not used in report form values
143 unset($params['parent_id']);
144 unset($params['is_navigation']);
145 }
146
147 $viewMode = !empty($params['view_mode']);
148 if ($viewMode) {
149 // Do not save to the DB - it's saved in the url.
150 unset($params['view_mode']);
151 }
152
153 // add to dashboard
154 $dashletParams = [];
155 if (!empty($params['addToDashboard'])) {
156 $dashletParams = [
157 'label' => $params['title'],
158 'is_active' => 1,
159 ];
160 if ($permission = CRM_Utils_Array::value('permission', $params)) {
161 $dashletParams['permission'][] = $permission;
162 }
163 }
164
165 $transaction = new CRM_Core_Transaction();
166
167 $instance = self::add($params);
168 if (is_a($instance, 'CRM_Core_Error')) {
169 $transaction->rollback();
170 return $instance;
171 }
172
173 // add / update navigation as required
174 if (!empty($navigationParams)) {
175 if (empty($params['id']) && empty($params['instance_id']) && !empty($navigationParams['id'])) {
176 unset($navigationParams['id']);
177 }
178 $navigationParams['url'] = "civicrm/report/instance/{$instance->id}" . ($viewMode == 'view' ? '?reset=1&force=1' : '?reset=1&output=criteria');
179 $navigation = CRM_Core_BAO_Navigation::add($navigationParams);
180
181 if (!empty($navigationParams['is_active'])) {
182 //set the navigation id in report instance table
183 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', $navigation->id);
184 }
185 else {
186 // has been removed from the navigation bar
187 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', 'NULL');
188 }
189 //reset navigation
190 CRM_Core_BAO_Navigation::resetNavigation();
191 }
192
193 // add to dashlet
194 if (!empty($dashletParams)) {
195 $section = 2;
196 $chart = $limitResult = '';
197 if (!empty($params['charts'])) {
198 $section = 1;
199 $chart = "&charts=" . $params['charts'];
200 }
201 if (!empty($params['row_count']) && CRM_Utils_Rule::positiveInteger($params['row_count'])) {
202 $limitResult = '&rowCount=' . $params['row_count'];
203 }
204 if (!empty($params['cache_minutes']) && CRM_Utils_Rule::positiveInteger($params['cache_minutes'])) {
205 $dashletParams['cache_minutes'] = $params['cache_minutes'];
206 }
207 $dashletParams['name'] = "report/{$instance->id}";
208 $dashletParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}{$chart}&context=dashlet" . $limitResult;
209 $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}{$chart}&context=dashletFullscreen" . $limitResult;
210 $dashletParams['instanceURL'] = "civicrm/report/instance/{$instance->id}";
211 CRM_Core_BAO_Dashboard::addDashlet($dashletParams);
212 }
213 $transaction->commit();
214
215 return $instance;
216 }
217
218 /**
219 * Delete the instance of the Report.
220 *
221 * @param int $id
222 * @deprecated
223 * @return mixed
224 */
225 public static function del($id = NULL) {
226 self::deleteRecord(['id' => $id]);
227 return 1;
228 }
229
230 /**
231 * Event fired prior to modifying a ReportInstance.
232 * @param \Civi\Core\Event\PreEvent $event
233 */
234 public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) {
235 if ($event->action === 'delete' && $event->id) {
236 // When deleting a report, also delete from navigation menu
237 $navId = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $event->id, 'navigation_id');
238 if ($navId) {
239 CRM_Core_BAO_Navigation::processDelete($navId);
240 CRM_Core_BAO_Navigation::resetNavigation();
241 }
242 }
243 }
244
245 /**
246 * Retrieve instance.
247 *
248 * @param array $params
249 * @param array $defaults
250 *
251 * @return CRM_Report_DAO_ReportInstance|null
252 */
253 public static function retrieve($params, &$defaults) {
254 $instance = new CRM_Report_DAO_ReportInstance();
255 $instance->copyValues($params);
256
257 if ($instance->find(TRUE)) {
258 CRM_Core_DAO::storeValues($instance, $defaults);
259 return $instance;
260 }
261 return NULL;
262 }
263
264 /**
265 * Check if report is private.
266 *
267 * @param int $instance_id
268 *
269 * @return bool
270 */
271 public static function reportIsPrivate($instance_id) {
272 $owner_id = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instance_id, 'owner_id', 'id');
273 if ($owner_id) {
274 return TRUE;
275 }
276 return FALSE;
277 }
278
279 /**
280 * Check if the logged in user is the owner.
281 *
282 * @param int $instance_id
283 *
284 * @return TRUE if contact owns the report, FALSE if not
285 */
286 public static function contactIsOwner($instance_id) {
287 $session = CRM_Core_Session::singleton();
288 $contact_id = $session->get('userID');
289 $owner_id = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instance_id, 'owner_id', 'id');
290 if ($contact_id === $owner_id) {
291 return TRUE;
292 }
293 return FALSE;
294 }
295
296 /**
297 * Check if the logged in contact can administer the report.
298 *
299 * @param int $instance_id
300 *
301 * @return bool
302 * True if contact can edit the private report, FALSE if not.
303 */
304 public static function contactCanAdministerReport($instance_id) {
305 if (self::reportIsPrivate($instance_id)) {
306 if (self::contactIsOwner($instance_id) || CRM_Core_Permission::check('access all private reports')) {
307 return TRUE;
308 }
309 }
310 elseif (CRM_Core_Permission::check('administer Reports')) {
311 return TRUE;
312 }
313 return FALSE;
314 }
315
316 /**
317 * Delete a report instance wrapped in handling for the form layer.
318 *
319 * @param int $instanceId
320 * @param string $bounceTo
321 * Url to redirect the browser to on fail.
322 * @param string $successRedirect
323 */
324 public static function doFormDelete($instanceId, $bounceTo = 'civicrm/report/list?reset=1', $successRedirect = NULL) {
325 if (!CRM_Core_Permission::check('administer Reports')) {
326 $statusMessage = ts('You do not have permission to Delete Report.');
327 CRM_Core_Error::statusBounce($statusMessage, $bounceTo);
328 }
329
330 CRM_Report_BAO_ReportInstance::del($instanceId);
331
332 CRM_Core_Session::setStatus(ts('Selected report has been deleted.'), ts('Deleted'), 'success');
333 if ($successRedirect) {
334 CRM_Utils_System::redirect(CRM_Utils_System::url($successRedirect));
335 }
336 }
337
338 /**
339 * Get the metadata of actions available for this entity.
340 *
341 * The thinking here is to describe the various actions on the BAO and then functions
342 * can add a mix of actions from different BAO as appropriate. The crm.SearchForm.js code
343 * transforms the 'confirm_mesage' into a message that needs to be confirmed.
344 * confirm_refresh_fields need to be reviewed & potentially updated at the confirm stage.
345 *
346 * Ideas not yet implemented:
347 * - supports_modal task can be loaded in a popup, theoretically worked, not attempted.
348 * - class and or icon - per option icons or classes (I added these in addTaskMenu::addTaskMenu
349 * but I didn't have the right classes). ie adding 'class' => 'crm-i fa-print' to print / class looked
350 * wrong, but at the php level it worked https://github.com/civicrm/civicrm-core/pull/8529#issuecomment-227639091
351 * - general script-add.
352 */
353 public static function getActionMetadata() {
354 $actions = [];
355 if (CRM_Core_Permission::check('save Report Criteria')) {
356 $actions['report_instance.save'] = ['title' => ts('Save')];
357 $actions['report_instance.copy'] = [
358 'title' => ts('Save a Copy'),
359 'data' => [
360 'is_confirm' => TRUE,
361 'confirm_title' => ts('Save a copy...'),
362 'confirm_refresh_fields' => json_encode([
363 'title' => [
364 'selector' => '.crm-report-instanceForm-form-block-title',
365 'prepend' => ts('(Copy) '),
366 ],
367 'description' => [
368 'selector' => '.crm-report-instanceForm-form-block-description',
369 'prepend' => '',
370 ],
371 'parent_id' => [
372 'selector' => '.crm-report-instanceForm-form-block-parent_id',
373 'prepend' => '',
374 ],
375 ]),
376 ],
377 ];
378 }
379 $actions['report_instance.print'] = ['title' => ts('Print Report')];
380 $actions['report_instance.pdf'] = ['title' => ts('Print to PDF')];
381 $actions['report_instance.csv'] = ['title' => ts('Export as CSV')];
382
383 if (CRM_Core_Permission::check('administer Reports')) {
384 $actions['report_instance.delete'] = [
385 'title' => ts('Delete report'),
386 'data' => [
387 'is_confirm' => TRUE,
388 'confirm_message' => ts('Are you sure you want delete this report? This action cannot be undone.'),
389 ],
390 ];
391 }
392 return $actions;
393 }
394
395 }