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