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