3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
29 * Class CRM_Report_Form_Instance
31 class CRM_Report_Form_Instance
{
36 * @param CRM_Core_Form $form
38 public static function buildForm(&$form) {
39 // We should not build form elements in dashlet mode.
40 if ($form->_section
) {
44 // Check role based permission.
45 $instanceID = $form->getVar('_id');
46 if ($instanceID && !CRM_Report_Utils_Report
::isInstanceGroupRoleAllowed($instanceID)) {
47 $url = CRM_Utils_System
::url('civicrm/report/list', 'reset=1');
48 CRM_Core_Error
::statusBounce(ts('You do not have permission to access this report.'),
53 $attributes = CRM_Core_DAO
::getAttribute('CRM_Report_DAO_ReportInstance');
63 ts('Report Description'),
64 $attributes['description']
70 $attributes['email_subject']
76 $attributes['email_to']
82 $attributes['email_subject']
87 ts('Limit Dashboard Results'),
88 array('class' => 'four', 'min' => 1)
91 $form->add('textarea',
97 $form->add('textarea',
100 $attributes['footer']
103 $form->addElement('checkbox', 'is_navigation', ts('Include Report in Navigation Menu?'), NULL,
104 array('onclick' => "return showHideByValue('is_navigation','','navigation_menu','table-row','radio',false);")
107 $form->addElement('select', 'view_mode', ts('Configure link to...'), array(
108 'view' => ts('View Results'),
109 'criteria' => ts('Show Criteria'),
112 $form->addElement('checkbox', 'addToDashboard', ts('Available for Dashboard?'));
113 $form->add('number', 'cache_minutes', ts('Cache dashlet for'), array('class' => 'four', 'min' => 1));
114 $form->addElement('checkbox', 'add_to_my_reports', ts('Add to My Reports?'), NULL);
116 $form->addElement('checkbox', 'is_reserved', ts('Reserved Report?'));
117 if (!CRM_Core_Permission
::check('administer reserved reports')) {
118 $form->freeze('is_reserved');
121 $config = CRM_Core_Config
::singleton();
122 if ($config->userFramework
!= 'Joomla' ||
123 $config->userFramework
!= 'WordPress'
125 $form->addElement('select',
128 array('0' => ts('Everyone (includes anonymous)')) + CRM_Core_Permission
::basicPermissions()
131 // prepare user_roles to save as names not as ids
132 if (function_exists('user_roles')) {
133 $user_roles_array = user_roles();
134 foreach ($user_roles_array as $key => $value) {
135 $user_roles[$value] = $value;
137 $grouprole = &$form->addElement('advmultiselect',
139 ts('ACL Group/Role'),
143 'style' => 'width:240px',
144 'class' => 'advmultiselect',
147 $grouprole->setButtonAttributes('add', array('value' => ts('Add >>')));
148 $grouprole->setButtonAttributes('remove', array('value' => ts('<< Remove')));
153 $parentMenu = CRM_Core_BAO_Navigation
::getNavigationList();
155 $form->add('select', 'parent_id', ts('Parent Menu'), array('' => ts('- select -')) +
$parentMenu);
157 // For now we only providing drilldown for one primary detail report only. In future this could be multiple reports
158 foreach ($form->_drilldownReport
as $reportUrl => $drillLabel) {
159 $instanceList = CRM_Report_Utils_Report
::getInstanceList($reportUrl);
160 if (count($instanceList) > 1) {
161 $form->add('select', 'drilldown_id', $drillLabel, array('' => ts('- select -')) +
$instanceList);
166 $form->addButtons(array(
169 'name' => ts('Save Report'),
174 'name' => ts('Cancel'),
179 $form->addFormRule(array('CRM_Report_Form_Instance', 'formRule'), $form);
185 * @param array $fields
186 * @param array $errors
187 * @param CRM_Report_Form_Instance $self
191 public static function formRule($fields, $errors, $self) {
192 // Validate both the "next" and "save" buttons for creating/updating a report.
193 $nextButton = $self->controller
->getButtonName();
194 $saveButton = str_replace('_next', '_save', $nextButton);
195 $clickedButton = $self->getVar('_instanceButtonName');
198 if ($clickedButton == $nextButton ||
$clickedButton == $saveButton) {
199 if (empty($fields['title'])) {
200 $errors['title'] = ts('Title is a required field.');
201 $self->assign('instanceFormError', TRUE);
205 return empty($errors) ?
TRUE : $errors;
209 * Set default values.
211 * @param CRM_Core_Form $form
212 * @param array $defaults
214 public static function setDefaultValues(&$form, &$defaults) {
215 // we should not build form elements in dashlet mode.
216 if ($form->_section
) {
220 $instanceID = $form->getVar('_id');
221 $navigationDefaults = array();
223 if (!isset($defaults['permission'])) {
224 $permissions = array_flip(CRM_Core_Permission
::basicPermissions());
225 $defaults['permission'] = $permissions['CiviReport: access CiviReport'];
228 $userFrameworkResourceURL = CRM_Core_Config
::singleton()->userFrameworkResourceURL
;
230 // Add a special region for the default HTML header of printed reports. It
231 // won't affect reports with customized headers, just ones with the default.
232 $printHeaderRegion = CRM_Core_Region
::instance('default-report-header', FALSE);
233 $htmlHeader = ($printHeaderRegion) ?
$printHeaderRegion->render('', FALSE) : '';
235 $defaults['report_header'] = $report_header = "<html>
237 <title>CiviCRM Report</title>
238 <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
239 <style type=\"text/css\">@import url({$userFrameworkResourceURL}css/print.css);</style>
242 <body><div id=\"crm-container\">";
244 $defaults['report_footer'] = $report_footer = "<p><img src=\"{$userFrameworkResourceURL}i/powered_by.png\" /></p></div></body>
248 // CRM-17225 view_mode currently supports 'view' or 'criteria'.
249 // Prior to 4.7 'view' meant reset=1 in the url & if not set
250 // then show criteria.
251 // From 4.7 we will pro-actively set 'force=1' but still respect the old behaviour.
252 // we may look to add pdf, print_view, csv & various charts as these could simply
253 // be added to the url allowing us to conceptualise 'view right now' vs saved view
254 // & using a multiselect (option value?) could help here.
255 // Note that accessing reports without reset=1 in the url turns out to be
256 // dangerous as it seems to carry actions like 'delete' from one report to another.
257 $defaults['view_mode'] = 'view';
258 $output = CRM_Utils_Request
::retrieve('output', 'String');
259 if ($output == 'criteria') {
260 $defaults['view_mode'] = 'criteria';
263 if (empty($defaults['cache_minutes'])) {
264 $defaults['cache_minutes'] = '60';
268 // this is already retrieved via Form.php
269 $defaults['description'] = CRM_Utils_Array
::value('description', $defaults);
270 if (!empty($defaults['header'])) {
271 $defaults['report_header'] = $defaults['header'];
273 if (!empty($defaults['footer'])) {
274 $defaults['report_footer'] = $defaults['footer'];
277 // CRM-17310 private reports option.
278 $defaults['add_to_my_reports'] = 0;
279 if (CRM_Utils_Array
::value('owner_id', $defaults) != NULL) {
280 $defaults['add_to_my_reports'] = 1;
283 if (!empty($defaults['navigation_id'])) {
284 // Get the default navigation parent id.
285 $params = array('id' => $defaults['navigation_id']);
286 CRM_Core_BAO_Navigation
::retrieve($params, $navigationDefaults);
287 $defaults['is_navigation'] = 1;
288 $defaults['parent_id'] = CRM_Utils_Array
::value('parent_id', $navigationDefaults);
289 if (!empty($navigationDefaults['is_active'])) {
290 $form->assign('is_navigation', TRUE);
292 // A saved view mode will over-ride any url assumptions.
293 if (strpos($navigationDefaults['url'], 'output=criteria')) {
294 $defaults['view_mode'] = 'criteria';
297 if (!empty($navigationDefaults['id'])) {
298 $form->_navigation
['id'] = $navigationDefaults['id'];
299 $form->_navigation
['parent_id'] = !empty($navigationDefaults['parent_id']) ?
300 $navigationDefaults['parent_id'] : NULL;
304 if (!empty($defaults['grouprole'])) {
305 foreach (explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defaults['grouprole']) as $value) {
306 $groupRoles[] = $value;
308 $defaults['grouprole'] = $groupRoles;
311 elseif (property_exists($form, '_description')) {
312 $defaults['description'] = $form->_description
;
317 * Post process function.
319 * @param CRM_Core_Form $form
320 * @param bool $redirect
322 public static function postProcess(&$form, $redirect = TRUE) {
323 $params = $form->getVar('_params');
324 $instanceID = $form->getVar('_id');
326 if ($isNew = $form->getVar('_createNew')) {
327 // set the report_id since base template is going to be same, and we going to unset $instanceID
328 // which will make it difficult later on, to compute report_id
329 $params['report_id'] = CRM_Report_Utils_Report
::getValueFromUrl($instanceID);
330 // Unset $instanceID so a new copy would be created.
333 $params['instance_id'] = $instanceID;
334 if (!empty($params['is_navigation'])) {
335 $params['navigation'] = $form->_navigation
;
337 elseif ($instanceID) {
338 // Delete navigation if exists.
339 $navId = CRM_Core_DAO
::getFieldValue('CRM_Report_DAO_ReportInstance', $instanceID, 'navigation_id', 'id');
341 CRM_Core_BAO_Navigation
::processDelete($navId);
342 CRM_Core_BAO_Navigation
::resetNavigation();
346 // make a copy of params
347 $formValues = $params;
349 // unset params from $formValues that doesn't match with DB columns of instance tables, and also not required in form-values for sure
350 $unsetFields = array(
364 foreach ($unsetFields as $field) {
365 unset($formValues[$field]);
367 $view_mode = $formValues['view_mode'];
369 // CRM-17310 my reports functionality - we should set owner if the checkbox is 1,
370 // it seems to be not set at all if unchecked.
371 if (!empty($formValues['add_to_my_reports'])) {
372 $params['owner_id'] = CRM_Core_Session
::singleton()->getLoggedInContactID();
375 $params['owner_id'] = 'null';
377 unset($formValues['add_to_my_reports']);
379 // pass form_values as string
380 $params['form_values'] = serialize($formValues);
382 $instance = CRM_Report_BAO_ReportInstance
::create($params);
383 $form->set('id', $instance->id
);
385 if ($instanceID && !$isNew) {
386 // updating existing instance
387 $statusMsg = ts('"%1" report has been updated.', array(1 => $instance->title
));
389 elseif ($form->getVar('_id') && $isNew) {
390 $statusMsg = ts('Your report has been successfully copied as "%1". You are currently viewing the new copy.', array(1 => $instance->title
));
393 $statusMsg = ts('"%1" report has been successfully created. You are currently viewing the new report instance.', array(1 => $instance->title
));
395 CRM_Core_Session
::setStatus($statusMsg);
398 $urlParams = array('reset' => 1);
399 if ($view_mode == 'view') {
400 $urlParams['force'] = 1;
403 $urlParams['output'] = 'criteria';
405 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/report/instance/{$instance->id}", $urlParams));