Merge pull request #11337 from WeMoveEU/CRM-21460
[civicrm-core.git] / CRM / Report / Utils / Get.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Report_Utils_Get {
36
74cf4551 37 /**
100fef9d 38 * @param string $name
74cf4551
EM
39 * @param $type
40 *
41 * @return mixed|null
42 */
00be9182 43 public static function getTypedValue($name, $type) {
6a488035
TO
44 $value = CRM_Utils_Array::value($name, $_GET);
45 if ($value === NULL) {
46 return NULL;
47 }
48 return CRM_Utils_Type::escape($value,
49 CRM_Utils_Type::typeToString($type),
50 FALSE
51 );
52 }
53
74cf4551 54 /**
100fef9d 55 * @param string $fieldName
74cf4551
EM
56 * @param $field
57 * @param $defaults
ab432335
EM
58 *
59 * @return bool
74cf4551 60 */
00be9182 61 public static function dateParam($fieldName, &$field, &$defaults) {
6a488035
TO
62 // type = 12 (datetime) is not recognized by Utils_Type::escape() method,
63 // and therefore the below hack
64 $type = 4;
65
66 $from = self::getTypedValue("{$fieldName}_from", $type);
67 $to = self::getTypedValue("{$fieldName}_to", $type);
68
0ceea711
PN
69 $relative = self::getTypedValue("{$fieldName}_relative", CRM_Utils_Type::T_STRING);
70 if ($relative !== NULL) {
71 $defaults["{$fieldName}_relative"] = $relative;
72 }
6a488035 73 if ($relative) {
dd56d5dc 74 list($from, $to) = CRM_Utils_Date::getFromTo($relative, NULL, NULL);
6a488035
TO
75 $from = substr($from, 0, 8);
76 $to = substr($to, 0, 8);
77 }
78
79 if (!($from || $to)) {
80 return FALSE;
81 }
82
83 if ($from !== NULL) {
84 $dateFrom = CRM_Utils_Date::setDateDefaults($from);
85 if ($dateFrom !== NULL &&
86 !empty($dateFrom[0])
87 ) {
88 $defaults["{$fieldName}_from"] = $dateFrom[0];
89 }
90 }
91
92 if ($to !== NULL) {
93 $dateTo = CRM_Utils_Date::setDateDefaults($to);
94 if ($dateTo !== NULL &&
95 !empty($dateTo[0])
96 ) {
97 $defaults["{$fieldName}_to"] = $dateTo[0];
98 }
99 }
100 }
101
74cf4551 102 /**
100fef9d 103 * @param string $fieldName
74cf4551
EM
104 * @param $field
105 * @param $defaults
106 */
00be9182 107 public static function stringParam($fieldName, &$field, &$defaults) {
6a488035
TO
108 $fieldOP = CRM_Utils_Array::value("{$fieldName}_op", $_GET, 'like');
109
110 switch ($fieldOP) {
111 case 'has':
112 case 'sw':
113 case 'ew':
114 case 'nhas':
115 case 'like':
1d12dfc9 116 case 'eq':
6a488035
TO
117 case 'neq':
118 $value = self::getTypedValue("{$fieldName}_value", CRM_Utils_Array::value('type', $field));
119 if ($value !== NULL) {
120 $defaults["{$fieldName}_value"] = $value;
121 $defaults["{$fieldName}_op"] = $fieldOP;
122 }
123 break;
124
125 case 'nll':
126 case 'nnll':
127 $defaults["{$fieldName}_op"] = $fieldOP;
128 break;
84178120 129
79ce64de
RN
130 case 'in':
131 case 'notin':
dd62d149 132 case 'mhas':
79ce64de
RN
133 $value = self::getTypedValue("{$fieldName}_value", CRM_Utils_Type::T_STRING);
134 if ($value !== NULL) {
135 $defaults["{$fieldName}_value"] = explode(",", $value);
136 $defaults["{$fieldName}_op"] = $fieldOP;
137 }
138 break;
6a488035
TO
139 }
140 }
141
74cf4551 142 /**
100fef9d 143 * @param string $fieldName
74cf4551
EM
144 * @param $field
145 * @param $defaults
146 */
00be9182 147 public static function intParam($fieldName, &$field, &$defaults) {
6a488035
TO
148 $fieldOP = CRM_Utils_Array::value("{$fieldName}_op", $_GET, 'eq');
149
150 switch ($fieldOP) {
151 case 'lte':
152 case 'gte':
153 case 'eq':
154 case 'lt':
155 case 'gt':
156 case 'neq':
157 $value = self::getTypedValue("{$fieldName}_value", $field['type']);
158 if ($value !== NULL) {
159 $defaults["{$fieldName}_value"] = $value;
160 $defaults["{$fieldName}_op"] = $fieldOP;
161 }
162 break;
163
164 case 'bw':
165 case 'nbw':
166 $minValue = self::getTypedValue("{$fieldName}_min", $field['type']);
167 $maxValue = self::getTypedValue("{$fieldName}_max", $field['type']);
168 if ($minValue !== NULL ||
169 $maxValue !== NULL
170 ) {
171 if ($minValue !== NULL) {
172 $defaults["{$fieldName}_min"] = $minValue;
173 }
174 if ($maxValue !== NULL) {
175 $defaults["{$fieldName}_max"] = $maxValue;
176 }
177 $defaults["{$fieldName}_op"] = $fieldOP;
178 }
179 break;
180
181 case 'in':
182 case 'notin':
183 // send the type as string so that multiple values can also be retrieved from url.
184 // for e.g url like - "memtype_in=in&memtype_value=1,2,3"
185 $value = self::getTypedValue("{$fieldName}_value", CRM_Utils_Type::T_STRING);
186 if (!preg_match('/^(\d+)(,\d+){0,14}$/', $value)) {
187 // extra check. Also put a limit of 15 max values.
188 $value = NULL;
189 }
190 if ($value !== NULL) {
191 $defaults["{$fieldName}_value"] = explode(",", $value);
192 $defaults["{$fieldName}_op"] = $fieldOP;
193 }
194 break;
195 }
196 }
197
74cf4551
EM
198 /**
199 * @param $defaults
200 */
00be9182 201 public static function processChart(&$defaults) {
6a488035
TO
202 $chartType = CRM_Utils_Array::value("charts", $_GET);
203 if (in_array($chartType, array(
353ffa53 204 'barChart',
317fceb4 205 'pieChart',
353ffa53 206 ))) {
6a488035
TO
207 $defaults["charts"] = $chartType;
208 }
209 }
210
74cf4551
EM
211 /**
212 * @param $fieldGrp
213 * @param $defaults
214 */
00be9182 215 public static function processFilter(&$fieldGrp, &$defaults) {
6a488035
TO
216 // process only filters for now
217 foreach ($fieldGrp as $tableName => $fields) {
218 foreach ($fields as $fieldName => $field) {
219 switch (CRM_Utils_Array::value('type', $field)) {
220 case CRM_Utils_Type::T_INT:
912a8063 221 case CRM_Utils_Type::T_FLOAT:
6a488035
TO
222 case CRM_Utils_Type::T_MONEY:
223 self::intParam($fieldName, $field, $defaults);
224 break;
225
226 case CRM_Utils_Type::T_DATE:
227 case CRM_Utils_Type::T_DATE | CRM_Utils_Type::T_TIME:
228 self::dateParam($fieldName, $field, $defaults);
229 break;
230
231 case CRM_Utils_Type::T_STRING:
232 default:
233 self::stringParam($fieldName, $field, $defaults);
234 break;
235 }
236 }
237 }
238 }
239
74cf4551 240 /**
fe482240 241 * unset default filters.
74cf4551
EM
242 * @param $defaults
243 */
00be9182 244 public static function unsetFilters(&$defaults) {
6a488035
TO
245 static $unsetFlag = TRUE;
246 if ($unsetFlag) {
247 foreach ($defaults as $field_name => $field_value) {
248 $newstr = substr($field_name, strrpos($field_name, '_'));
249 if ($newstr == '_value' || $newstr == '_op' ||
250 $newstr == '_min' || $newstr == '_max' ||
251 $newstr == '_from' || $newstr == '_to' ||
252 $newstr == '_relative'
253 ) {
254 unset($defaults[$field_name]);
255 }
256 }
257 $unsetFlag = FALSE;
258 }
259 }
260
74cf4551
EM
261 /**
262 * @param $fieldGrp
263 * @param $defaults
264 */
00be9182 265 public static function processGroupBy(&$fieldGrp, &$defaults) {
6a488035
TO
266 // process only group_bys for now
267 $flag = FALSE;
268
269 if (is_array($fieldGrp)) {
270 foreach ($fieldGrp as $tableName => $fields) {
271 if ($groupBys = CRM_Utils_Array::value("gby", $_GET)) {
272 $groupBys = explode(' ', $groupBys);
273 if (!empty($groupBys)) {
274 if (!$flag) {
275 unset($defaults['group_bys']);
276 $flag = TRUE;
277 }
278 foreach ($groupBys as $gby) {
279 if (array_key_exists($gby, $fields)) {
280 $defaults['group_bys'][$gby] = 1;
281 }
282 }
283 }
284 }
285 }
286 }
287 }
288
74cf4551
EM
289 /**
290 * @param $reportFields
291 * @param $defaults
292 */
00be9182 293 public static function processFields(&$reportFields, &$defaults) {
6a488035
TO
294 //add filters from url
295 if (is_array($reportFields)) {
296 if ($urlFields = CRM_Utils_Array::value("fld", $_GET)) {
297 $urlFields = explode(',', $urlFields);
298 }
ae555e90
DS
299 if (CRM_Utils_Array::value("ufld", $_GET) == 1) {
300 // unset all display columns
301 $defaults['fields'] = array();
302 }
6a488035
TO
303 if (!empty($urlFields)) {
304 foreach ($reportFields as $tableName => $fields) {
305 foreach ($urlFields as $fld) {
306 if (array_key_exists($fld, $fields)) {
307 $defaults['fields'][$fld] = 1;
308 }
309 }
310 }
311 }
312 }
313 }
96025800 314
6a488035 315}