commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / CRM / Report / BAO / ReportInstance.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 * $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 // CRM-17256 set created_id on report creation.
72 $params['created_id'] = isset($params['created_id']) ? $params['created_id'] : CRM_Core_Session::getLoggedInContactID();
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 }
102 elseif ($instanceID) {
103 $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl($instanceID);
104 }
105 else {
106 // just take it from current url
107 $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl();
108 }
109 }
110
111 $instance->save();
112
113 if ($instanceID) {
114 CRM_Utils_Hook::pre('edit', 'ReportInstance', $instance->id, $instance);
115 }
116 else {
117 CRM_Utils_Hook::pre('create', 'ReportInstance', $instance->id, $instance);
118 }
119 return $instance;
120 }
121
122 /**
123 * Create instance.
124 * takes an associative array and creates a instance object and does any related work like permissioning, adding to dashboard etc.
125 *
126 * This function is invoked from within the web form layer and also from the api layer
127 *
128 * @param array $params
129 * (reference ) an assoc array of name/value pairs.
130 *
131 * @return CRM_Report_BAO_ReportInstance
132 */
133 public static function &create(&$params) {
134 if (isset($params['report_header'])) {
135 $params['header'] = CRM_Utils_Array::value('report_header', $params);
136 }
137 if (isset($params['report_footer'])) {
138 $params['footer'] = CRM_Utils_Array::value('report_footer', $params);
139 }
140
141 // build navigation parameters
142 if (!empty($params['is_navigation'])) {
143 if (!array_key_exists('navigation', $params)) {
144 $params['navigation'] = array();
145 }
146 $navigationParams =& $params['navigation'];
147
148 $navigationParams['permission'] = array();
149 $navigationParams['label'] = $params['title'];
150 $navigationParams['name'] = $params['title'];
151
152 $navigationParams['current_parent_id'] = CRM_Utils_Array::value('parent_id', $navigationParams);
153 $navigationParams['parent_id'] = CRM_Utils_Array::value('parent_id', $params);
154 $navigationParams['is_active'] = 1;
155
156 if ($permission = CRM_Utils_Array::value('permission', $params)) {
157 $navigationParams['permission'][] = $permission;
158 }
159
160 // unset the navigation related elements, not used in report form values
161 unset($params['parent_id']);
162 unset($params['is_navigation']);
163 }
164
165 // add to dashboard
166 $dashletParams = array();
167 if (!empty($params['addToDashboard'])) {
168 $dashletParams = array(
169 'label' => $params['title'],
170 'is_active' => 1,
171 );
172 if ($permission = CRM_Utils_Array::value('permission', $params)) {
173 $dashletParams['permission'][] = $permission;
174 }
175 }
176
177 $transaction = new CRM_Core_Transaction();
178
179 $instance = self::add($params);
180 if (is_a($instance, 'CRM_Core_Error')) {
181 $transaction->rollback();
182 return $instance;
183 }
184
185 // add / update navigation as required
186 if (!empty($navigationParams)) {
187 if (empty($params['id']) && empty($params['instance_id']) && !empty($navigationParams['id'])) {
188 unset($navigationParams['id']);
189 }
190 $navigationParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1";
191 $navigation = CRM_Core_BAO_Navigation::add($navigationParams);
192
193 if (!empty($navigationParams['is_active'])) {
194 //set the navigation id in report instance table
195 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', $navigation->id);
196 }
197 else {
198 // has been removed from the navigation bar
199 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', 'NULL');
200 }
201 //reset navigation
202 CRM_Core_BAO_Navigation::resetNavigation();
203 }
204
205 // add to dashlet
206 if (!empty($dashletParams)) {
207 $section = 2;
208 $chart = '';
209 if (!empty($params['charts'])) {
210 $section = 1;
211 $chart = "&charts=" . $params['charts'];
212 }
213 $limitResult = NULL;
214 if (!empty($params['row_count'])) {
215 $limitResult = '&rowCount=' . $params['row_count'];
216 }
217 $dashletParams['name'] = "report/{$instance->id}";
218 $dashletParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}&snippet=5{$chart}&context=dashlet" . $limitResult;
219 $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}&snippet=5{$chart}&context=dashletFullscreen" . $limitResult;
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
228 /**
229 * Delete the instance of the Report.
230 *
231 * @param int $id
232 *
233 * @return mixed
234 * $results no of deleted Instance on success, false otherwise
235 */
236 public static function del($id = NULL) {
237 $dao = new CRM_Report_DAO_ReportInstance();
238 $dao->id = $id;
239 return $dao->delete();
240 }
241
242 /**
243 * @param array $params
244 * @param $defaults
245 *
246 * @return CRM_Report_DAO_ReportInstance|null
247 */
248 public static function retrieve($params, &$defaults) {
249 $instance = new CRM_Report_DAO_ReportInstance();
250 $instance->copyValues($params);
251
252 if ($instance->find(TRUE)) {
253 CRM_Core_DAO::storeValues($instance, $defaults);
254 $instance->free();
255 return $instance;
256 }
257 return NULL;
258 }
259
260 }