Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
4e580cdf | 13 | * Class CRM_Report_Form_Instance |
6a488035 TO |
14 | */ |
15 | class CRM_Report_Form_Instance { | |
16 | ||
74cf4551 | 17 | /** |
4e580cdf EM |
18 | * Build form. |
19 | * | |
c490a46a | 20 | * @param CRM_Core_Form $form |
74cf4551 | 21 | */ |
00be9182 | 22 | public static function buildForm(&$form) { |
4e580cdf | 23 | // We should not build form elements in dashlet mode. |
6a488035 TO |
24 | if ($form->_section) { |
25 | return; | |
26 | } | |
27 | ||
4e580cdf | 28 | // Check role based permission. |
6a488035 TO |
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.'), | |
33 | $url | |
34 | ); | |
35 | } | |
36 | ||
0b25329b | 37 | $attributes = CRM_Core_DAO::getAttribute('CRM_Report_DAO_ReportInstance'); |
6a488035 TO |
38 | |
39 | $form->add('text', | |
40 | 'title', | |
41 | ts('Report Title'), | |
42 | $attributes['title'] | |
43 | ); | |
44 | ||
45 | $form->add('text', | |
46 | 'description', | |
47 | ts('Report Description'), | |
48 | $attributes['description'] | |
49 | ); | |
50 | ||
51 | $form->add('text', | |
52 | 'email_subject', | |
53 | ts('Subject'), | |
54 | $attributes['email_subject'] | |
55 | ); | |
56 | ||
57 | $form->add('text', | |
58 | 'email_to', | |
59 | ts('To'), | |
60 | $attributes['email_to'] | |
61 | ); | |
62 | ||
63 | $form->add('text', | |
64 | 'email_cc', | |
65 | ts('CC'), | |
66 | $attributes['email_subject'] | |
67 | ); | |
74cf4551 | 68 | |
a8f56d71 | 69 | $form->add('number', |
dbb4a0f9 PN |
70 | 'row_count', |
71 | ts('Limit Dashboard Results'), | |
be2fb01f | 72 | ['class' => 'four', 'min' => 1] |
dbb4a0f9 | 73 | ); |
74cf4551 | 74 | |
6a488035 TO |
75 | $form->add('textarea', |
76 | 'report_header', | |
77 | ts('Report Header'), | |
78 | $attributes['header'] | |
79 | ); | |
80 | ||
81 | $form->add('textarea', | |
82 | 'report_footer', | |
83 | ts('Report Footer'), | |
84 | $attributes['footer'] | |
85 | ); | |
86 | ||
87 | $form->addElement('checkbox', 'is_navigation', ts('Include Report in Navigation Menu?'), NULL, | |
be2fb01f | 88 | ['onclick' => "return showHideByValue('is_navigation','','navigation_menu','table-row','radio',false);"] |
6a488035 TO |
89 | ); |
90 | ||
be2fb01f | 91 | $form->addElement('select', 'view_mode', ts('Configure link to...'), [ |
e6e7e540 | 92 | 'view' => ts('View Results'), |
93 | 'criteria' => ts('Show Criteria'), | |
be2fb01f | 94 | ]); |
e6e7e540 | 95 | |
a8f56d71 | 96 | $form->addElement('checkbox', 'addToDashboard', ts('Available for Dashboard?')); |
be2fb01f | 97 | $form->add('number', 'cache_minutes', ts('Cache dashlet for'), ['class' => 'four', 'min' => 1]); |
0f8c6e58 | 98 | $form->addElement('checkbox', 'add_to_my_reports', ts('Add to My Reports?'), NULL); |
99 | ||
6a488035 TO |
100 | $form->addElement('checkbox', 'is_reserved', ts('Reserved Report?')); |
101 | if (!CRM_Core_Permission::check('administer reserved reports')) { | |
102 | $form->freeze('is_reserved'); | |
103 | } | |
104 | ||
79f05dc4 TO |
105 | $getPerms = \Civi\Api4\Permission::get(0) |
106 | ->addWhere('is_active', '=', 1) | |
107 | ->addWhere('group', 'IN', ['civicrm', 'cms', 'const']) | |
7efb95e7 | 108 | ->setOrderBy(['title' => 'ASC']) |
79f05dc4 | 109 | ->execute(); |
98a2e1b2 | 110 | $form->addElement('select', |
111 | 'permission', | |
112 | ts('Permission'), | |
79f05dc4 TO |
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'] | |
98a2e1b2 | 116 | ); |
6a488035 | 117 | |
98a2e1b2 | 118 | // prepare user_roles to save as names not as ids |
08d5b4df | 119 | if ($user_roles = CRM_Core_Config::singleton()->userSystem->getRoleNames()) { |
98a2e1b2 | 120 | $grouprole = $form->addElement('advmultiselect', |
121 | 'grouprole', | |
122 | ts('ACL Group/Role'), | |
123 | $user_roles, | |
124 | [ | |
125 | 'size' => 5, | |
126 | 'style' => 'width:240px', | |
127 | 'class' => 'advmultiselect', | |
128 | ] | |
129 | ); | |
130 | $grouprole->setButtonAttributes('add', ['value' => ts('Add >>')]); | |
131 | $grouprole->setButtonAttributes('remove', ['value' => ts('<< Remove')]); | |
6a488035 TO |
132 | } |
133 | ||
134 | // navigation field | |
135 | $parentMenu = CRM_Core_BAO_Navigation::getNavigationList(); | |
136 | ||
be2fb01f | 137 | $form->add('select', 'parent_id', ts('Parent Menu'), ['' => ts('- select -')] + $parentMenu); |
6a488035 TO |
138 | |
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); | |
84178120 | 142 | if (count($instanceList) > 1) { |
be2fb01f | 143 | $form->add('select', 'drilldown_id', $drillLabel, ['' => ts('- select -')] + $instanceList); |
84178120 | 144 | } |
6a488035 TO |
145 | break; |
146 | } | |
147 | ||
be2fb01f CW |
148 | $form->addButtons([ |
149 | [ | |
6a488035 TO |
150 | 'type' => 'submit', |
151 | 'name' => ts('Save Report'), | |
152 | 'isDefault' => TRUE, | |
be2fb01f CW |
153 | ], |
154 | [ | |
6a488035 TO |
155 | 'type' => 'cancel', |
156 | 'name' => ts('Cancel'), | |
be2fb01f | 157 | ], |
c86d4e7c | 158 | ]); |
6a488035 | 159 | |
be2fb01f | 160 | $form->addFormRule(['CRM_Report_Form_Instance', 'formRule'], $form); |
6a488035 TO |
161 | } |
162 | ||
74cf4551 | 163 | /** |
4e580cdf EM |
164 | * Add form rules. |
165 | * | |
166 | * @param array $fields | |
167 | * @param array $errors | |
168 | * @param CRM_Report_Form_Instance $self | |
74cf4551 EM |
169 | * |
170 | * @return array|bool | |
171 | */ | |
00be9182 | 172 | public static function formRule($fields, $errors, $self) { |
4e580cdf | 173 | // Validate both the "next" and "save" buttons for creating/updating a report. |
c9cf6554 CW |
174 | $nextButton = $self->controller->getButtonName(); |
175 | $saveButton = str_replace('_next', '_save', $nextButton); | |
176 | $clickedButton = $self->getVar('_instanceButtonName'); | |
6a488035 | 177 | |
be2fb01f | 178 | $errors = []; |
c9cf6554 | 179 | if ($clickedButton == $nextButton || $clickedButton == $saveButton) { |
6a488035 | 180 | if (empty($fields['title'])) { |
161f6a74 | 181 | $errors['title'] = ts('Title is a required field.'); |
6a488035 TO |
182 | $self->assign('instanceFormError', TRUE); |
183 | } | |
184 | } | |
185 | ||
186 | return empty($errors) ? TRUE : $errors; | |
187 | } | |
188 | ||
74cf4551 | 189 | /** |
4e580cdf EM |
190 | * Set default values. |
191 | * | |
c490a46a | 192 | * @param CRM_Core_Form $form |
4e580cdf | 193 | * @param array $defaults |
74cf4551 | 194 | */ |
00be9182 | 195 | public static function setDefaultValues(&$form, &$defaults) { |
4e580cdf | 196 | // we should not build form elements in dashlet mode. |
8b95ac26 | 197 | if (!empty($form->_section)) { |
6a488035 TO |
198 | return; |
199 | } | |
200 | ||
201 | $instanceID = $form->getVar('_id'); | |
be2fb01f | 202 | $navigationDefaults = []; |
6a488035 | 203 | |
9b873358 | 204 | if (!isset($defaults['permission'])) { |
f420456d | 205 | $defaults['permission'] = 'access CiviReport'; |
6a488035 TO |
206 | } |
207 | ||
1a7356e7 | 208 | $userFrameworkResourceURL = CRM_Core_Config::singleton()->userFrameworkResourceURL; |
78b06a37 AH |
209 | |
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) : ''; | |
214 | ||
6a488035 TO |
215 | $defaults['report_header'] = $report_header = "<html> |
216 | <head> | |
217 | <title>CiviCRM Report</title> | |
218 | <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> | |
1a7356e7 | 219 | <style type=\"text/css\">@import url({$userFrameworkResourceURL}css/print.css);</style> |
78b06a37 | 220 | {$htmlHeader} |
6a488035 TO |
221 | </head> |
222 | <body><div id=\"crm-container\">"; | |
223 | ||
1a7356e7 | 224 | $defaults['report_footer'] = $report_footer = "<p><img src=\"{$userFrameworkResourceURL}i/powered_by.png\" /></p></div></body> |
6a488035 TO |
225 | </html> |
226 | "; | |
227 | ||
e6e7e540 | 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'; | |
241 | } | |
242 | ||
a8f56d71 CW |
243 | if (empty($defaults['cache_minutes'])) { |
244 | $defaults['cache_minutes'] = '60'; | |
245 | } | |
246 | ||
6a488035 TO |
247 | if ($instanceID) { |
248 | // this is already retrieved via Form.php | |
9c1bc317 | 249 | $defaults['description'] = $defaults['description'] ?? NULL; |
0642ec63 AH |
250 | if (!empty($defaults['header'])) { |
251 | $defaults['report_header'] = $defaults['header']; | |
252 | } | |
253 | if (!empty($defaults['footer'])) { | |
254 | $defaults['report_footer'] = $defaults['footer']; | |
255 | } | |
6a488035 | 256 | |
0f8c6e58 | 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; | |
261 | } | |
262 | ||
a7488080 | 263 | if (!empty($defaults['navigation_id'])) { |
734d3871 | 264 | // Get the default navigation parent id. |
be2fb01f | 265 | $params = ['id' => $defaults['navigation_id']]; |
6a488035 TO |
266 | CRM_Core_BAO_Navigation::retrieve($params, $navigationDefaults); |
267 | $defaults['is_navigation'] = 1; | |
9c1bc317 | 268 | $defaults['parent_id'] = $navigationDefaults['parent_id'] ?? NULL; |
a7488080 | 269 | if (!empty($navigationDefaults['is_active'])) { |
6a488035 TO |
270 | $form->assign('is_navigation', TRUE); |
271 | } | |
e6e7e540 | 272 | // A saved view mode will over-ride any url assumptions. |
273 | if (strpos($navigationDefaults['url'], 'output=criteria')) { | |
274 | $defaults['view_mode'] = 'criteria'; | |
275 | } | |
6a488035 | 276 | |
a7488080 | 277 | if (!empty($navigationDefaults['id'])) { |
6a488035 | 278 | $form->_navigation['id'] = $navigationDefaults['id']; |
734d3871 EM |
279 | $form->_navigation['parent_id'] = !empty($navigationDefaults['parent_id']) ? |
280 | $navigationDefaults['parent_id'] : NULL; | |
6a488035 TO |
281 | } |
282 | } | |
283 | ||
a7488080 | 284 | if (!empty($defaults['grouprole'])) { |
6a488035 | 285 | foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['grouprole']) as $value) { |
4e580cdf | 286 | $groupRoles[] = $value; |
6a488035 | 287 | } |
4e580cdf | 288 | $defaults['grouprole'] = $groupRoles; |
6a488035 TO |
289 | } |
290 | } | |
4c9b6178 | 291 | elseif (property_exists($form, '_description')) { |
6a488035 TO |
292 | $defaults['description'] = $form->_description; |
293 | } | |
294 | } | |
295 | ||
74cf4551 | 296 | /** |
4e580cdf EM |
297 | * Post process function. |
298 | * | |
c490a46a | 299 | * @param CRM_Core_Form $form |
74cf4551 EM |
300 | * @param bool $redirect |
301 | */ | |
00be9182 | 302 | public static function postProcess(&$form, $redirect = TRUE) { |
81583117 | 303 | $params = $form->getVar('_params'); |
6a488035 | 304 | $instanceID = $form->getVar('_id'); |
f813f78e | 305 | |
6ea7a229 | 306 | if ($isNew = $form->getVar('_createNew')) { |
e41b14ce DS |
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); | |
4e580cdf EM |
310 | // Unset $instanceID so a new copy would be created. |
311 | $instanceID = NULL; | |
6a488035 | 312 | } |
6ea7a229 | 313 | $params['instance_id'] = $instanceID; |
a7488080 | 314 | if (!empty($params['is_navigation'])) { |
6ea7a229 | 315 | $params['navigation'] = $form->_navigation; |
6a488035 | 316 | } |
9b873358 | 317 | elseif ($instanceID) { |
4e580cdf | 318 | // Delete navigation if exists. |
81583117 BS |
319 | $navId = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instanceID, 'navigation_id', 'id'); |
320 | if ($navId) { | |
321 | CRM_Core_BAO_Navigation::processDelete($navId); | |
322 | CRM_Core_BAO_Navigation::resetNavigation(); | |
323 | } | |
324 | } | |
6a488035 | 325 | |
c5958fbe DS |
326 | // make a copy of params |
327 | $formValues = $params; | |
328 | ||
329 | // unset params from $formValues that doesn't match with DB columns of instance tables, and also not required in form-values for sure | |
be2fb01f | 330 | $unsetFields = [ |
353ffa53 TO |
331 | 'title', |
332 | 'to_emails', | |
333 | 'cc_emails', | |
334 | 'header', | |
335 | 'footer', | |
336 | 'qfKey', | |
337 | 'id', | |
338 | '_qf_default', | |
339 | 'report_header', | |
340 | 'report_footer', | |
341 | 'grouprole', | |
87ecd5b7 | 342 | 'task', |
be2fb01f | 343 | ]; |
c5958fbe DS |
344 | foreach ($unsetFields as $field) { |
345 | unset($formValues[$field]); | |
346 | } | |
e6e7e540 | 347 | $view_mode = $formValues['view_mode']; |
0f8c6e58 | 348 | |
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'])) { | |
3bdcd4ec | 352 | $params['owner_id'] = CRM_Core_Session::getLoggedInContactID(); |
0f8c6e58 | 353 | } |
354 | else { | |
355 | $params['owner_id'] = 'null'; | |
356 | } | |
357 | unset($formValues['add_to_my_reports']); | |
358 | ||
c5958fbe DS |
359 | // pass form_values as string |
360 | $params['form_values'] = serialize($formValues); | |
361 | ||
0b25329b | 362 | $instance = CRM_Report_BAO_ReportInstance::create($params); |
6ea7a229 | 363 | $form->set('id', $instance->id); |
6a488035 TO |
364 | |
365 | if ($instanceID && !$isNew) { | |
366 | // updating existing instance | |
be2fb01f | 367 | $statusMsg = ts('"%1" report has been updated.', [1 => $instance->title]); |
0db6c3e1 TO |
368 | } |
369 | elseif ($form->getVar('_id') && $isNew) { | |
be2fb01f | 370 | $statusMsg = ts('Your report has been successfully copied as "%1". You are currently viewing the new copy.', [1 => $instance->title]); |
0db6c3e1 TO |
371 | } |
372 | else { | |
be2fb01f | 373 | $statusMsg = ts('"%1" report has been successfully created. You are currently viewing the new report instance.', [1 => $instance->title]); |
6a488035 | 374 | } |
7af040c2 | 375 | CRM_Core_Session::setStatus($statusMsg, '', 'success'); |
6a488035 | 376 | |
481a74f4 | 377 | if ($redirect) { |
be2fb01f | 378 | $urlParams = ['reset' => 1]; |
e6e7e540 | 379 | if ($view_mode == 'view') { |
380 | $urlParams['force'] = 1; | |
381 | } | |
382 | else { | |
383 | $urlParams['output'] = 'criteria'; | |
384 | } | |
385 | CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/report/instance/{$instance->id}", $urlParams)); | |
6a488035 TO |
386 | } |
387 | } | |
96025800 | 388 | |
6a488035 | 389 | } |