3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Report_Utils_Get
{
25 public static function getTypedValue($name, $type) {
26 $value = $_GET[$name] ??
NULL;
27 if ($value === NULL) {
30 return CRM_Utils_Type
::escape($value,
31 CRM_Utils_Type
::typeToString($type),
37 * @param string $fieldName
43 public static function dateParam($fieldName, &$field, &$defaults) {
44 // type = 12 (datetime) is not recognized by Utils_Type::escape() method,
45 // and therefore the below hack
48 $from = self
::getTypedValue("{$fieldName}_from", $type);
49 $to = self
::getTypedValue("{$fieldName}_to", $type);
51 $relative = self
::getTypedValue("{$fieldName}_relative", CRM_Utils_Type
::T_STRING
);
52 if ($relative !== NULL) {
53 $defaults["{$fieldName}_relative"] = $relative;
56 list($from, $to) = CRM_Utils_Date
::getFromTo($relative, NULL, NULL);
57 $from = substr($from, 0, 8);
58 $to = substr($to, 0, 8);
61 if (!($from ||
$to)) {
66 $dateFrom = CRM_Utils_Date
::setDateDefaults($from);
67 if ($dateFrom !== NULL &&
70 $defaults["{$fieldName}_from"] = $dateFrom[0];
75 $dateTo = CRM_Utils_Date
::setDateDefaults($to);
76 if ($dateTo !== NULL &&
79 $defaults["{$fieldName}_to"] = $dateTo[0];
85 * @param string $fieldName
89 public static function stringParam($fieldName, &$field, &$defaults) {
90 $fieldOP = CRM_Utils_Array
::value("{$fieldName}_op", $_GET, 'like');
100 $value = self
::getTypedValue("{$fieldName}_value", CRM_Utils_Array
::value('type', $field));
101 if ($value !== NULL) {
102 $defaults["{$fieldName}_value"] = $value;
103 $defaults["{$fieldName}_op"] = $fieldOP;
109 $defaults["{$fieldName}_op"] = $fieldOP;
115 $value = self
::getTypedValue("{$fieldName}_value", CRM_Utils_Type
::T_STRING
);
116 if ($value !== NULL) {
117 $defaults["{$fieldName}_value"] = explode(",", $value);
118 $defaults["{$fieldName}_op"] = $fieldOP;
125 * @param string $fieldName
129 public static function intParam($fieldName, &$field, &$defaults) {
130 $fieldOP = CRM_Utils_Array
::value("{$fieldName}_op", $_GET, 'eq');
139 $value = self
::getTypedValue("{$fieldName}_value", $field['type']);
140 if ($value !== NULL) {
141 $defaults["{$fieldName}_value"] = $value;
142 $defaults["{$fieldName}_op"] = $fieldOP;
148 $minValue = self
::getTypedValue("{$fieldName}_min", $field['type']);
149 $maxValue = self
::getTypedValue("{$fieldName}_max", $field['type']);
150 if ($minValue !== NULL ||
153 if ($minValue !== NULL) {
154 $defaults["{$fieldName}_min"] = $minValue;
156 if ($maxValue !== NULL) {
157 $defaults["{$fieldName}_max"] = $maxValue;
159 $defaults["{$fieldName}_op"] = $fieldOP;
165 $defaults["{$fieldName}_op"] = $fieldOP;
170 // send the type as string so that multiple values can also be retrieved from url.
171 // for e.g url like - "memtype_in=in&memtype_value=1,2,3"
172 $value = self
::getTypedValue("{$fieldName}_value", CRM_Utils_Type
::T_STRING
);
174 //change the max value to 20, ideally remove condition
175 if (!preg_match('/^(\d+)(,\d+){0,20}$/', $value)) {
179 if ($value !== NULL) {
180 $defaults["{$fieldName}_value"] = explode(",", $value);
181 $defaults["{$fieldName}_op"] = $fieldOP;
190 public static function processChart(&$defaults) {
191 $chartType = $_GET["charts"] ??
NULL;
192 if (in_array($chartType, [
196 $defaults["charts"] = $chartType;
204 public static function processFilter(&$fieldGrp, &$defaults) {
205 // process only filters for now
206 foreach ($fieldGrp as $tableName => $fields) {
207 foreach ($fields as $fieldName => $field) {
208 switch (CRM_Utils_Array
::value('type', $field)) {
209 case CRM_Utils_Type
::T_INT
:
210 case CRM_Utils_Type
::T_FLOAT
:
211 case CRM_Utils_Type
::T_MONEY
:
212 self
::intParam($fieldName, $field, $defaults);
215 case CRM_Utils_Type
::T_DATE
:
216 case CRM_Utils_Type
::T_DATE | CRM_Utils_Type
::T_TIME
:
217 self
::dateParam($fieldName, $field, $defaults);
220 case CRM_Utils_Type
::T_STRING
:
222 self
::stringParam($fieldName, $field, $defaults);
230 * unset default filters.
233 public static function unsetFilters(&$defaults) {
234 static $unsetFlag = TRUE;
236 foreach ($defaults as $field_name => $field_value) {
237 $newstr = substr($field_name, strrpos($field_name, '_'));
238 if ($newstr == '_value' ||
$newstr == '_op' ||
239 $newstr == '_min' ||
$newstr == '_max' ||
240 $newstr == '_from' ||
$newstr == '_to' ||
241 $newstr == '_relative'
243 unset($defaults[$field_name]);
254 public static function processGroupBy(&$fieldGrp, &$defaults) {
255 // process only group_bys for now
258 if (is_array($fieldGrp)) {
259 foreach ($fieldGrp as $tableName => $fields) {
260 if ($groupBys = CRM_Utils_Array
::value("gby", $_GET)) {
261 $groupBys = explode(' ', $groupBys);
262 if (!empty($groupBys)) {
264 unset($defaults['group_bys']);
267 foreach ($groupBys as $gby) {
268 if (array_key_exists($gby, $fields)) {
269 $defaults['group_bys'][$gby] = 1;
279 * @param $reportFields
282 public static function processFields(&$reportFields, &$defaults) {
283 //add filters from url
284 if (is_array($reportFields)) {
285 if ($urlFields = CRM_Utils_Array
::value("fld", $_GET)) {
286 $urlFields = explode(',', $urlFields);
288 if (CRM_Utils_Array
::value("ufld", $_GET) == 1) {
289 // unset all display columns
290 $defaults['fields'] = [];
292 if (!empty($urlFields)) {
293 foreach ($reportFields as $tableName => $fields) {
294 foreach ($urlFields as $fld) {
295 if (array_key_exists($fld, $fields)) {
296 $defaults['fields'][$fld] = 1;