| Conditions | 32 |
| Paths | > 20000 |
| Total Lines | 146 |
| Code Lines | 91 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 219 | protected function &getSearchResults(\Base $f3, \FFCMS\Mappers\Mapper $m, string $users_uuid = null): array |
||
| 220 | { |
||
| 221 | // set up paging limits |
||
| 222 | $minPerPage = 10; |
||
| 223 | $maxPerPage = 100; |
||
| 224 | $perPage = (int) $f3->get('REQUEST.per_page'); |
||
| 225 | if ($perPage < $minPerPage) { |
||
| 226 | $perPage = $minPerPage; |
||
| 227 | } |
||
| 228 | if ($perPage > $maxPerPage) { |
||
| 229 | $perPage = $maxPerPage; |
||
| 230 | } |
||
| 231 | |||
| 232 | $page = $f3->get('REQUEST.page'); |
||
| 233 | if ($page < 1) { |
||
| 234 | $page = 1; |
||
| 235 | } |
||
| 236 | |||
| 237 | // fetch data (paging is 0 based) |
||
| 238 | $allFields = $m->fields(); |
||
| 239 | |||
| 240 | // validate order field |
||
| 241 | $order = self::checkOrderField($f3->get('REQUEST.order'), $allFields); |
||
| 242 | $validFields = self::checkFieldsExist([$f3->get('REQUEST.fields'), $f3->get('REQUEST.search_fields')], $allFields); |
||
| 243 | |||
| 244 | // validated fields to return |
||
| 245 | $fields = empty($validFields['fields']) ? join(',', $allFields) : $validFields['fields']; |
||
| 246 | |||
| 247 | // validated fields to search in, use all if empty |
||
| 248 | $searchFields = empty($validFields['search_fields']) ? join(',', $allFields) : $validFields['search_fields']; |
||
| 249 | |||
| 250 | // get search type |
||
| 251 | $search = $f3->get('REQUEST.search'); |
||
| 252 | if (!empty($search)) { |
||
| 253 | $search = trim(strtolower($search)); |
||
| 254 | } |
||
| 255 | $search_type = $f3->get('REQUEST.search_type'); |
||
| 256 | if (empty($search_type)) { |
||
| 257 | $search_type = 'exact'; |
||
| 258 | } elseif ($search_type !== 'exact') { |
||
| 259 | $search_type = 'fuzzy'; |
||
| 260 | } |
||
| 261 | |||
| 262 | // construct search query |
||
| 263 | $db = \Registry::get('db'); |
||
| 264 | $sqlClauses = []; |
||
| 265 | $searchFieldsArray = preg_split("/[,]/", $searchFields); |
||
| 266 | foreach ($searchFieldsArray as $field) { |
||
| 267 | $sqlClauses[] = 'LOWER(' . $db->quotekey($field) . ') = ' . $db->quote($search); |
||
| 268 | if ($search_type == 'fuzzy') { |
||
| 269 | $sqlClauses[] = 'LOWER(' . $db->quotekey($field) . ') LIKE ' . $db->quote('%' . $search . '%'); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | |||
| 274 | // get total results |
||
| 275 | $isAdmin = $f3->get('isAdmin'); |
||
| 276 | $query = 'SELECT COUNT(*) AS results FROM ' . $db->quotekey($m->table()) . ' WHERE '; |
||
| 277 | if (in_array('users_uuid', $allFields) && !empty($users_uuid)) { |
||
| 278 | $query .= ' users_uuid = ' . $db->quote($users_uuid) . ' AND ('. join(' OR ', $sqlClauses) . ')'; |
||
| 279 | } elseif ($isAdmin && empty($users_uuid)) { |
||
| 280 | $query .= join(' OR ', $sqlClauses); |
||
| 281 | } |
||
| 282 | |||
| 283 | $data = []; |
||
| 284 | $rows = $db->exec($query); |
||
| 285 | $rows = (int) $rows[0]['results']; |
||
| 286 | if ($rows < 1) { |
||
| 287 | return $data; |
||
| 288 | } |
||
| 289 | |||
| 290 | // if fewer results than per page, set per_page |
||
| 291 | if ($page == 1 && $perPage > $rows) { |
||
| 292 | $perPage = $rows; |
||
| 293 | } |
||
| 294 | |||
| 295 | $pagination = []; |
||
| 296 | $pagination['count'] = (int) ceil($rows / $perPage); |
||
| 297 | |||
| 298 | // too high page number? |
||
| 299 | if ($page > $pagination['count']) { |
||
| 300 | $page = $pagination['count']; |
||
| 301 | } |
||
| 302 | |||
| 303 | // set up page URLs |
||
| 304 | $url = $f3->get('PATH'); |
||
| 305 | $urlParams = [ |
||
| 306 | 'per_page' => $perPage, |
||
| 307 | 'search' => $search, |
||
| 308 | 'search_type' => $search_type, |
||
| 309 | ]; |
||
| 310 | if (!empty($order)) { |
||
| 311 | $urlParams['order'] = $order; |
||
| 312 | } |
||
| 313 | if (!empty($fields)) { |
||
| 314 | $urlParams['fields'] = $fields; |
||
| 315 | } |
||
| 316 | ksort($urlParams); |
||
| 317 | |||
| 318 | // previous page url |
||
| 319 | $prevPage = (1 > $page - 1 ) ? null : $page - 1; |
||
| 320 | $nextPage = (1 + $page > $pagination['count']) ? null : $page + 1; |
||
| 321 | |||
| 322 | $resultsFrom = 1 + ($page * $perPage) - $perPage; |
||
| 323 | $resultsTo = $resultsFrom + $perPage - 1; |
||
| 324 | if ($resultsTo > $rows) { |
||
| 325 | $resultsTo = $rows; |
||
| 326 | } |
||
| 327 | |||
| 328 | // return data |
||
| 329 | $data['pagination'] = [ |
||
| 330 | 'url_base' => $this->url($url, $urlParams), |
||
| 331 | 'url_current' => $this->url($url, $urlParams + ['page' => $page]), |
||
| 332 | 'url_first' => $this->url($url, $urlParams + ['page' => 1]), |
||
| 333 | 'url_last' => $this->url($url, $urlParams + ['page' => $pagination['count']]), |
||
| 334 | 'url_next' => (null == $nextPage) ? null : $this->url($url, $urlParams + ['page' => $nextPage]), |
||
| 335 | 'url_previous' => (null == $prevPage) ? null : $this->url($url, $urlParams + ['page' => $prevPage]), |
||
| 336 | 'results' => $rows, |
||
| 337 | 'results_from' => $resultsFrom, |
||
| 338 | 'results_to' => $resultsTo, |
||
| 339 | 'per_page' => $perPage, |
||
| 340 | 'pages' => $pagination['count'], |
||
| 341 | 'page' => $page, |
||
| 342 | 'object' => $m->table(), |
||
| 343 | 'fields' => preg_split("/[,]/", $fields), |
||
| 344 | 'view' => $f3->get('REQUEST.view') |
||
| 345 | ]; |
||
| 346 | |||
| 347 | |||
| 348 | // retrieve results |
||
| 349 | $query = 'SELECT * FROM ' . $db->quotekey($m->table()) . ' WHERE '; |
||
| 350 | if (empty($users_uuid)) { |
||
| 351 | $query .= join(' OR ', $sqlClauses); |
||
| 352 | } else { |
||
| 353 | $query .= ' users_uuid = ' . $db->quote($users_uuid) . ' AND ('. join(' OR ', $sqlClauses) . ')'; |
||
| 354 | } |
||
| 355 | $query .= sprintf(' LIMIT %d,%d', (1 == $page) ? 0 : ($page - 1) * $perPage, $perPage); |
||
| 356 | |||
| 357 | $adminView = $isAdmin || ($isAdmin && 'admin' == $f3->get('REQUEST.view')); |
||
| 358 | $results = $db->exec($query); |
||
| 359 | foreach ($results as $row) { |
||
| 360 | $data['objects'][] = $adminView ? $m->castFields($fields, $row) : $m->exportArray($fields, $row); |
||
| 361 | } |
||
| 362 | |||
| 363 | return $data; |
||
| 364 | } |
||
| 365 | |||
| 367 |