Code

Allow to set multiple queries for a user filter.
[gosa.git] / gosa-core / include / class_userFilterEditor.inc
1 <?php
3 class userFilterEditor extends plugin 
4 {
5   public $pathTitle= "Edit";
7   // The original entry else array(), allows us to perform existence checks.
8   public $entry = array();
10   // The values
11   public $name = "";
12   public $description = "";
13   public $parent = "";
14   public $selectedCategories = array();
15   public $share = FALSE;
16   public $enabled = TRUE;
17   public $queries = array();
19   // The list of all categories mangaged by the current filter object.
20   // Used in the grop-down box.
21   public $availableCategories = array();
22   public $fixedFilters = array();
23   public $orig_name = "";
26   /*! \brief    Instantiate the filter editing dialog. 
27    *            Parses the filter info into editable data.
28    */
29   function __construct($entry, $categories, $fixedFilters)
30   {
31     $this->availableCategories = $categories;
32     $this->fixedFilters = $fixedFilters;
33     if($entry){
34       $this->entry = $entry;
35       $this->parent = $entry['parent'];
36       $this->name = $entry['name'];
37       $this->description = $entry['description'];
38       
39       foreach($entry['queries'] as $query){
40         $query['filter'] = userFilterEditor::_autoIndentFilter($query['filter'], "  ");
41         $this->queries[] = $query;
42       }
43       $this->selectedCategories = $entry['categories'];
44       $this->share = in_array("share",$entry['flags']);
45       $this->enable = in_array("enable",$entry['flags']);
46     }
47     $this->orig_name = $this->name;
48   }
51   /*! \brief    Automatic indent indentation for filters.
52    */
53   static function _autoIndentFilter($str, $indent = " ")
54   {
55     // Remove line breaks and escaped brackets 
56     $str = preg_replace('/[\t ]*\n[\t ]*/', "", $str);
57     $str = preg_replace('/\\\\\\(/', "::OPEN::", $str);
58     $str = preg_replace('/\\\\\\)/', "::CLOSE::", $str);
59    
60     // Add a line break infront of every bracket 
61     $str = preg_replace('/\\(/', "\n(", $str);
62     $str = preg_replace('/\\)/', ")\n", $str);
64     // Split by linebreaks
65     $lines = preg_split("/\n/", $str);
66     $str = "";
67     $i = 0;
68  
69     // Walk trough search blocks 
70     foreach($lines as $line){
71       $line = trim($line);
72       if(empty($line)) continue;
74       // Go back one level in indentation  
75       if(!preg_match("/\\(.*\\)/", $line) && preg_match('/\\)$/', $line)){
76         $i --;
77       }
79       $str.= "\n";
80       $str = str_pad($str,strlen($str)+$i, $indent); 
81       $str.= $line;
83       // Go one level deeper in indentation 
84       if(!preg_match("/\\(.*\\)/", $line) && preg_match('/^\\(/', $line)){
85         $i ++;
86       }
87     }
88     $str = preg_replace('/::OPEN::/', '\(', $str);
89     $str = preg_replace('/::CLOSE::/', '\)', $str);
90     return($str);
91   }
94   /*! \brief    Retunrs the filters original name 
95    *  @param    The original name of the filter (if none was given 
96    *             an empty string is returned)
97    */
98   function getOriginalName()
99   {
100     return($this->orig_name);
101   }
104   /*! \brief    Retunrs the filters name.
105    *  @param    The name of the filter
106    */
107   function getCurrentName()
108   {
109     return($this->name);
110   }
113   /*! \brief    Generates the <HTML> content, to edit the filter settings.
114    *  @return   String  HTML form.
115    */
116   function execute()
117   {
118     plugin::execute();
120     // Build up HTML compliant html output
121     $queries = array();
122     foreach($this->queries as $key => $query){
123       $query['filter'] =  htmlentities($query['filter'],ENT_COMPAT,'UTF-8');
124       $queries[$key] = $query; 
125     }  
127     $smarty = get_smarty();
128     $smarty->assign('parent', $this->parent);
129     $smarty->assign('name', htmlentities($this->name,ENT_COMPAT,'UTF-8'));
130     $smarty->assign('queries', $queries);
131     $smarty->assign('share', $this->share);
132     $smarty->assign('enable', $this->enabled);
133     $smarty->assign('description', htmlentities($this->description,ENT_COMPAT,'UTF-8'));
134     $smarty->assign('selectedCategories', $this->selectedCategories);
135     $smarty->assign('availableCategories', $this->availableCategories);
136     $smarty->assign('fixedFilters', $this->fixedFilters);
137     return($smarty->fetch(get_template_path('userFilterEditor.tpl', FALSE)));
138   }
141   /*! \brief    Keep values entered in the input form of the dialog. (POST/GET)
142    */
143   function save_object()
144   {
145     if(isset($_POST['userFilterEditor'])){
147       // Get posted strings
148       foreach(array('name','description', 'parent') as $attr){
149         if(isset($_POST[$attr])){
150           $this->$attr = get_post($attr);
151         }
152       }
154       // Filter needs special handling, it may contain charactes like < and >
155       //  wich are stipped out by get_post() && validate()
156       foreach($this->queries as $key => $query){
157         if(isset($_POST['filter_'.$key])){
158           $f = mb_convert_encoding($_POST['filter_'.$key], 'UTF-8');
159           if(get_magic_quotes_gpc()){
160             $f = stripcslashes($f);
161           }
162           $this->queries[$key]['filter'] = $f;
163           $this->queries[$key]['backend'] = get_post('backend_'.$key);
164         }
165       }
166       
167       foreach($this->queries as $key => $query){
168         if(isset($_POST['removeQuery_'.$key])){
169           unset($this->queries[$key]);
170           $this->queries = array_values($this->queries);
171         }   
172       }
174       // Get posted flags 
175       $this->share = isset($_POST['shareFilter']);
176       $this->enable = isset($_POST['enableFilter']);
178       // Get additional category  
179       if(isset($_POST['addCategory'])){
180         if(isset($_POST['manualCategory']) && !empty($_POST['manualCategory'])){
181           $this->selectedCategories[] = get_post('manualCategory');
182         }elseif(isset($_POST['availableCategory']) && !empty($_POST['availableCategory'])){
183           $this->selectedCategories[] = get_post('availableCategory');
184         }
185       }
187       // Remove categories
188       if(isset($_POST['delCategory']) && isset($_POST['usedCategory'])){
189         foreach($_POST['usedCategory'] as $cat){
190           if(isset($this->selectedCategories[$cat])) unset($this->selectedCategories[$cat]);
191         }
192       }
194       // Add new query 
195       if(isset($_POST['addQuery'])){
196         $this->queries[] = array('backend'=>'filterLDAP', 'filter' => '(objectClass=*)');
197       }
198     }
199   }
201   
202   /*! \brief    Validate user input 
203    *  @return   Array   An Array containing potential error messages
204    */
205   function check()
206   {
207     $msgs = plugin::check();
208   
209     // Check if the name is given
210     if(empty($this->name)){
211       $msgs[] = msgPool::required(_("Name"));
212     }elseif(preg_match("/[^a-z0-9]/i", $this->name)){
213       
214       // Check for a valid name, no special chars here - in particular no ; 
215       $msgs[] = msgPool::invalid(_("Name"), $this->name,"/[a-z0-9]/i");
216     }  
218     // Description is a must value.
219     if(empty($this->description)){
220       $msgs[] = msgPool::required(_("Description"));
221     }
223     // Count the number of opening and closing brackets - exclude escaped ones.
224     foreach($this->queries as $key => $query){
225       $f = preg_replace('/\\\\[\(\)]/',"",$query['filter']);
226       $o = substr_count($f, '(');
227       $c = substr_count($f, ')');
228       if($o != $c){
229         $msgs[] = sprintf(_("Please check your filter #%s. You have '%s' opening and '%s' closing brackets!"), ($key+1),$o, $c);
230       }
231     }
233     return($msgs);
234   }
237   /*! \brief    Transforms the entered values into a filter object (array) which is useable
238    *             for the userFilter overview dialog.
239    *  @return   Returns transformed filter data.
240    */
241   function save()
242   {
243     $ret= array();
244     $ret['parent'] = $this->parent;
245     $ret['name'] = $this->name;
246     $ret['description'] = $this->description;
247     $ret['categories'] = $this->selectedCategories;
248     $ret['queries'] = $this->queries;
249     $ret['flags'] = array();
250     if($this->share){
251       $ret['flags'][] = "share";
252     }
253     if($this->enable){
254       $ret['flags'][] = "enable";
255     }
256     return($ret);
257   }
260 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
261 ?>