Code

Prevent multiple categories of the same type in userFilterEditor
[gosa.git] / gosa-core / include / class_userFilterEditor.inc
index 82cf1b11a3ee226850ce2cb52e90642e6a34d55d..3cdbc6f2c69843eccb4ee054ee49564ede316376 100644 (file)
@@ -14,28 +14,33 @@ class userFilterEditor extends plugin
   public $selectedCategories = array();
   public $share = FALSE;
   public $enabled = TRUE;
-  public $filter = "(objectClass=*)";
+  public $queries = array();
+
+  public $listing = NULL;
 
   // The list of all categories mangaged by the current filter object.
   // Used in the grop-down box.
-  public $availableCategories = array();
-  public $fixedFilters = array();
   public $orig_name = "";
 
+  public $backends = array('LDAP', 'SYSTEMS', 'FAI', 'GroupLDAP','ACL');  
+
 
   /*! \brief    Instantiate the filter editing dialog. 
    *            Parses the filter info into editable data.
    */
-  function __construct($entry, $categories, $fixedFilters)
+  function __construct($entry, $listing)
   {
-    $this->availableCategories = $categories;
-    $this->fixedFilters = $fixedFilters;
+    $this->listing = &$listing;
     if($entry){
       $this->entry = $entry;
       $this->parent = $entry['parent'];
       $this->name = $entry['name'];
       $this->description = $entry['description'];
-      $this->filter = userFilterEditor::_autoIndentFilter($entry['filter'], "\t");
+      
+      foreach($entry['queries'] as $query){
+        $query['filter'] = userFilterEditor::_autoIndentFilter($query['filter'], "  ");
+        $this->queries[] = $query;
+      }
       $this->selectedCategories = $entry['categories'];
       $this->share = in_array("share",$entry['flags']);
       $this->enable = in_array("enable",$entry['flags']);
@@ -112,16 +117,29 @@ class userFilterEditor extends plugin
   function execute()
   {
     plugin::execute();
+
     $smarty = get_smarty();
+
+    // Build up HTML compliant html output
+    $queries = array();
+    foreach($this->queries as $key => $query){
+      $query['filter'] =  htmlentities($query['filter'],ENT_COMPAT,'UTF-8');
+      $queries[$key] = $query; 
+    }  
+
+    // Build up list of hard coded filters 
+    $filter= $this->listing->getFilter();
+
+    $smarty->assign("fixedFilters", array_keys($filter->searches));
     $smarty->assign('parent', $this->parent);
+    $smarty->assign('backends', $this->backends);
     $smarty->assign('name', htmlentities($this->name,ENT_COMPAT,'UTF-8'));
-    $smarty->assign('filter', htmlentities($this->filter,ENT_COMPAT,'UTF-8'));
+    $smarty->assign('queries', $queries);
     $smarty->assign('share', $this->share);
     $smarty->assign('enable', $this->enabled);
     $smarty->assign('description', htmlentities($this->description,ENT_COMPAT,'UTF-8'));
     $smarty->assign('selectedCategories', $this->selectedCategories);
-    $smarty->assign('availableCategories', $this->availableCategories);
-    $smarty->assign('fixedFilters', $this->fixedFilters);
+    $smarty->assign('availableCategories', array_unique($this->listing->categories));
     return($smarty->fetch(get_template_path('userFilterEditor.tpl', FALSE)));
   }
 
@@ -141,12 +159,22 @@ class userFilterEditor extends plugin
 
       // Filter needs special handling, it may contain charactes like < and >
       //  wich are stipped out by get_post() && validate()
-      if(isset($_POST['filter'])){
-        $f = mb_convert_encoding($_POST['filter'], 'UTF-8');
-        if(get_magic_quotes_gpc()){
-          $f = stripcslashes($f);
+      foreach($this->queries as $key => $query){
+        if(isset($_POST['filter_'.$key])){
+          $f = mb_convert_encoding($_POST['filter_'.$key], 'UTF-8');
+          if(get_magic_quotes_gpc()){
+            $f = stripcslashes($f);
+          }
+          $this->queries[$key]['filter'] = $f;
+          $this->queries[$key]['backend'] = get_post('backend_'.$key);
         }
-        $this->filter = $f;
+      }
+      
+      foreach($this->queries as $key => $query){
+        if(isset($_POST['removeQuery_'.$key])){
+          unset($this->queries[$key]);
+          $this->queries = array_values($this->queries);
+        }   
       }
 
       // Get posted flags 
@@ -160,6 +188,7 @@ class userFilterEditor extends plugin
         }elseif(isset($_POST['availableCategory']) && !empty($_POST['availableCategory'])){
           $this->selectedCategories[] = get_post('availableCategory');
         }
+        $this->selectedCategories = array_unique($this->selectedCategories);
       }
 
       // Remove categories
@@ -168,6 +197,27 @@ class userFilterEditor extends plugin
           if(isset($this->selectedCategories[$cat])) unset($this->selectedCategories[$cat]);
         }
       }
+
+      // Add new query 
+      if(isset($_POST['addQuery'])){
+
+        $filter= $this->listing->getFilter();
+        $backend = 'LDAP';
+        $query = "(objectClass=*)";
+        if(isset($filter->searches[$this->parent])){
+      
+          $tmp = $filter->searches[$this->parent];  
+          if(isset($tmp['query'][count($this->queries)])){
+            $query = $tmp['query'][count($this->queries)]['filter'];
+            $backend = $tmp['query'][count($this->queries)]['backend'];
+          }elseif(isset($tmp['query']['filter'])){
+            $query = $tmp['query']['filter'];
+            $backend = $tmp['query']['backend'];
+          }
+        }
+
+        $this->queries[] = array('backend'=> $backend, 'filter' => userFilterEditor::_autoIndentFilter($query,"  "));
+      }
     }
   }
 
@@ -194,11 +244,13 @@ class userFilterEditor extends plugin
     }
 
     // Count the number of opening and closing brackets - exclude escaped ones.
-    $f = preg_replace('/\\\\[\(\)]/',"",$this->filter);
-    $o = substr_count($f, '(');
-    $c = substr_count($f, ')');
-    if($o != $c){
-      $msgs[] = sprintf(_("Please check your filter. You have '%s' opening and '%s' closing brackets!"), $o, $c);
+    foreach($this->queries as $key => $query){
+      $f = preg_replace('/\\\\[\(\)]/',"",$query['filter']);
+      $o = substr_count($f, '(');
+      $c = substr_count($f, ')');
+      if($o != $c){
+        $msgs[] = sprintf(_("Please check your filter #%s. You have '%s' opening and '%s' closing brackets!"), ($key+1),$o, $c);
+      }
     }
 
     return($msgs);
@@ -216,7 +268,7 @@ class userFilterEditor extends plugin
     $ret['name'] = $this->name;
     $ret['description'] = $this->description;
     $ret['categories'] = $this->selectedCategories;
-    $ret['filter'] = $this->filter;
+    $ret['queries'] = $this->queries;
     $ret['flags'] = array();
     if($this->share){
       $ret['flags'][] = "share";