5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
33 * @copyright CiviCRM LLC (c) 2004-2013
37 class CRM_Report_Form_Instance
{
39 static function buildForm(&$form) {
40 // we should not build form elements in dashlet mode
41 if ($form->_section
) {
45 // check role based permission
46 $instanceID = $form->getVar('_id');
47 if ($instanceID && !CRM_Report_Utils_Report
::isInstanceGroupRoleAllowed($instanceID)) {
48 $url = CRM_Utils_System
::url('civicrm/report/list', 'reset=1');
49 CRM_Core_Error
::statusBounce(ts('You do not have permission to access this report.'),
54 $attributes = CRM_Core_DAO
::getAttribute('CRM_Report_DAO_Instance');
64 ts('Report Description'),
65 $attributes['description']
71 $attributes['email_subject']
77 $attributes['email_to']
83 $attributes['email_subject']
86 $form->add('textarea',
92 $form->add('textarea',
98 $form->addElement('checkbox', 'is_navigation', ts('Include Report in Navigation Menu?'), NULL,
99 array('onclick' => "return showHideByValue('is_navigation','','navigation_menu','table-row','radio',false);")
102 $form->addElement('checkbox', 'addToDashboard', ts('Available for Dashboard?'));
103 $form->addElement('checkbox', 'is_reserved', ts('Reserved Report?'));
104 if (!CRM_Core_Permission
::check('administer reserved reports')) {
105 $form->freeze('is_reserved');
108 $config = CRM_Core_Config
::singleton();
109 if ($config->userFramework
!= 'Joomla' ||
110 $config->userFramework
!= 'WordPress'
112 $form->addElement('select',
115 array('0' => ts('Everyone (includes anonymous)')) + CRM_Core_Permission
::basicPermissions()
118 // prepare user_roles to save as names not as ids
119 if (function_exists('user_roles')) {
120 $user_roles_array = user_roles();
121 foreach ($user_roles_array as $key => $value) {
122 $user_roles[$value] = $value;
124 $grouprole = &$form->addElement('advmultiselect',
126 ts('ACL Group/Role'),
130 'style' => 'width:240px',
131 'class' => 'advmultiselect',
134 $grouprole->setButtonAttributes('add', array('value' => ts('Add >>')));
135 $grouprole->setButtonAttributes('remove', array('value' => ts('<< Remove')));
140 $parentMenu = CRM_Core_BAO_Navigation
::getNavigationList();
142 $form->add('select', 'parent_id', ts('Parent Menu'), array('' => ts('-- select --')) +
$parentMenu);
144 // For now we only providing drilldown for one primary detail report only. In future this could be multiple reports
145 foreach ($form->_drilldownReport
as $reportUrl => $drillLabel) {
146 $instanceList = CRM_Report_Utils_Report
::getInstanceList($reportUrl);
147 if (count($instanceList) > 1)
148 $form->add('select', 'drilldown_id', $drillLabel, array('' => ts('- select -')) +
$instanceList);
152 $form->addButtons(array(
155 'name' => ts('Save Report'),
160 'name' => ts('Cancel'),
165 $form->addFormRule(array('CRM_Report_Form_Instance', 'formRule'), $form);
168 static function formRule($fields, $errors, $self) {
169 $buttonName = $self->controller
->getButtonName();
170 $selfButtonName = $self->getVar('_instanceButtonName');
173 if ($selfButtonName == $buttonName) {
174 if (empty($fields['title'])) {
175 $errors['title'] = ts('Title is a required field');
176 $self->assign('instanceFormError', TRUE);
180 return empty($errors) ?
TRUE : $errors;
183 static function setDefaultValues(&$form, &$defaults) {
184 // we should not build form elements in dashlet mode
185 if ($form->_section
) {
189 $instanceID = $form->getVar('_id');
190 $navigationDefaults = array();
192 if (!isset($defaults['permission'])){
193 $permissions = array_flip(CRM_Core_Permission
::basicPermissions( ));
194 $defaults['permission'] = $permissions['CiviReport: access CiviReport'];
197 $config = CRM_Core_Config
::singleton();
198 $defaults['report_header'] = $report_header = "<html>
200 <title>CiviCRM Report</title>
201 <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
202 <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style>
204 <body><div id=\"crm-container\">";
206 $defaults['report_footer'] = $report_footer = "<p><img src=\"{$config->userFrameworkResourceURL}i/powered_by.png\" /></p></div></body>
211 // this is already retrieved via Form.php
212 $defaults['description'] = CRM_Utils_Array
::value('description', $defaults);
213 $defaults['report_header'] = CRM_Utils_Array
::value('header', $defaults);
214 $defaults['report_footer'] = CRM_Utils_Array
::value('footer', $defaults);
216 if (CRM_Utils_Array
::value('navigation_id', $defaults)) {
217 //get the default navigation parent id
218 $params = array('id' => $defaults['navigation_id']);
219 CRM_Core_BAO_Navigation
::retrieve($params, $navigationDefaults);
220 $defaults['is_navigation'] = 1;
221 $defaults['parent_id'] = CRM_Utils_Array
::value('parent_id', $navigationDefaults);
223 if (CRM_Utils_Array
::value('is_active', $navigationDefaults)) {
224 $form->assign('is_navigation', TRUE);
227 if (CRM_Utils_Array
::value('id', $navigationDefaults)) {
228 $form->_navigation
['id'] = $navigationDefaults['id'];
229 $form->_navigation
['parent_id'] = $navigationDefaults['parent_id'];
233 if (CRM_Utils_Array
::value('grouprole', $defaults)) {
234 foreach (explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defaults['grouprole']) as $value) {
235 $grouproles[] = $value;
237 $defaults['grouprole'] = $grouproles;
241 $defaults['description'] = $form->_description
;
245 static function postProcess(&$form, $redirect = TRUE) {
246 $params = $form->getVar('_params');
247 $config = CRM_Core_Config
::singleton();
248 $params['header'] = CRM_Utils_Array
::value('report_header',$params);
249 $params['footer'] = CRM_Utils_Array
::value('report_footer',$params);
250 $params['domain_id'] = CRM_Core_Config
::domainID();
252 //navigation parameters
253 if (CRM_Utils_Array
::value('is_navigation', $params)) {
254 $form->_navigation
['permission'] = array();
255 $form->_navigation
['label'] = $params['title'];
256 $form->_navigation
['name'] = $params['title'];
258 $permission = CRM_Utils_Array
::value('permission', $params);
260 $form->_navigation
['current_parent_id'] = CRM_Utils_Array
::value('parent_id', $form->_navigation
);
261 $form->_navigation
['parent_id'] = CRM_Utils_Array
::value('parent_id', $params);
262 $form->_navigation
['is_active'] = 1;
265 $form->_navigation
['permission'][] = $permission;
267 //unset the navigation related element,
268 //not used in report form values
269 unset($params['parent_id']);
270 unset($params['is_navigation']);
273 // convert roles array to string
274 if (isset($params['grouprole']) && is_array($params['grouprole'])) {
275 $grouprole_array = array();
276 foreach ($params['grouprole'] as $key => $value) {
277 $grouprole_array[$value] = $value;
279 $params['grouprole'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
280 array_keys($grouprole_array)
285 $dashletParams = array();
286 if (CRM_Utils_Array
::value('addToDashboard', $params)) {
287 $dashletParams = array(
288 'label' => $params['title'],
292 $permission = CRM_Utils_Array
::value('permission', $params);
294 $dashletParams['permission'][] = $permission;
297 $params['is_reserved'] = CRM_Utils_Array
::value('is_reserved', $params, FALSE);
299 $dao = new CRM_Report_DAO_Instance();
300 $dao->copyValues($params);
302 if ($config->userFramework
== 'Joomla') {
303 $dao->permission
= 'null';
306 // explicitly set to null if params value is empty
307 if (empty($params['grouprole'])) {
308 $dao->grouprole
= 'null';
311 // unset all the params that we use
313 'title', 'to_emails', 'cc_emails', 'header', 'footer',
314 'qfKey', '_qf_default', 'report_header', 'report_footer', 'grouprole',
316 foreach ($fields as $field) {
317 unset($params[$field]);
319 $dao->form_values
= serialize($params);
321 $instanceID = $form->getVar('_id');
322 $isNew = $form->getVar('_createNew');
327 // updating an existing report instance
328 $dao->id
= $instanceID;
330 // making a copy of an existing instance
335 $dao->report_id
= CRM_Report_Utils_Report
::getValueFromUrl($instanceID);
339 $form->set('id', $dao->id
);
341 if (!empty($form->_navigation
)) {
342 if ($isNew && CRM_Utils_Array
::value('id', $form->_navigation
)) {
343 unset($form->_navigation
['id']);
345 $form->_navigation
['url'] = "civicrm/report/instance/{$dao->id}&reset=1";
346 $navigation = CRM_Core_BAO_Navigation
::add($form->_navigation
);
348 if (CRM_Utils_Array
::value('is_active', $form->_navigation
)) {
349 //set the navigation id in report instance table
350 CRM_Core_DAO
::setFieldValue('CRM_Report_DAO_Instance', $dao->id
, 'navigation_id', $navigation->id
);
353 // has been removed from the navigation bar
354 CRM_Core_DAO
::setFieldValue('CRM_Report_DAO_Instance', $dao->id
, 'navigation_id', 'NULL');
358 CRM_Core_BAO_Navigation
::resetNavigation();
362 if (!empty($dashletParams)) {
365 if (CRM_Utils_Array
::value('charts', $params)) {
367 $chart = "&charts=" . $params['charts'];
370 $dashletParams['url'] = "civicrm/report/instance/{$dao->id}&reset=1§ion={$section}&snippet=5{$chart}&context=dashlet";
371 $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$dao->id}&reset=1§ion={$section}&snippet=5{$chart}&context=dashletFullscreen";
372 $dashletParams['instanceURL'] = "civicrm/report/instance/{$dao->id}";
373 CRM_Core_BAO_Dashboard
::addDashlet($dashletParams);
376 $instanceParams = array('value' => $dao->report_id
);
377 $instanceDefaults = array();
378 $cmpName = "Contact";
380 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_OptionValue',
385 if ($cmpID = CRM_Utils_Array
::value('component_id', $instanceDefaults)) {
386 $cmpName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Component', $cmpID,
389 $cmpName = substr($cmpName, 4);
392 if ($instanceID && !$isNew) {
393 // updating existing instance
394 $statusMsg = ts('"%1" report has been updated.', array(1 => $dao->title
));
396 $statusMsg = ts('Your report has been successfully copied as "%1". You are currently viewing the new copy.', array(1 => $dao->title
));
398 $statusMsg = ts('"%1" report has been successfully created. You are currently viewing the new report instance.', array(1 => $dao->title
));
401 $instanceUrl = CRM_Utils_System
::url("civicrm/report/instance/{$dao->id}",
405 CRM_Core_Session
::setStatus($statusMsg);
408 CRM_Utils_System
::redirect($instanceUrl);