Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-06-23-18-25-12
[civicrm-core.git] / CRM / Report / Utils / Get.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
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 $name
40 * @param $type
41 *
42 * @return mixed|null
43 */
44 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 $fieldName
57 * @param $field
58 * @param $defaults
59 */
60 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 $fieldName
100 * @param $field
101 * @param $defaults
102 */
103 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 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 $fieldName
138 * @param $field
139 * @param $defaults
140 */
141 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 static function processChart(&$defaults) {
196 $chartType = CRM_Utils_Array::value("charts", $_GET);
197 if (in_array($chartType, array(
198 'barChart', 'pieChart'))) {
199 $defaults["charts"] = $chartType;
200 }
201 }
202
203 /**
204 * @param $fieldGrp
205 * @param $defaults
206 */
207 static function processFilter(&$fieldGrp, &$defaults) {
208 // process only filters for now
209 foreach ($fieldGrp as $tableName => $fields) {
210 foreach ($fields as $fieldName => $field) {
211 switch (CRM_Utils_Array::value('type', $field)) {
212 case CRM_Utils_Type::T_INT:
213 case CRM_Utils_Type::T_FLOAT:
214 case CRM_Utils_Type::T_MONEY:
215 self::intParam($fieldName, $field, $defaults);
216 break;
217
218 case CRM_Utils_Type::T_DATE:
219 case CRM_Utils_Type::T_DATE | CRM_Utils_Type::T_TIME:
220 self::dateParam($fieldName, $field, $defaults);
221 break;
222
223 case CRM_Utils_Type::T_STRING:
224 default:
225 self::stringParam($fieldName, $field, $defaults);
226 break;
227 }
228 }
229 }
230 }
231
232 //unset default filters
233 /**
234 * @param $defaults
235 */
236 static function unsetFilters(&$defaults) {
237 static $unsetFlag = TRUE;
238 if ($unsetFlag) {
239 foreach ($defaults as $field_name => $field_value) {
240 $newstr = substr($field_name, strrpos($field_name, '_'));
241 if ($newstr == '_value' || $newstr == '_op' ||
242 $newstr == '_min' || $newstr == '_max' ||
243 $newstr == '_from' || $newstr == '_to' ||
244 $newstr == '_relative'
245 ) {
246 unset($defaults[$field_name]);
247 }
248 }
249 $unsetFlag = FALSE;
250 }
251 }
252
253 /**
254 * @param $fieldGrp
255 * @param $defaults
256 */
257 static function processGroupBy(&$fieldGrp, &$defaults) {
258 // process only group_bys for now
259 $flag = FALSE;
260
261 if (is_array($fieldGrp)) {
262 foreach ($fieldGrp as $tableName => $fields) {
263 if ($groupBys = CRM_Utils_Array::value("gby", $_GET)) {
264 $groupBys = explode(' ', $groupBys);
265 if (!empty($groupBys)) {
266 if (!$flag) {
267 unset($defaults['group_bys']);
268 $flag = TRUE;
269 }
270 foreach ($groupBys as $gby) {
271 if (array_key_exists($gby, $fields)) {
272 $defaults['group_bys'][$gby] = 1;
273 }
274 }
275 }
276 }
277 }
278 }
279 }
280
281 /**
282 * @param $reportFields
283 * @param $defaults
284 */
285 static function processFields(&$reportFields, &$defaults) {
286 //add filters from url
287 if (is_array($reportFields)) {
288 if ($urlFields = CRM_Utils_Array::value("fld", $_GET)) {
289 $urlFields = explode(',', $urlFields);
290 }
291 if (CRM_Utils_Array::value("ufld", $_GET) == 1) {
292 // unset all display columns
293 $defaults['fields'] = array();
294 }
295 if (!empty($urlFields)) {
296 foreach ($reportFields as $tableName => $fields) {
297 foreach ($urlFields as $fld) {
298 if (array_key_exists($fld, $fields)) {
299 $defaults['fields'][$fld] = 1;
300 }
301 }
302 }
303 }
304 }
305 }
306 }
307