Code

5816218782f1589c9fa1ffa8772b8b92e2d44a7c
[gosa.git] / gosa-core / include / class_filter.inc
1 <?php
3 class filter {
5   var $xmlData;
6   var $elements= array();
7   var $elementValues= array();
8   var $alphabetElements= array();
9   var $search;
10   var $definition;
11   var $category= "";
12   var $objectStorage= array();
13   var $objectBase= "";
14   var $base= "";
15   var $bases= array();
16   var $scope= "";
17   var $query;
18   var $baseMode= false;
19   var $scopeMode= "auto";
20   var $alphabet= null;
21   var $templatePath= "";
22   var $target= "";
24   function filter($filename)
25   {
26     if (!$this->load($filename)) {
27       die("Cannot parse $filename!");
28     }
29   }
31   function load($filename)
32   {
33     // Load data into array
34     $xmlData= simplexml_load_file($filename);
35     if ($xmlData === false) {
36       return false;
37     }
39     // Load definition
40     if (isset($xmlData->definition)) {
41       foreach ($xmlData->definition as $entry) {
42         if (!isset($entry->target) || !isset($entry->template)) {
43           return false;
44         }
46         // Move information
47         $this->templatePath= (string)$entry->template;
48         $this->target= (string)$entry->target;
50         // One is enough
51         break;
52       }
53     }
55     // Load filter
56     if (isset($xmlData->search)) {
57       foreach ($xmlData->search as $entry) {
58         if (!isset($entry->query) || !isset($entry->base) || !isset($entry->scope)) {
59           return false;
60         }
62         // Move information
63         $this->baseMode= (string)$entry->base;
64         $this->scopeMode= (string)$entry->scope;
65         $this->query= $entry->query;
67         // One is enough
68         break;
69       }
70     }
72     // Generate formular data
73     if (isset($xmlData->element)) {
74       foreach ($xmlData->element as $element) {
76         // Ignore elements without type
77         if (!isset($element->type)) {
78           next;
79         }
81         $tag= (string)$element->tag;
83         // Store element for quick access
84         $this->elements[$tag] = $element;
86         // Preset elementValues with default values if exist
87         if (isset($element->default)) {
88           $this->elementValues[$tag] = (string)$element->default;
89         } else {
90           $this->elementValues[$tag] = "";
91         }
93         // Does this element react on alphabet links?
94         if (isset($element->alphabet) && (string)$element->alphabet == "true") {
95           $this->alphabetElements[]= $tag;
96         }
97       }
98     }
100     // Save definition
101     if (isset($xmlData->definition)) {
102      $this->definition = $xmlData->definition;
103     }
105     // Save search
106     if (isset($xmlData->search)) {
107      $this->search = $xmlData->search;
108     }
110     return true;  
111   }
114   function getTextfield($element)
115   {
116     $result= "<input class='filter_textfield' name='$element' type='text' size='30' maxlength='30' value='".$this->elementValues[$element]."'>";
117     return $result;
118   }
121   function getCheckbox($element)
122   {
123     $checked= "";
124     if ($this->elementValues[$element] == "true") {
125       $checked= " checked";
126     }
128     $result= "<input class='filter_checkbox' name='$element' type='checkbox' onClick='document.mainform.submit();' value='true'$checked>";
129     return $result;
130   }
133   function getCombobox($element)
134   {
135     $result= "<select name='".$element->tag."' size='1' onClick='document.mainform.submit();'>";
136     foreach ($element->value as $key=>$value) {
137       $selected= "";
138       if ($this->elementValues[(string)$element->tag] == $value->key) {
139         $selected= " selected";
140       }
141       $result.= "<option value='".$value->key."'$selected>{t}".$value->set."{/t}</option>";
142     }
143     $result.= "</select>";
145     return $result;
146   }
149   function getCurrentBase()
150   {
151     if (isset($this->search->base) && (string)$this->search->scope != "auto") {
152       return false;
153     }
155     return $this->base;
156   }
159   function getCurrentScope()
160   {
161     if (isset($this->search->scope) && (string)$this->search->scope != "auto") {
162       return (string)$this->search->scope;
163     }
165     return $this->scope;
166   }
169   function setBases($bases) {
170     $this->bases= $bases;    
171   }
174   function setObjectStorage($storage) {
175     $this->objectStorage= $storage;    
176   }
179   function setObjectBase($base) {
180     $this->objectBase= $base;    
181   }
184   function setCategory($category) {
185     $this->category= $category;    
186   }
189   function setCurrentBase($base) {
190     $this->base= $base;
191   }
194   function setCurrentScope($scope) {
195     $this->scope= $scope;
196   }
199   function renderBase()
200   {
201     $result= "<select name='currentMainBase' onChange='mainform.submit()' size='1'>";
203     foreach ($this->bases as $key=>$value) {
204       $selected= "";
205       if ($key == $this->base) {
206         $selected= " selected";
207       }
208       $result.= "<option value='".$key."'$selected>".$value."</option>";
209     }
210     $result.= "</select>";
212     return $result;
213   }
216   function renderAlphabet($columns= 10)
217   {
218     // Return pre-rendered alphabet if available
219     if ($this->alphabet) {
220       return ($this->alphabet);
221     }
223     $characters= _("*ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
224     $alphabet= "";
225     $c= 0;
227     /* Fill cells with charaters */
228     for ($i= 0, $l= mb_strlen($characters, 'UTF8'); $i<$l; $i++){
229       if ($c == 0){
230         $alphabet.= "<tr>";
231       }
233       $ch = mb_substr($characters, $i, 1, "UTF8");
234       $alphabet.= "<td><a class=\"alphaselect\" href=\"main.php?plug=".
235         validate($_GET['plug'])."&amp;filter=".$ch."\">&nbsp;".$ch."&nbsp;</a></td>";
237       if ($c++ == $columns){
238         $alphabet.= "</tr>";
239         $c= 0;
240       }
241     }
243     /* Fill remaining cells */
244     while ($c++ <= $columns){
245       $alphabet.= "<td>&nbsp;</td>";
246     }
248     /* Save alphabet */
249     $this->alphabet= "<table width='100%'>$alphabet</table>";
251     return ($this->alphabet);
252   }
255   function renderApply()
256   {
257     return ("<input type='submit' name='apply' value='"._("Apply filter")."'>");
258   }
261   function renderScope()
262   {
263     $checked= $this->scope == "sub"?" checked":"";
264     return "<input type='checkbox' name='SCOPE' value='1' onClick='document.mainform.submit();'$checked>&nbsp;"._("Search in subtrees");
265   }
268   function renderFilter()
269   {
270     $smarty= get_smarty();
271     $smarty->assign("ALPHABET", $this->renderAlphabet());
272     $smarty->assign("APPLY", $this->renderApply());
273     $smarty->assign("SCOPE", $this->renderScope());
274     $smarty->assign("BASE", $this->renderBase());
276     // Load template and replace elementsHtml[]
277     foreach ($this->elements as $tag => $element) {
278       $htmlCode= "";
279       switch ($element->type) {
280         case "textfield":
281           $htmlCode = $this->getTextfield($tag);
282           break;
284         case "checkbox":
285           $htmlCode = $this->getCheckbox($tag);
286           break;
288         case "combobox":
289           $htmlCode = $this->getCombobox($element);
290           break;
292         default:
293           die ("Unknown element type specified!");
294       }
295       $smarty->assign("$tag", $htmlCode);
296     }
298     // Load template
299     return ("<input type='hidden' name='FILTER_LOADED' value='1'>".$smarty->fetch(get_template_path("filter/$this->templatePath")));
300   }
303   function query()
304   {
305     global $class_mapping;
306     $result= array();
308     // Go thru all queries and merge results
309     foreach ($this->query as $query) {
310       if (!isset($query->backend) || !isset($query->filter) || !isset($query->attribute)) {
311         die("No backend specified in search config.");
312       }
314       // Is backend available?
315       $backend= "filter".(string)$query->backend;
316       if (!isset($class_mapping["$backend"])) {
317         die("Invalid backend specified in search config.");
318       }
320       // Load filter and attributes
321       $filter= $query->filter;
322       $attributes= array();
323       foreach($query->attribute as $attr) {
324         $attributes[]= (string)$attr;
325       }
327       // Generate final filter
328       foreach ($this->elements as $tag => $element) {
329         $e_set= (string)$element->set;
330         $e_unset= (string)$element->unset;
332         if ($this->elementValues[$tag] == "") {
333           $e_unset= preg_replace('/\$/', normalizeLdap($this->elementValues[$tag]), $e_unset);
334           $filter= preg_replace("/\\$$tag/", $e_unset, $filter);
335         } else {
336           $e_set= preg_replace('/\$/', normalizeLdap($this->elementValues[$tag]), $e_set);
337           $filter= preg_replace("/\\$$tag/", $e_set, $filter);
338         }
339       }
341       $result= array_merge($result, call_user_func(array($backend, 'query'), $this->base, $this->scope, $filter, $attributes,
342                                $this->category, $this->objectStorage, $this->objectBase));
343     }
344     
346     return ($result);
347   }
350   function isValid()
351   {
352     foreach ($this->elements as $tag => $element) {
353       if (isset($element->regex)){
354         if (!preg_match('/'.(string)$element->regex.'/', $this->elementValues[$tag])){
355           return false;
356         }
357       }
358     }
359     return true;
360   }
363   function update()
364   {
366     /* React on alphabet links if needed */
367     if (isset($_GET['filter'])){
368       $s= mb_substr(validate($_GET['filter']), 0, 1, "UTF8")."*";
369       if ($s == "**"){
370         $s= "*";
371       }
372       foreach ($this->alphabetElements as $tag) {
373         $this->elementValues[$tag]= $s;
374       }
375     }
377     if (isset($_POST['FILTER_LOADED'])) {
378       // Load post values and adapt filter, base and scope accordingly - but
379       // only if we didn't get a _GET
380       foreach ($this->elements as $tag => $element) {
381         if (isset($_POST[$tag])){
382           $this->elementValues[$tag]= validate($_POST[$tag]);
383         } else {
384           $this->elementValues[$tag]= "";
385         }
386       }
387     }
389     // Save base
390     if (isset($_POST['BASE']) && $this->baseMode == "true") {
391       $base= validate($_POST['BASE']);
392       if (isset($this->bases[$base])) {
393         $this->base= $base;
394       }
395     }
397     // Save scope if needed
398     if ($this->scopeMode == "auto") {
399       $this->scope= isset($_POST['SCOPE'])?"sub":"one";
400     }
401   }
405 ?>