Merge pull request #136 from pratik-joshi/CRM-12084
[civicrm-core.git] / CRM / Report / Utils / Get.php
CommitLineData
6a488035
TO
1<?php
2// $Id$
3
4/*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28*/
29
30/**
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2013
34 * $Id$
35 *
36 */
37class CRM_Report_Utils_Get {
38
39 static function getTypedValue($name, $type) {
40 $value = CRM_Utils_Array::value($name, $_GET);
41 if ($value === NULL) {
42 return NULL;
43 }
44 return CRM_Utils_Type::escape($value,
45 CRM_Utils_Type::typeToString($type),
46 FALSE
47 );
48 }
49
50 static function dateParam($fieldName, &$field, &$defaults) {
51 // type = 12 (datetime) is not recognized by Utils_Type::escape() method,
52 // and therefore the below hack
53 $type = 4;
54
55 $from = self::getTypedValue("{$fieldName}_from", $type);
56 $to = self::getTypedValue("{$fieldName}_to", $type);
57
58 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $_GET);
59 if ($relative) {
60 list($from, $to) = CRM_Report_Form::getFromTo($relative, NULL, NULL);
61 $from = substr($from, 0, 8);
62 $to = substr($to, 0, 8);
63 }
64
65 if (!($from || $to)) {
66 return FALSE;
67 }
68
69 if ($from !== NULL) {
70 $dateFrom = CRM_Utils_Date::setDateDefaults($from);
71 if ($dateFrom !== NULL &&
72 !empty($dateFrom[0])
73 ) {
74 $defaults["{$fieldName}_from"] = $dateFrom[0];
75 }
76 }
77
78 if ($to !== NULL) {
79 $dateTo = CRM_Utils_Date::setDateDefaults($to);
80 if ($dateTo !== NULL &&
81 !empty($dateTo[0])
82 ) {
83 $defaults["{$fieldName}_to"] = $dateTo[0];
84 }
85 }
86 }
87
88 static function stringParam($fieldName, &$field, &$defaults) {
89 $fieldOP = CRM_Utils_Array::value("{$fieldName}_op", $_GET, 'like');
90
91 switch ($fieldOP) {
92 case 'has':
93 case 'sw':
94 case 'ew':
95 case 'nhas':
96 case 'like':
97 case 'neq':
98 $value = self::getTypedValue("{$fieldName}_value", CRM_Utils_Array::value('type', $field));
99 if ($value !== NULL) {
100 $defaults["{$fieldName}_value"] = $value;
101 $defaults["{$fieldName}_op"] = $fieldOP;
102 }
103 break;
104
105 case 'nll':
106 case 'nnll':
107 $defaults["{$fieldName}_op"] = $fieldOP;
108 break;
109 }
110 }
111
112 static function intParam($fieldName, &$field, &$defaults) {
113 $fieldOP = CRM_Utils_Array::value("{$fieldName}_op", $_GET, 'eq');
114
115 switch ($fieldOP) {
116 case 'lte':
117 case 'gte':
118 case 'eq':
119 case 'lt':
120 case 'gt':
121 case 'neq':
122 $value = self::getTypedValue("{$fieldName}_value", $field['type']);
123 if ($value !== NULL) {
124 $defaults["{$fieldName}_value"] = $value;
125 $defaults["{$fieldName}_op"] = $fieldOP;
126 }
127 break;
128
129 case 'bw':
130 case 'nbw':
131 $minValue = self::getTypedValue("{$fieldName}_min", $field['type']);
132 $maxValue = self::getTypedValue("{$fieldName}_max", $field['type']);
133 if ($minValue !== NULL ||
134 $maxValue !== NULL
135 ) {
136 if ($minValue !== NULL) {
137 $defaults["{$fieldName}_min"] = $minValue;
138 }
139 if ($maxValue !== NULL) {
140 $defaults["{$fieldName}_max"] = $maxValue;
141 }
142 $defaults["{$fieldName}_op"] = $fieldOP;
143 }
144 break;
145
146 case 'in':
147 case 'notin':
148 // send the type as string so that multiple values can also be retrieved from url.
149 // for e.g url like - "memtype_in=in&memtype_value=1,2,3"
150 $value = self::getTypedValue("{$fieldName}_value", CRM_Utils_Type::T_STRING);
151 if (!preg_match('/^(\d+)(,\d+){0,14}$/', $value)) {
152 // extra check. Also put a limit of 15 max values.
153 $value = NULL;
154 }
155 if ($value !== NULL) {
156 $defaults["{$fieldName}_value"] = explode(",", $value);
157 $defaults["{$fieldName}_op"] = $fieldOP;
158 }
159 break;
160 }
161 }
162
163 static function processChart(&$defaults) {
164 $chartType = CRM_Utils_Array::value("charts", $_GET);
165 if (in_array($chartType, array(
166 'barChart', 'pieChart'))) {
167 $defaults["charts"] = $chartType;
168 }
169 }
170
171 static function processFilter(&$fieldGrp, &$defaults) {
172 // process only filters for now
173 foreach ($fieldGrp as $tableName => $fields) {
174 foreach ($fields as $fieldName => $field) {
175 switch (CRM_Utils_Array::value('type', $field)) {
176 case CRM_Utils_Type::T_INT:
177 case CRM_Utils_Type::T_MONEY:
178 self::intParam($fieldName, $field, $defaults);
179 break;
180
181 case CRM_Utils_Type::T_DATE:
182 case CRM_Utils_Type::T_DATE | CRM_Utils_Type::T_TIME:
183 self::dateParam($fieldName, $field, $defaults);
184 break;
185
186 case CRM_Utils_Type::T_STRING:
187 default:
188 self::stringParam($fieldName, $field, $defaults);
189 break;
190 }
191 }
192 }
193 }
194
195 //unset default filters
196 static function unsetFilters(&$defaults) {
197 static $unsetFlag = TRUE;
198 if ($unsetFlag) {
199 foreach ($defaults as $field_name => $field_value) {
200 $newstr = substr($field_name, strrpos($field_name, '_'));
201 if ($newstr == '_value' || $newstr == '_op' ||
202 $newstr == '_min' || $newstr == '_max' ||
203 $newstr == '_from' || $newstr == '_to' ||
204 $newstr == '_relative'
205 ) {
206 unset($defaults[$field_name]);
207 }
208 }
209 $unsetFlag = FALSE;
210 }
211 }
212
213 static function processGroupBy(&$fieldGrp, &$defaults) {
214 // process only group_bys for now
215 $flag = FALSE;
216
217 if (is_array($fieldGrp)) {
218 foreach ($fieldGrp as $tableName => $fields) {
219 if ($groupBys = CRM_Utils_Array::value("gby", $_GET)) {
220 $groupBys = explode(' ', $groupBys);
221 if (!empty($groupBys)) {
222 if (!$flag) {
223 unset($defaults['group_bys']);
224 $flag = TRUE;
225 }
226 foreach ($groupBys as $gby) {
227 if (array_key_exists($gby, $fields)) {
228 $defaults['group_bys'][$gby] = 1;
229 }
230 }
231 }
232 }
233 }
234 }
235 }
236
237 static function processFields(&$reportFields, &$defaults) {
238 //add filters from url
239 if (is_array($reportFields)) {
240 if ($urlFields = CRM_Utils_Array::value("fld", $_GET)) {
241 $urlFields = explode(',', $urlFields);
242 }
243 if (!empty($urlFields)) {
244 foreach ($reportFields as $tableName => $fields) {
245 foreach ($urlFields as $fld) {
246 if (array_key_exists($fld, $fields)) {
247 $defaults['fields'][$fld] = 1;
248 }
249 }
250 }
251 }
252 }
253 }
254}
255