Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // $Id$ | |
3 | ||
4 | /* | |
5 | +--------------------------------------------------------------------+ | |
6 | | CiviCRM version 4.3 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
9 | +--------------------------------------------------------------------+ | |
10 | | This file is a part of CiviCRM. | | |
11 | | | | |
12 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
13 | | under the terms of the GNU Affero General Public License | | |
14 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
15 | | | | |
16 | | CiviCRM is distributed in the hope that it will be useful, but | | |
17 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
19 | | See the GNU Affero General Public License for more details. | | |
20 | | | | |
21 | | You should have received a copy of the GNU Affero General Public | | |
22 | | License and the CiviCRM Licensing Exception along | | |
23 | | with this program; if not, contact CiviCRM LLC | | |
24 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
25 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
26 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
27 | +--------------------------------------------------------------------+ | |
28 | */ | |
29 | ||
30 | /** | |
31 | * | |
32 | * @package CRM | |
33 | * @copyright CiviCRM LLC (c) 2004-2013 | |
34 | * $Id$ | |
35 | * | |
36 | */ | |
37 | class CRM_Report_Form_Instance { | |
38 | ||
39 | static function buildForm(&$form) { | |
40 | // we should not build form elements in dashlet mode | |
41 | if ($form->_section) { | |
42 | return; | |
43 | } | |
44 | ||
45 | // check role based permission | |
46 | $instanceID = $form->getVar('_id'); | |
47 | if ($instanceID && !CRM_Report_Utils_Report::isInstanceGroupRoleAllowed($instanceID)) { | |
48 | $url = CRM_Utils_System::url('civicrm/report/list', 'reset=1'); | |
49 | CRM_Core_Error::statusBounce(ts('You do not have permission to access this report.'), | |
50 | $url | |
51 | ); | |
52 | } | |
53 | ||
54 | $attributes = CRM_Core_DAO::getAttribute('CRM_Report_DAO_Instance'); | |
55 | ||
56 | $form->add('text', | |
57 | 'title', | |
58 | ts('Report Title'), | |
59 | $attributes['title'] | |
60 | ); | |
61 | ||
62 | $form->add('text', | |
63 | 'description', | |
64 | ts('Report Description'), | |
65 | $attributes['description'] | |
66 | ); | |
67 | ||
68 | $form->add('text', | |
69 | 'email_subject', | |
70 | ts('Subject'), | |
71 | $attributes['email_subject'] | |
72 | ); | |
73 | ||
74 | $form->add('text', | |
75 | 'email_to', | |
76 | ts('To'), | |
77 | $attributes['email_to'] | |
78 | ); | |
79 | ||
80 | $form->add('text', | |
81 | 'email_cc', | |
82 | ts('CC'), | |
83 | $attributes['email_subject'] | |
84 | ); | |
85 | ||
86 | $form->add('textarea', | |
87 | 'report_header', | |
88 | ts('Report Header'), | |
89 | $attributes['header'] | |
90 | ); | |
91 | ||
92 | $form->add('textarea', | |
93 | 'report_footer', | |
94 | ts('Report Footer'), | |
95 | $attributes['footer'] | |
96 | ); | |
97 | ||
98 | $form->addElement('checkbox', 'is_navigation', ts('Include Report in Navigation Menu?'), NULL, | |
99 | array('onclick' => "return showHideByValue('is_navigation','','navigation_menu','table-row','radio',false);") | |
100 | ); | |
101 | ||
102 | $form->addElement('checkbox', 'addToDashboard', ts('Available for Dashboard?')); | |
103 | $form->addElement('checkbox', 'is_reserved', ts('Reserved Report?')); | |
104 | if (!CRM_Core_Permission::check('administer reserved reports')) { | |
105 | $form->freeze('is_reserved'); | |
106 | } | |
107 | ||
108 | $config = CRM_Core_Config::singleton(); | |
109 | if ($config->userFramework != 'Joomla' || | |
110 | $config->userFramework != 'WordPress' | |
111 | ) { | |
112 | $form->addElement('select', | |
113 | 'permission', | |
114 | ts('Permission'), | |
115 | array('0' => ts('Everyone (includes anonymous)')) + CRM_Core_Permission::basicPermissions() | |
116 | ); | |
117 | ||
118 | // prepare user_roles to save as names not as ids | |
119 | if (function_exists('user_roles')) { | |
120 | $user_roles_array = user_roles(); | |
121 | foreach ($user_roles_array as $key => $value) { | |
122 | $user_roles[$value] = $value; | |
123 | } | |
1c6954cb | 124 | $grouprole = &$form->addElement('advmultiselect', |
6a488035 TO |
125 | 'grouprole', |
126 | ts('ACL Group/Role'), | |
127 | $user_roles, | |
128 | array( | |
129 | 'size' => 5, | |
130 | 'style' => 'width:240px', | |
131 | 'class' => 'advmultiselect', | |
132 | ) | |
133 | ); | |
1c6954cb AH |
134 | $grouprole->setButtonAttributes('add', array('value' => ts('Add >>'))); |
135 | $grouprole->setButtonAttributes('remove', array('value' => ts('<< Remove'))); | |
6a488035 TO |
136 | } |
137 | } | |
138 | ||
139 | // navigation field | |
140 | $parentMenu = CRM_Core_BAO_Navigation::getNavigationList(); | |
141 | ||
142 | $form->add('select', 'parent_id', ts('Parent Menu'), array('' => ts('-- select --')) + $parentMenu); | |
143 | ||
144 | // For now we only providing drilldown for one primary detail report only. In future this could be multiple reports | |
145 | foreach ($form->_drilldownReport as $reportUrl => $drillLabel) { | |
146 | $instanceList = CRM_Report_Utils_Report::getInstanceList($reportUrl); | |
147 | if (count($instanceList) > 1) | |
148 | $form->add('select', 'drilldown_id', $drillLabel, array('' => ts('- select -')) + $instanceList); | |
149 | break; | |
150 | } | |
151 | ||
152 | $form->addButtons(array( | |
153 | array( | |
154 | 'type' => 'submit', | |
155 | 'name' => ts('Save Report'), | |
156 | 'isDefault' => TRUE, | |
157 | ), | |
158 | array( | |
159 | 'type' => 'cancel', | |
160 | 'name' => ts('Cancel'), | |
161 | ), | |
162 | ) | |
163 | ); | |
164 | ||
165 | $form->addFormRule(array('CRM_Report_Form_Instance', 'formRule'), $form); | |
166 | } | |
167 | ||
168 | static function formRule($fields, $errors, $self) { | |
169 | $buttonName = $self->controller->getButtonName(); | |
170 | $selfButtonName = $self->getVar('_instanceButtonName'); | |
171 | ||
172 | $errors = array(); | |
173 | if ($selfButtonName == $buttonName) { | |
174 | if (empty($fields['title'])) { | |
175 | $errors['title'] = ts('Title is a required field'); | |
176 | $self->assign('instanceFormError', TRUE); | |
177 | } | |
178 | } | |
179 | ||
180 | return empty($errors) ? TRUE : $errors; | |
181 | } | |
182 | ||
183 | static function setDefaultValues(&$form, &$defaults) { | |
184 | // we should not build form elements in dashlet mode | |
185 | if ($form->_section) { | |
186 | return; | |
187 | } | |
188 | ||
189 | $instanceID = $form->getVar('_id'); | |
190 | $navigationDefaults = array(); | |
191 | ||
192 | if (!isset($defaults['permission'])){ | |
193 | $permissions = array_flip(CRM_Core_Permission::basicPermissions( )); | |
194 | $defaults['permission'] = $permissions['CiviReport: access CiviReport']; | |
195 | } | |
196 | ||
197 | $config = CRM_Core_Config::singleton(); | |
198 | $defaults['report_header'] = $report_header = "<html> | |
199 | <head> | |
200 | <title>CiviCRM Report</title> | |
201 | <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> | |
202 | <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style> | |
203 | </head> | |
204 | <body><div id=\"crm-container\">"; | |
205 | ||
206 | $defaults['report_footer'] = $report_footer = "<p><img src=\"{$config->userFrameworkResourceURL}i/powered_by.png\" /></p></div></body> | |
207 | </html> | |
208 | "; | |
209 | ||
210 | if ($instanceID) { | |
211 | // this is already retrieved via Form.php | |
212 | $defaults['description'] = CRM_Utils_Array::value('description', $defaults); | |
213 | $defaults['report_header'] = CRM_Utils_Array::value('header', $defaults); | |
214 | $defaults['report_footer'] = CRM_Utils_Array::value('footer', $defaults); | |
215 | ||
216 | if (CRM_Utils_Array::value('navigation_id', $defaults)) { | |
217 | //get the default navigation parent id | |
218 | $params = array('id' => $defaults['navigation_id']); | |
219 | CRM_Core_BAO_Navigation::retrieve($params, $navigationDefaults); | |
220 | $defaults['is_navigation'] = 1; | |
221 | $defaults['parent_id'] = CRM_Utils_Array::value('parent_id', $navigationDefaults); | |
222 | ||
223 | if (CRM_Utils_Array::value('is_active', $navigationDefaults)) { | |
224 | $form->assign('is_navigation', TRUE); | |
225 | } | |
226 | ||
227 | if (CRM_Utils_Array::value('id', $navigationDefaults)) { | |
228 | $form->_navigation['id'] = $navigationDefaults['id']; | |
229 | $form->_navigation['parent_id'] = $navigationDefaults['parent_id']; | |
230 | } | |
231 | } | |
232 | ||
233 | if (CRM_Utils_Array::value('grouprole', $defaults)) { | |
234 | foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['grouprole']) as $value) { | |
235 | $grouproles[] = $value; | |
236 | } | |
237 | $defaults['grouprole'] = $grouproles; | |
238 | } | |
239 | } | |
240 | else { | |
241 | $defaults['description'] = $form->_description; | |
242 | } | |
243 | } | |
244 | ||
245 | static function postProcess(&$form, $redirect = TRUE) { | |
246 | $params = $form->getVar('_params'); | |
247 | $config = CRM_Core_Config::singleton(); | |
248 | $params['header'] = CRM_Utils_Array::value('report_header',$params); | |
249 | $params['footer'] = CRM_Utils_Array::value('report_footer',$params); | |
250 | $params['domain_id'] = CRM_Core_Config::domainID(); | |
251 | ||
252 | //navigation parameters | |
253 | if (CRM_Utils_Array::value('is_navigation', $params)) { | |
254 | $form->_navigation['permission'] = array(); | |
255 | $form->_navigation['label'] = $params['title']; | |
256 | $form->_navigation['name'] = $params['title']; | |
257 | ||
258 | $permission = CRM_Utils_Array::value('permission', $params); | |
259 | ||
260 | $form->_navigation['current_parent_id'] = CRM_Utils_Array::value('parent_id', $form->_navigation); | |
261 | $form->_navigation['parent_id'] = CRM_Utils_Array::value('parent_id', $params); | |
262 | $form->_navigation['is_active'] = 1; | |
263 | ||
264 | if ($permission) { | |
265 | $form->_navigation['permission'][] = $permission; | |
266 | } | |
267 | //unset the navigation related element, | |
268 | //not used in report form values | |
269 | unset($params['parent_id']); | |
270 | unset($params['is_navigation']); | |
271 | } | |
272 | ||
273 | // convert roles array to string | |
274 | if (isset($params['grouprole']) && is_array($params['grouprole'])) { | |
275 | $grouprole_array = array(); | |
276 | foreach ($params['grouprole'] as $key => $value) { | |
277 | $grouprole_array[$value] = $value; | |
278 | } | |
279 | $params['grouprole'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
280 | array_keys($grouprole_array) | |
281 | ); | |
282 | } | |
283 | ||
284 | // add to dashboard | |
285 | $dashletParams = array(); | |
286 | if (CRM_Utils_Array::value('addToDashboard', $params)) { | |
287 | $dashletParams = array( | |
288 | 'label' => $params['title'], | |
289 | 'is_active' => 1, | |
290 | ); | |
291 | ||
292 | $permission = CRM_Utils_Array::value('permission', $params); | |
293 | if ($permission) { | |
294 | $dashletParams['permission'][] = $permission; | |
295 | } | |
296 | } | |
297 | $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE); | |
298 | ||
299 | $dao = new CRM_Report_DAO_Instance(); | |
300 | $dao->copyValues($params); | |
301 | ||
302 | if ($config->userFramework == 'Joomla') { | |
303 | $dao->permission = 'null'; | |
304 | } | |
305 | ||
306 | // explicitly set to null if params value is empty | |
307 | if (empty($params['grouprole'])) { | |
308 | $dao->grouprole = 'null'; | |
309 | } | |
310 | ||
311 | // unset all the params that we use | |
312 | $fields = array( | |
313 | 'title', 'to_emails', 'cc_emails', 'header', 'footer', | |
314 | 'qfKey', '_qf_default', 'report_header', 'report_footer', 'grouprole', | |
315 | ); | |
316 | foreach ($fields as $field) { | |
317 | unset($params[$field]); | |
318 | } | |
319 | $dao->form_values = serialize($params); | |
320 | ||
321 | $instanceID = $form->getVar('_id'); | |
322 | $isNew = $form->getVar('_createNew'); | |
323 | $isCopy = 0; | |
324 | ||
325 | if ($instanceID) { | |
326 | if (!$isNew) { | |
327 | // updating an existing report instance | |
328 | $dao->id = $instanceID; | |
329 | } else { | |
330 | // making a copy of an existing instance | |
331 | $isCopy = 1; | |
332 | } | |
333 | } | |
334 | ||
335 | $dao->report_id = CRM_Report_Utils_Report::getValueFromUrl($instanceID); | |
336 | ||
337 | $dao->save(); | |
338 | ||
339 | $form->set('id', $dao->id); | |
340 | ||
341 | if (!empty($form->_navigation)) { | |
342 | if ($isNew && CRM_Utils_Array::value('id', $form->_navigation)) { | |
343 | unset($form->_navigation['id']); | |
344 | } | |
345 | $form->_navigation['url'] = "civicrm/report/instance/{$dao->id}&reset=1"; | |
346 | $navigation = CRM_Core_BAO_Navigation::add($form->_navigation); | |
347 | ||
348 | if (CRM_Utils_Array::value('is_active', $form->_navigation)) { | |
349 | //set the navigation id in report instance table | |
350 | CRM_Core_DAO::setFieldValue('CRM_Report_DAO_Instance', $dao->id, 'navigation_id', $navigation->id); | |
351 | } | |
352 | else { | |
353 | // has been removed from the navigation bar | |
354 | CRM_Core_DAO::setFieldValue('CRM_Report_DAO_Instance', $dao->id, 'navigation_id', 'NULL'); | |
355 | } | |
356 | ||
357 | //reset navigation | |
358 | CRM_Core_BAO_Navigation::resetNavigation(); | |
359 | } | |
360 | ||
361 | // add to dashlet | |
362 | if (!empty($dashletParams)) { | |
363 | $section = 2; | |
364 | $chart = ''; | |
365 | if (CRM_Utils_Array::value('charts', $params)) { | |
366 | $section = 1; | |
367 | $chart = "&charts=" . $params['charts']; | |
368 | } | |
369 | ||
370 | $dashletParams['url'] = "civicrm/report/instance/{$dao->id}&reset=1§ion={$section}&snippet=5{$chart}&context=dashlet"; | |
371 | $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$dao->id}&reset=1§ion={$section}&snippet=5{$chart}&context=dashletFullscreen"; | |
372 | $dashletParams['instanceURL'] = "civicrm/report/instance/{$dao->id}"; | |
373 | CRM_Core_BAO_Dashboard::addDashlet($dashletParams); | |
374 | } | |
375 | ||
376 | $instanceParams = array('value' => $dao->report_id); | |
377 | $instanceDefaults = array(); | |
378 | $cmpName = "Contact"; | |
379 | $statusMsg = "null"; | |
380 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_OptionValue', | |
381 | $instanceParams, | |
382 | $instanceDefaults | |
383 | ); | |
384 | ||
385 | if ($cmpID = CRM_Utils_Array::value('component_id', $instanceDefaults)) { | |
386 | $cmpName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Component', $cmpID, | |
387 | 'name', 'id' | |
388 | ); | |
389 | $cmpName = substr($cmpName, 4); | |
390 | } | |
391 | ||
392 | if ($instanceID && !$isNew) { | |
393 | // updating existing instance | |
394 | $statusMsg = ts('"%1" report has been updated.', array(1 => $dao->title)); | |
395 | } elseif ($isCopy) { | |
396 | $statusMsg = ts('Your report has been successfully copied as "%1". You are currently viewing the new copy.', array(1 => $dao->title)); | |
397 | } else { | |
398 | $statusMsg = ts('"%1" report has been successfully created. You are currently viewing the new report instance.', array(1 => $dao->title)); | |
399 | } | |
400 | ||
401 | $instanceUrl = CRM_Utils_System::url("civicrm/report/instance/{$dao->id}", | |
402 | "reset=1" | |
403 | ); | |
404 | ||
405 | CRM_Core_Session::setStatus($statusMsg); | |
406 | ||
407 | if ( $redirect ) { | |
408 | CRM_Utils_System::redirect($instanceUrl); | |
409 | } | |
410 | } | |
411 | } | |
412 |