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