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