Code

5cfc7c073d44f9eb3a823a433131d5bce4ee16a8
[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 = "";
24     public $backends = array('LDAP','LDAPBlacklist', 'SYSTEMS', 'FAI', 'GroupLDAP','ACL', 'OPSIPackages','APPLICATIONS','MIMETYPES','CONFIGPROPERTIES');  
27     /*! \brief    Instantiate the filter editing dialog. 
28      *            Parses the filter info into editable data.
29      */
30     function __construct($entry, $listing)
31     {
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'];
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;
50         // Create statistic table entry
51         $this->initTime = microtime(TRUE);
52         stats::log('plugin', $class = get_class($this), $category = array($this->acl_category),  $action = 'open',
53                 $amount = 1, $duration = (microtime(TRUE) - $this->initTime));
54     }
57     /*! \brief    Automatic indent indentation for filters.
58      */
59     static function _autoIndentFilter($str, $indent = " ")
60     {
61         // Remove line breaks and escaped brackets 
62         $str = preg_replace('/[\t ]*\n[\t ]*/', "", $str);
63         $str = preg_replace('/\\\\\\(/', "::OPEN::", $str);
64                 $str = preg_replace('/\\\\\\)/', "::CLOSE::", $str);
66         // Add a line break infront of every bracket 
67         $str = preg_replace('/\\(/', "\n(", $str);
68                 $str = preg_replace('/\\)/', ")\n", $str);
70         // Split by linebreaks
71         $lines = preg_split("/\n/", $str);
72         $str = "";
73         $i = 0;
75         // Walk trough search blocks 
76         foreach($lines as $line){
77             $line = trim($line);
78             if(empty($line)) continue;
80             // Go back one level in indentation  
81             if(!preg_match("/\\(.*\\)/", $line) && preg_match('/\\)$/', $line)){
82                 $i --;
83             }
85             $str.= "\n";
86             $str = str_pad($str,strlen($str)+$i, $indent); 
87             $str.= $line;
89             // Go one level deeper in indentation 
90             if(!preg_match("/\\(.*\\)/", $line) && preg_match('/^\\(/', $line)){
91                     $i ++;
92                     }
93                     }
94                     $str = preg_replace('/::OPEN::/', '\(', $str);
95                     $str = preg_replace('/::CLOSE::/', '\)', $str);
96                     return($str);
97                     }
100                     /*! \brief    Retunrs the filters original name 
101                      *  @param    The original name of the filter (if none was given 
102                      *             an empty string is returned)
103                      */
104                     function getOriginalName()
105                     {
106                     return($this->orig_name);
107                     }
110                     /*! \brief    Retunrs the filters name.
111                      *  @param    The name of the filter
112                      */
113                     function getCurrentName()
114                     {
115                         return($this->name);
116                     }
119                     /*! \brief    Generates the <HTML> content, to edit the filter settings.
120                      *  @return   String  HTML form.
121                      */
122                     function execute()
123                     {
124                         plugin::execute();
126                         $smarty = get_smarty();
128                         // Build up HTML compliant html output
129                         $queries = array();
130                         foreach($this->queries as $key => $query){
131                             $query['filter'] =  htmlentities($query['filter'],ENT_COMPAT,'UTF-8');
132                             $queries[$key] = $query; 
133                         }  
135                         // Build up list of hard coded filters 
136                         $filter= $this->listing->getFilter();
138                         $smarty->assign("fixedFilters", array_keys($filter->searches));
139                         $smarty->assign('parent', $this->parent);
140                         $smarty->assign('backends', $this->backends);
141                         $smarty->assign('name', htmlentities($this->name,ENT_COMPAT,'UTF-8'));
142                         $smarty->assign('queries', $queries);
143                         $smarty->assign('share', $this->share);
144                         $smarty->assign('enable', $this->enabled);
145                         $smarty->assign('description', htmlentities($this->description,ENT_COMPAT,'UTF-8'));
146                         $smarty->assign('selectedCategories', $this->selectedCategories);
147                         $smarty->assign('availableCategories', array_unique($this->listing->categories));
148                         return($smarty->fetch(get_template_path('userFilterEditor.tpl', FALSE)));
149                     }
152                     /*! \brief    Keep values entered in the input form of the dialog. (POST/GET)
153                      */
154                     function save_object()
155                     {
156                         if(isset($_POST['userFilterEditor'])){
158                             // Get posted strings
159                             foreach(array('name','description', 'parent') as $attr){
160                                 if(isset($_POST[$attr])){
161                                     $this->$attr = get_post($attr);
162                                 }
163                             }
165                             // Filter needs special handling, it may contain charactes like < and >
166                             //  wich are stipped out by get_post() && validate()
167                             foreach($this->queries as $key => $query){
168                                 if(isset($_POST['filter_'.$key])){
169                                     $f = get_post('filter_'.$key);
170                                     $this->queries[$key]['filter'] = $f;
171                                     $this->queries[$key]['backend'] = get_post('backend_'.$key);
172                                 }
173                             }
175                             foreach($this->queries as $key => $query){
176                                 if(isset($_POST['removeQuery_'.$key])){
177                                     unset($this->queries[$key]);
178                                     $this->queries = array_values($this->queries);
179                                 }   
180                             }
182                             // Get posted flags 
183                             $this->share = isset($_POST['shareFilter']);
184                             $this->enable = isset($_POST['enableFilter']);
186                             // Get additional category  
187                             if(isset($_POST['addCategory'])){
188                                 if(isset($_POST['manualCategory']) && !empty($_POST['manualCategory'])){
189                                     $this->selectedCategories[] = get_post('manualCategory');
190                                 }elseif(isset($_POST['availableCategory']) && !empty($_POST['availableCategory'])){
191                                     $this->selectedCategories[] = get_post('availableCategory');
192                                 }
193                                 $this->selectedCategories = array_unique($this->selectedCategories);
194                             }
196                             // Remove categories
197                             if(isset($_POST['delCategory']) && isset($_POST['usedCategory'])){
198                                 foreach($_POST['usedCategory'] as $cat){
199                                     if(isset($this->selectedCategories[$cat])) unset($this->selectedCategories[$cat]);
200                                 }
201                             }
203                             // Add new query 
204                             if(isset($_POST['addQuery'])){
206                                 $filter= $this->listing->getFilter();
207                                 $backend = 'LDAP';
208                                 $query = "(objectClass=*)";
209                                 if(isset($filter->searches[$this->parent])){
211                                     $tmp = $filter->searches[$this->parent];  
212                                     if(isset($tmp['query'][count($this->queries)])){
213                                         $query = $tmp['query'][count($this->queries)]['filter'];
214                                         $backend = $tmp['query'][count($this->queries)]['backend'];
215                                     }elseif(isset($tmp['query']['filter'])){
216                                         $query = $tmp['query']['filter'];
217                                         $backend = $tmp['query']['backend'];
218                                     }
219                                 }
221                                 $this->queries[] = array('backend'=> $backend, 'filter' => userFilterEditor::_autoIndentFilter($query,"  "));
222                             }
223                         }
224                     }
227                     /*! \brief    Validate user input 
228                      *  @return   Array   An Array containing potential error messages
229                      */
230                     function check()
231                     {
232                         $msgs = plugin::check();
234                         // Check if the name is given
235                         if(empty($this->name)){
236                             $msgs[] = msgPool::required(_("Name"));
237                         }elseif(preg_match("/[^a-z0-9]/i", $this->name)){
239                             // Check for a valid name, no special chars here - in particular no ; 
240                             $msgs[] = msgPool::invalid(_("Name"), $this->name,"/[a-z0-9]/i");
241                         }  
243                         // Description is a must value.
244                         if(empty($this->description)){
245                             $msgs[] = msgPool::required(_("Description"));
246                         }
248                         // Count the number of opening and closing brackets - exclude escaped ones.
249                         foreach($this->queries as $key => $query){
250                             $f = preg_replace('/\\\\[\(\)]/',"",$query['filter']);
251                             $o = substr_count($f, '(');
252                             $c = substr_count($f, ')');
253                             if($o != $c){
254                                 $msgs[] = sprintf(_("Error in filter #%s: %s opening and %s closing brackets detected!"), bold($key+1), bold($o), bold($c));
255                             }
256                         }
258                         return($msgs);
259                     }
262                     /*! \brief    Transforms the entered values into a filter object (array) which is useable
263                      *             for the userFilter overview dialog.
264                      *  @return   Returns transformed filter data.
265                      */
266                     function save()
267                     {
268                         $ret= array();
269                         $ret['parent'] = $this->parent;
270                         $ret['tag'] = $this->name;
271                         $ret['description'] = $this->description;
272                         $ret['categories'] = $this->selectedCategories;
273                         $ret['query'] = $this->queries;
274                         $ret['flags'] = array();
275                         if($this->share){
276                             $ret['flags'][] = "share";
277                         }
278                         if($this->enable){
279                             $ret['flags'][] = "enable";
280                         }
281                         return($ret);
282                     }
285 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
286 ?>