Merge pull request #7150 from colemanw/CRM-13823
[civicrm-core.git] / CRM / Report / BAO / ReportInstance.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035 32 */
0b25329b 33class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance {
6a488035 34
6ea7a229 35 /**
fe482240 36 * Takes an associative array and creates an instance object.
6ea7a229
DS
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 *
7e06c9f5
TO
42 * @param array $params
43 * (reference ) an assoc array of name/value pairs.
6ea7a229 44 *
16b10e64 45 * @return CRM_Report_DAO_ReportInstance
6ea7a229 46 */
00be9182 47 public static function add(&$params) {
0b25329b 48 $instance = new CRM_Report_DAO_ReportInstance();
6ea7a229
DS
49 if (empty($params)) {
50 return NULL;
51 }
52
edb1d052 53 $instanceID = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('instance_id', $params));
6ea7a229
DS
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
08f06889 66 if (!$instanceID || !isset($params['id'])) {
0bbbbae1 67 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
edb1d052 68 $params['domain_id'] = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
aaf5f726 69 // CRM-17256 set created_id on report creation.
70 $params['created_id'] = isset($params['created_id']) ? $params['created_id'] : CRM_Core_Session::getLoggedInContactID();
6ea7a229
DS
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
0b25329b 80 $instance = new CRM_Report_DAO_ReportInstance();
6ea7a229
DS
81 $instance->copyValues($params);
82
edb1d052 83 if (CRM_Core_Config::singleton()->userFramework == 'Joomla') {
6ea7a229
DS
84 $instance->permission = 'null';
85 }
86
87 // explicitly set to null if params value is empty
0bbbbae1 88 if (!$instanceID && empty($params['grouprole'])) {
6ea7a229
DS
89 $instance->grouprole = 'null';
90 }
91
92 if ($instanceID) {
93 $instance->id = $instanceID;
94 }
95
353ffa53 96 if (!$instanceID) {
a8d9a461
TO
97 if ($reportID = CRM_Utils_Array::value('report_id', $params)) {
98 $instance->report_id = $reportID;
0db6c3e1 99 }
4c9b6178 100 elseif ($instanceID) {
a8d9a461 101 $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl($instanceID);
0db6c3e1
TO
102 }
103 else {
a8d9a461
TO
104 // just take it from current url
105 $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl();
106 }
6ea7a229 107 }
a8d9a461 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 /**
fe482240 121 * Create instance.
6ea7a229
DS
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 *
7e06c9f5
TO
126 * @param array $params
127 * (reference ) an assoc array of name/value pairs.
6ea7a229 128 *
16b10e64 129 * @return CRM_Report_BAO_ReportInstance
6ea7a229 130 */
00be9182 131 public static function &create(&$params) {
0bbbbae1 132 if (isset($params['report_header'])) {
353ffa53 133 $params['header'] = CRM_Utils_Array::value('report_header', $params);
0bbbbae1
TO
134 }
135 if (isset($params['report_footer'])) {
353ffa53 136 $params['footer'] = CRM_Utils_Array::value('report_footer', $params);
0bbbbae1 137 }
6ea7a229
DS
138
139 // build navigation parameters
a7488080 140 if (!empty($params['is_navigation'])) {
6ea7a229
DS
141 if (!array_key_exists('navigation', $params)) {
142 $params['navigation'] = array();
143 }
144 $navigationParams =& $params['navigation'];
145
146 $navigationParams['permission'] = array();
147 $navigationParams['label'] = $params['title'];
353ffa53 148 $navigationParams['name'] = $params['title'];
6ea7a229
DS
149
150 $navigationParams['current_parent_id'] = CRM_Utils_Array::value('parent_id', $navigationParams);
151 $navigationParams['parent_id'] = CRM_Utils_Array::value('parent_id', $params);
152 $navigationParams['is_active'] = 1;
153
154 if ($permission = CRM_Utils_Array::value('permission', $params)) {
155 $navigationParams['permission'][] = $permission;
156 }
157
158 // unset the navigation related elements, not used in report form values
159 unset($params['parent_id']);
160 unset($params['is_navigation']);
161 }
162
e6e7e540 163 $viewMode = !empty($params['view_mode']) ? $params['view_mode'] : FALSE;
164 if ($viewMode) {
165 // Do not save to the DB - it's saved in the url.
166 unset($params['view_mode']);
167 }
168
6ea7a229
DS
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 }
e6e7e540 194 $navigationParams['url'] = "civicrm/report/instance/{$instance->id}" . ($viewMode == 'view' ? '?reset=1&force=1' : '?reset=1&output=criteria');
6ea7a229
DS
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;
353ffa53 212 $chart = '';
a7488080 213 if (!empty($params['charts'])) {
6ea7a229 214 $section = 1;
353ffa53 215 $chart = "&charts=" . $params['charts'];
6ea7a229 216 }
dbb4a0f9 217 $limitResult = NULL;
b53cbfbc 218 if (!empty($params['row_count'])) {
dbb4a0f9
PN
219 $limitResult = '&rowCount=' . $params['row_count'];
220 }
db049bcd 221 $dashletParams['name'] = "report/{$instance->id}";
f687ec36
DJ
222 $dashletParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}&snippet=5{$chart}&context=dashlet" . $limitResult;
223 $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}&snippet=5{$chart}&context=dashletFullscreen" . $limitResult;
6ea7a229
DS
224 $dashletParams['instanceURL'] = "civicrm/report/instance/{$instance->id}";
225 CRM_Core_BAO_Dashboard::addDashlet($dashletParams);
226 }
227 $transaction->commit();
228
229 return $instance;
230 }
231
6a488035 232 /**
fe482240 233 * Delete the instance of the Report.
6a488035 234 *
100fef9d 235 * @param int $id
6a488035 236 *
72b3a70c
CW
237 * @return mixed
238 * $results no of deleted Instance on success, false otherwise
6a488035 239 */
00be9182 240 public static function del($id = NULL) {
0b25329b 241 $dao = new CRM_Report_DAO_ReportInstance();
6a488035
TO
242 $dao->id = $id;
243 return $dao->delete();
244 }
245
74cf4551 246 /**
98f1f7a1 247 * Retrieve instance.
248 *
c490a46a 249 * @param array $params
98f1f7a1 250 * @param array $defaults
74cf4551
EM
251 *
252 * @return CRM_Report_DAO_ReportInstance|null
253 */
00be9182 254 public static function retrieve($params, &$defaults) {
0b25329b 255 $instance = new CRM_Report_DAO_ReportInstance();
6a488035
TO
256 $instance->copyValues($params);
257
258 if ($instance->find(TRUE)) {
259 CRM_Core_DAO::storeValues($instance, $defaults);
260 $instance->free();
261 return $instance;
262 }
263 return NULL;
264 }
96025800 265
6a488035 266}