Code

Limit for autocompleter, sort autocompletition results
[gosa.git] / gosa-core / include / class_filter.inc
index 3dca983e0da73e86e7cd05643ff0a02bb567bb50..4a132154ccdc52eed46a0e982dc3dc193604c48e 100644 (file)
@@ -1,4 +1,24 @@
 <?php
+/*
+ * This code is part of GOsa (http://www.gosa-project.org)
+ * Copyright (C) 2003-2008 GONICUS GmbH
+ *
+ * ID: $$Id$$
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
 
 class filter {
 
@@ -9,7 +29,6 @@ class filter {
   var $autocompleter= array();
   var $category= "";
   var $objectStorage= array();
-  var $objectBase= "";
   var $base= "";
   var $scope= "";
   var $query;
@@ -103,6 +122,17 @@ class filter {
           $this->alphabetElements[]= $tag;
         }
       }
+
+      // Sort elements for element length to allow proper replacing later on
+      function strlenSort($a, $b) {
+        if (strlen($a['tag']) == strlen($b['tag'])) {
+          return 0;
+        }
+       return (strlen($a['tag']) < strlen($b['tag']) ? -1 : 1);
+      } 
+      uasort($this->elements, 'strlenSort');
+      $this->elements= array_reverse($this->elements);
+
     }
 
     return true;  
@@ -112,7 +142,15 @@ class filter {
   function getTextfield($element)
   {
     $tag= $element['tag'];
-    $result= "<input class='filter_textfield' id='$tag' name='$tag' type='text' size='30' maxlength='30' value='".$this->elementValues[$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'])) {
       $frequency= "0.5";
       $characters= "1";
@@ -204,11 +242,6 @@ class filter {
   }
 
 
-  function setObjectBase($base) {
-    $this->objectBase= $base;    
-  }
-
-
   function setBase($base) {
     $this->base= $base;
   }
@@ -344,7 +377,7 @@ class filter {
       }
 
       $result= array_merge($result, call_user_func(array($backend, 'query'), $this->base, $this->scope, $filter, $attributes,
-                               $this->category, $this->objectStorage, $this->objectBase));
+                               $this->category, $this->objectStorage));
     }
     
 
@@ -389,12 +422,13 @@ class filter {
           $this->elementValues[$tag]= "";
         }
       }
-    }
 
-    // Save scope if needed
-    if ($this->scopeMode == "auto") {
-      $this->scope= isset($_POST['SCOPE'])?"sub":"one";
+      // Save scope if needed
+      if ($this->scopeMode == "auto") {
+        $this->scope= isset($_POST['SCOPE'])?"sub":"one";
+      }
     }
+
   }
 
 
@@ -418,13 +452,12 @@ class filter {
 
     // Make filter
     $filter= preg_replace("/\\$$tag/", normalizeLDAP($value), $filter);
-    if (isset($config['base']) && isset($config['scope'])
-        && isset($config['category']) && isset($config['objectbase']) ) {
+    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["objectbase"]);
+                           $config["category"], $config["objectStorage"]);
     } else {
       $result= call_user_func(array($backend, 'query'), $this->base, $this->scope, $filter, $attributes,
-                           $this->category, $this->objectStorage, $this->objectBase);
+                           $this->category, $this->objectStorage);
     }
 
     foreach ($result as $entry) {
@@ -448,18 +481,25 @@ class filter {
     global $class_mapping;
     $result= array();
 
+    // Introduce maximum number of entries
+    $max= 10;
+
     foreach ($this->autocompleters as $tag => $config) {
       if(isset($_POST[$tag])){
         $result= $this->getCompletitionList($config, $tag, $_POST[$tag]);
+        $result= array_unique($result);
+        asort($result);
 
         echo '<ul>';
         foreach ($result as $entry) {
           echo '<li>'.$entry.'</li>';
+          if ($max-- == 0) {
+            break;
+          }
         }
 
         echo '</ul>';
       }
-
     }
   }