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 $params['is_reserved'] = CRM_Utils_Array
::value('is_reserved', $params, FALSE);
73 $instanceID = CRM_Utils_Array
::value('id', $params);
74 if (CRM_Utils_Array
::value('instance_id', $params)) {
75 $instanceID = CRM_Utils_Array
::value('instance_id', $params);
79 CRM_Utils_Hook
::pre('edit', 'ReportInstance', $instanceID, $params);
82 CRM_Utils_Hook
::pre('create', 'ReportInstance', NULL, $params);
85 $instance = new CRM_Report_DAO_ReportInstance();
86 $instance->copyValues($params);
88 if ($config->userFramework
== 'Joomla') {
89 $instance->permission
= 'null';
92 // explicitly set to null if params value is empty
93 if (empty($params['grouprole'])) {
94 $instance->grouprole
= 'null';
98 $instance->id
= $instanceID;
101 if ($reportID = CRM_Utils_Array
::value('report_id', $params)) {
102 $instance->report_id
= $reportID;
103 } else if ($instanceID) {
104 $instance->report_id
= CRM_Report_Utils_Report
::getValueFromUrl($instanceID);
106 // just take it from current url
107 $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 $params['header'] = CRM_Utils_Array
::value('report_header',$params);
134 $params['footer'] = CRM_Utils_Array
::value('report_footer',$params);
136 // build navigation parameters
137 if (CRM_Utils_Array
::value('is_navigation', $params)) {
138 if (!array_key_exists('navigation', $params)) {
139 $params['navigation'] = array();
141 $navigationParams =& $params['navigation'];
143 $navigationParams['permission'] = array();
144 $navigationParams['label'] = $params['title'];
145 $navigationParams['name'] = $params['title'];
147 $navigationParams['current_parent_id'] = CRM_Utils_Array
::value('parent_id', $navigationParams);
148 $navigationParams['parent_id'] = CRM_Utils_Array
::value('parent_id', $params);
149 $navigationParams['is_active'] = 1;
151 if ($permission = CRM_Utils_Array
::value('permission', $params)) {
152 $navigationParams['permission'][] = $permission;
155 // unset the navigation related elements, not used in report form values
156 unset($params['parent_id']);
157 unset($params['is_navigation']);
161 $dashletParams = array();
162 if (CRM_Utils_Array
::value('addToDashboard', $params)) {
163 $dashletParams = array(
164 'label' => $params['title'],
167 if ($permission = CRM_Utils_Array
::value('permission', $params)) {
168 $dashletParams['permission'][] = $permission;
172 $transaction = new CRM_Core_Transaction();
174 $instance = self
::add($params);
175 if (is_a($instance, 'CRM_Core_Error')) {
176 $transaction->rollback();
180 // add / update navigation as required
181 if (!empty($navigationParams)) {
182 if (!CRM_Utils_Array
::value('id',$params) &&
183 !CRM_Utils_Array
::value('instance_id',$params) &&
184 CRM_Utils_Array
::value('id', $navigationParams)) {
185 unset($navigationParams['id']);
187 $navigationParams['url'] = "civicrm/report/instance/{$instance->id}&reset=1";
188 $navigation = CRM_Core_BAO_Navigation
::add($navigationParams);
190 if (CRM_Utils_Array
::value('is_active', $navigationParams)) {
191 //set the navigation id in report instance table
192 CRM_Core_DAO
::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id
, 'navigation_id', $navigation->id
);
195 // has been removed from the navigation bar
196 CRM_Core_DAO
::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id
, 'navigation_id', 'NULL');
199 CRM_Core_BAO_Navigation
::resetNavigation();
203 if (!empty($dashletParams)) {
206 if (CRM_Utils_Array
::value('charts', $params)) {
208 $chart = "&charts=" . $params['charts'];
211 $dashletParams['url'] = "civicrm/report/instance/{$instance->id}&reset=1§ion={$section}&snippet=5{$chart}&context=dashlet";
212 $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}&reset=1§ion={$section}&snippet=5{$chart}&context=dashletFullscreen";
213 $dashletParams['instanceURL'] = "civicrm/report/instance/{$instance->id}";
214 CRM_Core_BAO_Dashboard
::addDashlet($dashletParams);
216 $transaction->commit();
222 * Delete the instance of the Report
224 * @return $results no of deleted Instance on success, false otherwise
228 static function del($id = NULL) {
229 $dao = new CRM_Report_DAO_ReportInstance();
231 return $dao->delete();
234 static function retrieve($params, &$defaults) {
235 $instance = new CRM_Report_DAO_ReportInstance();
236 $instance->copyValues($params);
238 if ($instance->find(TRUE)) {
239 CRM_Core_DAO
::storeValues($instance, $defaults);