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 +--------------------------------------------------------------------+
13 * Class CRM_Report_Form_Instance
15 class CRM_Report_Form_Instance
{
20 * @param CRM_Core_Form $form
22 public static function buildForm(&$form) {
23 // We should not build form elements in dashlet mode.
24 if ($form->_section
) {
28 // Check role based permission.
29 $instanceID = $form->getVar('_id');
30 if ($instanceID && !CRM_Report_Utils_Report
::isInstanceGroupRoleAllowed($instanceID)) {
31 $url = CRM_Utils_System
::url('civicrm/report/list', 'reset=1');
32 CRM_Core_Error
::statusBounce(ts('You do not have permission to access this report.'),
37 $attributes = CRM_Core_DAO
::getAttribute('CRM_Report_DAO_ReportInstance');
47 ts('Report Description'),
48 $attributes['description']
54 $attributes['email_subject']
60 $attributes['email_to']
66 $attributes['email_subject']
71 ts('Limit Dashboard Results'),
72 ['class' => 'four', 'min' => 1]
75 $form->add('textarea',
81 $form->add('textarea',
87 $form->addElement('checkbox', 'is_navigation', ts('Include Report in Navigation Menu?'), NULL,
88 ['onclick' => "return showHideByValue('is_navigation','','navigation_menu','table-row','radio',false);"]
91 $form->addElement('select', 'view_mode', ts('Configure link to...'), [
92 'view' => ts('View Results'),
93 'criteria' => ts('Show Criteria'),
96 $form->addElement('checkbox', 'addToDashboard', ts('Available for Dashboard?'));
97 $form->add('number', 'cache_minutes', ts('Cache dashlet for'), ['class' => 'four', 'min' => 1]);
98 $form->addElement('checkbox', 'add_to_my_reports', ts('Add to My Reports?'), NULL);
100 $form->addElement('checkbox', 'is_reserved', ts('Reserved Report?'));
101 if (!CRM_Core_Permission
::check('administer reserved reports')) {
102 $form->freeze('is_reserved');
105 $getPerms = \Civi\Api4\Permission
::get(0)
106 ->addWhere('is_active', '=', 1)
107 ->addWhere('group', 'IN', ['civicrm', 'cms', 'const'])
108 ->setOrderBy(['title' => 'ASC'])
110 $form->addElement('select',
113 // FIXME: Historically, CiviReport hard-coded an extra '0' option. This should change to the more general ALWAYS_ALLOW_PERMISSION (but may require testing/migration).
114 ['0' => ts('Everyone (includes anonymous)')] +
array_combine($getPerms->column('name'), $getPerms->column('title')),
115 ['class' => 'crm-select2']
118 // prepare user_roles to save as names not as ids
119 if ($user_roles = CRM_Core_Config
::singleton()->userSystem
->getRoleNames()) {
120 $grouprole = $form->addElement('advmultiselect',
122 ts('ACL Group/Role'),
126 'style' => 'width:240px',
127 'class' => 'advmultiselect',
130 $grouprole->setButtonAttributes('add', ['value' => ts('Add >>')]);
131 $grouprole->setButtonAttributes('remove', ['value' => ts('<< Remove')]);
135 $parentMenu = CRM_Core_BAO_Navigation
::getNavigationList();
137 $form->add('select', 'parent_id', ts('Parent Menu'), ['' => ts('- select -')] +
$parentMenu);
139 // For now we only providing drilldown for one primary detail report only. In future this could be multiple reports
140 foreach ($form->_drilldownReport
as $reportUrl => $drillLabel) {
141 $instanceList = CRM_Report_Utils_Report
::getInstanceList($reportUrl);
142 if (count($instanceList) > 1) {
143 $form->add('select', 'drilldown_id', $drillLabel, ['' => ts('- select -')] +
$instanceList);
151 'name' => ts('Save Report'),
156 'name' => ts('Cancel'),
160 $form->addFormRule(['CRM_Report_Form_Instance', 'formRule'], $form);
166 * @param array $fields
167 * @param array $errors
168 * @param CRM_Report_Form_Instance $self
172 public static function formRule($fields, $errors, $self) {
173 // Validate both the "next" and "save" buttons for creating/updating a report.
174 $nextButton = $self->controller
->getButtonName();
175 $saveButton = str_replace('_next', '_save', $nextButton);
176 $clickedButton = $self->getVar('_instanceButtonName');
179 if ($clickedButton == $nextButton ||
$clickedButton == $saveButton) {
180 if (empty($fields['title'])) {
181 $errors['title'] = ts('Title is a required field.');
182 $self->assign('instanceFormError', TRUE);
186 return empty($errors) ?
TRUE : $errors;
190 * Set default values.
192 * @param CRM_Core_Form $form
193 * @param array $defaults
195 public static function setDefaultValues(&$form, &$defaults) {
196 // we should not build form elements in dashlet mode.
197 if (!empty($form->_section
)) {
201 $instanceID = $form->getVar('_id');
202 $navigationDefaults = [];
204 if (!isset($defaults['permission'])) {
205 $defaults['permission'] = 'access CiviReport';
208 $userFrameworkResourceURL = CRM_Core_Config
::singleton()->userFrameworkResourceURL
;
210 // Add a special region for the default HTML header of printed reports. It
211 // won't affect reports with customized headers, just ones with the default.
212 $printHeaderRegion = CRM_Core_Region
::instance('default-report-header', FALSE);
213 $htmlHeader = ($printHeaderRegion) ?
$printHeaderRegion->render('', FALSE) : '';
215 $defaults['report_header'] = $report_header = "<html>
217 <title>CiviCRM Report</title>
218 <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
219 <style type=\"text/css\">@import url({$userFrameworkResourceURL}css/print.css);</style>
222 <body><div id=\"crm-container\">";
224 $defaults['report_footer'] = $report_footer = "<p><img src=\"{$userFrameworkResourceURL}i/powered_by.png\" /></p></div></body>
228 // CRM-17225 view_mode currently supports 'view' or 'criteria'.
229 // Prior to 4.7 'view' meant reset=1 in the url & if not set
230 // then show criteria.
231 // From 4.7 we will pro-actively set 'force=1' but still respect the old behaviour.
232 // we may look to add pdf, print_view, csv & various charts as these could simply
233 // be added to the url allowing us to conceptualise 'view right now' vs saved view
234 // & using a multiselect (option value?) could help here.
235 // Note that accessing reports without reset=1 in the url turns out to be
236 // dangerous as it seems to carry actions like 'delete' from one report to another.
237 $defaults['view_mode'] = 'view';
238 $output = CRM_Utils_Request
::retrieve('output', 'String');
239 if ($output == 'criteria') {
240 $defaults['view_mode'] = 'criteria';
243 if (empty($defaults['cache_minutes'])) {
244 $defaults['cache_minutes'] = '60';
248 // this is already retrieved via Form.php
249 $defaults['description'] = $defaults['description'] ??
NULL;
250 if (!empty($defaults['header'])) {
251 $defaults['report_header'] = $defaults['header'];
253 if (!empty($defaults['footer'])) {
254 $defaults['report_footer'] = $defaults['footer'];
257 // CRM-17310 private reports option.
258 $defaults['add_to_my_reports'] = 0;
259 if (CRM_Utils_Array
::value('owner_id', $defaults) != NULL) {
260 $defaults['add_to_my_reports'] = 1;
263 if (!empty($defaults['navigation_id'])) {
264 // Get the default navigation parent id.
265 $params = ['id' => $defaults['navigation_id']];
266 CRM_Core_BAO_Navigation
::retrieve($params, $navigationDefaults);
267 $defaults['is_navigation'] = 1;
268 $defaults['parent_id'] = $navigationDefaults['parent_id'] ??
NULL;
269 if (!empty($navigationDefaults['is_active'])) {
270 $form->assign('is_navigation', TRUE);
272 // A saved view mode will over-ride any url assumptions.
273 if (strpos($navigationDefaults['url'], 'output=criteria')) {
274 $defaults['view_mode'] = 'criteria';
277 if (!empty($navigationDefaults['id'])) {
278 $form->_navigation
['id'] = $navigationDefaults['id'];
279 $form->_navigation
['parent_id'] = !empty($navigationDefaults['parent_id']) ?
280 $navigationDefaults['parent_id'] : NULL;
284 if (!empty($defaults['grouprole'])) {
285 foreach (explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defaults['grouprole']) as $value) {
286 $groupRoles[] = $value;
288 $defaults['grouprole'] = $groupRoles;
291 elseif (property_exists($form, '_description')) {
292 $defaults['description'] = $form->_description
;
297 * Post process function.
299 * @param CRM_Core_Form $form
300 * @param bool $redirect
302 public static function postProcess(&$form, $redirect = TRUE) {
303 $params = $form->getVar('_params');
304 $instanceID = $form->getVar('_id');
306 if ($isNew = $form->getVar('_createNew')) {
307 // set the report_id since base template is going to be same, and we going to unset $instanceID
308 // which will make it difficult later on, to compute report_id
309 $params['report_id'] = CRM_Report_Utils_Report
::getValueFromUrl($instanceID);
310 // Unset $instanceID so a new copy would be created.
313 $params['instance_id'] = $instanceID;
314 if (!empty($params['is_navigation'])) {
315 $params['navigation'] = $form->_navigation
;
317 elseif ($instanceID) {
318 // Delete navigation if exists.
319 $navId = CRM_Core_DAO
::getFieldValue('CRM_Report_DAO_ReportInstance', $instanceID, 'navigation_id', 'id');
321 CRM_Core_BAO_Navigation
::processDelete($navId);
322 CRM_Core_BAO_Navigation
::resetNavigation();
326 // make a copy of params
327 $formValues = $params;
329 // unset params from $formValues that doesn't match with DB columns of instance tables, and also not required in form-values for sure
344 foreach ($unsetFields as $field) {
345 unset($formValues[$field]);
347 $view_mode = $formValues['view_mode'];
349 // CRM-17310 my reports functionality - we should set owner if the checkbox is 1,
350 // it seems to be not set at all if unchecked.
351 if (!empty($formValues['add_to_my_reports'])) {
352 $params['owner_id'] = CRM_Core_Session
::getLoggedInContactID();
355 $params['owner_id'] = 'null';
357 unset($formValues['add_to_my_reports']);
359 // pass form_values as string
360 $params['form_values'] = serialize($formValues);
362 $instance = CRM_Report_BAO_ReportInstance
::create($params);
363 $form->set('id', $instance->id
);
365 if ($instanceID && !$isNew) {
366 // updating existing instance
367 $statusMsg = ts('"%1" report has been updated.', [1 => $instance->title
]);
369 elseif ($form->getVar('_id') && $isNew) {
370 $statusMsg = ts('Your report has been successfully copied as "%1". You are currently viewing the new copy.', [1 => $instance->title
]);
373 $statusMsg = ts('"%1" report has been successfully created. You are currently viewing the new report instance.', [1 => $instance->title
]);
375 CRM_Core_Session
::setStatus($statusMsg, '', 'success');
378 $urlParams = ['reset' => 1];
379 if ($view_mode == 'view') {
380 $urlParams['force'] = 1;
383 $urlParams['output'] = 'criteria';
385 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/report/instance/{$instance->id}", $urlParams));