Merge pull request #6861 from totten/master-getsrt-setting
[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 }
70
71 if ($instanceID) {
72 CRM_Utils_Hook::pre('edit', 'ReportInstance', $instanceID, $params);
73 }
74 else {
75 CRM_Utils_Hook::pre('create', 'ReportInstance', NULL, $params);
76 }
77
78 $instance = new CRM_Report_DAO_ReportInstance();
79 $instance->copyValues($params);
80
81 if (CRM_Core_Config::singleton()->userFramework == 'Joomla') {
82 $instance->permission = 'null';
83 }
84
85 // explicitly set to null if params value is empty
86 if (!$instanceID && empty($params['grouprole'])) {
87 $instance->grouprole = 'null';
88 }
89
90 if ($instanceID) {
91 $instance->id = $instanceID;
92 }
93
94 if (!$instanceID) {
95 if ($reportID = CRM_Utils_Array::value('report_id', $params)) {
96 $instance->report_id = $reportID;
97 }
98 elseif ($instanceID) {
99 $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl($instanceID);
100 }
101 else {
102 // just take it from current url
103 $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl();
104 }
105 }
106
107 $instance->save();
108
109 if ($instanceID) {
110 CRM_Utils_Hook::pre('edit', 'ReportInstance', $instance->id, $instance);
111 }
112 else {
113 CRM_Utils_Hook::pre('create', 'ReportInstance', $instance->id, $instance);
114 }
115 return $instance;
116 }
117
118 /**
119 * Create instance.
120 * takes an associative array and creates a instance object and does any related work like permissioning, adding to dashboard etc.
121 *
122 * This function is invoked from within the web form layer and also from the api layer
123 *
124 * @param array $params
125 * (reference ) an assoc array of name/value pairs.
126 *
127 * @return CRM_Report_BAO_ReportInstance
128 */
129 public static function &create(&$params) {
130 if (isset($params['report_header'])) {
131 $params['header'] = CRM_Utils_Array::value('report_header', $params);
132 }
133 if (isset($params['report_footer'])) {
134 $params['footer'] = CRM_Utils_Array::value('report_footer', $params);
135 }
136
137 // build navigation parameters
138 if (!empty($params['is_navigation'])) {
139 if (!array_key_exists('navigation', $params)) {
140 $params['navigation'] = array();
141 }
142 $navigationParams =& $params['navigation'];
143
144 $navigationParams['permission'] = array();
145 $navigationParams['label'] = $params['title'];
146 $navigationParams['name'] = $params['title'];
147
148 $navigationParams['current_parent_id'] = CRM_Utils_Array::value('parent_id', $navigationParams);
149 $navigationParams['parent_id'] = CRM_Utils_Array::value('parent_id', $params);
150 $navigationParams['is_active'] = 1;
151
152 if ($permission = CRM_Utils_Array::value('permission', $params)) {
153 $navigationParams['permission'][] = $permission;
154 }
155
156 // unset the navigation related elements, not used in report form values
157 unset($params['parent_id']);
158 unset($params['is_navigation']);
159 }
160
161 // add to dashboard
162 $dashletParams = array();
163 if (!empty($params['addToDashboard'])) {
164 $dashletParams = array(
165 'label' => $params['title'],
166 'is_active' => 1,
167 );
168 if ($permission = CRM_Utils_Array::value('permission', $params)) {
169 $dashletParams['permission'][] = $permission;
170 }
171 }
172
173 $transaction = new CRM_Core_Transaction();
174
175 $instance = self::add($params);
176 if (is_a($instance, 'CRM_Core_Error')) {
177 $transaction->rollback();
178 return $instance;
179 }
180
181 // add / update navigation as required
182 if (!empty($navigationParams)) {
183 if (empty($params['id']) && empty($params['instance_id']) && !empty($navigationParams['id'])) {
184 unset($navigationParams['id']);
185 }
186 $navigationParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1";
187 $navigation = CRM_Core_BAO_Navigation::add($navigationParams);
188
189 if (!empty($navigationParams['is_active'])) {
190 //set the navigation id in report instance table
191 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', $navigation->id);
192 }
193 else {
194 // has been removed from the navigation bar
195 CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', 'NULL');
196 }
197 //reset navigation
198 CRM_Core_BAO_Navigation::resetNavigation();
199 }
200
201 // add to dashlet
202 if (!empty($dashletParams)) {
203 $section = 2;
204 $chart = '';
205 if (!empty($params['charts'])) {
206 $section = 1;
207 $chart = "&charts=" . $params['charts'];
208 }
209 $limitResult = NULL;
210 if (!empty($params['row_count'])) {
211 $limitResult = '&rowCount=' . $params['row_count'];
212 }
213 $dashletParams['name'] = "report/{$instance->id}";
214 $dashletParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}&snippet=5{$chart}&context=dashlet" . $limitResult;
215 $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}&snippet=5{$chart}&context=dashletFullscreen" . $limitResult;
216 $dashletParams['instanceURL'] = "civicrm/report/instance/{$instance->id}";
217 CRM_Core_BAO_Dashboard::addDashlet($dashletParams);
218 }
219 $transaction->commit();
220
221 return $instance;
222 }
223
224 /**
225 * Delete the instance of the Report.
226 *
227 * @param int $id
228 *
229 * @return mixed
230 * $results no of deleted Instance on success, false otherwise
231 */
232 public static function del($id = NULL) {
233 $dao = new CRM_Report_DAO_ReportInstance();
234 $dao->id = $id;
235 return $dao->delete();
236 }
237
238 /**
239 * Retrieve instance.
240 *
241 * @param array $params
242 * @param array $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 }