/** * Search for a text in an array, removing the rows which haven't got it * * @param array $array * @param string $text Fragment of text to look for (search is case insensitive) * @return array $array The filtered array */ function array_filter_quicksearch($array, $text) { $out = array(); $text = mb_strtolower($text); foreach ($array as $key => $value) { $keep = false; if (is_array($value)) { $keep = array_filter_quicksearch($value, $text); } else { $keep = mb_strpos(strtolower($value), $text) !== false; } if ($keep) { $out[$key] = $value; } } $out = array_filter($out); return $out; }
public static function search($orderby = "", $filters = false) { global $DB; global $website; global $theme; // retrieve custom templates $DB->queryLimit('id,title,NULL as theme,permission,enabled', 'nv_templates', 'website = ' . $website->id, $orderby, 0, PHP_INT_MAX); $dataset = $DB->result(); // retrieve theme templates for ($t = 0; $t < count($theme->templates); $t++) { $template = new template(); $template->load_from_theme($theme->templates[$t]->type); $dataset[] = array('id' => $template->id, 'title' => $template->title, 'theme' => $theme->title, 'permission' => $template->permission, 'enabled' => $template->enabled); unset($template); } // filter results if (!empty($filters)) { if (!empty($filters['quicksearch'])) { $dataset = array_filter_quicksearch($dataset, $filters['quicksearch']); } } // reorder results // SQL format: column direction (only ONE column allowed) // example: id desc $column = array(); list($order_column, $order_direction) = explode(' ', $orderby); for ($d = 0; $d < count($dataset); $d++) { $column[] = $dataset[$d][$order_column]; } array_multisort($column, $order_direction == 'desc' ? SORT_DESC : SORT_ASC, $dataset); return $dataset; }