Code

Updated filter handling, moved attributes up one level, added reload after edit.
[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   public $listing = NULL;
21   // The list of all categories mangaged by the current filter object.
22   // Used in the grop-down box.
23   public $orig_name = "";
25   public $backends = array('LDAP', 'SYSTEMS', 'FAI', 'GroupLDAP','ACL');  
28   /*! \brief    Instantiate the filter editing dialog. 
29    *            Parses the filter info into editable data.
30    */
31   function __construct($entry, $listing)
32   {
33     $this->listing = &$listing;
34     if($entry){
35       $this->entry = $entry;
36       $this->parent = $entry['parent'];
37       $this->name = $entry['tag'];
38       $this->description = $entry['description'];
39       
40       foreach($entry['query'] as $query){
41         $query['filter'] = userFilterEditor::_autoIndentFilter($query['filter'], "  ");
42         $this->queries[] = $query;
43       }
44       $this->selectedCategories = $entry['categories'];
45       $this->share = in_array("share",$entry['flags']);
46       $this->enable = in_array("enable",$entry['flags']);
47     }
48     $this->orig_name = $this->name;
49   }
52   /*! \brief    Automatic indent indentation for filters.
53    */
54   static function _autoIndentFilter($str, $indent = " ")
55   {
56     // Remove line breaks and escaped brackets 
57     $str = preg_replace('/[\t ]*\n[\t ]*/', "", $str);
58     $str = preg_replace('/\\\\\\(/', "::OPEN::", $str);
59     $str = preg_replace('/\\\\\\)/', "::CLOSE::", $str);
60    
61     // Add a line break infront of every bracket 
62     $str = preg_replace('/\\(/', "\n(", $str);
63     $str = preg_replace('/\\)/', ")\n", $str);
65     // Split by linebreaks
66     $lines = preg_split("/\n/", $str);
67     $str = "";
68     $i = 0;
69  
70     // Walk trough search blocks 
71     foreach($lines as $line){
72       $line = trim($line);
73       if(empty($line)) continue;
75       // Go back one level in indentation  
76       if(!preg_match("/\\(.*\\)/", $line) && preg_match('/\\)$/', $line)){
77         $i --;
78       }
80       $str.= "\n";
81       $str = str_pad($str,strlen($str)+$i, $indent); 
82       $str.= $line;
84       // Go one level deeper in indentation 
85       if(!preg_match("/\\(.*\\)/", $line) && preg_match('/^\\(/', $line)){
86         $i ++;
87       }
88     }
89     $str = preg_replace('/::OPEN::/', '\(', $str);
90     $str = preg_replace('/::CLOSE::/', '\)', $str);
91     return($str);
92   }
95   /*! \brief    Retunrs the filters original name 
96    *  @param    The original name of the filter (if none was given 
97    *             an empty string is returned)
98    */
99   function getOriginalName()
100   {
101     return($this->orig_name);
102   }
105   /*! \brief    Retunrs the filters name.
106    *  @param    The name of the filter
107    */
108   function getCurrentName()
109   {
110     return($this->name);
111   }
114   /*! \brief    Generates the <HTML> content, to edit the filter settings.
115    *  @return   String  HTML form.
116    */
117   function execute()
118   {
119     plugin::execute();
121     $smarty = get_smarty();
123     // Build up HTML compliant html output
124     $queries = array();
125     foreach($this->queries as $key => $query){
126       $query['filter'] =  htmlentities($query['filter'],ENT_COMPAT,'UTF-8');
127       $queries[$key] = $query; 
128     }  
130     // Build up list of hard coded filters 
131     $filter= $this->listing->getFilter();
133     $smarty->assign("fixedFilters", array_keys($filter->searches));
134     $smarty->assign('parent', $this->parent);
135     $smarty->assign('backends', $this->backends);
136     $smarty->assign('tag', htmlentities($this->name,ENT_COMPAT,'UTF-8'));
137     $smarty->assign('queries', $queries);
138     $smarty->assign('share', $this->share);
139     $smarty->assign('enable', $this->enabled);
140     $smarty->assign('description', htmlentities($this->description,ENT_COMPAT,'UTF-8'));
141     $smarty->assign('selectedCategories', $this->selectedCategories);
142     $smarty->assign('availableCategories', array_unique($this->listing->categories));
143     return($smarty->fetch(get_template_path('userFilterEditor.tpl', FALSE)));
144   }
147   /*! \brief    Keep values entered in the input form of the dialog. (POST/GET)
148    */
149   function save_object()
150   {
151     if(isset($_POST['userFilterEditor'])){
153       // Get posted strings
154       foreach(array('tag','description', 'parent') as $attr){
155         if(isset($_POST[$attr])){
156           $this->$attr = get_post($attr);
157         }
158       }
160       // Filter needs special handling, it may contain charactes like < and >
161       //  wich are stipped out by get_post() && validate()
162       foreach($this->queries as $key => $query){
163         if(isset($_POST['filter_'.$key])){
164           $f = mb_convert_encoding($_POST['filter_'.$key], 'UTF-8');
165           if(get_magic_quotes_gpc()){
166             $f = stripcslashes($f);
167           }
168           $this->queries[$key]['filter'] = $f;
169           $this->queries[$key]['backend'] = get_post('backend_'.$key);
170         }
171       }
172       
173       foreach($this->queries as $key => $query){
174         if(isset($_POST['removeQuery_'.$key])){
175           unset($this->queries[$key]);
176           $this->queries = array_values($this->queries);
177         }   
178       }
180       // Get posted flags 
181       $this->share = isset($_POST['shareFilter']);
182       $this->enable = isset($_POST['enableFilter']);
184       // Get additional category  
185       if(isset($_POST['addCategory'])){
186         if(isset($_POST['manualCategory']) && !empty($_POST['manualCategory'])){
187           $this->selectedCategories[] = get_post('manualCategory');
188         }elseif(isset($_POST['availableCategory']) && !empty($_POST['availableCategory'])){
189           $this->selectedCategories[] = get_post('availableCategory');
190         }
191         $this->selectedCategories = array_unique($this->selectedCategories);
192       }
194       // Remove categories
195       if(isset($_POST['delCategory']) && isset($_POST['usedCategory'])){
196         foreach($_POST['usedCategory'] as $cat){
197           if(isset($this->selectedCategories[$cat])) unset($this->selectedCategories[$cat]);
198         }
199       }
201       // Add new query 
202       if(isset($_POST['addQuery'])){
204         $filter= $this->listing->getFilter();
205         $backend = 'LDAP';
206         $query = "(objectClass=*)";
207         if(isset($filter->searches[$this->parent])){
208       
209           $tmp = $filter->searches[$this->parent];  
210           if(isset($tmp['query'][count($this->queries)])){
211             $query = $tmp['query'][count($this->queries)]['filter'];
212             $backend = $tmp['query'][count($this->queries)]['backend'];
213           }elseif(isset($tmp['query']['filter'])){
214             $query = $tmp['query']['filter'];
215             $backend = $tmp['query']['backend'];
216           }
217         }
219         $this->queries[] = array('backend'=> $backend, 'filter' => userFilterEditor::_autoIndentFilter($query,"  "));
220       }
221     }
222   }
224   
225   /*! \brief    Validate user input 
226    *  @return   Array   An Array containing potential error messages
227    */
228   function check()
229   {
230     $msgs = plugin::check();
231   
232     // Check if the name is given
233     if(empty($this->name)){
234       $msgs[] = msgPool::required(_("Name"));
235     }elseif(preg_match("/[^a-z0-9]/i", $this->name)){
236       
237       // Check for a valid name, no special chars here - in particular no ; 
238       $msgs[] = msgPool::invalid(_("Name"), $this->name,"/[a-z0-9]/i");
239     }  
241     // Description is a must value.
242     if(empty($this->description)){
243       $msgs[] = msgPool::required(_("Description"));
244     }
246     // Count the number of opening and closing brackets - exclude escaped ones.
247     foreach($this->queries as $key => $query){
248       $f = preg_replace('/\\\\[\(\)]/',"",$query['filter']);
249       $o = substr_count($f, '(');
250       $c = substr_count($f, ')');
251       if($o != $c){
252         $msgs[] = sprintf(_("Please check your filter #%s. You have '%s' opening and '%s' closing brackets!"), ($key+1),$o, $c);
253       }
254     }
256     return($msgs);
257   }
260   /*! \brief    Transforms the entered values into a filter object (array) which is useable
261    *             for the userFilter overview dialog.
262    *  @return   Returns transformed filter data.
263    */
264   function save()
265   {
266     $ret= array();
267     $ret['parent'] = $this->parent;
268     $ret['tag'] = $this->name;
269     $ret['description'] = $this->description;
270     $ret['categories'] = $this->selectedCategories;
271     $ret['query'] = $this->queries;
272     $ret['flags'] = array();
273     if($this->share){
274       $ret['flags'][] = "share";
275     }
276     if($this->enable){
277       $ret['flags'][] = "enable";
278     }
279     return($ret);
280   }
283 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
284 ?>