From f6d7db5f0748f8af75d712e9a243a5b8ea5fc5e4 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Sun, 23 Jan 2005 17:54:13 +0000 Subject: [PATCH] added functions to filter account lists --- lam/lib/lists.inc | 62 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/lam/lib/lists.inc b/lam/lib/lists.inc index 14747323..9678d984 100644 --- a/lam/lib/lists.inc +++ b/lam/lib/lists.inc @@ -29,6 +29,62 @@ $Id$ * @author Roland Gruber */ +/** +* Builds the regular expressions from the filter values. +* +* @param array $post HTTP Post values +* @param array $attributes list of displayed attributes +* @return array filter data array($attribute => array('regex' => $reg, 'original' => $orig)) +* $reg is the regular expression to use, $orig the user's unmodified input string +*/ +function listBuildFilter($post, $attributes) { + $filter = array(); + for ($i = 0; $i < sizeof($attributes); $i++) { + if (eregi('^([0-9a-z_\\*\\$])+$', $_POST["filter" . strtolower($attributes[$i])])) { + $filter[$attributes[$i]]['original'] = $_POST["filter" . strtolower($attributes[$i])]; + $filter[$attributes[$i]]['regex'] = $_POST["filter" . strtolower($attributes[$i])]; + // replace special characters + $filter[$attributes[$i]]['regex'] = str_replace("*", "(.)*", $filter[$attributes[$i]]['regex']); + $filter[$attributes[$i]]['regex'] = str_replace('$', '[$]', $filter[$attributes[$i]]['regex']); + // add string begin and end + $filter[$attributes[$i]]['regex'] = "^" . $filter[$attributes[$i]]['regex'] . "$"; + } + } + return $filter; +} + + +/** +* Removes all entries which do not fit to the filter. +* +* @param array $entries list of accounts +* @param array $filter attribute filter +* @return array filtered list of accounts +*/ +function listFilterAccounts($entries, $filter) { + $attributes = array_keys($filter); + for ($r = 0; $r < sizeof($entries); $r++) { + for ($a = 0; $a < sizeof($attributes); $a++) { + // check if filter fits + $found = false; + for ($i = 0; $i < sizeof($entries[$r][$attributes[$a]]); $i++) { + if (eregi($filter[$attributes[$a]]['regex'], $entries[$r][$attributes[$a]][$i])) { + $found = true; + break; + } + } + if (!$found) { + // remove account and reindex array + unset($entries[$r]); + $entries = array_values($entries); + $r--; + break; + } + } + } + return $entries; +} + /** * Sorts an account list by a given attribute @@ -94,7 +150,7 @@ function listDrawNavigationBar($count, $max_page_entries, $page, $sort, $searchF echo("\n"); echo("  "); if ($page != 1) { - echo("<=\n"); + echo("<=\n"); } else { echo("<="); @@ -102,7 +158,7 @@ function listDrawNavigationBar($count, $max_page_entries, $page, $sort, $searchF echo(" "); if ($page < ($count / $max_page_entries)) { - echo("=>\n"); + echo("=>\n"); } else { echo("=>"); @@ -119,7 +175,7 @@ function listDrawNavigationBar($count, $max_page_entries, $page, $sort, $searchF echo(" " . ($i + 1)); } else { - echo(" " . ($i + 1) . "\n"); + echo(" " . ($i + 1) . "\n"); } } echo("\n");