Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 EM |
37 | /** |
38 | * @param $form | |
39 | */ | |
6a488035 TO |
40 | static function buildForm(&$form) { |
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'), | |
90 | array('maxlength' => 64, | |
91 | 'size' => 5 | |
74cf4551 | 92 | ) |
dbb4a0f9 | 93 | ); |
74cf4551 | 94 | |
6a488035 TO |
95 | $form->add('textarea', |
96 | 'report_header', | |
97 | ts('Report Header'), | |
98 | $attributes['header'] | |
99 | ); | |
100 | ||
101 | $form->add('textarea', | |
102 | 'report_footer', | |
103 | ts('Report Footer'), | |
104 | $attributes['footer'] | |
105 | ); | |
106 | ||
107 | $form->addElement('checkbox', 'is_navigation', ts('Include Report in Navigation Menu?'), NULL, | |
108 | array('onclick' => "return showHideByValue('is_navigation','','navigation_menu','table-row','radio',false);") | |
109 | ); | |
110 | ||
dbb4a0f9 PN |
111 | $form->addElement('checkbox', 'addToDashboard', ts('Available for Dashboard?'), NULL, |
112 | array('onclick' => "return showHideByValue('addToDashboard','','limit_result','table-row','radio',false);")); | |
6a488035 TO |
113 | $form->addElement('checkbox', 'is_reserved', ts('Reserved Report?')); |
114 | if (!CRM_Core_Permission::check('administer reserved reports')) { | |
115 | $form->freeze('is_reserved'); | |
116 | } | |
117 | ||
118 | $config = CRM_Core_Config::singleton(); | |
119 | if ($config->userFramework != 'Joomla' || | |
120 | $config->userFramework != 'WordPress' | |
121 | ) { | |
122 | $form->addElement('select', | |
123 | 'permission', | |
124 | ts('Permission'), | |
125 | array('0' => ts('Everyone (includes anonymous)')) + CRM_Core_Permission::basicPermissions() | |
126 | ); | |
127 | ||
128 | // prepare user_roles to save as names not as ids | |
129 | if (function_exists('user_roles')) { | |
130 | $user_roles_array = user_roles(); | |
131 | foreach ($user_roles_array as $key => $value) { | |
132 | $user_roles[$value] = $value; | |
133 | } | |
1c6954cb | 134 | $grouprole = &$form->addElement('advmultiselect', |
6a488035 TO |
135 | 'grouprole', |
136 | ts('ACL Group/Role'), | |
137 | $user_roles, | |
138 | array( | |
139 | 'size' => 5, | |
140 | 'style' => 'width:240px', | |
141 | 'class' => 'advmultiselect', | |
142 | ) | |
143 | ); | |
1c6954cb AH |
144 | $grouprole->setButtonAttributes('add', array('value' => ts('Add >>'))); |
145 | $grouprole->setButtonAttributes('remove', array('value' => ts('<< Remove'))); | |
6a488035 TO |
146 | } |
147 | } | |
148 | ||
149 | // navigation field | |
150 | $parentMenu = CRM_Core_BAO_Navigation::getNavigationList(); | |
151 | ||
152 | $form->add('select', 'parent_id', ts('Parent Menu'), array('' => ts('-- select --')) + $parentMenu); | |
153 | ||
154 | // For now we only providing drilldown for one primary detail report only. In future this could be multiple reports | |
155 | foreach ($form->_drilldownReport as $reportUrl => $drillLabel) { | |
156 | $instanceList = CRM_Report_Utils_Report::getInstanceList($reportUrl); | |
157 | if (count($instanceList) > 1) | |
158 | $form->add('select', 'drilldown_id', $drillLabel, array('' => ts('- select -')) + $instanceList); | |
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 EM |
178 | /** |
179 | * @param $fields | |
180 | * @param $errors | |
181 | * @param $self | |
182 | * | |
183 | * @return array|bool | |
184 | */ | |
6a488035 TO |
185 | static function formRule($fields, $errors, $self) { |
186 | $buttonName = $self->controller->getButtonName(); | |
187 | $selfButtonName = $self->getVar('_instanceButtonName'); | |
188 | ||
189 | $errors = array(); | |
190 | if ($selfButtonName == $buttonName) { | |
191 | if (empty($fields['title'])) { | |
192 | $errors['title'] = ts('Title is a required field'); | |
193 | $self->assign('instanceFormError', TRUE); | |
194 | } | |
195 | } | |
196 | ||
197 | return empty($errors) ? TRUE : $errors; | |
198 | } | |
199 | ||
74cf4551 EM |
200 | /** |
201 | * @param $form | |
202 | * @param $defaults | |
203 | */ | |
6a488035 TO |
204 | static function setDefaultValues(&$form, &$defaults) { |
205 | // we should not build form elements in dashlet mode | |
206 | if ($form->_section) { | |
207 | return; | |
208 | } | |
209 | ||
210 | $instanceID = $form->getVar('_id'); | |
211 | $navigationDefaults = array(); | |
212 | ||
213 | if (!isset($defaults['permission'])){ | |
214 | $permissions = array_flip(CRM_Core_Permission::basicPermissions( )); | |
215 | $defaults['permission'] = $permissions['CiviReport: access CiviReport']; | |
216 | } | |
217 | ||
218 | $config = CRM_Core_Config::singleton(); | |
219 | $defaults['report_header'] = $report_header = "<html> | |
220 | <head> | |
221 | <title>CiviCRM Report</title> | |
222 | <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> | |
223 | <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style> | |
224 | </head> | |
225 | <body><div id=\"crm-container\">"; | |
226 | ||
227 | $defaults['report_footer'] = $report_footer = "<p><img src=\"{$config->userFrameworkResourceURL}i/powered_by.png\" /></p></div></body> | |
228 | </html> | |
229 | "; | |
230 | ||
231 | if ($instanceID) { | |
232 | // this is already retrieved via Form.php | |
233 | $defaults['description'] = CRM_Utils_Array::value('description', $defaults); | |
234 | $defaults['report_header'] = CRM_Utils_Array::value('header', $defaults); | |
235 | $defaults['report_footer'] = CRM_Utils_Array::value('footer', $defaults); | |
236 | ||
a7488080 | 237 | if (!empty($defaults['navigation_id'])) { |
6a488035 TO |
238 | //get the default navigation parent id |
239 | $params = array('id' => $defaults['navigation_id']); | |
240 | CRM_Core_BAO_Navigation::retrieve($params, $navigationDefaults); | |
241 | $defaults['is_navigation'] = 1; | |
242 | $defaults['parent_id'] = CRM_Utils_Array::value('parent_id', $navigationDefaults); | |
243 | ||
a7488080 | 244 | if (!empty($navigationDefaults['is_active'])) { |
6a488035 TO |
245 | $form->assign('is_navigation', TRUE); |
246 | } | |
247 | ||
a7488080 | 248 | if (!empty($navigationDefaults['id'])) { |
6a488035 TO |
249 | $form->_navigation['id'] = $navigationDefaults['id']; |
250 | $form->_navigation['parent_id'] = $navigationDefaults['parent_id']; | |
251 | } | |
252 | } | |
253 | ||
a7488080 | 254 | if (!empty($defaults['grouprole'])) { |
6a488035 TO |
255 | foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['grouprole']) as $value) { |
256 | $grouproles[] = $value; | |
257 | } | |
258 | $defaults['grouprole'] = $grouproles; | |
259 | } | |
260 | } | |
ae555e90 | 261 | else if (property_exists($form, '_description')) { |
6a488035 TO |
262 | $defaults['description'] = $form->_description; |
263 | } | |
264 | } | |
265 | ||
74cf4551 EM |
266 | /** |
267 | * @param $form | |
268 | * @param bool $redirect | |
269 | */ | |
6a488035 | 270 | static function postProcess(&$form, $redirect = TRUE) { |
81583117 | 271 | $params = $form->getVar('_params'); |
6a488035 | 272 | $instanceID = $form->getVar('_id'); |
f813f78e | 273 | |
6ea7a229 | 274 | if ($isNew = $form->getVar('_createNew')) { |
e41b14ce DS |
275 | // set the report_id since base template is going to be same, and we going to unset $instanceID |
276 | // which will make it difficult later on, to compute report_id | |
277 | $params['report_id'] = CRM_Report_Utils_Report::getValueFromUrl($instanceID); | |
278 | $instanceID = NULL; //unset $instanceID so a new copy would be created | |
6a488035 | 279 | } |
6ea7a229 | 280 | $params['instance_id'] = $instanceID; |
a7488080 | 281 | if (!empty($params['is_navigation'])) { |
6ea7a229 | 282 | $params['navigation'] = $form->_navigation; |
6a488035 | 283 | } |
355a8202 | 284 | elseif ($instanceID){ |
81583117 BS |
285 | //delete navigation if exists |
286 | $navId = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instanceID, 'navigation_id', 'id'); | |
287 | if ($navId) { | |
288 | CRM_Core_BAO_Navigation::processDelete($navId); | |
289 | CRM_Core_BAO_Navigation::resetNavigation(); | |
290 | } | |
291 | } | |
6a488035 | 292 | |
c5958fbe DS |
293 | // make a copy of params |
294 | $formValues = $params; | |
295 | ||
296 | // unset params from $formValues that doesn't match with DB columns of instance tables, and also not required in form-values for sure | |
297 | $unsetFields = array( | |
298 | 'title', 'to_emails', 'cc_emails', 'header', 'footer', | |
299 | 'qfKey', 'id', '_qf_default', 'report_header', 'report_footer', 'grouprole', | |
300 | ); | |
301 | foreach ($unsetFields as $field) { | |
302 | unset($formValues[$field]); | |
303 | } | |
304 | // pass form_values as string | |
305 | $params['form_values'] = serialize($formValues); | |
306 | ||
0b25329b | 307 | $instance = CRM_Report_BAO_ReportInstance::create($params); |
6ea7a229 | 308 | $form->set('id', $instance->id); |
6a488035 TO |
309 | |
310 | if ($instanceID && !$isNew) { | |
311 | // updating existing instance | |
6ea7a229 DS |
312 | $statusMsg = ts('"%1" report has been updated.', array(1 => $instance->title)); |
313 | } elseif ($form->getVar('_id') && $isNew) { | |
314 | $statusMsg = ts('Your report has been successfully copied as "%1". You are currently viewing the new copy.', array(1 => $instance->title)); | |
6a488035 | 315 | } else { |
6ea7a229 | 316 | $statusMsg = ts('"%1" report has been successfully created. You are currently viewing the new report instance.', array(1 => $instance->title)); |
6a488035 | 317 | } |
6a488035 TO |
318 | CRM_Core_Session::setStatus($statusMsg); |
319 | ||
320 | if ( $redirect ) { | |
6ea7a229 | 321 | CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/report/instance/{$instance->id}", "reset=1")); |
6a488035 TO |
322 | } |
323 | } | |
324 | } |