3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Report_Form_Instance
{
40 static function buildForm(&$form) {
41 // we should not build form elements in dashlet mode
42 if ($form->_section
) {
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.'),
55 $attributes = CRM_Core_DAO
::getAttribute('CRM_Report_DAO_ReportInstance');
65 ts('Report Description'),
66 $attributes['description']
72 $attributes['email_subject']
78 $attributes['email_to']
84 $attributes['email_subject']
89 ts('Limit Dashboard Results'),
90 array('maxlength' => 64,
95 $form->add('textarea',
101 $form->add('textarea',
104 $attributes['footer']
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);")
111 $form->addElement('checkbox', 'addToDashboard', ts('Available for Dashboard?'), NULL,
112 array('onclick' => "return showHideByValue('addToDashboard','','limit_result','table-row','radio',false);"));
113 $form->addElement('checkbox', 'is_reserved', ts('Reserved Report?'));
114 if (!CRM_Core_Permission
::check('administer reserved reports')) {
115 $form->freeze('is_reserved');
118 $config = CRM_Core_Config
::singleton();
119 if ($config->userFramework
!= 'Joomla' ||
120 $config->userFramework
!= 'WordPress'
122 $form->addElement('select',
125 array('0' => ts('Everyone (includes anonymous)')) + CRM_Core_Permission
::basicPermissions()
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;
134 $grouprole = &$form->addElement('advmultiselect',
136 ts('ACL Group/Role'),
140 'style' => 'width:240px',
141 'class' => 'advmultiselect',
144 $grouprole->setButtonAttributes('add', array('value' => ts('Add >>')));
145 $grouprole->setButtonAttributes('remove', array('value' => ts('<< Remove')));
150 $parentMenu = CRM_Core_BAO_Navigation
::getNavigationList();
152 $form->add('select', 'parent_id', ts('Parent Menu'), array('' => ts('-- select --')) +
$parentMenu);
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);
162 $form->addButtons(array(
165 'name' => ts('Save Report'),
170 'name' => ts('Cancel'),
175 $form->addFormRule(array('CRM_Report_Form_Instance', 'formRule'), $form);
185 static function formRule($fields, $errors, $self) {
186 $buttonName = $self->controller
->getButtonName();
187 $selfButtonName = $self->getVar('_instanceButtonName');
190 if ($selfButtonName == $buttonName) {
191 if (empty($fields['title'])) {
192 $errors['title'] = ts('Title is a required field');
193 $self->assign('instanceFormError', TRUE);
197 return empty($errors) ?
TRUE : $errors;
204 static function setDefaultValues(&$form, &$defaults) {
205 // we should not build form elements in dashlet mode
206 if ($form->_section
) {
210 $instanceID = $form->getVar('_id');
211 $navigationDefaults = array();
213 if (!isset($defaults['permission'])){
214 $permissions = array_flip(CRM_Core_Permission
::basicPermissions( ));
215 $defaults['permission'] = $permissions['CiviReport: access CiviReport'];
218 $config = CRM_Core_Config
::singleton();
219 $defaults['report_header'] = $report_header = "<html>
221 <title>CiviCRM Report</title>
222 <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
223 <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style>
225 <body><div id=\"crm-container\">";
227 $defaults['report_footer'] = $report_footer = "<p><img src=\"{$config->userFrameworkResourceURL}i/powered_by.png\" /></p></div></body>
232 // this is already retrieved via Form.php
233 $defaults['description'] = CRM_Utils_Array
::value('description', $defaults);
234 $defaults['report_header'] = CRM_Utils_Array
::value('header', $defaults);
235 $defaults['report_footer'] = CRM_Utils_Array
::value('footer', $defaults);
237 if (!empty($defaults['navigation_id'])) {
238 //get the default navigation parent id
239 $params = array('id' => $defaults['navigation_id']);
240 CRM_Core_BAO_Navigation
::retrieve($params, $navigationDefaults);
241 $defaults['is_navigation'] = 1;
242 $defaults['parent_id'] = CRM_Utils_Array
::value('parent_id', $navigationDefaults);
244 if (!empty($navigationDefaults['is_active'])) {
245 $form->assign('is_navigation', TRUE);
248 if (!empty($navigationDefaults['id'])) {
249 $form->_navigation
['id'] = $navigationDefaults['id'];
250 $form->_navigation
['parent_id'] = $navigationDefaults['parent_id'];
254 if (!empty($defaults['grouprole'])) {
255 foreach (explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defaults['grouprole']) as $value) {
256 $grouproles[] = $value;
258 $defaults['grouprole'] = $grouproles;
261 else if (property_exists($form, '_description')) {
262 $defaults['description'] = $form->_description
;
268 * @param bool $redirect
270 static function postProcess(&$form, $redirect = TRUE) {
271 $params = $form->getVar('_params');
272 $instanceID = $form->getVar('_id');
274 if ($isNew = $form->getVar('_createNew')) {
275 // set the report_id since base template is going to be same, and we going to unset $instanceID
276 // which will make it difficult later on, to compute report_id
277 $params['report_id'] = CRM_Report_Utils_Report
::getValueFromUrl($instanceID);
278 $instanceID = NULL; //unset $instanceID so a new copy would be created
280 $params['instance_id'] = $instanceID;
281 if (!empty($params['is_navigation'])) {
282 $params['navigation'] = $form->_navigation
;
284 elseif ($instanceID){
285 //delete navigation if exists
286 $navId = CRM_Core_DAO
::getFieldValue('CRM_Report_DAO_ReportInstance', $instanceID, 'navigation_id', 'id');
288 CRM_Core_BAO_Navigation
::processDelete($navId);
289 CRM_Core_BAO_Navigation
::resetNavigation();
293 // make a copy of params
294 $formValues = $params;
296 // unset params from $formValues that doesn't match with DB columns of instance tables, and also not required in form-values for sure
297 $unsetFields = array(
298 'title', 'to_emails', 'cc_emails', 'header', 'footer',
299 'qfKey', 'id', '_qf_default', 'report_header', 'report_footer', 'grouprole',
301 foreach ($unsetFields as $field) {
302 unset($formValues[$field]);
304 // pass form_values as string
305 $params['form_values'] = serialize($formValues);
307 $instance = CRM_Report_BAO_ReportInstance
::create($params);
308 $form->set('id', $instance->id
);
310 if ($instanceID && !$isNew) {
311 // updating existing instance
312 $statusMsg = ts('"%1" report has been updated.', array(1 => $instance->title
));
313 } elseif ($form->getVar('_id') && $isNew) {
314 $statusMsg = ts('Your report has been successfully copied as "%1". You are currently viewing the new copy.', array(1 => $instance->title
));
316 $statusMsg = ts('"%1" report has been successfully created. You are currently viewing the new report instance.', array(1 => $instance->title
));
318 CRM_Core_Session
::setStatus($statusMsg);
321 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/report/instance/{$instance->id}", "reset=1"));