3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
35 class CRM_Report_Form_Instance
{
37 static function buildForm(&$form) {
38 // we should not build form elements in dashlet mode
39 if ($form->_section
) {
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.'),
52 $attributes = CRM_Core_DAO
::getAttribute('CRM_Report_DAO_ReportInstance');
62 ts('Report Description'),
63 $attributes['description']
69 $attributes['email_subject']
75 $attributes['email_to']
81 $attributes['email_subject']
84 $form->add('textarea',
90 $form->add('textarea',
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);")
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');
106 $config = CRM_Core_Config
::singleton();
107 if ($config->userFramework
!= 'Joomla' ||
108 $config->userFramework
!= 'WordPress'
110 $form->addElement('select',
113 array('0' => ts('Everyone (includes anonymous)')) + CRM_Core_Permission
::basicPermissions()
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;
122 $grouprole = &$form->addElement('advmultiselect',
124 ts('ACL Group/Role'),
128 'style' => 'width:240px',
129 'class' => 'advmultiselect',
132 $grouprole->setButtonAttributes('add', array('value' => ts('Add >>')));
133 $grouprole->setButtonAttributes('remove', array('value' => ts('<< Remove')));
138 $parentMenu = CRM_Core_BAO_Navigation
::getNavigationList();
140 $form->add('select', 'parent_id', ts('Parent Menu'), array('' => ts('-- select --')) +
$parentMenu);
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);
150 $form->addButtons(array(
153 'name' => ts('Save Report'),
158 'name' => ts('Cancel'),
163 $form->addFormRule(array('CRM_Report_Form_Instance', 'formRule'), $form);
166 static function formRule($fields, $errors, $self) {
167 $buttonName = $self->controller
->getButtonName();
168 $selfButtonName = $self->getVar('_instanceButtonName');
171 if ($selfButtonName == $buttonName) {
172 if (empty($fields['title'])) {
173 $errors['title'] = ts('Title is a required field');
174 $self->assign('instanceFormError', TRUE);
178 return empty($errors) ?
TRUE : $errors;
181 static function setDefaultValues(&$form, &$defaults) {
182 // we should not build form elements in dashlet mode
183 if ($form->_section
) {
187 $instanceID = $form->getVar('_id');
188 $navigationDefaults = array();
190 if (!isset($defaults['permission'])){
191 $permissions = array_flip(CRM_Core_Permission
::basicPermissions( ));
192 $defaults['permission'] = $permissions['CiviReport: access CiviReport'];
195 $config = CRM_Core_Config
::singleton();
196 $defaults['report_header'] = $report_header = "<html>
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>
202 <body><div id=\"crm-container\">";
204 $defaults['report_footer'] = $report_footer = "<p><img src=\"{$config->userFrameworkResourceURL}i/powered_by.png\" /></p></div></body>
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);
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);
221 if (CRM_Utils_Array
::value('is_active', $navigationDefaults)) {
222 $form->assign('is_navigation', TRUE);
225 if (CRM_Utils_Array
::value('id', $navigationDefaults)) {
226 $form->_navigation
['id'] = $navigationDefaults['id'];
227 $form->_navigation
['parent_id'] = $navigationDefaults['parent_id'];
231 if (CRM_Utils_Array
::value('grouprole', $defaults)) {
232 foreach (explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defaults['grouprole']) as $value) {
233 $grouproles[] = $value;
235 $defaults['grouprole'] = $grouproles;
238 else if (property_exists($form, '_description')) {
239 $defaults['description'] = $form->_description
;
243 static function postProcess(&$form, $redirect = TRUE) {
244 $params = $form->getVar('_params');
245 $instanceID = $form->getVar('_id');
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
253 $params['instance_id'] = $instanceID;
254 if (CRM_Utils_Array
::value('is_navigation', $params)) {
255 $params['navigation'] = $form->_navigation
;
258 // make a copy of params
259 $formValues = $params;
261 // unset params from $formValues that doesn't match with DB columns of instance tables, and also not required in form-values for sure
262 $unsetFields = array(
263 'title', 'to_emails', 'cc_emails', 'header', 'footer',
264 'qfKey', 'id', '_qf_default', 'report_header', 'report_footer', 'grouprole',
266 foreach ($unsetFields as $field) {
267 unset($formValues[$field]);
269 // pass form_values as string
270 $params['form_values'] = serialize($formValues);
272 $instance = CRM_Report_BAO_ReportInstance
::create($params);
273 $form->set('id', $instance->id
);
275 if ($instanceID && !$isNew) {
276 // updating existing instance
277 $statusMsg = ts('"%1" report has been updated.', array(1 => $instance->title
));
278 } elseif ($form->getVar('_id') && $isNew) {
279 $statusMsg = ts('Your report has been successfully copied as "%1". You are currently viewing the new copy.', array(1 => $instance->title
));
281 $statusMsg = ts('"%1" report has been successfully created. You are currently viewing the new report instance.', array(1 => $instance->title
));
283 CRM_Core_Session
::setStatus($statusMsg);
286 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/report/instance/{$instance->id}", "reset=1"));