Merge pull request #1222 from lcdservices/CRM-13099
[civicrm-core.git] / CRM / Report / BAO / ReportInstance.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27*/
28
29/**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2013
33 * $Id$
34 *
35 */
0b25329b 36class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance {
6a488035 37
6ea7a229
DS
38 /**
39 * takes an associative array and creates an instance object
40 *
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
43 * pairs
44 *
45 * @param array $params (reference ) an assoc array of name/value pairs
46 *
0b25329b 47 * @return object CRM_Report_DAO_ReportInstance object
6ea7a229
DS
48 * @access public
49 * @static
50 */
51 static function add(&$params) {
0b25329b 52 $instance = new CRM_Report_DAO_ReportInstance();
6ea7a229
DS
53 if (empty($params)) {
54 return NULL;
55 }
56
57 $config = CRM_Core_Config::singleton();
58 $params['domain_id'] = CRM_Core_Config::domainID();
59
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;
65 }
66 $params['grouprole'] = implode(CRM_Core_DAO::VALUE_SEPARATOR,
67 array_keys($grouprole_array)
68 );
69 }
70
71 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
72
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);
76 }
77
78 if ($instanceID) {
79 CRM_Utils_Hook::pre('edit', 'ReportInstance', $instanceID, $params);
80 }
81 else {
82 CRM_Utils_Hook::pre('create', 'ReportInstance', NULL, $params);
83 }
84
0b25329b 85 $instance = new CRM_Report_DAO_ReportInstance();
6ea7a229
DS
86 $instance->copyValues($params);
87
88 if ($config->userFramework == 'Joomla') {
89 $instance->permission = 'null';
90 }
91
92 // explicitly set to null if params value is empty
93 if (empty($params['grouprole'])) {
94 $instance->grouprole = 'null';
95 }
96
97 if ($instanceID) {
98 $instance->id = $instanceID;
99 }
100
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);
105 } else {
106 // just take it from current url
107 $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl();
108 }
6ea7a229 109 $instance->save();
2efcf0c2 110
6ea7a229
DS
111 if ($instanceID) {
112 CRM_Utils_Hook::pre('edit', 'ReportInstance', $instance->id, $instance);
113 }
114 else {
115 CRM_Utils_Hook::pre('create', 'ReportInstance', $instance->id, $instance);
116 }
117 return $instance;
118 }
119
120 /**
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.
123 *
124 * This function is invoked from within the web form layer and also from the api layer
125 *
126 * @param array $params (reference ) an assoc array of name/value pairs
127 *
0b25329b 128 * @return object CRM_Report_BAO_ReportInstance object
6ea7a229
DS
129 * @access public
130 * @static
131 */
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);
135
136 // build navigation parameters
137 if (CRM_Utils_Array::value('is_navigation', $params)) {
138 if (!array_key_exists('navigation', $params)) {
139 $params['navigation'] = array();
140 }
141 $navigationParams =& $params['navigation'];
142
143 $navigationParams['permission'] = array();
144 $navigationParams['label'] = $params['title'];
145 $navigationParams['name'] = $params['title'];
146
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;
150
151 if ($permission = CRM_Utils_Array::value('permission', $params)) {
152 $navigationParams['permission'][] = $permission;
153 }
154
155 // unset the navigation related elements, not used in report form values
156 unset($params['parent_id']);
157 unset($params['is_navigation']);
158 }
159
160 // add to dashboard
161 $dashletParams = array();
162 if (CRM_Utils_Array::value('addToDashboard', $params)) {
163 $dashletParams = array(
164 'label' => $params['title'],
165 'is_active' => 1,
166 );
167 if ($permission = CRM_Utils_Array::value('permission', $params)) {
168 $dashletParams['permission'][] = $permission;
169 }
170 }
171
172 $transaction = new CRM_Core_Transaction();
173
174 $instance = self::add($params);
175 if (is_a($instance, 'CRM_Core_Error')) {
176 $transaction->rollback();
177 return $instance;
178 }
179
180 // add / update navigation as required
181 if (!empty($navigationParams)) {
2efcf0c2 182 if (!CRM_Utils_Array::value('id',$params) &&
183 !CRM_Utils_Array::value('instance_id',$params) &&
6ea7a229
DS
184 CRM_Utils_Array::value('id', $navigationParams)) {
185 unset($navigationParams['id']);
186 }
187 $navigationParams['url'] = "civicrm/report/instance/{$instance->id}&reset=1";
188 $navigation = CRM_Core_BAO_Navigation::add($navigationParams);
189
190 if (CRM_Utils_Array::value('is_active', $navigationParams)) {
191 //set the navigation id in report instance table
0b25329b 192 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', $navigation->id);
6ea7a229
DS
193 }
194 else {
195 // has been removed from the navigation bar
0b25329b 196 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', 'NULL');
6ea7a229
DS
197 }
198 //reset navigation
199 CRM_Core_BAO_Navigation::resetNavigation();
200 }
201
202 // add to dashlet
203 if (!empty($dashletParams)) {
204 $section = 2;
205 $chart = '';
206 if (CRM_Utils_Array::value('charts', $params)) {
207 $section = 1;
208 $chart = "&charts=" . $params['charts'];
209 }
210
211 $dashletParams['url'] = "civicrm/report/instance/{$instance->id}&reset=1&section={$section}&snippet=5{$chart}&context=dashlet";
212 $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}&reset=1&section={$section}&snippet=5{$chart}&context=dashletFullscreen";
213 $dashletParams['instanceURL'] = "civicrm/report/instance/{$instance->id}";
214 CRM_Core_BAO_Dashboard::addDashlet($dashletParams);
215 }
216 $transaction->commit();
217
218 return $instance;
219 }
220
6a488035
TO
221 /**
222 * Delete the instance of the Report
223 *
224 * @return $results no of deleted Instance on success, false otherwise
225 * @access public
226 *
227 */
0b25329b
DS
228 static function del($id = NULL) {
229 $dao = new CRM_Report_DAO_ReportInstance();
6a488035
TO
230 $dao->id = $id;
231 return $dao->delete();
232 }
233
234 static function retrieve($params, &$defaults) {
0b25329b 235 $instance = new CRM_Report_DAO_ReportInstance();
6a488035
TO
236 $instance->copyValues($params);
237
238 if ($instance->find(TRUE)) {
239 CRM_Core_DAO::storeValues($instance, $defaults);
240 $instance->free();
241 return $instance;
242 }
243 return NULL;
244 }
245}