Merge pull request #7244 from systopia/CRM-17589
[civicrm-core.git] / CRM / Report / BAO / ReportInstance.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 */
33 class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance {
34
35 /**
36 * Takes an associative array and creates an instance object.
37 *
38 * the function extract all the params it needs to initialize the create a
39 * instance object. the params array could contain additional unused name/value
40 * pairs
41 *
42 * @param array $params
43 * (reference ) an assoc array of name/value pairs.
44 *
45 * @return CRM_Report_DAO_ReportInstance
46 */
47 public static function add(&$params) {
48 $instance = new CRM_Report_DAO_ReportInstance();
49 if (empty($params)) {
50 return NULL;
51 }
52
53 $instanceID = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('instance_id', $params));
54
55 // convert roles array to string
56 if (isset($params['grouprole']) && is_array($params['grouprole'])) {
57 $grouprole_array = array();
58 foreach ($params['grouprole'] as $key => $value) {
59 $grouprole_array[$value] = $value;
60 }
61 $params['grouprole'] = implode(CRM_Core_DAO::VALUE_SEPARATOR,
62 array_keys($grouprole_array)
63 );
64 }
65
66 if (!$instanceID || !isset($params['id'])) {
67 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
68 $params['domain_id'] = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
69 // CRM-17256 set created_id on report creation.
70 $params['created_id'] = isset($params['created_id']) ? $params['created_id'] : CRM_Core_Session::getLoggedInContactID();
71 }
72
73 if ($instanceID) {
74 CRM_Utils_Hook::pre('edit', 'ReportInstance', $instanceID, $params);
75 }
76 else {
77 CRM_Utils_Hook::pre('create', 'ReportInstance', NULL, $params);
78 }
79
80 $instance = new CRM_Report_DAO_ReportInstance();
81 $instance->copyValues($params);
82
83 if (CRM_Core_Config::singleton()->userFramework == 'Joomla') {
84 $instance->permission = 'null';
85 }
86
87 // explicitly set to null if params value is empty
88 if (!$instanceID && empty($params['grouprole'])) {
89 $instance->grouprole = 'null';
90 }
91
92 if ($instanceID) {
93 $instance->id = $instanceID;
94 }
95
96 if (!$instanceID) {
97 if ($reportID = CRM_Utils_Array::value('report_id', $params)) {
98 $instance->report_id = $reportID;
99 }
100 elseif ($instanceID) {
101 $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl($instanceID);
102 }
103 else {
104 // just take it from current url
105 $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl();
106 }
107 }
108
109 $instance->save();
110
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 * Create instance.
122 *
123 * takes an associative array and creates a instance object and does any related work like permissioning, adding to dashboard etc.
124 *
125 * This function is invoked from within the web form layer and also from the api layer
126 *
127 * @param array $params
128 * (reference ) an assoc array of name/value pairs.
129 *
130 * @return CRM_Report_BAO_ReportInstance
131 */
132 public static function &create(&$params) {
133 if (isset($params['report_header'])) {
134 $params['header'] = CRM_Utils_Array::value('report_header', $params);
135 }
136 if (isset($params['report_footer'])) {
137 $params['footer'] = CRM_Utils_Array::value('report_footer', $params);
138 }
139
140 // build navigation parameters
141 if (!empty($params['is_navigation'])) {
142 if (!array_key_exists('navigation', $params)) {
143 $params['navigation'] = array();
144 }
145 $navigationParams =& $params['navigation'];
146
147 $navigationParams['permission'] = array();
148 $navigationParams['label'] = $params['title'];
149 $navigationParams['name'] = $params['title'];
150
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;
154
155 if ($permission = CRM_Utils_Array::value('permission', $params)) {
156 $navigationParams['permission'][] = $permission;
157 }
158
159 // unset the navigation related elements, not used in report form values
160 unset($params['parent_id']);
161 unset($params['is_navigation']);
162 }
163
164 $viewMode = !empty($params['view_mode']) ? $params['view_mode'] : FALSE;
165 if ($viewMode) {
166 // Do not save to the DB - it's saved in the url.
167 unset($params['view_mode']);
168 }
169
170 // add to dashboard
171 $dashletParams = array();
172 if (!empty($params['addToDashboard'])) {
173 $dashletParams = array(
174 'label' => $params['title'],
175 'is_active' => 1,
176 );
177 if ($permission = CRM_Utils_Array::value('permission', $params)) {
178 $dashletParams['permission'][] = $permission;
179 }
180 }
181
182 $transaction = new CRM_Core_Transaction();
183
184 $instance = self::add($params);
185 if (is_a($instance, 'CRM_Core_Error')) {
186 $transaction->rollback();
187 return $instance;
188 }
189
190 // add / update navigation as required
191 if (!empty($navigationParams)) {
192 if (empty($params['id']) && empty($params['instance_id']) && !empty($navigationParams['id'])) {
193 unset($navigationParams['id']);
194 }
195 $navigationParams['url'] = "civicrm/report/instance/{$instance->id}" . ($viewMode == 'view' ? '?reset=1&force=1' : '?reset=1&output=criteria');
196 $navigation = CRM_Core_BAO_Navigation::add($navigationParams);
197
198 if (!empty($navigationParams['is_active'])) {
199 //set the navigation id in report instance table
200 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', $navigation->id);
201 }
202 else {
203 // has been removed from the navigation bar
204 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', 'NULL');
205 }
206 //reset navigation
207 CRM_Core_BAO_Navigation::resetNavigation();
208 }
209
210 // add to dashlet
211 if (!empty($dashletParams)) {
212 $section = 2;
213 $chart = '';
214 if (!empty($params['charts'])) {
215 $section = 1;
216 $chart = "&charts=" . $params['charts'];
217 }
218 $limitResult = NULL;
219 if (!empty($params['row_count'])) {
220 $limitResult = '&rowCount=' . $params['row_count'];
221 }
222 $dashletParams['name'] = "report/{$instance->id}";
223 $dashletParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}&snippet=5{$chart}&context=dashlet" . $limitResult;
224 $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}&snippet=5{$chart}&context=dashletFullscreen" . $limitResult;
225 $dashletParams['instanceURL'] = "civicrm/report/instance/{$instance->id}";
226 CRM_Core_BAO_Dashboard::addDashlet($dashletParams);
227 }
228 $transaction->commit();
229
230 return $instance;
231 }
232
233 /**
234 * Delete the instance of the Report.
235 *
236 * @param int $id
237 *
238 * @return mixed
239 * $results no of deleted Instance on success, false otherwise
240 */
241 public static function del($id = NULL) {
242 $dao = new CRM_Report_DAO_ReportInstance();
243 $dao->id = $id;
244 return $dao->delete();
245 }
246
247 /**
248 * Retrieve instance.
249 *
250 * @param array $params
251 * @param array $defaults
252 *
253 * @return CRM_Report_DAO_ReportInstance|null
254 */
255 public static function retrieve($params, &$defaults) {
256 $instance = new CRM_Report_DAO_ReportInstance();
257 $instance->copyValues($params);
258
259 if ($instance->find(TRUE)) {
260 CRM_Core_DAO::storeValues($instance, $defaults);
261 $instance->free();
262 return $instance;
263 }
264 return NULL;
265 }
266
267 }