4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 $instanceID = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('instance_id', $params));
59 // convert roles array to string
60 if (isset($params['grouprole']) && is_array($params['grouprole'])) {
61 $grouprole_array = array();
62 foreach ($params['grouprole'] as $key => $value) {
63 $grouprole_array[$value] = $value;
65 $params['grouprole'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
66 array_keys($grouprole_array)
71 $params['is_reserved'] = CRM_Utils_Array
::value('is_reserved', $params, FALSE);
72 $params['domain_id'] = CRM_Utils_Array
::value('domain_id', $params, CRM_Core_Config
::domainID());
76 CRM_Utils_Hook
::pre('edit', 'ReportInstance', $instanceID, $params);
79 CRM_Utils_Hook
::pre('create', 'ReportInstance', NULL, $params);
82 $instance = new CRM_Report_DAO_ReportInstance();
83 $instance->copyValues($params);
85 if (CRM_Core_Config
::singleton()->userFramework
== 'Joomla') {
86 $instance->permission
= 'null';
89 // explicitly set to null if params value is empty
90 if (!$instanceID && empty($params['grouprole'])) {
91 $instance->grouprole
= 'null';
95 $instance->id
= $instanceID;
99 if ($reportID = CRM_Utils_Array
::value('report_id', $params)) {
100 $instance->report_id
= $reportID;
101 } else if ($instanceID) {
102 $instance->report_id
= CRM_Report_Utils_Report
::getValueFromUrl($instanceID);
104 // just take it from current url
105 $instance->report_id
= CRM_Report_Utils_Report
::getValueFromUrl();
112 CRM_Utils_Hook
::pre('edit', 'ReportInstance', $instance->id
, $instance);
115 CRM_Utils_Hook
::pre('create', 'ReportInstance', $instance->id
, $instance);
121 * Function to create instance
122 * takes an associative array and creates a instance object and does any related work like permissioning, adding to dashboard etc.
124 * This function is invoked from within the web form layer and also from the api layer
126 * @param array $params (reference ) an assoc array of name/value pairs
128 * @return object CRM_Report_BAO_ReportInstance object
132 static function &create(&$params) {
133 if (isset($params['report_header'])) {
134 $params['header'] = CRM_Utils_Array
::value('report_header',$params);
136 if (isset($params['report_footer'])) {
137 $params['footer'] = CRM_Utils_Array
::value('report_footer',$params);
140 // build navigation parameters
141 if (!empty($params['is_navigation'])) {
142 if (!array_key_exists('navigation', $params)) {
143 $params['navigation'] = array();
145 $navigationParams =& $params['navigation'];
147 $navigationParams['permission'] = array();
148 $navigationParams['label'] = $params['title'];
149 $navigationParams['name'] = $params['title'];
151 $navigationParams['current_parent_id'] = CRM_Utils_Array
::value('parent_id', $navigationParams);
152 $navigationParams['parent_id'] = CRM_Utils_Array
::value('parent_id', $params);
153 $navigationParams['is_active'] = 1;
155 if ($permission = CRM_Utils_Array
::value('permission', $params)) {
156 $navigationParams['permission'][] = $permission;
159 // unset the navigation related elements, not used in report form values
160 unset($params['parent_id']);
161 unset($params['is_navigation']);
165 $dashletParams = array();
166 if (!empty($params['addToDashboard'])) {
167 $dashletParams = array(
168 'label' => $params['title'],
171 if ($permission = CRM_Utils_Array
::value('permission', $params)) {
172 $dashletParams['permission'][] = $permission;
176 $transaction = new CRM_Core_Transaction();
178 $instance = self
::add($params);
179 if (is_a($instance, 'CRM_Core_Error')) {
180 $transaction->rollback();
184 // add / update navigation as required
185 if (!empty($navigationParams)) {
186 if (empty($params['id']) && empty($params['instance_id']) && !empty($navigationParams['id'])) {
187 unset($navigationParams['id']);
189 $navigationParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1";
190 $navigation = CRM_Core_BAO_Navigation
::add($navigationParams);
192 if (!empty($navigationParams['is_active'])) {
193 //set the navigation id in report instance table
194 CRM_Core_DAO
::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id
, 'navigation_id', $navigation->id
);
197 // has been removed from the navigation bar
198 CRM_Core_DAO
::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id
, 'navigation_id', 'NULL');
201 CRM_Core_BAO_Navigation
::resetNavigation();
205 if (!empty($dashletParams)) {
208 if (!empty($params['charts'])) {
210 $chart = "&charts=" . $params['charts'];
213 if (CRM_Utils_Array
::value('row_count', $params)) {
214 $limitResult = '&rowCount=' . $params['row_count'];
216 $dashletParams['name'] = "report/{$instance->id}";
217 $dashletParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1§ion={$section}&snippet=5{$chart}&context=dashlet" . $limitResult;
218 $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}?reset=1§ion={$section}&snippet=5{$chart}&context=dashletFullscreen" . $limitResult;
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
232 * @return mixed $results no of deleted Instance on success, false otherwise@access public
234 static function del($id = NULL) {
235 $dao = new CRM_Report_DAO_ReportInstance();
237 return $dao->delete();
244 * @return CRM_Report_DAO_ReportInstance|null
246 static function retrieve($params, &$defaults) {
247 $instance = new CRM_Report_DAO_ReportInstance();
248 $instance->copyValues($params);
250 if ($instance->find(TRUE)) {
251 CRM_Core_DAO
::storeValues($instance, $defaults);