Merge pull request #4627 from colemanw/docblocks
[civicrm-core.git] / CRM / Report / BAO / ReportInstance.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
33 * $Id$
34 *
35 */
36 class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance {
37
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 *
47 * @return CRM_Report_DAO_ReportInstance object
48 * @access public
49 * @static
50 */
51 static function add(&$params) {
52 $instance = new CRM_Report_DAO_ReportInstance();
53 if (empty($params)) {
54 return NULL;
55 }
56
57 $instanceID = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('instance_id', $params));
58
59 // convert roles array to string
60 if (isset($params['grouprole']) && is_array($params['grouprole'])) {
61 $grouprole_array = array();
62 foreach ($params['grouprole'] as $key => $value) {
63 $grouprole_array[$value] = $value;
64 }
65 $params['grouprole'] = implode(CRM_Core_DAO::VALUE_SEPARATOR,
66 array_keys($grouprole_array)
67 );
68 }
69
70 if (!$instanceID || !isset($params['id'])) {
71 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
72 $params['domain_id'] = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
73 }
74
75 if ($instanceID) {
76 CRM_Utils_Hook::pre('edit', 'ReportInstance', $instanceID, $params);
77 }
78 else {
79 CRM_Utils_Hook::pre('create', 'ReportInstance', NULL, $params);
80 }
81
82 $instance = new CRM_Report_DAO_ReportInstance();
83 $instance->copyValues($params);
84
85 if (CRM_Core_Config::singleton()->userFramework == 'Joomla') {
86 $instance->permission = 'null';
87 }
88
89 // explicitly set to null if params value is empty
90 if (!$instanceID && empty($params['grouprole'])) {
91 $instance->grouprole = 'null';
92 }
93
94 if ($instanceID) {
95 $instance->id = $instanceID;
96 }
97
98 if (! $instanceID) {
99 if ($reportID = CRM_Utils_Array::value('report_id', $params)) {
100 $instance->report_id = $reportID;
101 } else if ($instanceID) {
102 $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl($instanceID);
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 (reference ) an assoc array of name/value pairs
127 *
128 * @return CRM_Report_BAO_ReportInstance object
129 * @access public
130 * @static
131 */
132 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 // add to dashboard
165 $dashletParams = array();
166 if (!empty($params['addToDashboard'])) {
167 $dashletParams = array(
168 'label' => $params['title'],
169 'is_active' => 1,
170 );
171 if ($permission = CRM_Utils_Array::value('permission', $params)) {
172 $dashletParams['permission'][] = $permission;
173 }
174 }
175
176 $transaction = new CRM_Core_Transaction();
177
178 $instance = self::add($params);
179 if (is_a($instance, 'CRM_Core_Error')) {
180 $transaction->rollback();
181 return $instance;
182 }
183
184 // add / update navigation as required
185 if (!empty($navigationParams)) {
186 if (empty($params['id']) && empty($params['instance_id']) && !empty($navigationParams['id'])) {
187 unset($navigationParams['id']);
188 }
189 $navigationParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1";
190 $navigation = CRM_Core_BAO_Navigation::add($navigationParams);
191
192 if (!empty($navigationParams['is_active'])) {
193 //set the navigation id in report instance table
194 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', $navigation->id);
195 }
196 else {
197 // has been removed from the navigation bar
198 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', 'NULL');
199 }
200 //reset navigation
201 CRM_Core_BAO_Navigation::resetNavigation();
202 }
203
204 // add to dashlet
205 if (!empty($dashletParams)) {
206 $section = 2;
207 $chart = '';
208 if (!empty($params['charts'])) {
209 $section = 1;
210 $chart = "&charts=" . $params['charts'];
211 }
212 $limitResult = NULL;
213 if (CRM_Utils_Array::value('row_count', $params)) {
214 $limitResult = '&rowCount=' . $params['row_count'];
215 }
216 $dashletParams['name'] = "report/{$instance->id}";
217 $dashletParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}&snippet=5{$chart}&context=dashlet" . $limitResult;
218 $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}&snippet=5{$chart}&context=dashletFullscreen" . $limitResult;
219 $dashletParams['instanceURL'] = "civicrm/report/instance/{$instance->id}";
220 CRM_Core_BAO_Dashboard::addDashlet($dashletParams);
221 }
222 $transaction->commit();
223
224 return $instance;
225 }
226
227 /**
228 * Delete the instance of the Report
229 *
230 * @param null $id
231 *
232 * @return mixed $results no of deleted Instance on success, false otherwise@access public
233 */
234 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 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 }