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 | /** | |
4e580cdf | 29 | * Class CRM_Report_Form_Instance |
6a488035 TO |
30 | */ |
31 | class CRM_Report_Form_Instance { | |
32 | ||
74cf4551 | 33 | /** |
4e580cdf EM |
34 | * Build form. |
35 | * | |
c490a46a | 36 | * @param CRM_Core_Form $form |
74cf4551 | 37 | */ |
00be9182 | 38 | public static function buildForm(&$form) { |
4e580cdf | 39 | // We should not build form elements in dashlet mode. |
6a488035 TO |
40 | if ($form->_section) { |
41 | return; | |
42 | } | |
43 | ||
4e580cdf | 44 | // Check role based permission. |
6a488035 TO |
45 | $instanceID = $form->getVar('_id'); |
46 | if ($instanceID && !CRM_Report_Utils_Report::isInstanceGroupRoleAllowed($instanceID)) { | |
47 | $url = CRM_Utils_System::url('civicrm/report/list', 'reset=1'); | |
48 | CRM_Core_Error::statusBounce(ts('You do not have permission to access this report.'), | |
49 | $url | |
50 | ); | |
51 | } | |
52 | ||
0b25329b | 53 | $attributes = CRM_Core_DAO::getAttribute('CRM_Report_DAO_ReportInstance'); |
6a488035 TO |
54 | |
55 | $form->add('text', | |
56 | 'title', | |
57 | ts('Report Title'), | |
58 | $attributes['title'] | |
59 | ); | |
60 | ||
61 | $form->add('text', | |
62 | 'description', | |
63 | ts('Report Description'), | |
64 | $attributes['description'] | |
65 | ); | |
66 | ||
67 | $form->add('text', | |
68 | 'email_subject', | |
69 | ts('Subject'), | |
70 | $attributes['email_subject'] | |
71 | ); | |
72 | ||
73 | $form->add('text', | |
74 | 'email_to', | |
75 | ts('To'), | |
76 | $attributes['email_to'] | |
77 | ); | |
78 | ||
79 | $form->add('text', | |
80 | 'email_cc', | |
81 | ts('CC'), | |
82 | $attributes['email_subject'] | |
83 | ); | |
74cf4551 | 84 | |
dbb4a0f9 PN |
85 | $form->add('text', |
86 | 'row_count', | |
87 | ts('Limit Dashboard Results'), | |
84178120 | 88 | array( |
353ffa53 | 89 | 'maxlength' => 64, |
21dfd5f5 | 90 | 'size' => 5, |
74cf4551 | 91 | ) |
dbb4a0f9 | 92 | ); |
74cf4551 | 93 | |
6a488035 TO |
94 | $form->add('textarea', |
95 | 'report_header', | |
96 | ts('Report Header'), | |
97 | $attributes['header'] | |
98 | ); | |
99 | ||
100 | $form->add('textarea', | |
101 | 'report_footer', | |
102 | ts('Report Footer'), | |
103 | $attributes['footer'] | |
104 | ); | |
105 | ||
106 | $form->addElement('checkbox', 'is_navigation', ts('Include Report in Navigation Menu?'), NULL, | |
107 | array('onclick' => "return showHideByValue('is_navigation','','navigation_menu','table-row','radio',false);") | |
108 | ); | |
109 | ||
e6e7e540 | 110 | $form->addElement('select', 'view_mode', ts('Configure link to...'), array( |
111 | 'view' => ts('View Results'), | |
112 | 'criteria' => ts('Show Criteria'), | |
113 | )); | |
114 | ||
dbb4a0f9 PN |
115 | $form->addElement('checkbox', 'addToDashboard', ts('Available for Dashboard?'), NULL, |
116 | array('onclick' => "return showHideByValue('addToDashboard','','limit_result','table-row','radio',false);")); | |
0f8c6e58 | 117 | $form->addElement('checkbox', 'add_to_my_reports', ts('Add to My Reports?'), NULL); |
118 | ||
6a488035 TO |
119 | $form->addElement('checkbox', 'is_reserved', ts('Reserved Report?')); |
120 | if (!CRM_Core_Permission::check('administer reserved reports')) { | |
121 | $form->freeze('is_reserved'); | |
122 | } | |
123 | ||
124 | $config = CRM_Core_Config::singleton(); | |
125 | if ($config->userFramework != 'Joomla' || | |
126 | $config->userFramework != 'WordPress' | |
127 | ) { | |
128 | $form->addElement('select', | |
129 | 'permission', | |
130 | ts('Permission'), | |
131 | array('0' => ts('Everyone (includes anonymous)')) + CRM_Core_Permission::basicPermissions() | |
132 | ); | |
133 | ||
134 | // prepare user_roles to save as names not as ids | |
135 | if (function_exists('user_roles')) { | |
136 | $user_roles_array = user_roles(); | |
137 | foreach ($user_roles_array as $key => $value) { | |
138 | $user_roles[$value] = $value; | |
139 | } | |
1c6954cb | 140 | $grouprole = &$form->addElement('advmultiselect', |
6a488035 TO |
141 | 'grouprole', |
142 | ts('ACL Group/Role'), | |
143 | $user_roles, | |
144 | array( | |
145 | 'size' => 5, | |
146 | 'style' => 'width:240px', | |
147 | 'class' => 'advmultiselect', | |
148 | ) | |
149 | ); | |
1c6954cb AH |
150 | $grouprole->setButtonAttributes('add', array('value' => ts('Add >>'))); |
151 | $grouprole->setButtonAttributes('remove', array('value' => ts('<< Remove'))); | |
6a488035 TO |
152 | } |
153 | } | |
154 | ||
155 | // navigation field | |
156 | $parentMenu = CRM_Core_BAO_Navigation::getNavigationList(); | |
157 | ||
2b27d9df | 158 | $form->add('select', 'parent_id', ts('Parent Menu'), array('' => ts('- select -')) + $parentMenu); |
6a488035 TO |
159 | |
160 | // For now we only providing drilldown for one primary detail report only. In future this could be multiple reports | |
161 | foreach ($form->_drilldownReport as $reportUrl => $drillLabel) { | |
162 | $instanceList = CRM_Report_Utils_Report::getInstanceList($reportUrl); | |
84178120 | 163 | if (count($instanceList) > 1) { |
6a488035 | 164 | $form->add('select', 'drilldown_id', $drillLabel, array('' => ts('- select -')) + $instanceList); |
84178120 | 165 | } |
6a488035 TO |
166 | break; |
167 | } | |
168 | ||
169 | $form->addButtons(array( | |
170 | array( | |
171 | 'type' => 'submit', | |
172 | 'name' => ts('Save Report'), | |
173 | 'isDefault' => TRUE, | |
174 | ), | |
175 | array( | |
176 | 'type' => 'cancel', | |
177 | 'name' => ts('Cancel'), | |
178 | ), | |
179 | ) | |
180 | ); | |
181 | ||
182 | $form->addFormRule(array('CRM_Report_Form_Instance', 'formRule'), $form); | |
183 | } | |
184 | ||
74cf4551 | 185 | /** |
4e580cdf EM |
186 | * Add form rules. |
187 | * | |
188 | * @param array $fields | |
189 | * @param array $errors | |
190 | * @param CRM_Report_Form_Instance $self | |
74cf4551 EM |
191 | * |
192 | * @return array|bool | |
193 | */ | |
00be9182 | 194 | public static function formRule($fields, $errors, $self) { |
4e580cdf | 195 | // Validate both the "next" and "save" buttons for creating/updating a report. |
c9cf6554 CW |
196 | $nextButton = $self->controller->getButtonName(); |
197 | $saveButton = str_replace('_next', '_save', $nextButton); | |
198 | $clickedButton = $self->getVar('_instanceButtonName'); | |
6a488035 TO |
199 | |
200 | $errors = array(); | |
c9cf6554 | 201 | if ($clickedButton == $nextButton || $clickedButton == $saveButton) { |
6a488035 | 202 | if (empty($fields['title'])) { |
161f6a74 | 203 | $errors['title'] = ts('Title is a required field.'); |
6a488035 TO |
204 | $self->assign('instanceFormError', TRUE); |
205 | } | |
206 | } | |
207 | ||
208 | return empty($errors) ? TRUE : $errors; | |
209 | } | |
210 | ||
74cf4551 | 211 | /** |
4e580cdf EM |
212 | * Set default values. |
213 | * | |
c490a46a | 214 | * @param CRM_Core_Form $form |
4e580cdf | 215 | * @param array $defaults |
74cf4551 | 216 | */ |
00be9182 | 217 | public static function setDefaultValues(&$form, &$defaults) { |
4e580cdf | 218 | // we should not build form elements in dashlet mode. |
6a488035 TO |
219 | if ($form->_section) { |
220 | return; | |
221 | } | |
222 | ||
223 | $instanceID = $form->getVar('_id'); | |
224 | $navigationDefaults = array(); | |
225 | ||
9b873358 | 226 | if (!isset($defaults['permission'])) { |
481a74f4 | 227 | $permissions = array_flip(CRM_Core_Permission::basicPermissions()); |
84178120 | 228 | $defaults['permission'] = $permissions['CiviReport: access CiviReport']; |
6a488035 TO |
229 | } |
230 | ||
231 | $config = CRM_Core_Config::singleton(); | |
78b06a37 AH |
232 | |
233 | // Add a special region for the default HTML header of printed reports. It | |
234 | // won't affect reports with customized headers, just ones with the default. | |
235 | $printHeaderRegion = CRM_Core_Region::instance('default-report-header', FALSE); | |
236 | $htmlHeader = ($printHeaderRegion) ? $printHeaderRegion->render('', FALSE) : ''; | |
237 | ||
6a488035 TO |
238 | $defaults['report_header'] = $report_header = "<html> |
239 | <head> | |
240 | <title>CiviCRM Report</title> | |
241 | <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> | |
242 | <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style> | |
78b06a37 | 243 | {$htmlHeader} |
6a488035 TO |
244 | </head> |
245 | <body><div id=\"crm-container\">"; | |
246 | ||
247 | $defaults['report_footer'] = $report_footer = "<p><img src=\"{$config->userFrameworkResourceURL}i/powered_by.png\" /></p></div></body> | |
248 | </html> | |
249 | "; | |
250 | ||
e6e7e540 | 251 | // CRM-17225 view_mode currently supports 'view' or 'criteria'. |
252 | // Prior to 4.7 'view' meant reset=1 in the url & if not set | |
253 | // then show criteria. | |
254 | // From 4.7 we will pro-actively set 'force=1' but still respect the old behaviour. | |
255 | // we may look to add pdf, print_view, csv & various charts as these could simply | |
256 | // be added to the url allowing us to conceptualise 'view right now' vs saved view | |
257 | // & using a multiselect (option value?) could help here. | |
258 | // Note that accessing reports without reset=1 in the url turns out to be | |
259 | // dangerous as it seems to carry actions like 'delete' from one report to another. | |
260 | $defaults['view_mode'] = 'view'; | |
261 | $output = CRM_Utils_Request::retrieve('output', 'String'); | |
262 | if ($output == 'criteria') { | |
263 | $defaults['view_mode'] = 'criteria'; | |
264 | } | |
265 | ||
6a488035 TO |
266 | if ($instanceID) { |
267 | // this is already retrieved via Form.php | |
268 | $defaults['description'] = CRM_Utils_Array::value('description', $defaults); | |
0642ec63 AH |
269 | if (!empty($defaults['header'])) { |
270 | $defaults['report_header'] = $defaults['header']; | |
271 | } | |
272 | if (!empty($defaults['footer'])) { | |
273 | $defaults['report_footer'] = $defaults['footer']; | |
274 | } | |
6a488035 | 275 | |
0f8c6e58 | 276 | // CRM-17310 private reports option. |
277 | $defaults['add_to_my_reports'] = 0; | |
278 | if (CRM_Utils_Array::value('owner_id', $defaults) != NULL) { | |
279 | $defaults['add_to_my_reports'] = 1; | |
280 | } | |
281 | ||
a7488080 | 282 | if (!empty($defaults['navigation_id'])) { |
734d3871 | 283 | // Get the default navigation parent id. |
6a488035 TO |
284 | $params = array('id' => $defaults['navigation_id']); |
285 | CRM_Core_BAO_Navigation::retrieve($params, $navigationDefaults); | |
286 | $defaults['is_navigation'] = 1; | |
287 | $defaults['parent_id'] = CRM_Utils_Array::value('parent_id', $navigationDefaults); | |
a7488080 | 288 | if (!empty($navigationDefaults['is_active'])) { |
6a488035 TO |
289 | $form->assign('is_navigation', TRUE); |
290 | } | |
e6e7e540 | 291 | // A saved view mode will over-ride any url assumptions. |
292 | if (strpos($navigationDefaults['url'], 'output=criteria')) { | |
293 | $defaults['view_mode'] = 'criteria'; | |
294 | } | |
6a488035 | 295 | |
a7488080 | 296 | if (!empty($navigationDefaults['id'])) { |
6a488035 | 297 | $form->_navigation['id'] = $navigationDefaults['id']; |
734d3871 EM |
298 | $form->_navigation['parent_id'] = !empty($navigationDefaults['parent_id']) ? |
299 | $navigationDefaults['parent_id'] : NULL; | |
6a488035 TO |
300 | } |
301 | } | |
302 | ||
a7488080 | 303 | if (!empty($defaults['grouprole'])) { |
6a488035 | 304 | foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['grouprole']) as $value) { |
4e580cdf | 305 | $groupRoles[] = $value; |
6a488035 | 306 | } |
4e580cdf | 307 | $defaults['grouprole'] = $groupRoles; |
6a488035 TO |
308 | } |
309 | } | |
4c9b6178 | 310 | elseif (property_exists($form, '_description')) { |
6a488035 TO |
311 | $defaults['description'] = $form->_description; |
312 | } | |
313 | } | |
314 | ||
74cf4551 | 315 | /** |
4e580cdf EM |
316 | * Post process function. |
317 | * | |
c490a46a | 318 | * @param CRM_Core_Form $form |
74cf4551 EM |
319 | * @param bool $redirect |
320 | */ | |
00be9182 | 321 | public static function postProcess(&$form, $redirect = TRUE) { |
81583117 | 322 | $params = $form->getVar('_params'); |
6a488035 | 323 | $instanceID = $form->getVar('_id'); |
f813f78e | 324 | |
6ea7a229 | 325 | if ($isNew = $form->getVar('_createNew')) { |
e41b14ce DS |
326 | // set the report_id since base template is going to be same, and we going to unset $instanceID |
327 | // which will make it difficult later on, to compute report_id | |
328 | $params['report_id'] = CRM_Report_Utils_Report::getValueFromUrl($instanceID); | |
4e580cdf EM |
329 | // Unset $instanceID so a new copy would be created. |
330 | $instanceID = NULL; | |
6a488035 | 331 | } |
6ea7a229 | 332 | $params['instance_id'] = $instanceID; |
a7488080 | 333 | if (!empty($params['is_navigation'])) { |
6ea7a229 | 334 | $params['navigation'] = $form->_navigation; |
6a488035 | 335 | } |
9b873358 | 336 | elseif ($instanceID) { |
4e580cdf | 337 | // Delete navigation if exists. |
81583117 BS |
338 | $navId = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instanceID, 'navigation_id', 'id'); |
339 | if ($navId) { | |
340 | CRM_Core_BAO_Navigation::processDelete($navId); | |
341 | CRM_Core_BAO_Navigation::resetNavigation(); | |
342 | } | |
343 | } | |
6a488035 | 344 | |
c5958fbe DS |
345 | // make a copy of params |
346 | $formValues = $params; | |
347 | ||
348 | // unset params from $formValues that doesn't match with DB columns of instance tables, and also not required in form-values for sure | |
349 | $unsetFields = array( | |
353ffa53 TO |
350 | 'title', |
351 | 'to_emails', | |
352 | 'cc_emails', | |
353 | 'header', | |
354 | 'footer', | |
355 | 'qfKey', | |
356 | 'id', | |
357 | '_qf_default', | |
358 | 'report_header', | |
359 | 'report_footer', | |
360 | 'grouprole', | |
c5958fbe DS |
361 | ); |
362 | foreach ($unsetFields as $field) { | |
363 | unset($formValues[$field]); | |
364 | } | |
e6e7e540 | 365 | $view_mode = $formValues['view_mode']; |
0f8c6e58 | 366 | |
367 | // CRM-17310 my reports functionality - we should set owner if the checkbox is 1, | |
368 | // it seems to be not set at all if unchecked. | |
369 | if (!empty($formValues['add_to_my_reports'])) { | |
370 | $params['owner_id'] = CRM_Core_Session::singleton()->getLoggedInContactID(); | |
371 | $params['permission'] = 'access own private reports'; | |
372 | $params['grouprole'] = array(); | |
373 | } | |
374 | else { | |
375 | $params['owner_id'] = 'null'; | |
376 | } | |
377 | unset($formValues['add_to_my_reports']); | |
378 | ||
c5958fbe DS |
379 | // pass form_values as string |
380 | $params['form_values'] = serialize($formValues); | |
381 | ||
0b25329b | 382 | $instance = CRM_Report_BAO_ReportInstance::create($params); |
6ea7a229 | 383 | $form->set('id', $instance->id); |
6a488035 TO |
384 | |
385 | if ($instanceID && !$isNew) { | |
386 | // updating existing instance | |
6ea7a229 | 387 | $statusMsg = ts('"%1" report has been updated.', array(1 => $instance->title)); |
0db6c3e1 TO |
388 | } |
389 | elseif ($form->getVar('_id') && $isNew) { | |
6ea7a229 | 390 | $statusMsg = ts('Your report has been successfully copied as "%1". You are currently viewing the new copy.', array(1 => $instance->title)); |
0db6c3e1 TO |
391 | } |
392 | else { | |
6ea7a229 | 393 | $statusMsg = ts('"%1" report has been successfully created. You are currently viewing the new report instance.', array(1 => $instance->title)); |
6a488035 | 394 | } |
6a488035 TO |
395 | CRM_Core_Session::setStatus($statusMsg); |
396 | ||
481a74f4 | 397 | if ($redirect) { |
e6e7e540 | 398 | $urlParams = array('reset' => 1); |
399 | if ($view_mode == 'view') { | |
400 | $urlParams['force'] = 1; | |
401 | } | |
402 | else { | |
403 | $urlParams['output'] = 'criteria'; | |
404 | } | |
405 | CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/report/instance/{$instance->id}", $urlParams)); | |
6a488035 TO |
406 | } |
407 | } | |
96025800 | 408 | |
6a488035 | 409 | } |