3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Report_BAO_ReportInstance
extends CRM_Report_DAO_ReportInstance
implements Civi\Test\HookInterface
{
20 * Takes an associative array and creates an instance object.
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
26 * @param array $params
27 * (reference ) an assoc array of name/value pairs.
29 * @return CRM_Report_DAO_ReportInstance
31 public static function add(&$params) {
36 $instanceID = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('instance_id', $params));
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;
44 $params['grouprole'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
45 array_keys($grouprole_array)
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();
57 CRM_Utils_Hook
::pre('edit', 'ReportInstance', $instanceID, $params);
60 CRM_Utils_Hook
::pre('create', 'ReportInstance', NULL, $params);
63 $instance = new CRM_Report_DAO_ReportInstance();
64 $instance->copyValues($params);
66 if (CRM_Core_Config
::singleton()->userFramework
== 'Joomla') {
67 $instance->permission
= 'null';
70 // explicitly set to null if params value is empty
71 if (!$instanceID && empty($params['grouprole'])) {
72 $instance->grouprole
= 'null';
76 $instance->id
= $instanceID;
80 if ($reportID = CRM_Utils_Array
::value('report_id', $params)) {
81 $instance->report_id
= $reportID;
83 elseif ($instanceID) {
84 $instance->report_id
= CRM_Report_Utils_Report
::getValueFromUrl($instanceID);
87 // just take it from current url
88 $instance->report_id
= CRM_Report_Utils_Report
::getValueFromUrl();
95 CRM_Utils_Hook
::post('edit', 'ReportInstance', $instance->id
, $instance);
98 CRM_Utils_Hook
::post('create', 'ReportInstance', $instance->id
, $instance);
106 * takes an associative array and creates a instance object and does any related work like permissioning, adding to dashboard etc.
108 * This function is invoked from within the web form layer and also from the api layer
110 * @param array $params
111 * (reference ) an assoc array of name/value pairs.
113 * @return CRM_Report_BAO_ReportInstance
115 public static function &create(&$params) {
116 if (isset($params['report_header'])) {
117 $params['header'] = $params['report_header'];
119 if (isset($params['report_footer'])) {
120 $params['footer'] = $params['report_footer'];
123 // build navigation parameters
124 if (!empty($params['is_navigation'])) {
125 if (!array_key_exists('navigation', $params)) {
126 $params['navigation'] = [];
128 $navigationParams =& $params['navigation'];
130 $navigationParams['permission'] = [];
131 $navigationParams['label'] = $params['title'];
132 $navigationParams['name'] = $params['title'];
134 $navigationParams['current_parent_id'] = $navigationParams['parent_id'] ??
NULL;
135 $navigationParams['parent_id'] = $params['parent_id'] ??
NULL;
136 $navigationParams['is_active'] = 1;
138 if ($permission = CRM_Utils_Array
::value('permission', $params)) {
139 $navigationParams['permission'][] = $permission;
142 // unset the navigation related elements, not used in report form values
143 unset($params['parent_id']);
144 unset($params['is_navigation']);
147 $viewMode = !empty($params['view_mode']);
149 // Do not save to the DB - it's saved in the url.
150 unset($params['view_mode']);
155 if (!empty($params['addToDashboard'])) {
157 'label' => $params['title'],
160 if ($permission = CRM_Utils_Array
::value('permission', $params)) {
161 $dashletParams['permission'][] = $permission;
165 $transaction = new CRM_Core_Transaction();
167 $instance = self
::add($params);
168 if (is_a($instance, 'CRM_Core_Error')) {
169 $transaction->rollback();
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']);
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);
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
);
186 // has been removed from the navigation bar
187 CRM_Core_DAO
::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id
, 'navigation_id', 'NULL');
190 CRM_Core_BAO_Navigation
::resetNavigation();
194 if (!empty($dashletParams)) {
196 $chart = $limitResult = '';
197 if (!empty($params['charts'])) {
199 $chart = "&charts=" . $params['charts'];
201 if (!empty($params['row_count']) && CRM_Utils_Rule
::positiveInteger($params['row_count'])) {
202 $limitResult = '&rowCount=' . $params['row_count'];
204 if (!empty($params['cache_minutes']) && CRM_Utils_Rule
::positiveInteger($params['cache_minutes'])) {
205 $dashletParams['cache_minutes'] = $params['cache_minutes'];
207 $dashletParams['name'] = "report/{$instance->id}";
208 $dashletParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1§ion={$section}{$chart}&context=dashlet" . $limitResult;
209 $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}?reset=1§ion={$section}{$chart}&context=dashletFullscreen" . $limitResult;
210 $dashletParams['instanceURL'] = "civicrm/report/instance/{$instance->id}";
211 CRM_Core_BAO_Dashboard
::addDashlet($dashletParams);
213 $transaction->commit();
219 * Delete the instance of the Report.
225 public static function del($id = NULL) {
226 self
::deleteRecord(['id' => $id]);
231 * Event fired prior to modifying a ReportInstance.
232 * @param \Civi\Core\Event\PreEvent $event
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');
239 CRM_Core_BAO_Navigation
::processDelete($navId);
240 CRM_Core_BAO_Navigation
::resetNavigation();
248 * @param array $params
249 * @param array $defaults
251 * @return CRM_Report_DAO_ReportInstance|null
253 public static function retrieve($params, &$defaults) {
254 $instance = new CRM_Report_DAO_ReportInstance();
255 $instance->copyValues($params);
257 if ($instance->find(TRUE)) {
258 CRM_Core_DAO
::storeValues($instance, $defaults);
265 * Check if report is private.
267 * @param int $instance_id
271 public static function reportIsPrivate($instance_id) {
272 $owner_id = CRM_Core_DAO
::getFieldValue('CRM_Report_DAO_ReportInstance', $instance_id, 'owner_id', 'id');
280 * Check if the logged in user is the owner.
282 * @param int $instance_id
284 * @return TRUE if contact owns the report, FALSE if not
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) {
297 * Check if the logged in contact can administer the report.
299 * @param int $instance_id
302 * True if contact can edit the private report, FALSE if not.
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')) {
310 elseif (CRM_Core_Permission
::check('administer Reports')) {
317 * Delete a report instance wrapped in handling for the form layer.
319 * @param int $instanceId
320 * @param string $bounceTo
321 * Url to redirect the browser to on fail.
322 * @param string $successRedirect
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);
330 CRM_Report_BAO_ReportInstance
::del($instanceId);
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));
339 * Get the metadata of actions available for this entity.
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.
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.
353 public static function getActionMetadata() {
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'),
360 'is_confirm' => TRUE,
361 'confirm_title' => ts('Save a copy...'),
362 'confirm_refresh_fields' => json_encode([
364 'selector' => '.crm-report-instanceForm-form-block-title',
365 'prepend' => ts('(Copy) '),
368 'selector' => '.crm-report-instanceForm-form-block-description',
372 'selector' => '.crm-report-instanceForm-form-block-parent_id',
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')];
383 if (CRM_Core_Permission
::check('administer Reports')) {
384 $actions['report_instance.delete'] = [
385 'title' => ts('Delete report'),
387 'is_confirm' => TRUE,
388 'confirm_message' => ts('Are you sure you want delete this report? This action cannot be undone.'),