Code

Replaced config->search with get_cfg_value
[gosa.git] / gosa-core / include / class_filter.inc
index 8a0a9dfc13abf12efef2246ae0c459aeccaceb5e..04de2dae2c5cb13fce48ef21f3638fe6ce823937 100644 (file)
 
 class filter {
 
-  var $xmlData;
   var $searches= array();
+  var $xmlSearches= array();
+  var $attributes= array();
   var $search;
+  var $defaultSearch;
   var $category= "";
   var $objectStorage= array();
   var $base= "";
@@ -53,34 +55,34 @@ class filter {
   function load($filename)
   {
     $contents = file_get_contents($filename);
-    $this->xmlData= xml::xml2array($contents, 1);
+    $xmlData= xml::xml2array($contents, 1);
 
-    if (!isset($this->xmlData['filterdef'])) {
+    if (!isset($xmlData['filterdef'])) {
       return false;
     }
 
-    $this->xmlData= $this->xmlData["filterdef"];
+    $xmlData= $xmlData["filterdef"];
 
     // Load filter
-    if (isset($this->xmlData['search'])) {
+    if (isset($xmlData['search'])) {
 
       // Array conversion
-      if (!is_array($this->xmlData['search'])) {
-        $searches= array($this->xmlData['search']);
+      if (!isset($xmlData['search'][0])) {
+        $searches= array($xmlData['search']);
       } else {
-        $searches= $this->xmlData['search'];
+        $searches= $xmlData['search'];
       }
 
       /* Store available searches */
-      foreach ($this->xmlData['search'] as $search) {
+      foreach ($searches as $search) {
 
         /* Do multi conversation */ 
-        if (!is_array($search['query'])){
+        if (!isset($search['query'][0])){
           $search['query']= array($search['query']);
         }
 
         /* Store search */
-        $this->searches[$search['label']]= $search;
+        $this->xmlSearches[$search['tag']]= $search;
 
       }
     } else {
@@ -88,38 +90,60 @@ class filter {
     }
 
     // Transfer scope
-    $this->scopeMode= $this->xmlData['definition']['scope'];
+    $this->scopeMode= $xmlData['definition']['scope'];
     if ($this->scopeMode == "auto") {
       $this->scope= "one";
     } else {
       $this->scope= $this->scopeMode;
     }
 
+    // Transfer attributes
+    $this->attributes= $xmlData['definition']['attribute'];
+    if (!is_array($this->attributes)) {
+      $this->attributes= array($this->attributes);
+    }
+
     // Transfer initial value
-    if (isset($this->xmlData['definition']['initial']) && $this->xmlData['definition']['initial'] == "true"){
+    if (isset($xmlData['definition']['initial']) && $xmlData['definition']['initial'] == "true"){
       $this->initial= true;
     }
 
     // Transfer category
-    if (isset($this->xmlData['definition']['category'])){
-      $this->category= $this->xmlData['definition']['category'];
+    if (isset($xmlData['definition']['category'])){
+      $this->category= $xmlData['definition']['category'];
+    }
+    if (!is_array($this->category)) {
+      $this->category= array($this->category);
     }
 
-    // Set default search mode
-    $this->setSearch($this->xmlData['definition']['default']);
+    // Initialize searches and default search mode
+    $this->defaultSearch= $xmlData['definition']['default'];
+    $this->reloadFilters();
+    $this->setSearch($this->defaultSearch);
 
     return true;  
   }
 
 
-  function setSearch($method)
+  function reloadFilters()
   {
-    // Move information
+    $this->searches= array_merge($this->xmlSearches, userFilter::getFilter($this->category));
+    $this->setSearch($this->search);
+  }
+
+
+  function setSearch($method= null)
+  {
+    $patch= null;
+
+    // Maybe our search method has gone?
+    if (!isset($this->searches[$method])) {
+      $method= $this->defaultSearch;
+    }
+
+    // Try to use it, but bail out if there's no help...
     if (isset($this->searches[$method])) {
       $this->query= $this->searches[$method]['query'];
-      if (!isset($this->query[0])) {
-        $this->query= array($this->query);
-      }
       $this->search= $method;
     } else {
       die ("Invalid search module!");
@@ -127,19 +151,12 @@ class filter {
   }
 
 
-  function getTextfield($element)
+  function getTextfield($tag, $value= "", $element= null)
   {
-    $tag= $element['tag'];
     $size= 30;
-    if (isset($element['size'])){
-      $size= $element['size'];
-    }
     $maxlength= 30;
-    if (isset($element['maxlength'])){
-      $maxlength= $element['maxlength'];
-    }
-    $result= "<input class='filter_textfield' id='$tag' name='$tag' type='text' size='$size' maxlength='{$maxlength}' value='".$this->elementValues[$tag]."'>";
-    if (isset($element['autocomplete'])) {
+    $result= "<input class='filter_textfield' placeholder='"._("Search")."...' id='$tag' name='$tag' type='text' size='$size' maxlength='{$maxlength}' value='".$value."'>";
+    if ($element && isset($element['autocomplete'])) {
       $frequency= "0.5";
       $characters= "1";
       if (isset($element['autocomplete']['frequency'])) {
@@ -149,11 +166,11 @@ class filter {
         $characters= $element['autocomplete']['characters'];
       }
       $result.= "<div id='autocomplete$tag' class='autocomplete'></div>".
-                "<script type='text/javascript'>".
-                "new Ajax.Autocompleter('$tag', 'autocomplete$tag', 'autocomplete.php', { minChars: $characters, frequency: $frequency });".
-                "</script>";
+        "<script type='text/javascript'>".
+        "new Ajax.Autocompleter('$tag', 'autocomplete$tag', 'autocomplete.php', { minChars: $characters, frequency: $frequency });".
+        "</script>";
 
-       $this->autocompleters[$tag]= $element['autocomplete'];
+      $this->autocompleters[$tag]= $element;
     }
     return $result;
   }
@@ -203,22 +220,12 @@ class filter {
   }
 
 
-  function renderApply()
-  {
-    return ("<input type='submit' name='apply' value='"._("Apply filter")."'>");
-  }
-
-
-  function renderScope()
-  {
-    $checked= $this->scope == "sub"?" checked":"";
-    return "<input type='checkbox' id='SCOPE' name='SCOPE' value='1' onClick='document.mainform.submit();'$checked>&nbsp;<LABEL for='SCOPE'>"._("Search in subtrees")."</LABEL>";
-  }
-
 
   function render()
   {
-    $content= "Search comes here...";
+    $content= "<table class='filter-wrapper' summary='"._("Search filter")."'><tr><td>".$this->renderFilterMenu()."</td><td>";
+    $content.= "<div class='search-filter'>".$this->getTextfield('search_filter', $this->value, $this->searches[$this->search])."</div>".
+      "&nbsp;<button class='search-filter' type='submit' title='"._("Search")."'>".image("images/find.png")."</button></td></tr></table>";
 
     // Return meta data
     return ("<input type='hidden' name='FILTER_PID' value='".$this->pid."'>".$content);
@@ -235,10 +242,9 @@ class filter {
       $this->initial= true;
       return $result;
     }
-
     // Go thru all queries and merge results
     foreach ($this->query as $query) {
-      if (!isset($query['backend']) || !isset($query['filter']) || !isset($query['attribute'])) {
+      if (!isset($query['backend']) || !isset($query['filter'])) {
         die("No backend specified in search config.");
       }
 
@@ -250,24 +256,27 @@ class filter {
 
       // Load filter and attributes
       $filter= $query['filter'];
-      $attributes= $query['attribute'];
 
       // Handle converters if present
       if ($this->converter) {
         preg_match('/([^:]+)::(.*)$/', $this->converter, $m);
-        $filter= call_user_func(array($m[1], $m[2]), preg_replace('/\$/', $this->value, $filter));
+        if ($this->value == "") {
+          $filter= call_user_func(array($m[1], $m[2]), preg_replace('/\$/', "*", $filter));
+        } else {
+          $filter= call_user_func(array($m[1], $m[2]), preg_replace('/\$/', $this->value, $filter));
+        }
       }
 
       // Do not replace escaped \$ - This is required to be able to search for e.g. windows machines.
       if ($this->value == "") {
         $filter= preg_replace("/\\$/", '*', $filter);
       } else {
-        $filter= preg_replace("/\\$/", normalizeLdap($this->value), $filter);
+        $filter= preg_replace("/\\$/", "*".normalizeLdap($this->value)."*", $filter);
       }
 
-      $result= array_merge($result, call_user_func(array($backend, 'query'), $this->base, $this->scope, $filter, $attributes, $this->category, $this->objectStorage));
+      $result= array_merge($result, call_user_func(array($backend, 'query'), $this->base, $this->scope, $filter, $this->attributes, $this->category, $this->objectStorage));
     }
-    
+
     return ($result);
   }
 
@@ -275,15 +284,24 @@ class filter {
   function update()
   {
     if (isset($_POST['FILTER_PID']) && $_POST['FILTER_PID'] == $this->pid) {
-
       // Save input field
       if (isset($_POST['search_filter'])) {
         $this->value= validate($_POST['search_filter']);
       }
 
       // Save scope if needed
-      if ($this->scopeMode == "auto") {
-        $this->scope= isset($_POST['SCOPE'])?"sub":"one";
+      if ($this->scopeMode == "auto" && isset($_POST['act']) && $_POST['act'] == "toggle-subtree") {
+        $this->scope= ($this->scope == "one")?"sub":"one";
+      }
+
+      // Switch filter?
+      if (isset($_POST['act'])) {
+        foreach ($this->searches as $tag => $cfg) {
+          if ($_POST['act'] == "filter-$tag") {
+            $this->setSearch($tag);
+            break;
+          }
+        }
       }
     }
 
@@ -295,27 +313,30 @@ class filter {
     global $class_mapping;
     $res= array();
 
-    // Is backend available?
-    $backend= "filter".$config['backend'];
-    if (!isset($class_mapping["$backend"])) {
-      die("Invalid backend specified in search config.");
-    }
-
-    // Load filter and attributes
-    $filter= $config['filter'];
-    $attributes= $config['attribute'];
+    // Load result attributes
+    $attributes= $config['autocomplete']['attribute'];
     if (!is_array($attributes)) {
       $attributes= array($attributes);
     }
 
-    // Make filter
-    $filter= preg_replace("/\\$/", normalizeLdap($value), $filter);
-    if (isset($config['base']) && isset($config['scope']) && isset($config['category'])) {
-      $result= call_user_func(array($backend, 'query'), $config['base'], $config['scope'], $filter, $attributes,
-                           $config["category"], $config["objectStorage"]);
-    } else {
-      $result= call_user_func(array($backend, 'query'), $this->base, $this->scope, $filter, $attributes,
-                           $this->category, $this->objectStorage);
+    // Do the query
+    $result= array();
+
+    // Is backend available?
+    # FIXME
+    $queries= $config['query'];
+    if (!isset($queries[0])){
+      $queries= array($queries);
+    }
+    foreach ($queries as $query) {
+      $backend= "filter".$query['backend'];
+      if (!isset($class_mapping["$backend"])) {
+        die("Invalid backend specified in search config.");
+      }
+      $filter= preg_replace("/\\$/", "*".normalizeLdap($value)."*", $query['filter']);
+
+      $result= array_merge($result, call_user_func(array($backend, 'query'), $this->base, $this->scope, $filter, $attributes,
+          $this->category, $this->objectStorage));
     }
 
     foreach ($result as $entry) {
@@ -345,7 +366,7 @@ class filter {
     $max= 25;
 
     if(isset($this->searches[$this->search]['autocomplete'])){
-      $result= $this->getCompletitionList($this->searches[$this->search]['autocomplete'], $_POST['search_filter']);
+      $result= $this->getCompletitionList($this->searches[$this->search], $_POST['search_filter']);
       $result= array_unique($result);
       asort($result);
 
@@ -384,7 +405,64 @@ class filter {
 
     return $base;
   }
+
+
+
+  function renderFilterMenu()
+  {
+    // Load shortcut
+    $result= "<ul class='level1' id='filter-root'><li><a href='#'>".image("images/filter.png").image("images/lists/sort-down.png")."</a>";
+
+    // Build ul/li list
+    $separator= " style='border-top:1px solid #AAA' ";
+    $result.= "<ul class='level2'>";
+
+    // Build in filters
+    foreach ($this->xmlSearches as $tag => $config) {
+      $label = htmlentities(_($config['label']),ENT_COMPAT,'UTF-8');
+      if ($tag == $this->search) {
+        $result.= "<li><a href='#'>".image("images/checked.png")."&nbsp;".$label."</a></li>";
+      } else {
+        $result.= "<li><a href='#' onClick='\$(\"act\").value= \"filter-$tag\";\$(\"exec_act\").click();'>".image("images/empty.png")."&nbsp;".$label."</a></li>";
+      }
+    }
+
+    // User defined filters
+    $first= true;
+    foreach (userFilter::getFilter($this->category) as $tag => $config) {
+      if ($tag == $this->search) {
+        $result.= "<li".($first?$separator:"")."><a href='#'>".image("images/checked.png")."&nbsp;"._($config['description'])."</a></li>";
+      } else {
+        $result.= "<li".($first?$separator:"")."><a href='#' onClick='\$(\"act\").value= \"filter-$tag\";\$(\"exec_act\").click();'>".image("images/empty.png")."&nbsp;"._($config['description'])."</a></li>";
+      }
+
+      $first= false;
+    }
+
+    // Render scope if set to auto
+    if ($this->scopeMode == "auto") {
+      $result.= "<li$separator><a href='#' onClick='\$(\"act\").value= \"toggle-subtree\";\$(\"exec_act\").click();'>".($this->scope=="one"?image("images/empty.png"):image("images/checked.png"))."&nbsp;"._("Search in subtrees")."</a></li>";
+    }
+
+
+    // Edit filter menu
+    $result.= "<li$separator><a href='#' onClick='\$(\"act\").value= \"config-filter\";\$(\"exec_act\").click();'>".image("images/configure.png")."&nbsp;"._("Edit filters")."...</a></li>";
+
+    $result.= "</ul>";
+
+    $script= '<script type="text/javascript" language="JavaScript">var menu2; menu2= new Menu("filter-root", "menu2", configMenu)</script>';
+
+    return "<div id='filtermenu'>".$result."</li></ul></div>$script";
+  }
+
+
+  function getFixedFilters()
+  {
+    return array_keys($this->searches);
+  }
+
 }
 
 
+// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>