Merge pull request #3154 from eileenmcnaughton/tests
[civicrm-core.git] / CRM / Report / BAO / ReportInstance.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
06b69b18 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
06b69b18 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
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
0bbbbae1
TO
71 if (!isset($params['id'])) {
72 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
73 }
6ea7a229
DS
74
75 $instanceID = CRM_Utils_Array::value('id', $params);
a7488080 76 if (!empty($params['instance_id'])) {
6ea7a229
DS
77 $instanceID = CRM_Utils_Array::value('instance_id', $params);
78 }
79
80 if ($instanceID) {
81 CRM_Utils_Hook::pre('edit', 'ReportInstance', $instanceID, $params);
82 }
83 else {
84 CRM_Utils_Hook::pre('create', 'ReportInstance', NULL, $params);
85 }
86
0b25329b 87 $instance = new CRM_Report_DAO_ReportInstance();
6ea7a229
DS
88 $instance->copyValues($params);
89
90 if ($config->userFramework == 'Joomla') {
91 $instance->permission = 'null';
92 }
93
94 // explicitly set to null if params value is empty
0bbbbae1 95 if (!$instanceID && empty($params['grouprole'])) {
6ea7a229
DS
96 $instance->grouprole = 'null';
97 }
98
99 if ($instanceID) {
100 $instance->id = $instanceID;
101 }
102
a8d9a461
TO
103 if (! $instanceID) {
104 if ($reportID = CRM_Utils_Array::value('report_id', $params)) {
105 $instance->report_id = $reportID;
106 } else if ($instanceID) {
107 $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl($instanceID);
108 } else {
109 // just take it from current url
110 $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl();
111 }
6ea7a229 112 }
a8d9a461 113
6ea7a229 114 $instance->save();
2efcf0c2 115
6ea7a229
DS
116 if ($instanceID) {
117 CRM_Utils_Hook::pre('edit', 'ReportInstance', $instance->id, $instance);
118 }
119 else {
120 CRM_Utils_Hook::pre('create', 'ReportInstance', $instance->id, $instance);
121 }
122 return $instance;
123 }
124
125 /**
126 * Function to create instance
127 * takes an associative array and creates a instance object and does any related work like permissioning, adding to dashboard etc.
128 *
129 * This function is invoked from within the web form layer and also from the api layer
130 *
131 * @param array $params (reference ) an assoc array of name/value pairs
132 *
0b25329b 133 * @return object CRM_Report_BAO_ReportInstance object
6ea7a229
DS
134 * @access public
135 * @static
136 */
137 static function &create(&$params) {
0bbbbae1
TO
138 if (isset($params['report_header'])) {
139 $params['header'] = CRM_Utils_Array::value('report_header',$params);
140 }
141 if (isset($params['report_footer'])) {
142 $params['footer'] = CRM_Utils_Array::value('report_footer',$params);
143 }
6ea7a229
DS
144
145 // build navigation parameters
a7488080 146 if (!empty($params['is_navigation'])) {
6ea7a229
DS
147 if (!array_key_exists('navigation', $params)) {
148 $params['navigation'] = array();
149 }
150 $navigationParams =& $params['navigation'];
151
152 $navigationParams['permission'] = array();
153 $navigationParams['label'] = $params['title'];
154 $navigationParams['name'] = $params['title'];
155
156 $navigationParams['current_parent_id'] = CRM_Utils_Array::value('parent_id', $navigationParams);
157 $navigationParams['parent_id'] = CRM_Utils_Array::value('parent_id', $params);
158 $navigationParams['is_active'] = 1;
159
160 if ($permission = CRM_Utils_Array::value('permission', $params)) {
161 $navigationParams['permission'][] = $permission;
162 }
163
164 // unset the navigation related elements, not used in report form values
165 unset($params['parent_id']);
166 unset($params['is_navigation']);
167 }
168
169 // add to dashboard
170 $dashletParams = array();
a7488080 171 if (!empty($params['addToDashboard'])) {
6ea7a229
DS
172 $dashletParams = array(
173 'label' => $params['title'],
174 'is_active' => 1,
175 );
176 if ($permission = CRM_Utils_Array::value('permission', $params)) {
177 $dashletParams['permission'][] = $permission;
178 }
179 }
180
181 $transaction = new CRM_Core_Transaction();
182
183 $instance = self::add($params);
184 if (is_a($instance, 'CRM_Core_Error')) {
185 $transaction->rollback();
186 return $instance;
187 }
188
189 // add / update navigation as required
190 if (!empty($navigationParams)) {
8cc574cf 191 if (empty($params['id']) && empty($params['instance_id']) && !empty($navigationParams['id'])) {
6ea7a229
DS
192 unset($navigationParams['id']);
193 }
194 $navigationParams['url'] = "civicrm/report/instance/{$instance->id}&reset=1";
195 $navigation = CRM_Core_BAO_Navigation::add($navigationParams);
196
a7488080 197 if (!empty($navigationParams['is_active'])) {
6ea7a229 198 //set the navigation id in report instance table
0b25329b 199 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', $navigation->id);
6ea7a229
DS
200 }
201 else {
202 // has been removed from the navigation bar
0b25329b 203 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', 'NULL');
6ea7a229
DS
204 }
205 //reset navigation
206 CRM_Core_BAO_Navigation::resetNavigation();
207 }
208
209 // add to dashlet
210 if (!empty($dashletParams)) {
211 $section = 2;
212 $chart = '';
a7488080 213 if (!empty($params['charts'])) {
6ea7a229
DS
214 $section = 1;
215 $chart = "&charts=" . $params['charts'];
216 }
217
218 $dashletParams['url'] = "civicrm/report/instance/{$instance->id}&reset=1&section={$section}&snippet=5{$chart}&context=dashlet";
219 $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}&reset=1&section={$section}&snippet=5{$chart}&context=dashletFullscreen";
220 $dashletParams['instanceURL'] = "civicrm/report/instance/{$instance->id}";
221 CRM_Core_BAO_Dashboard::addDashlet($dashletParams);
222 }
223 $transaction->commit();
224
225 return $instance;
226 }
227
6a488035
TO
228 /**
229 * Delete the instance of the Report
230 *
231 * @return $results no of deleted Instance on success, false otherwise
232 * @access public
233 *
234 */
0b25329b
DS
235 static function del($id = NULL) {
236 $dao = new CRM_Report_DAO_ReportInstance();
6a488035
TO
237 $dao->id = $id;
238 return $dao->delete();
239 }
240
241 static function retrieve($params, &$defaults) {
0b25329b 242 $instance = new CRM_Report_DAO_ReportInstance();
6a488035
TO
243 $instance->copyValues($params);
244
245 if ($instance->find(TRUE)) {
246 CRM_Core_DAO::storeValues($instance, $defaults);
247 $instance->free();
248 return $instance;
249 }
250 return NULL;
251 }
252}