Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | ||
dbb4a0f9 PN |
110 | $form->addElement('checkbox', 'addToDashboard', ts('Available for Dashboard?'), NULL, |
111 | array('onclick' => "return showHideByValue('addToDashboard','','limit_result','table-row','radio',false);")); | |
6a488035 TO |
112 | $form->addElement('checkbox', 'is_reserved', ts('Reserved Report?')); |
113 | if (!CRM_Core_Permission::check('administer reserved reports')) { | |
114 | $form->freeze('is_reserved'); | |
115 | } | |
116 | ||
117 | $config = CRM_Core_Config::singleton(); | |
118 | if ($config->userFramework != 'Joomla' || | |
119 | $config->userFramework != 'WordPress' | |
120 | ) { | |
121 | $form->addElement('select', | |
122 | 'permission', | |
123 | ts('Permission'), | |
124 | array('0' => ts('Everyone (includes anonymous)')) + CRM_Core_Permission::basicPermissions() | |
125 | ); | |
126 | ||
127 | // prepare user_roles to save as names not as ids | |
128 | if (function_exists('user_roles')) { | |
129 | $user_roles_array = user_roles(); | |
130 | foreach ($user_roles_array as $key => $value) { | |
131 | $user_roles[$value] = $value; | |
132 | } | |
1c6954cb | 133 | $grouprole = &$form->addElement('advmultiselect', |
6a488035 TO |
134 | 'grouprole', |
135 | ts('ACL Group/Role'), | |
136 | $user_roles, | |
137 | array( | |
138 | 'size' => 5, | |
139 | 'style' => 'width:240px', | |
140 | 'class' => 'advmultiselect', | |
141 | ) | |
142 | ); | |
1c6954cb AH |
143 | $grouprole->setButtonAttributes('add', array('value' => ts('Add >>'))); |
144 | $grouprole->setButtonAttributes('remove', array('value' => ts('<< Remove'))); | |
6a488035 TO |
145 | } |
146 | } | |
147 | ||
148 | // navigation field | |
149 | $parentMenu = CRM_Core_BAO_Navigation::getNavigationList(); | |
150 | ||
2b27d9df | 151 | $form->add('select', 'parent_id', ts('Parent Menu'), array('' => ts('- select -')) + $parentMenu); |
6a488035 TO |
152 | |
153 | // For now we only providing drilldown for one primary detail report only. In future this could be multiple reports | |
154 | foreach ($form->_drilldownReport as $reportUrl => $drillLabel) { | |
155 | $instanceList = CRM_Report_Utils_Report::getInstanceList($reportUrl); | |
84178120 | 156 | if (count($instanceList) > 1) { |
6a488035 | 157 | $form->add('select', 'drilldown_id', $drillLabel, array('' => ts('- select -')) + $instanceList); |
84178120 | 158 | } |
6a488035 TO |
159 | break; |
160 | } | |
161 | ||
162 | $form->addButtons(array( | |
163 | array( | |
164 | 'type' => 'submit', | |
165 | 'name' => ts('Save Report'), | |
166 | 'isDefault' => TRUE, | |
167 | ), | |
168 | array( | |
169 | 'type' => 'cancel', | |
170 | 'name' => ts('Cancel'), | |
171 | ), | |
172 | ) | |
173 | ); | |
174 | ||
175 | $form->addFormRule(array('CRM_Report_Form_Instance', 'formRule'), $form); | |
176 | } | |
177 | ||
74cf4551 | 178 | /** |
4e580cdf EM |
179 | * Add form rules. |
180 | * | |
181 | * @param array $fields | |
182 | * @param array $errors | |
183 | * @param CRM_Report_Form_Instance $self | |
74cf4551 EM |
184 | * |
185 | * @return array|bool | |
186 | */ | |
00be9182 | 187 | public static function formRule($fields, $errors, $self) { |
4e580cdf | 188 | // Validate both the "next" and "save" buttons for creating/updating a report. |
c9cf6554 CW |
189 | $nextButton = $self->controller->getButtonName(); |
190 | $saveButton = str_replace('_next', '_save', $nextButton); | |
191 | $clickedButton = $self->getVar('_instanceButtonName'); | |
6a488035 TO |
192 | |
193 | $errors = array(); | |
c9cf6554 | 194 | if ($clickedButton == $nextButton || $clickedButton == $saveButton) { |
6a488035 | 195 | if (empty($fields['title'])) { |
161f6a74 | 196 | $errors['title'] = ts('Title is a required field.'); |
6a488035 TO |
197 | $self->assign('instanceFormError', TRUE); |
198 | } | |
199 | } | |
200 | ||
201 | return empty($errors) ? TRUE : $errors; | |
202 | } | |
203 | ||
74cf4551 | 204 | /** |
4e580cdf EM |
205 | * Set default values. |
206 | * | |
c490a46a | 207 | * @param CRM_Core_Form $form |
4e580cdf | 208 | * @param array $defaults |
74cf4551 | 209 | */ |
00be9182 | 210 | public static function setDefaultValues(&$form, &$defaults) { |
4e580cdf | 211 | // we should not build form elements in dashlet mode. |
6a488035 TO |
212 | if ($form->_section) { |
213 | return; | |
214 | } | |
215 | ||
216 | $instanceID = $form->getVar('_id'); | |
217 | $navigationDefaults = array(); | |
218 | ||
9b873358 | 219 | if (!isset($defaults['permission'])) { |
481a74f4 | 220 | $permissions = array_flip(CRM_Core_Permission::basicPermissions()); |
84178120 | 221 | $defaults['permission'] = $permissions['CiviReport: access CiviReport']; |
6a488035 TO |
222 | } |
223 | ||
224 | $config = CRM_Core_Config::singleton(); | |
225 | $defaults['report_header'] = $report_header = "<html> | |
226 | <head> | |
227 | <title>CiviCRM Report</title> | |
228 | <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> | |
229 | <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style> | |
230 | </head> | |
231 | <body><div id=\"crm-container\">"; | |
232 | ||
233 | $defaults['report_footer'] = $report_footer = "<p><img src=\"{$config->userFrameworkResourceURL}i/powered_by.png\" /></p></div></body> | |
234 | </html> | |
235 | "; | |
236 | ||
237 | if ($instanceID) { | |
238 | // this is already retrieved via Form.php | |
239 | $defaults['description'] = CRM_Utils_Array::value('description', $defaults); | |
240 | $defaults['report_header'] = CRM_Utils_Array::value('header', $defaults); | |
241 | $defaults['report_footer'] = CRM_Utils_Array::value('footer', $defaults); | |
242 | ||
a7488080 | 243 | if (!empty($defaults['navigation_id'])) { |
734d3871 | 244 | // Get the default navigation parent id. |
6a488035 TO |
245 | $params = array('id' => $defaults['navigation_id']); |
246 | CRM_Core_BAO_Navigation::retrieve($params, $navigationDefaults); | |
247 | $defaults['is_navigation'] = 1; | |
248 | $defaults['parent_id'] = CRM_Utils_Array::value('parent_id', $navigationDefaults); | |
249 | ||
a7488080 | 250 | if (!empty($navigationDefaults['is_active'])) { |
6a488035 TO |
251 | $form->assign('is_navigation', TRUE); |
252 | } | |
253 | ||
a7488080 | 254 | if (!empty($navigationDefaults['id'])) { |
6a488035 | 255 | $form->_navigation['id'] = $navigationDefaults['id']; |
734d3871 EM |
256 | $form->_navigation['parent_id'] = !empty($navigationDefaults['parent_id']) ? |
257 | $navigationDefaults['parent_id'] : NULL; | |
6a488035 TO |
258 | } |
259 | } | |
260 | ||
a7488080 | 261 | if (!empty($defaults['grouprole'])) { |
6a488035 | 262 | foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['grouprole']) as $value) { |
4e580cdf | 263 | $groupRoles[] = $value; |
6a488035 | 264 | } |
4e580cdf | 265 | $defaults['grouprole'] = $groupRoles; |
6a488035 TO |
266 | } |
267 | } | |
4c9b6178 | 268 | elseif (property_exists($form, '_description')) { |
6a488035 TO |
269 | $defaults['description'] = $form->_description; |
270 | } | |
271 | } | |
272 | ||
74cf4551 | 273 | /** |
4e580cdf EM |
274 | * Post process function. |
275 | * | |
c490a46a | 276 | * @param CRM_Core_Form $form |
74cf4551 EM |
277 | * @param bool $redirect |
278 | */ | |
00be9182 | 279 | public static function postProcess(&$form, $redirect = TRUE) { |
81583117 | 280 | $params = $form->getVar('_params'); |
6a488035 | 281 | $instanceID = $form->getVar('_id'); |
f813f78e | 282 | |
6ea7a229 | 283 | if ($isNew = $form->getVar('_createNew')) { |
e41b14ce DS |
284 | // set the report_id since base template is going to be same, and we going to unset $instanceID |
285 | // which will make it difficult later on, to compute report_id | |
286 | $params['report_id'] = CRM_Report_Utils_Report::getValueFromUrl($instanceID); | |
4e580cdf EM |
287 | // Unset $instanceID so a new copy would be created. |
288 | $instanceID = NULL; | |
6a488035 | 289 | } |
6ea7a229 | 290 | $params['instance_id'] = $instanceID; |
a7488080 | 291 | if (!empty($params['is_navigation'])) { |
6ea7a229 | 292 | $params['navigation'] = $form->_navigation; |
6a488035 | 293 | } |
9b873358 | 294 | elseif ($instanceID) { |
4e580cdf | 295 | // Delete navigation if exists. |
81583117 BS |
296 | $navId = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instanceID, 'navigation_id', 'id'); |
297 | if ($navId) { | |
298 | CRM_Core_BAO_Navigation::processDelete($navId); | |
299 | CRM_Core_BAO_Navigation::resetNavigation(); | |
300 | } | |
301 | } | |
6a488035 | 302 | |
c5958fbe DS |
303 | // make a copy of params |
304 | $formValues = $params; | |
305 | ||
306 | // unset params from $formValues that doesn't match with DB columns of instance tables, and also not required in form-values for sure | |
307 | $unsetFields = array( | |
353ffa53 TO |
308 | 'title', |
309 | 'to_emails', | |
310 | 'cc_emails', | |
311 | 'header', | |
312 | 'footer', | |
313 | 'qfKey', | |
314 | 'id', | |
315 | '_qf_default', | |
316 | 'report_header', | |
317 | 'report_footer', | |
318 | 'grouprole', | |
c5958fbe DS |
319 | ); |
320 | foreach ($unsetFields as $field) { | |
321 | unset($formValues[$field]); | |
322 | } | |
323 | // pass form_values as string | |
324 | $params['form_values'] = serialize($formValues); | |
325 | ||
0b25329b | 326 | $instance = CRM_Report_BAO_ReportInstance::create($params); |
6ea7a229 | 327 | $form->set('id', $instance->id); |
6a488035 TO |
328 | |
329 | if ($instanceID && !$isNew) { | |
330 | // updating existing instance | |
6ea7a229 | 331 | $statusMsg = ts('"%1" report has been updated.', array(1 => $instance->title)); |
0db6c3e1 TO |
332 | } |
333 | elseif ($form->getVar('_id') && $isNew) { | |
6ea7a229 | 334 | $statusMsg = ts('Your report has been successfully copied as "%1". You are currently viewing the new copy.', array(1 => $instance->title)); |
0db6c3e1 TO |
335 | } |
336 | else { | |
6ea7a229 | 337 | $statusMsg = ts('"%1" report has been successfully created. You are currently viewing the new report instance.', array(1 => $instance->title)); |
6a488035 | 338 | } |
6a488035 TO |
339 | CRM_Core_Session::setStatus($statusMsg); |
340 | ||
481a74f4 | 341 | if ($redirect) { |
6ea7a229 | 342 | CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/report/instance/{$instance->id}", "reset=1")); |
6a488035 TO |
343 | } |
344 | } | |
96025800 | 345 | |
6a488035 | 346 | } |