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