commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Report / Form / Instance.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 * Class CRM_Report_Form_Instance
30 */
31 class CRM_Report_Form_Instance {
32
33 /**
34 * Build form.
35 *
36 * @param CRM_Core_Form $form
37 */
38 public static function buildForm(&$form) {
39 // We should not build form elements in dashlet mode.
40 if ($form->_section) {
41 return;
42 }
43
44 // Check role based permission.
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
53 $attributes = CRM_Core_DAO::getAttribute('CRM_Report_DAO_ReportInstance');
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 );
84
85 $form->add('text',
86 'row_count',
87 ts('Limit Dashboard Results'),
88 array(
89 'maxlength' => 64,
90 'size' => 5,
91 )
92 );
93
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
110 $form->addElement('checkbox', 'addToDashboard', ts('Available for Dashboard?'), NULL,
111 array('onclick' => "return showHideByValue('addToDashboard','','limit_result','table-row','radio',false);"));
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 }
133 $grouprole = &$form->addElement('advmultiselect',
134 'grouprole',
135 ts('ACL Group/Role'),
136 $user_roles,
137 array(
138 'size' => 5,
139 'style' => 'width:240px',
140 'class' => 'advmultiselect',
141 )
142 );
143 $grouprole->setButtonAttributes('add', array('value' => ts('Add >>')));
144 $grouprole->setButtonAttributes('remove', array('value' => ts('<< Remove')));
145 }
146 }
147
148 // navigation field
149 $parentMenu = CRM_Core_BAO_Navigation::getNavigationList();
150
151 $form->add('select', 'parent_id', ts('Parent Menu'), array('' => ts('- select -')) + $parentMenu);
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);
156 if (count($instanceList) > 1) {
157 $form->add('select', 'drilldown_id', $drillLabel, array('' => ts('- select -')) + $instanceList);
158 }
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
178 /**
179 * Add form rules.
180 *
181 * @param array $fields
182 * @param array $errors
183 * @param CRM_Report_Form_Instance $self
184 *
185 * @return array|bool
186 */
187 public static function formRule($fields, $errors, $self) {
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');
192
193 $errors = array();
194 if ($clickedButton == $nextButton || $clickedButton == $saveButton) {
195 if (empty($fields['title'])) {
196 $errors['title'] = ts('Title is a required field.');
197 $self->assign('instanceFormError', TRUE);
198 }
199 }
200
201 return empty($errors) ? TRUE : $errors;
202 }
203
204 /**
205 * Set default values.
206 *
207 * @param CRM_Core_Form $form
208 * @param array $defaults
209 */
210 public static function setDefaultValues(&$form, &$defaults) {
211 // we should not build form elements in dashlet mode.
212 if ($form->_section) {
213 return;
214 }
215
216 $instanceID = $form->getVar('_id');
217 $navigationDefaults = array();
218
219 if (!isset($defaults['permission'])) {
220 $permissions = array_flip(CRM_Core_Permission::basicPermissions());
221 $defaults['permission'] = $permissions['CiviReport: access CiviReport'];
222 }
223
224 $config = CRM_Core_Config::singleton();
225
226 // Add a special region for the default HTML header of printed reports. It
227 // won't affect reports with customized headers, just ones with the default.
228 $printHeaderRegion = CRM_Core_Region::instance('default-report-header', FALSE);
229 $htmlHeader = ($printHeaderRegion) ? $printHeaderRegion->render('', FALSE) : '';
230
231 $defaults['report_header'] = $report_header = "<html>
232 <head>
233 <title>CiviCRM Report</title>
234 <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
235 <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style>
236 {$htmlHeader}
237 </head>
238 <body><div id=\"crm-container\">";
239
240 $defaults['report_footer'] = $report_footer = "<p><img src=\"{$config->userFrameworkResourceURL}i/powered_by.png\" /></p></div></body>
241 </html>
242 ";
243
244 if ($instanceID) {
245 // this is already retrieved via Form.php
246 $defaults['description'] = CRM_Utils_Array::value('description', $defaults);
247 if (!empty($defaults['header'])) {
248 $defaults['report_header'] = $defaults['header'];
249 }
250 if (!empty($defaults['footer'])) {
251 $defaults['report_footer'] = $defaults['footer'];
252 }
253
254 if (!empty($defaults['navigation_id'])) {
255 // Get the default navigation parent id.
256 $params = array('id' => $defaults['navigation_id']);
257 CRM_Core_BAO_Navigation::retrieve($params, $navigationDefaults);
258 $defaults['is_navigation'] = 1;
259 $defaults['parent_id'] = CRM_Utils_Array::value('parent_id', $navigationDefaults);
260
261 if (!empty($navigationDefaults['is_active'])) {
262 $form->assign('is_navigation', TRUE);
263 }
264
265 if (!empty($navigationDefaults['id'])) {
266 $form->_navigation['id'] = $navigationDefaults['id'];
267 $form->_navigation['parent_id'] = !empty($navigationDefaults['parent_id']) ?
268 $navigationDefaults['parent_id'] : NULL;
269 }
270 }
271
272 if (!empty($defaults['grouprole'])) {
273 foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['grouprole']) as $value) {
274 $groupRoles[] = $value;
275 }
276 $defaults['grouprole'] = $groupRoles;
277 }
278 }
279 elseif (property_exists($form, '_description')) {
280 $defaults['description'] = $form->_description;
281 }
282 }
283
284 /**
285 * Post process function.
286 *
287 * @param CRM_Core_Form $form
288 * @param bool $redirect
289 */
290 public static function postProcess(&$form, $redirect = TRUE) {
291 $params = $form->getVar('_params');
292 $instanceID = $form->getVar('_id');
293
294 if ($isNew = $form->getVar('_createNew')) {
295 // set the report_id since base template is going to be same, and we going to unset $instanceID
296 // which will make it difficult later on, to compute report_id
297 $params['report_id'] = CRM_Report_Utils_Report::getValueFromUrl($instanceID);
298 // Unset $instanceID so a new copy would be created.
299 $instanceID = NULL;
300 }
301 $params['instance_id'] = $instanceID;
302 if (!empty($params['is_navigation'])) {
303 $params['navigation'] = $form->_navigation;
304 }
305 elseif ($instanceID) {
306 // Delete navigation if exists.
307 $navId = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instanceID, 'navigation_id', 'id');
308 if ($navId) {
309 CRM_Core_BAO_Navigation::processDelete($navId);
310 CRM_Core_BAO_Navigation::resetNavigation();
311 }
312 }
313
314 // make a copy of params
315 $formValues = $params;
316
317 // unset params from $formValues that doesn't match with DB columns of instance tables, and also not required in form-values for sure
318 $unsetFields = array(
319 'title',
320 'to_emails',
321 'cc_emails',
322 'header',
323 'footer',
324 'qfKey',
325 'id',
326 '_qf_default',
327 'report_header',
328 'report_footer',
329 'grouprole',
330 );
331 foreach ($unsetFields as $field) {
332 unset($formValues[$field]);
333 }
334 // pass form_values as string
335 $params['form_values'] = serialize($formValues);
336
337 $instance = CRM_Report_BAO_ReportInstance::create($params);
338 $form->set('id', $instance->id);
339
340 if ($instanceID && !$isNew) {
341 // updating existing instance
342 $statusMsg = ts('"%1" report has been updated.', array(1 => $instance->title));
343 }
344 elseif ($form->getVar('_id') && $isNew) {
345 $statusMsg = ts('Your report has been successfully copied as "%1". You are currently viewing the new copy.', array(1 => $instance->title));
346 }
347 else {
348 $statusMsg = ts('"%1" report has been successfully created. You are currently viewing the new report instance.', array(1 => $instance->title));
349 }
350 CRM_Core_Session::setStatus($statusMsg);
351
352 if ($redirect) {
353 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/report/instance/{$instance->id}", "reset=1"));
354 }
355 }
356
357 }