Code

Made query backend selectable.
[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['name'];
38       $this->description = $entry['description'];
39       
40       foreach($entry['queries'] 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('name', 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('name','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       }
193       // Remove categories
194       if(isset($_POST['delCategory']) && isset($_POST['usedCategory'])){
195         foreach($_POST['usedCategory'] as $cat){
196           if(isset($this->selectedCategories[$cat])) unset($this->selectedCategories[$cat]);
197         }
198       }
200       // Add new query 
201       if(isset($_POST['addQuery'])){
203         $filter= $this->listing->getFilter();
204         $backend = 'LDAP';
205         $query = "(objectClass=*)";
206         if(isset($filter->searches[$this->parent])){
207       
208           $tmp = $filter->searches[$this->parent];  
209           if(isset($tmp['query'][count($this->queries)])){
210             $query = $tmp['query'][count($this->queries)]['filter'];
211             $backend = $tmp['query'][count($this->queries)]['backend'];
212           }elseif(isset($tmp['query']['filter'])){
213             $query = $tmp['query']['filter'];
214             $backend = $tmp['query']['backend'];
215           }
216         }
218         $this->queries[] = array('backend'=> $backend, 'filter' => userFilterEditor::_autoIndentFilter($query,"  "));
219       }
220     }
221   }
223   
224   /*! \brief    Validate user input 
225    *  @return   Array   An Array containing potential error messages
226    */
227   function check()
228   {
229     $msgs = plugin::check();
230   
231     // Check if the name is given
232     if(empty($this->name)){
233       $msgs[] = msgPool::required(_("Name"));
234     }elseif(preg_match("/[^a-z0-9]/i", $this->name)){
235       
236       // Check for a valid name, no special chars here - in particular no ; 
237       $msgs[] = msgPool::invalid(_("Name"), $this->name,"/[a-z0-9]/i");
238     }  
240     // Description is a must value.
241     if(empty($this->description)){
242       $msgs[] = msgPool::required(_("Description"));
243     }
245     // Count the number of opening and closing brackets - exclude escaped ones.
246     foreach($this->queries as $key => $query){
247       $f = preg_replace('/\\\\[\(\)]/',"",$query['filter']);
248       $o = substr_count($f, '(');
249       $c = substr_count($f, ')');
250       if($o != $c){
251         $msgs[] = sprintf(_("Please check your filter #%s. You have '%s' opening and '%s' closing brackets!"), ($key+1),$o, $c);
252       }
253     }
255     return($msgs);
256   }
259   /*! \brief    Transforms the entered values into a filter object (array) which is useable
260    *             for the userFilter overview dialog.
261    *  @return   Returns transformed filter data.
262    */
263   function save()
264   {
265     $ret= array();
266     $ret['parent'] = $this->parent;
267     $ret['name'] = $this->name;
268     $ret['description'] = $this->description;
269     $ret['categories'] = $this->selectedCategories;
270     $ret['queries'] = $this->queries;
271     $ret['flags'] = array();
272     if($this->share){
273       $ret['flags'][] = "share";
274     }
275     if($this->enable){
276       $ret['flags'][] = "enable";
277     }
278     return($ret);
279   }
282 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
283 ?>