Code

Removed old stuff
[gosa.git] / gosa-core / include / class_filter.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class filter {
25   var $xmlData;
26   var $searches= array();
27   var $search;
28   var $category= "";
29   var $objectStorage= array();
30   var $base= "";
31   var $scope= "";
32   var $query;
33   var $value= "";
34   var $initial= false;
35   var $scopeMode= "auto";
36   var $converter= null;
37   var $pid;
40   function filter($filename)
41   {
42     global $config;
44     // Load eventually passed filename
45     if (!$this->load($filename)) {
46       die("Cannot parse $filename!");
47     }
49     $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE)); 
50   }
53   function load($filename)
54   {
55     $contents = file_get_contents($filename);
56     $this->xmlData= xml::xml2array($contents, 1);
58     if (!isset($this->xmlData['filterdef'])) {
59       return false;
60     }
62     $this->xmlData= $this->xmlData["filterdef"];
64     // Load filter
65     if (isset($this->xmlData['search'])) {
67       // Array conversion
68       if (!is_array($this->xmlData['search'])) {
69         $searches= array($this->xmlData['search']);
70       } else {
71         $searches= $this->xmlData['search'];
72       }
74       /* Store available searches */
75       foreach ($this->xmlData['search'] as $search) {
77         /* Do multi conversation */ 
78         if (!is_array($search['query'])){
79           $search['query']= array($search['query']);
80         }
82         /* Store search */
83         $this->searches[$search['tag']]= $search;
85       }
86     } else {
87       return false;
88     }
90     // Transfer scope
91     $this->scopeMode= $this->xmlData['definition']['scope'];
92     if ($this->scopeMode == "auto") {
93       $this->scope= "one";
94     } else {
95       $this->scope= $this->scopeMode;
96     }
98     // Transfer initial value
99     if (isset($this->xmlData['definition']['initial']) && $this->xmlData['definition']['initial'] == "true"){
100       $this->initial= true;
101     }
103     // Transfer category
104     if (isset($this->xmlData['definition']['category'])){
105       $this->category= $this->xmlData['definition']['category'];
106     }
108     // Set default search mode
109     $this->setSearch($this->xmlData['definition']['default']);
111     return true;  
112   }
115   function setSearch($method)
116   {
117     $patch= null;
118     if (is_array($this->category)) {
119       $categories= $this->category;
120     } else {
121       $categories= array($this->category);
122     }
123     $userfilters= userFilter::getFilter($categories);
125     // User filter selected?
126     if (isset($userfilters[$method])){
127       $usermethod= $method;
128       $patch= $userfilters[$method]['filter'];
129       $method= $userfilters[$method]['parent'];
130     }
132     // Move information
133     if (isset($this->searches[$method])) {
134       $this->query= array_merge($this->searches[$method]['query']);
135       if (!isset($this->query[0])) {
136         $this->query= array($this->query);
137       }
138  
139       // Patch first filter?
140       if ($patch) {
141         $this->query[0]['filter']= $patch;
142         $method= $usermethod;
143       }
145       $this->search= $method;
146     } else {
147       die ("Invalid search module!");
148     }
149   }
152   function getTextfield($tag, $value= "", $element= null)
153   {
154     $size= 30;
155     $maxlength= 30;
156     $result= "<input class='filter_textfield' id='$tag' name='$tag' type='text' size='$size' maxlength='{$maxlength}' value='".$value."'>";
157     if ($element && isset($element['autocomplete'])) {
158       $frequency= "0.5";
159       $characters= "1";
160       if (isset($element['autocomplete']['frequency'])) {
161         $frequency= $element['autocomplete']['frequency'];
162       }
163       if (isset($element['autocomplete']['characters'])) {
164         $characters= $element['autocomplete']['characters'];
165       }
166       $result.= "<div id='autocomplete$tag' class='autocomplete'></div>".
167         "<script type='text/javascript'>".
168         "new Ajax.Autocompleter('$tag', 'autocomplete$tag', 'autocomplete.php', { minChars: $characters, frequency: $frequency });".
169         "</script>";
171       $this->autocompleters[$tag]= $element;
172     }
173     return $result;
174   }
177   function getCurrentBase()
178   {
179     if (isset($this->search->base) && (string)$this->search->scope != "auto") {
180       return false;
181     }
183     return $this->base;
184   }
187   function getCurrentScope()
188   {
189     if (isset($this->search->scope) && (string)$this->search->scope != "auto") {
190       return (string)$this->search->scope;
191     }
193     return $this->scope;
194   }
197   function setConverter($hook)
198   {
199     $this->converter= $hook;
200   }
203   function setObjectStorage($storage)
204   {
205     $this->objectStorage= $storage;    
206   }
209   function setBase($base)
210   {
211     $this->base= $base;
212   }
215   function setCurrentScope($scope)
216   {
217     $this->scope= $scope;
218   }
222   function render()
223   {
224     $content= "<table class='filter-wrapper'><tr><td>".$this->renderFilterMenu()."</td><td>";
225     $content.= "<div class='search-filter'>".$this->getTextfield('search_filter', $this->value, $this->searches[$this->search])."</div>".
226       "&nbsp;<button class='search-filter' type='submit' title='"._("Search")."'>".image("images/find.png")."</button></td></tr></table>";
228     // Return meta data
229     return ("<input type='hidden' name='FILTER_PID' value='".$this->pid."'>".$content);
230   }
233   function query()
234   {
235     global $class_mapping;
236     $result= array();
238     // Return empty list if initial is not set
239     if (!$this->initial) {
240       $this->initial= true;
241       return $result;
242     }
244     // Go thru all queries and merge results
245     foreach ($this->query as $query) {
246       if (!isset($query['backend']) || !isset($query['filter']) || !isset($query['attribute'])) {
247         die("No backend specified in search config.");
248       }
250       // Is backend available?
251       $backend= "filter".$query['backend'];
252       if (!isset($class_mapping["$backend"])) {
253         die("Invalid backend specified in search config.");
254       }
256       // Load filter and attributes
257       $filter= $query['filter'];
258       $attributes= $query['attribute'];
260       // Handle converters if present
261       if ($this->converter) {
262         preg_match('/([^:]+)::(.*)$/', $this->converter, $m);
263         $filter= call_user_func(array($m[1], $m[2]), preg_replace('/\$/', $this->value, $filter));
264       }
266       // Do not replace escaped \$ - This is required to be able to search for e.g. windows machines.
267       if ($this->value == "") {
268         $filter= preg_replace("/\\$/", '*', $filter);
269       } else {
270         $filter= preg_replace("/\\$/", "*".normalizeLdap($this->value)."*", $filter);
271       }
273       $result= array_merge($result, call_user_func(array($backend, 'query'), $this->base, $this->scope, $filter, $attributes, $this->category, $this->objectStorage));
274     }
276     return ($result);
277   }
280   function update()
281   {
282     if (isset($_POST['FILTER_PID']) && $_POST['FILTER_PID'] == $this->pid) {
284       // Save input field
285       if (isset($_POST['search_filter'])) {
286         $this->value= validate($_POST['search_filter']);
287       }
289       // Save scope if needed
290       if ($this->scopeMode == "auto" && isset($_POST['act']) && $_POST['act'] == "toggle-subtree") {
291         $this->scope= ($this->scope == "one")?"sub":"one";
292       }
294       // Switch filter?
295       if (is_array($this->category)) {
296         $categories= $this->category;
297       } else {
298         $categories= array($this->category);
299       }
301       if (isset($_POST['act'])) {
302         foreach (array_merge($this->searches, userFilter::getFilter($categories)) as $tag => $cfg) {
303           if ($_POST['act'] == "filter-$tag") {
304             $this->setSearch($tag);
305             break;
306           }
307         }
308       }
309     }
311   }
314   function getCompletitionList($config, $value="*")
315   {
316     global $class_mapping;
317     $res= array();
319     // Load result attributes
320     $attributes= $config['autocomplete']['attribute'];
321     if (!is_array($attributes)) {
322       $attributes= array($attributes);
323     }
325     // Do the query
326     $result= array();
328     // Is backend available?
329     $queries= $config['query'];
330     if (!isset($queries[0])){
331       $queries= array($queries);
332     }
333     foreach ($queries as $query) {
334       $backend= "filter".$query['backend'];
335       if (!isset($class_mapping["$backend"])) {
336         die("Invalid backend specified in search config.");
337       }
338       $filter= preg_replace("/\\$/", "*".normalizeLdap($value)."*", $query['filter']);
340       $result= array_merge($result, call_user_func(array($backend, 'query'), $this->base, $this->scope, $filter, $attributes,
341           $this->category, $this->objectStorage));
342     }
344     foreach ($result as $entry) {
345       foreach ($attributes as $attribute) {
346         if (is_array($entry[$attribute])) {
347           for ($i= 0; $i<$entry[$attribute]['count']; $i++) {
348             if (mb_stristr($entry[$attribute][$i], $value)) {
349               $res[]= $entry[$attribute][$i];
350             }
351           }
352         } else {
353           $res[]= $entry[$attribute];
354         }
355       }
356     }
358     return $res;
359   }
362   function processAutocomplete()
363   {
364     global $class_mapping;
365     $result= array();
367     // Introduce maximum number of entries
368     $max= 25;
370     if(isset($this->searches[$this->search]['autocomplete'])){
371       $result= $this->getCompletitionList($this->searches[$this->search], $_POST['search_filter']);
372       $result= array_unique($result);
373       asort($result);
375       echo '<ul>';
376       foreach ($result as $entry) {
377         echo '<li>'.mark($_POST['search_filter'], $entry).'</li>';
378         if ($max-- == 0) {
379           break;
380         }
381       }
383       echo '</ul>';
384     }
385   }
388   function getObjectBase($dn)
389   {
390     global $config;
391     $base= "";
393     // Try every object storage
394     $storage= $this->objectStorage;
395     if (!is_array($storage)){
396       $storage= array($storage);
397     }
398     foreach ($storage as $location) {
399       $pattern= "/^[^,]+,".preg_quote($location, '/')."/i";
400       $base= preg_replace($pattern, '', $dn);
401     }
403     /* Set to base, if we're not on a correct subtree */
404     if (!isset($config->idepartments[$base])){
405       $base= $config->current['BASE'];
406     }
408     return $base;
409   }
413   function renderFilterMenu()
414   {
415     // Load shortcut
416     $result= "<input type='hidden' name='act' id='filter' value=''><div style='display:none'><input type='submit' name='exec_filter' id='exec_filter' value=''></div>".
417       "<ul class='level1' id='filter-root'><li><a href='#'>".image("images/filter.png").image("images/lists/sort-down.png")."</a>";
419     // Build ul/li list
420     $separator= " style='border-top:1px solid #AAA' ";
421     $result.= "<ul class='level2'>";
423     // Build in filters
424     foreach ($this->searches as $tag => $config) {
425       if ($tag == $this->search) {
426         $result.= "<li><a href='#'>".image("images/checked.png")."&nbsp;"._($config['label'])."</a></li>";
427       } else {
428         $result.= "<li><a href='#' onClick='document.getElementById(\"filter\").value= \"filter-$tag\";mainform.submit();'>".image("images/empty.png")."&nbsp;"._($config['label'])."</a></li>";
429       }
430     }
432     // User defined filters
433     $first= true;
434     if (is_array($this->category)) {
435       $categories= $this->category;
436     } else {
437       $categories= array($this->category);
438     }
439     foreach (userFilter::getFilter($categories) as $tag => $config) {
440       if ($tag == $this->search) {
441         $result.= "<li".($first?$separator:"")."><a href='#'>".image("images/checked.png")."&nbsp;"._($config['description'])."</a></li>";
442       } else {
443         $result.= "<li".($first?$separator:"")."><a href='#' onClick='document.getElementById(\"filter\").value= \"filter-$tag\";mainform.submit();'>".image("images/empty.png")."&nbsp;"._($config['description'])."</a></li>";
444       }
446       $first= false;
447     }
449     // Render scope if set to auto
450     if ($this->scopeMode == "auto") {
451       $result.= "<li$separator><a href='#' onClick='document.getElementById(\"filter\").value= \"toggle-subtree\";mainform.submit();'>".($this->scope=="one"?image("images/empty.png"):image("images/checked.png"))."&nbsp;"._("Search in subtrees")."</a></li>";
452     }
455     // Edit filter menu
456     $result.= "<li$separator><a href='#' onClick='document.getElementById(\"filter\").value= \"config-filter\";mainform.submit();'>".image("images/configure.png")."&nbsp;"._("Edit filters")."...</a></li>";
458     $result.= "</ul>";
460     $script= '<script type="text/javascript" language="JavaScript">var menu2; menu2= new Menu("filter-root", "menu2", configMenu)</script>';
462     return "<div id='filtermenu'>".$result."</li></ul><div>$script";
463   }
466   function getFixedFilters()
467   {
468     return array_keys($this->searches);
469   }
474 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
475 ?>