Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Report_Form_Instance { | |
36 | ||
74cf4551 | 37 | /** |
c490a46a | 38 | * @param CRM_Core_Form $form |
74cf4551 | 39 | */ |
00be9182 | 40 | public static function buildForm(&$form) { |
6a488035 TO |
41 | // we should not build form elements in dashlet mode |
42 | if ($form->_section) { | |
43 | return; | |
44 | } | |
45 | ||
46 | // check role based permission | |
47 | $instanceID = $form->getVar('_id'); | |
48 | if ($instanceID && !CRM_Report_Utils_Report::isInstanceGroupRoleAllowed($instanceID)) { | |
49 | $url = CRM_Utils_System::url('civicrm/report/list', 'reset=1'); | |
50 | CRM_Core_Error::statusBounce(ts('You do not have permission to access this report.'), | |
51 | $url | |
52 | ); | |
53 | } | |
54 | ||
0b25329b | 55 | $attributes = CRM_Core_DAO::getAttribute('CRM_Report_DAO_ReportInstance'); |
6a488035 TO |
56 | |
57 | $form->add('text', | |
58 | 'title', | |
59 | ts('Report Title'), | |
60 | $attributes['title'] | |
61 | ); | |
62 | ||
63 | $form->add('text', | |
64 | 'description', | |
65 | ts('Report Description'), | |
66 | $attributes['description'] | |
67 | ); | |
68 | ||
69 | $form->add('text', | |
70 | 'email_subject', | |
71 | ts('Subject'), | |
72 | $attributes['email_subject'] | |
73 | ); | |
74 | ||
75 | $form->add('text', | |
76 | 'email_to', | |
77 | ts('To'), | |
78 | $attributes['email_to'] | |
79 | ); | |
80 | ||
81 | $form->add('text', | |
82 | 'email_cc', | |
83 | ts('CC'), | |
84 | $attributes['email_subject'] | |
85 | ); | |
74cf4551 | 86 | |
dbb4a0f9 PN |
87 | $form->add('text', |
88 | 'row_count', | |
89 | ts('Limit Dashboard Results'), | |
84178120 TO |
90 | array( |
91 | 'maxlength' => 64, | |
dbb4a0f9 | 92 | 'size' => 5 |
74cf4551 | 93 | ) |
dbb4a0f9 | 94 | ); |
74cf4551 | 95 | |
6a488035 TO |
96 | $form->add('textarea', |
97 | 'report_header', | |
98 | ts('Report Header'), | |
99 | $attributes['header'] | |
100 | ); | |
101 | ||
102 | $form->add('textarea', | |
103 | 'report_footer', | |
104 | ts('Report Footer'), | |
105 | $attributes['footer'] | |
106 | ); | |
107 | ||
108 | $form->addElement('checkbox', 'is_navigation', ts('Include Report in Navigation Menu?'), NULL, | |
109 | array('onclick' => "return showHideByValue('is_navigation','','navigation_menu','table-row','radio',false);") | |
110 | ); | |
111 | ||
dbb4a0f9 PN |
112 | $form->addElement('checkbox', 'addToDashboard', ts('Available for Dashboard?'), NULL, |
113 | array('onclick' => "return showHideByValue('addToDashboard','','limit_result','table-row','radio',false);")); | |
6a488035 TO |
114 | $form->addElement('checkbox', 'is_reserved', ts('Reserved Report?')); |
115 | if (!CRM_Core_Permission::check('administer reserved reports')) { | |
116 | $form->freeze('is_reserved'); | |
117 | } | |
118 | ||
119 | $config = CRM_Core_Config::singleton(); | |
120 | if ($config->userFramework != 'Joomla' || | |
121 | $config->userFramework != 'WordPress' | |
122 | ) { | |
123 | $form->addElement('select', | |
124 | 'permission', | |
125 | ts('Permission'), | |
126 | array('0' => ts('Everyone (includes anonymous)')) + CRM_Core_Permission::basicPermissions() | |
127 | ); | |
128 | ||
129 | // prepare user_roles to save as names not as ids | |
130 | if (function_exists('user_roles')) { | |
131 | $user_roles_array = user_roles(); | |
132 | foreach ($user_roles_array as $key => $value) { | |
133 | $user_roles[$value] = $value; | |
134 | } | |
1c6954cb | 135 | $grouprole = &$form->addElement('advmultiselect', |
6a488035 TO |
136 | 'grouprole', |
137 | ts('ACL Group/Role'), | |
138 | $user_roles, | |
139 | array( | |
140 | 'size' => 5, | |
141 | 'style' => 'width:240px', | |
142 | 'class' => 'advmultiselect', | |
143 | ) | |
144 | ); | |
1c6954cb AH |
145 | $grouprole->setButtonAttributes('add', array('value' => ts('Add >>'))); |
146 | $grouprole->setButtonAttributes('remove', array('value' => ts('<< Remove'))); | |
6a488035 TO |
147 | } |
148 | } | |
149 | ||
150 | // navigation field | |
151 | $parentMenu = CRM_Core_BAO_Navigation::getNavigationList(); | |
152 | ||
2b27d9df | 153 | $form->add('select', 'parent_id', ts('Parent Menu'), array('' => ts('- select -')) + $parentMenu); |
6a488035 TO |
154 | |
155 | // For now we only providing drilldown for one primary detail report only. In future this could be multiple reports | |
156 | foreach ($form->_drilldownReport as $reportUrl => $drillLabel) { | |
157 | $instanceList = CRM_Report_Utils_Report::getInstanceList($reportUrl); | |
84178120 | 158 | if (count($instanceList) > 1) { |
6a488035 | 159 | $form->add('select', 'drilldown_id', $drillLabel, array('' => ts('- select -')) + $instanceList); |
84178120 | 160 | } |
6a488035 TO |
161 | break; |
162 | } | |
163 | ||
164 | $form->addButtons(array( | |
165 | array( | |
166 | 'type' => 'submit', | |
167 | 'name' => ts('Save Report'), | |
168 | 'isDefault' => TRUE, | |
169 | ), | |
170 | array( | |
171 | 'type' => 'cancel', | |
172 | 'name' => ts('Cancel'), | |
173 | ), | |
174 | ) | |
175 | ); | |
176 | ||
177 | $form->addFormRule(array('CRM_Report_Form_Instance', 'formRule'), $form); | |
178 | } | |
179 | ||
74cf4551 EM |
180 | /** |
181 | * @param $fields | |
182 | * @param $errors | |
183 | * @param $self | |
184 | * | |
185 | * @return array|bool | |
186 | */ | |
00be9182 | 187 | public static function formRule($fields, $errors, $self) { |
c9cf6554 CW |
188 | // Validate both the "next" and "save" buttons for creating/updating a report |
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 | /** |
c490a46a | 205 | * @param CRM_Core_Form $form |
74cf4551 EM |
206 | * @param $defaults |
207 | */ | |
00be9182 | 208 | public static function setDefaultValues(&$form, &$defaults) { |
6a488035 TO |
209 | // we should not build form elements in dashlet mode |
210 | if ($form->_section) { | |
211 | return; | |
212 | } | |
213 | ||
214 | $instanceID = $form->getVar('_id'); | |
215 | $navigationDefaults = array(); | |
216 | ||
9b873358 | 217 | if (!isset($defaults['permission'])) { |
84178120 TO |
218 | $permissions = array_flip(CRM_Core_Permission::basicPermissions( )); |
219 | $defaults['permission'] = $permissions['CiviReport: access CiviReport']; | |
6a488035 TO |
220 | } |
221 | ||
222 | $config = CRM_Core_Config::singleton(); | |
223 | $defaults['report_header'] = $report_header = "<html> | |
224 | <head> | |
225 | <title>CiviCRM Report</title> | |
226 | <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> | |
227 | <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style> | |
228 | </head> | |
229 | <body><div id=\"crm-container\">"; | |
230 | ||
231 | $defaults['report_footer'] = $report_footer = "<p><img src=\"{$config->userFrameworkResourceURL}i/powered_by.png\" /></p></div></body> | |
232 | </html> | |
233 | "; | |
234 | ||
235 | if ($instanceID) { | |
236 | // this is already retrieved via Form.php | |
237 | $defaults['description'] = CRM_Utils_Array::value('description', $defaults); | |
238 | $defaults['report_header'] = CRM_Utils_Array::value('header', $defaults); | |
239 | $defaults['report_footer'] = CRM_Utils_Array::value('footer', $defaults); | |
240 | ||
a7488080 | 241 | if (!empty($defaults['navigation_id'])) { |
6a488035 TO |
242 | //get the default navigation parent id |
243 | $params = array('id' => $defaults['navigation_id']); | |
244 | CRM_Core_BAO_Navigation::retrieve($params, $navigationDefaults); | |
245 | $defaults['is_navigation'] = 1; | |
246 | $defaults['parent_id'] = CRM_Utils_Array::value('parent_id', $navigationDefaults); | |
247 | ||
a7488080 | 248 | if (!empty($navigationDefaults['is_active'])) { |
6a488035 TO |
249 | $form->assign('is_navigation', TRUE); |
250 | } | |
251 | ||
a7488080 | 252 | if (!empty($navigationDefaults['id'])) { |
6a488035 TO |
253 | $form->_navigation['id'] = $navigationDefaults['id']; |
254 | $form->_navigation['parent_id'] = $navigationDefaults['parent_id']; | |
255 | } | |
256 | } | |
257 | ||
a7488080 | 258 | if (!empty($defaults['grouprole'])) { |
6a488035 TO |
259 | foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['grouprole']) as $value) { |
260 | $grouproles[] = $value; | |
261 | } | |
262 | $defaults['grouprole'] = $grouproles; | |
263 | } | |
264 | } | |
ae555e90 | 265 | else if (property_exists($form, '_description')) { |
6a488035 TO |
266 | $defaults['description'] = $form->_description; |
267 | } | |
268 | } | |
269 | ||
74cf4551 | 270 | /** |
c490a46a | 271 | * @param CRM_Core_Form $form |
74cf4551 EM |
272 | * @param bool $redirect |
273 | */ | |
00be9182 | 274 | public static function postProcess(&$form, $redirect = TRUE) { |
81583117 | 275 | $params = $form->getVar('_params'); |
6a488035 | 276 | $instanceID = $form->getVar('_id'); |
f813f78e | 277 | |
6ea7a229 | 278 | if ($isNew = $form->getVar('_createNew')) { |
e41b14ce DS |
279 | // set the report_id since base template is going to be same, and we going to unset $instanceID |
280 | // which will make it difficult later on, to compute report_id | |
281 | $params['report_id'] = CRM_Report_Utils_Report::getValueFromUrl($instanceID); | |
282 | $instanceID = NULL; //unset $instanceID so a new copy would be created | |
6a488035 | 283 | } |
6ea7a229 | 284 | $params['instance_id'] = $instanceID; |
a7488080 | 285 | if (!empty($params['is_navigation'])) { |
6ea7a229 | 286 | $params['navigation'] = $form->_navigation; |
6a488035 | 287 | } |
9b873358 | 288 | elseif ($instanceID) { |
81583117 BS |
289 | //delete navigation if exists |
290 | $navId = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instanceID, 'navigation_id', 'id'); | |
291 | if ($navId) { | |
292 | CRM_Core_BAO_Navigation::processDelete($navId); | |
293 | CRM_Core_BAO_Navigation::resetNavigation(); | |
294 | } | |
295 | } | |
6a488035 | 296 | |
c5958fbe DS |
297 | // make a copy of params |
298 | $formValues = $params; | |
299 | ||
300 | // unset params from $formValues that doesn't match with DB columns of instance tables, and also not required in form-values for sure | |
301 | $unsetFields = array( | |
302 | 'title', 'to_emails', 'cc_emails', 'header', 'footer', | |
303 | 'qfKey', 'id', '_qf_default', 'report_header', 'report_footer', 'grouprole', | |
304 | ); | |
305 | foreach ($unsetFields as $field) { | |
306 | unset($formValues[$field]); | |
307 | } | |
308 | // pass form_values as string | |
309 | $params['form_values'] = serialize($formValues); | |
310 | ||
0b25329b | 311 | $instance = CRM_Report_BAO_ReportInstance::create($params); |
6ea7a229 | 312 | $form->set('id', $instance->id); |
6a488035 TO |
313 | |
314 | if ($instanceID && !$isNew) { | |
315 | // updating existing instance | |
6ea7a229 | 316 | $statusMsg = ts('"%1" report has been updated.', array(1 => $instance->title)); |
0db6c3e1 TO |
317 | } |
318 | elseif ($form->getVar('_id') && $isNew) { | |
6ea7a229 | 319 | $statusMsg = ts('Your report has been successfully copied as "%1". You are currently viewing the new copy.', array(1 => $instance->title)); |
0db6c3e1 TO |
320 | } |
321 | else { | |
6ea7a229 | 322 | $statusMsg = ts('"%1" report has been successfully created. You are currently viewing the new report instance.', array(1 => $instance->title)); |
6a488035 | 323 | } |
6a488035 TO |
324 | CRM_Core_Session::setStatus($statusMsg); |
325 | ||
326 | if ( $redirect ) { | |
6ea7a229 | 327 | CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/report/instance/{$instance->id}", "reset=1")); |
6a488035 TO |
328 | } |
329 | } | |
330 | } |