Merge pull request #5035 from TeNNoX/master
[civicrm-core.git] / CRM / Report / BAO / ReportInstance.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35 class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance {
36
37 /**
38 * Takes an associative array and creates an instance object.
39 *
40 * the function extract all the params it needs to initialize the create a
41 * instance object. the params array could contain additional unused name/value
42 * pairs
43 *
44 * @param array $params
45 * (reference ) an assoc array of name/value pairs.
46 *
47 * @return CRM_Report_DAO_ReportInstance
48 */
49 public static function add(&$params) {
50 $instance = new CRM_Report_DAO_ReportInstance();
51 if (empty($params)) {
52 return NULL;
53 }
54
55 $instanceID = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('instance_id', $params));
56
57 // convert roles array to string
58 if (isset($params['grouprole']) && is_array($params['grouprole'])) {
59 $grouprole_array = array();
60 foreach ($params['grouprole'] as $key => $value) {
61 $grouprole_array[$value] = $value;
62 }
63 $params['grouprole'] = implode(CRM_Core_DAO::VALUE_SEPARATOR,
64 array_keys($grouprole_array)
65 );
66 }
67
68 if (!$instanceID || !isset($params['id'])) {
69 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
70 $params['domain_id'] = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
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 * 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
127 * (reference ) an assoc array of name/value pairs.
128 *
129 * @return CRM_Report_BAO_ReportInstance
130 */
131 public static function &create(&$params) {
132 if (isset($params['report_header'])) {
133 $params['header'] = CRM_Utils_Array::value('report_header', $params);
134 }
135 if (isset($params['report_footer'])) {
136 $params['footer'] = CRM_Utils_Array::value('report_footer', $params);
137 }
138
139 // build navigation parameters
140 if (!empty($params['is_navigation'])) {
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'];
148 $navigationParams['name'] = $params['title'];
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
163 // add to dashboard
164 $dashletParams = array();
165 if (!empty($params['addToDashboard'])) {
166 $dashletParams = array(
167 'label' => $params['title'],
168 'is_active' => 1,
169 );
170 if ($permission = CRM_Utils_Array::value('permission', $params)) {
171 $dashletParams['permission'][] = $permission;
172 }
173 }
174
175 $transaction = new CRM_Core_Transaction();
176
177 $instance = self::add($params);
178 if (is_a($instance, 'CRM_Core_Error')) {
179 $transaction->rollback();
180 return $instance;
181 }
182
183 // add / update navigation as required
184 if (!empty($navigationParams)) {
185 if (empty($params['id']) && empty($params['instance_id']) && !empty($navigationParams['id'])) {
186 unset($navigationParams['id']);
187 }
188 $navigationParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1";
189 $navigation = CRM_Core_BAO_Navigation::add($navigationParams);
190
191 if (!empty($navigationParams['is_active'])) {
192 //set the navigation id in report instance table
193 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', $navigation->id);
194 }
195 else {
196 // has been removed from the navigation bar
197 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', 'NULL');
198 }
199 //reset navigation
200 CRM_Core_BAO_Navigation::resetNavigation();
201 }
202
203 // add to dashlet
204 if (!empty($dashletParams)) {
205 $section = 2;
206 $chart = '';
207 if (!empty($params['charts'])) {
208 $section = 1;
209 $chart = "&charts=" . $params['charts'];
210 }
211 $limitResult = NULL;
212 if (CRM_Utils_Array::value('row_count', $params)) {
213 $limitResult = '&rowCount=' . $params['row_count'];
214 }
215 $dashletParams['name'] = "report/{$instance->id}";
216 $dashletParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}&snippet=5{$chart}&context=dashlet" . $limitResult;
217 $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}&snippet=5{$chart}&context=dashletFullscreen" . $limitResult;
218 $dashletParams['instanceURL'] = "civicrm/report/instance/{$instance->id}";
219 CRM_Core_BAO_Dashboard::addDashlet($dashletParams);
220 }
221 $transaction->commit();
222
223 return $instance;
224 }
225
226 /**
227 * Delete the instance of the Report.
228 *
229 * @param int $id
230 *
231 * @return mixed
232 * $results no of deleted Instance on success, false otherwise
233 */
234 public static function del($id = NULL) {
235 $dao = new CRM_Report_DAO_ReportInstance();
236 $dao->id = $id;
237 return $dao->delete();
238 }
239
240 /**
241 * @param array $params
242 * @param $defaults
243 *
244 * @return CRM_Report_DAO_ReportInstance|null
245 */
246 public static function retrieve($params, &$defaults) {
247 $instance = new CRM_Report_DAO_ReportInstance();
248 $instance->copyValues($params);
249
250 if ($instance->find(TRUE)) {
251 CRM_Core_DAO::storeValues($instance, $defaults);
252 $instance->free();
253 return $instance;
254 }
255 return NULL;
256 }
257
258 }