CRM-12877, fixed save-a-copy bug
[civicrm-core.git] / CRM / Report / Form / Instance.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 */
35class 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_Instance');
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 }
1c6954cb 122 $grouprole = &$form->addElement('advmultiselect',
6a488035
TO
123 'grouprole',
124 ts('ACL Group/Role'),
125 $user_roles,
126 array(
127 'size' => 5,
128 'style' => 'width:240px',
129 'class' => 'advmultiselect',
130 )
131 );
1c6954cb
AH
132 $grouprole->setButtonAttributes('add', array('value' => ts('Add >>')));
133 $grouprole->setButtonAttributes('remove', array('value' => ts('<< Remove')));
6a488035
TO
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 }
ae555e90 238 else if (property_exists($form, '_description')) {
6a488035
TO
239 $defaults['description'] = $form->_description;
240 }
241 }
242
243 static function postProcess(&$form, $redirect = TRUE) {
6ea7a229 244 $params = $form->getVar('_params');
6a488035 245 $instanceID = $form->getVar('_id');
f813f78e 246
6ea7a229 247 if ($isNew = $form->getVar('_createNew')) {
e41b14ce
DS
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
6a488035 252 }
6ea7a229
DS
253 $params['instance_id'] = $instanceID;
254 if (CRM_Utils_Array::value('is_navigation', $params)) {
255 $params['navigation'] = $form->_navigation;
6a488035
TO
256 }
257
6ea7a229
DS
258 $instance = CRM_Report_BAO_Instance::create($params);
259 $form->set('id', $instance->id);
6a488035
TO
260
261 if ($instanceID && !$isNew) {
262 // updating existing instance
6ea7a229
DS
263 $statusMsg = ts('"%1" report has been updated.', array(1 => $instance->title));
264 } elseif ($form->getVar('_id') && $isNew) {
265 $statusMsg = ts('Your report has been successfully copied as "%1". You are currently viewing the new copy.', array(1 => $instance->title));
6a488035 266 } else {
6ea7a229 267 $statusMsg = ts('"%1" report has been successfully created. You are currently viewing the new report instance.', array(1 => $instance->title));
6a488035 268 }
6a488035
TO
269 CRM_Core_Session::setStatus($statusMsg);
270
271 if ( $redirect ) {
6ea7a229 272 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/report/instance/{$instance->id}", "reset=1"));
6a488035
TO
273 }
274 }
275}