Merge pull request #4739 from PalanteJon/CRM-15741
[civicrm-core.git] / CRM / Report / Form / Instance.php
CommitLineData
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 */
35class CRM_Report_Form_Instance {
36
74cf4551 37 /**
c490a46a 38 * @param CRM_Core_Form $form
74cf4551 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
2b27d9df 152 $form->add('select', 'parent_id', ts('Parent Menu'), array('' => ts('- select -')) + $parentMenu);
6a488035
TO
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 185 static function formRule($fields, $errors, $self) {
c9cf6554
CW
186 // Validate both the "next" and "save" buttons for creating/updating a report
187 $nextButton = $self->controller->getButtonName();
188 $saveButton = str_replace('_next', '_save', $nextButton);
189 $clickedButton = $self->getVar('_instanceButtonName');
6a488035
TO
190
191 $errors = array();
c9cf6554 192 if ($clickedButton == $nextButton || $clickedButton == $saveButton) {
6a488035 193 if (empty($fields['title'])) {
161f6a74 194 $errors['title'] = ts('Title is a required field.');
6a488035
TO
195 $self->assign('instanceFormError', TRUE);
196 }
197 }
198
199 return empty($errors) ? TRUE : $errors;
200 }
201
74cf4551 202 /**
c490a46a 203 * @param CRM_Core_Form $form
74cf4551
EM
204 * @param $defaults
205 */
6a488035
TO
206 static function setDefaultValues(&$form, &$defaults) {
207 // we should not build form elements in dashlet mode
208 if ($form->_section) {
209 return;
210 }
211
212 $instanceID = $form->getVar('_id');
213 $navigationDefaults = array();
214
215 if (!isset($defaults['permission'])){
216 $permissions = array_flip(CRM_Core_Permission::basicPermissions( ));
217 $defaults['permission'] = $permissions['CiviReport: access CiviReport'];
218 }
219
220 $config = CRM_Core_Config::singleton();
221 $defaults['report_header'] = $report_header = "<html>
222 <head>
223 <title>CiviCRM Report</title>
224 <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
225 <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style>
226 </head>
227 <body><div id=\"crm-container\">";
228
229 $defaults['report_footer'] = $report_footer = "<p><img src=\"{$config->userFrameworkResourceURL}i/powered_by.png\" /></p></div></body>
230</html>
231";
232
233 if ($instanceID) {
234 // this is already retrieved via Form.php
235 $defaults['description'] = CRM_Utils_Array::value('description', $defaults);
236 $defaults['report_header'] = CRM_Utils_Array::value('header', $defaults);
237 $defaults['report_footer'] = CRM_Utils_Array::value('footer', $defaults);
238
a7488080 239 if (!empty($defaults['navigation_id'])) {
6a488035
TO
240 //get the default navigation parent id
241 $params = array('id' => $defaults['navigation_id']);
242 CRM_Core_BAO_Navigation::retrieve($params, $navigationDefaults);
243 $defaults['is_navigation'] = 1;
244 $defaults['parent_id'] = CRM_Utils_Array::value('parent_id', $navigationDefaults);
245
a7488080 246 if (!empty($navigationDefaults['is_active'])) {
6a488035
TO
247 $form->assign('is_navigation', TRUE);
248 }
249
a7488080 250 if (!empty($navigationDefaults['id'])) {
6a488035
TO
251 $form->_navigation['id'] = $navigationDefaults['id'];
252 $form->_navigation['parent_id'] = $navigationDefaults['parent_id'];
253 }
254 }
255
a7488080 256 if (!empty($defaults['grouprole'])) {
6a488035
TO
257 foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['grouprole']) as $value) {
258 $grouproles[] = $value;
259 }
260 $defaults['grouprole'] = $grouproles;
261 }
262 }
ae555e90 263 else if (property_exists($form, '_description')) {
6a488035
TO
264 $defaults['description'] = $form->_description;
265 }
266 }
267
74cf4551 268 /**
c490a46a 269 * @param CRM_Core_Form $form
74cf4551
EM
270 * @param bool $redirect
271 */
6a488035 272 static function postProcess(&$form, $redirect = TRUE) {
81583117 273 $params = $form->getVar('_params');
6a488035 274 $instanceID = $form->getVar('_id');
f813f78e 275
6ea7a229 276 if ($isNew = $form->getVar('_createNew')) {
e41b14ce
DS
277 // set the report_id since base template is going to be same, and we going to unset $instanceID
278 // which will make it difficult later on, to compute report_id
279 $params['report_id'] = CRM_Report_Utils_Report::getValueFromUrl($instanceID);
280 $instanceID = NULL; //unset $instanceID so a new copy would be created
6a488035 281 }
6ea7a229 282 $params['instance_id'] = $instanceID;
a7488080 283 if (!empty($params['is_navigation'])) {
6ea7a229 284 $params['navigation'] = $form->_navigation;
6a488035 285 }
355a8202 286 elseif ($instanceID){
81583117
BS
287 //delete navigation if exists
288 $navId = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instanceID, 'navigation_id', 'id');
289 if ($navId) {
290 CRM_Core_BAO_Navigation::processDelete($navId);
291 CRM_Core_BAO_Navigation::resetNavigation();
292 }
293 }
6a488035 294
c5958fbe
DS
295 // make a copy of params
296 $formValues = $params;
297
298 // unset params from $formValues that doesn't match with DB columns of instance tables, and also not required in form-values for sure
299 $unsetFields = array(
300 'title', 'to_emails', 'cc_emails', 'header', 'footer',
301 'qfKey', 'id', '_qf_default', 'report_header', 'report_footer', 'grouprole',
302 );
303 foreach ($unsetFields as $field) {
304 unset($formValues[$field]);
305 }
306 // pass form_values as string
307 $params['form_values'] = serialize($formValues);
308
0b25329b 309 $instance = CRM_Report_BAO_ReportInstance::create($params);
6ea7a229 310 $form->set('id', $instance->id);
6a488035
TO
311
312 if ($instanceID && !$isNew) {
313 // updating existing instance
6ea7a229
DS
314 $statusMsg = ts('"%1" report has been updated.', array(1 => $instance->title));
315 } elseif ($form->getVar('_id') && $isNew) {
316 $statusMsg = ts('Your report has been successfully copied as "%1". You are currently viewing the new copy.', array(1 => $instance->title));
6a488035 317 } else {
6ea7a229 318 $statusMsg = ts('"%1" report has been successfully created. You are currently viewing the new report instance.', array(1 => $instance->title));
6a488035 319 }
6a488035
TO
320 CRM_Core_Session::setStatus($statusMsg);
321
322 if ( $redirect ) {
6ea7a229 323 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/report/instance/{$instance->id}", "reset=1"));
6a488035
TO
324 }
325 }
326}