4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2013
36 class CRM_Report_BAO_ReportInstance
extends CRM_Report_DAO_ReportInstance
{
39 * takes an associative array and creates an instance object
41 * the function extract all the params it needs to initialize the create a
42 * instance object. the params array could contain additional unused name/value
45 * @param array $params (reference ) an assoc array of name/value pairs
47 * @return object CRM_Report_DAO_ReportInstance object
51 static function add(&$params) {
52 $instance = new CRM_Report_DAO_ReportInstance();
57 $config = CRM_Core_Config
::singleton();
58 $params['domain_id'] = CRM_Core_Config
::domainID();
60 // convert roles array to string
61 if (isset($params['grouprole']) && is_array($params['grouprole'])) {
62 $grouprole_array = array();
63 foreach ($params['grouprole'] as $key => $value) {
64 $grouprole_array[$value] = $value;
66 $params['grouprole'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
67 array_keys($grouprole_array)
71 if (!isset($params['id'])) {
72 $params['is_reserved'] = CRM_Utils_Array
::value('is_reserved', $params, FALSE);
75 $instanceID = CRM_Utils_Array
::value('id', $params);
76 if (CRM_Utils_Array
::value('instance_id', $params)) {
77 $instanceID = CRM_Utils_Array
::value('instance_id', $params);
81 CRM_Utils_Hook
::pre('edit', 'ReportInstance', $instanceID, $params);
84 CRM_Utils_Hook
::pre('create', 'ReportInstance', NULL, $params);
87 $instance = new CRM_Report_DAO_ReportInstance();
88 $instance->copyValues($params);
90 if ($config->userFramework
== 'Joomla') {
91 $instance->permission
= 'null';
94 // explicitly set to null if params value is empty
95 if (!$instanceID && empty($params['grouprole'])) {
96 $instance->grouprole
= 'null';
100 $instance->id
= $instanceID;
103 if ($reportID = CRM_Utils_Array
::value('report_id', $params)) {
104 $instance->report_id
= $reportID;
105 } else if ($instanceID) {
106 $instance->report_id
= CRM_Report_Utils_Report
::getValueFromUrl($instanceID);
108 // just take it from current url
109 $instance->report_id
= CRM_Report_Utils_Report
::getValueFromUrl();
114 CRM_Utils_Hook
::pre('edit', 'ReportInstance', $instance->id
, $instance);
117 CRM_Utils_Hook
::pre('create', 'ReportInstance', $instance->id
, $instance);
123 * Function to create instance
124 * takes an associative array and creates a instance object and does any related work like permissioning, adding to dashboard etc.
126 * This function is invoked from within the web form layer and also from the api layer
128 * @param array $params (reference ) an assoc array of name/value pairs
130 * @return object CRM_Report_BAO_ReportInstance object
134 static function &create(&$params) {
135 if (isset($params['report_header'])) {
136 $params['header'] = CRM_Utils_Array
::value('report_header',$params);
138 if (isset($params['report_footer'])) {
139 $params['footer'] = CRM_Utils_Array
::value('report_footer',$params);
142 // build navigation parameters
143 if (CRM_Utils_Array
::value('is_navigation', $params)) {
144 if (!array_key_exists('navigation', $params)) {
145 $params['navigation'] = array();
147 $navigationParams =& $params['navigation'];
149 $navigationParams['permission'] = array();
150 $navigationParams['label'] = $params['title'];
151 $navigationParams['name'] = $params['title'];
153 $navigationParams['current_parent_id'] = CRM_Utils_Array
::value('parent_id', $navigationParams);
154 $navigationParams['parent_id'] = CRM_Utils_Array
::value('parent_id', $params);
155 $navigationParams['is_active'] = 1;
157 if ($permission = CRM_Utils_Array
::value('permission', $params)) {
158 $navigationParams['permission'][] = $permission;
161 // unset the navigation related elements, not used in report form values
162 unset($params['parent_id']);
163 unset($params['is_navigation']);
167 $dashletParams = array();
168 if (CRM_Utils_Array
::value('addToDashboard', $params)) {
169 $dashletParams = array(
170 'label' => $params['title'],
173 if ($permission = CRM_Utils_Array
::value('permission', $params)) {
174 $dashletParams['permission'][] = $permission;
178 $transaction = new CRM_Core_Transaction();
180 $instance = self
::add($params);
181 if (is_a($instance, 'CRM_Core_Error')) {
182 $transaction->rollback();
186 // add / update navigation as required
187 if (!empty($navigationParams)) {
188 if (!CRM_Utils_Array
::value('id',$params) &&
189 !CRM_Utils_Array
::value('instance_id',$params) &&
190 CRM_Utils_Array
::value('id', $navigationParams)) {
191 unset($navigationParams['id']);
193 $navigationParams['url'] = "civicrm/report/instance/{$instance->id}&reset=1";
194 $navigation = CRM_Core_BAO_Navigation
::add($navigationParams);
196 if (CRM_Utils_Array
::value('is_active', $navigationParams)) {
197 //set the navigation id in report instance table
198 CRM_Core_DAO
::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id
, 'navigation_id', $navigation->id
);
201 // has been removed from the navigation bar
202 CRM_Core_DAO
::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id
, 'navigation_id', 'NULL');
205 CRM_Core_BAO_Navigation
::resetNavigation();
209 if (!empty($dashletParams)) {
212 if (CRM_Utils_Array
::value('charts', $params)) {
214 $chart = "&charts=" . $params['charts'];
217 $dashletParams['url'] = "civicrm/report/instance/{$instance->id}&reset=1§ion={$section}&snippet=5{$chart}&context=dashlet";
218 $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}&reset=1§ion={$section}&snippet=5{$chart}&context=dashletFullscreen";
219 $dashletParams['instanceURL'] = "civicrm/report/instance/{$instance->id}";
220 CRM_Core_BAO_Dashboard
::addDashlet($dashletParams);
222 $transaction->commit();
228 * Delete the instance of the Report
230 * @return $results no of deleted Instance on success, false otherwise
234 static function del($id = NULL) {
235 $dao = new CRM_Report_DAO_ReportInstance();
237 return $dao->delete();
240 static function retrieve($params, &$defaults) {
241 $instance = new CRM_Report_DAO_ReportInstance();
242 $instance->copyValues($params);
244 if ($instance->find(TRUE)) {
245 CRM_Core_DAO
::storeValues($instance, $defaults);