1247ad781b62997165444711a8d4efc2d4368145
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 $elements= array();
27 var $elementValues= array();
28 var $alphabetElements= array();
29 var $autocompleter= array();
30 var $category= "";
31 var $objectStorage= array();
32 var $objectBase= "";
33 var $base= "";
34 var $scope= "";
35 var $query;
36 var $initial= false;
37 var $scopeMode= "auto";
38 var $alphabet= null;
41 function filter($filename)
42 {
43 global $config;
45 // Load eventually passed filename
46 if (!$this->load($filename)) {
47 die("Cannot parse $filename!");
48 }
49 }
52 function load($filename)
53 {
54 $contents = file_get_contents($filename);
55 $this->xmlData= xml::xml2array($contents, 1);
57 if (!isset($this->xmlData['filter'])) {
58 return false;
59 }
61 $this->xmlData= $this->xmlData["filter"];
63 // Load filter
64 if (isset($this->xmlData['search'])) {
65 if (!isset($this->xmlData['search']['query'][0])){
66 $this->xmlData['search']['query']= array($this->xmlData['search']['query']);
67 }
69 // Move information
70 $entry= $this->xmlData['search'];
71 $this->scopeMode= $entry['scope'];
72 if ($entry['scope'] == "auto") {
73 $this->scope= "one";
74 } else {
75 $this->scope= $entry['scope'];
76 }
77 $this->query= $entry['query'];
78 } else {
79 return false;
80 }
82 // Transfer initial value
83 if (isset($this->xmlData['definition']['initial']) && $this->xmlData['definition']['initial'] == "true"){
84 $this->initial= true;
85 }
87 // Transfer category
88 if (isset($this->xmlData['definition']['category'])){
89 $this->category= $this->xmlData['definition']['category'];
90 }
92 // Generate formular data
93 if (isset($this->xmlData['element'])) {
94 if (!isset($this->xmlData['element'][0])){
95 $this->xmlData['element']= array($this->xmlData['element']);
96 }
97 foreach ($this->xmlData['element'] as $element) {
99 // Ignore elements without type
100 if (!isset($element['type']) || !isset($element['tag'])) {
101 next;
102 }
104 $tag= $element['tag'];
106 // Fix arrays
107 if (isset($element['value']) && !isset($element['value'][0])) {
108 $element['value']= array($element['value']);
109 }
111 // Store element for quick access
112 $this->elements[$tag] = $element;
114 // Preset elementValues with default values if exist
115 if (isset($element['default']) && !is_array($element['default'])) {
116 $this->elementValues[$tag] = $element['default'];
117 } else {
118 $this->elementValues[$tag] = "";
119 }
121 // Does this element react on alphabet links?
122 if (isset($element['alphabet']) && $element['alphabet'] == "true") {
123 $this->alphabetElements[]= $tag;
124 }
125 }
126 }
128 return true;
129 }
132 function getTextfield($element)
133 {
134 $tag= $element['tag'];
135 $result= "<input class='filter_textfield' id='$tag' name='$tag' type='text' size='30' maxlength='30' value='".$this->elementValues[$tag]."'>";
136 if (isset($element['autocomplete'])) {
137 $frequency= "0.5";
138 $characters= "1";
139 if (isset($element['autocomplete']['frequency'])) {
140 $frequency= $element['autocomplete']['frequency'];
141 }
142 if (isset($element['autocomplete']['characters'])) {
143 $characters= $element['autocomplete']['characters'];
144 }
145 $result.= "<div id='autocomplete$tag' class='autocomplete'></div>".
146 "<script type='text/javascript'>".
147 "new Ajax.Autocompleter('$tag', 'autocomplete$tag', 'autocomplete.php', { minChars: $characters, frequency: $frequency });".
148 "</script>";
150 $this->autocompleters[$tag]= $element['autocomplete'];
151 }
152 return $result;
153 }
156 function getCheckbox($element)
157 {
158 $tag= $element['tag'];
159 $checked= "";
160 if ($this->elementValues[$tag] == "true") {
161 $checked= " checked";
162 }
164 $result= "<input class='filter_checkbox' name='$tag' type='checkbox' onClick='document.mainform.submit();' value='true'$checked>";
165 return $result;
166 }
169 function getCombobox($element)
170 {
171 $result= "<select name='".$element['tag']."' size='1' onClick='document.mainform.submit();'>";
173 // Fill with presets
174 foreach ($element['value'] as $value) {
175 $selected= "";
176 if ($this->elementValues[$element['tag']] == $value['key']) {
177 $selected= " selected";
178 }
180 // Handle translations
181 $result.= "<option value='".$value['key']."'$selected>"._($value['label'])."</option>";
182 }
184 // Use autocompleter for additional data
185 if (isset($element['autocomplete'])) {
186 $list= $this->getCompletitionList($element['autocomplete'], $element['tag']);
187 foreach ($list as $value) {
188 $selected= "";
189 if ($this->elementValues[$element['tag']] == $value) {
190 $selected= " selected";
191 }
192 $result.= "<option value='$value'$selected>$value</option>";
193 }
194 }
196 $result.= "</select>";
198 return $result;
199 }
202 function getCurrentBase()
203 {
204 if (isset($this->search->base) && (string)$this->search->scope != "auto") {
205 return false;
206 }
208 return $this->base;
209 }
212 function getCurrentScope()
213 {
214 if (isset($this->search->scope) && (string)$this->search->scope != "auto") {
215 return (string)$this->search->scope;
216 }
218 return $this->scope;
219 }
222 function setObjectStorage($storage) {
223 $this->objectStorage= $storage;
224 }
227 function setObjectBase($base) {
228 $this->objectBase= $base;
229 }
232 function setBase($base) {
233 $this->base= $base;
234 }
237 function setCurrentScope($scope) {
238 $this->scope= $scope;
239 }
242 function renderAlphabet($columns= 10)
243 {
244 // Return pre-rendered alphabet if available
245 if ($this->alphabet) {
246 return ($this->alphabet);
247 }
249 $characters= _("*ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
250 $alphabet= "";
251 $c= 0;
253 /* Fill cells with charaters */
254 for ($i= 0, $l= mb_strlen($characters, 'UTF8'); $i<$l; $i++){
255 if ($c == 0){
256 $alphabet.= "<tr>";
257 }
259 $ch = mb_substr($characters, $i, 1, "UTF8");
260 $alphabet.= "<td><a class=\"alphaselect\" href=\"main.php?plug=".
261 validate($_GET['plug'])."&filter=".$ch."\"> ".$ch." </a></td>";
263 if ($c++ == $columns){
264 $alphabet.= "</tr>";
265 $c= 0;
266 }
267 }
269 /* Fill remaining cells */
270 while ($c++ <= $columns){
271 $alphabet.= "<td> </td>";
272 }
274 /* Save alphabet */
275 $this->alphabet= "<table width='100%'>$alphabet</table>";
277 return ($this->alphabet);
278 }
281 function renderApply()
282 {
283 return ("<input type='submit' name='apply' value='"._("Apply filter")."'>");
284 }
287 function renderScope()
288 {
289 $checked= $this->scope == "sub"?" checked":"";
290 return "<input type='checkbox' name='SCOPE' value='1' onClick='document.mainform.submit();'$checked> "._("Search in subtrees");
291 }
294 function render()
295 {
296 $smarty= get_smarty();
297 $smarty->assign("ALPHABET", $this->renderAlphabet());
298 $smarty->assign("APPLY", $this->renderApply());
299 $smarty->assign("SCOPE", $this->renderScope());
301 // Load template and replace elementsHtml[]
302 foreach ($this->elements as $tag => $element) {
303 $htmlCode= "";
304 switch ($element['type']) {
305 case "textfield":
306 $htmlCode = $this->getTextfield($element);
307 break;
309 case "checkbox":
310 $htmlCode = $this->getCheckbox($element);
311 break;
313 case "combobox":
314 $htmlCode = $this->getCombobox($element);
315 break;
317 default:
318 die ("Unknown element type specified!");
319 }
320 $smarty->assign("$tag", $htmlCode);
321 }
323 // Load template
324 return ("<input type='hidden' name='FILTER_LOADED' value='1'>".$smarty->fetch(get_template_path($this->xmlData['definition']['template'], true)));
325 }
328 function query()
329 {
330 global $class_mapping;
331 $result= array();
333 // Go thru all queries and merge results
334 foreach ($this->query as $query) {
335 if (!isset($query['backend']) || !isset($query['filter']) || !isset($query['attribute'])) {
336 die("No backend specified in search config.");
337 }
339 // Is backend available?
340 $backend= "filter".$query['backend'];
341 if (!isset($class_mapping["$backend"])) {
342 die("Invalid backend specified in search config.");
343 }
345 // Load filter and attributes
346 $filter= $query['filter'];
347 $attributes= $query['attribute'];
349 // Generate final filter
350 foreach ($this->elements as $tag => $element) {
351 if (!isset($element['set']) || !isset($element['unset'])) {
352 continue;
353 }
354 $e_set= is_array($element['set'])?"":$element['set'];
355 $e_unset= is_array($element['unset'])?"":$element['unset'];
357 if ($this->elementValues[$tag] == "") {
358 $e_unset= preg_replace('/\$/', normalizeLdap($this->elementValues[$tag]), $e_unset);
359 $filter= preg_replace("/\\$$tag/", $e_unset, $filter);
360 } else {
361 $e_set= preg_replace('/\$/', normalizeLdap($this->elementValues[$tag]), $e_set);
362 $filter= preg_replace("/\\$$tag/", $e_set, $filter);
363 }
364 }
366 $result= array_merge($result, call_user_func(array($backend, 'query'), $this->base, $this->scope, $filter, $attributes,
367 $this->category, $this->objectStorage, $this->objectBase));
368 }
371 return ($result);
372 }
375 function isValid()
376 {
377 foreach ($this->elements as $tag => $element) {
378 if (isset($element->regex)){
379 if (!preg_match('/'.(string)$element->regex.'/', $this->elementValues[$tag])){
380 return false;
381 }
382 }
383 }
384 return true;
385 }
388 function update()
389 {
391 /* React on alphabet links if needed */
392 if (isset($_GET['filter'])){
393 $s= mb_substr(validate($_GET['filter']), 0, 1, "UTF8")."*";
394 if ($s == "**"){
395 $s= "*";
396 }
397 foreach ($this->alphabetElements as $tag) {
398 $this->elementValues[$tag]= $s;
399 }
400 }
402 if (isset($_POST['FILTER_LOADED'])) {
403 // Load post values and adapt filter, base and scope accordingly - but
404 // only if we didn't get a _GET
405 foreach ($this->elements as $tag => $element) {
406 if (isset($_POST[$tag])){
407 $this->elementValues[$tag]= validate($_POST[$tag]);
408 } else {
409 $this->elementValues[$tag]= "";
410 }
411 }
413 // Save scope if needed
414 if ($this->scopeMode == "auto") {
415 $this->scope= isset($_POST['SCOPE'])?"sub":"one";
416 }
417 }
419 }
422 function getCompletitionList($config, $tag, $value="*")
423 {
424 global $class_mapping;
425 $res= array();
427 // Is backend available?
428 $backend= "filter".$config['backend'];
429 if (!isset($class_mapping["$backend"])) {
430 die("Invalid backend specified in search config.");
431 }
433 // Load filter and attributes
434 $filter= $config['filter'];
435 $attributes= $config['attribute'];
436 if (!is_array($attributes)) {
437 $attributes= array($attributes);
438 }
440 // Make filter
441 $filter= preg_replace("/\\$$tag/", normalizeLDAP($value), $filter);
442 if (isset($config['base']) && isset($config['scope'])
443 && isset($config['category']) && isset($config['objectbase']) ) {
444 $result= call_user_func(array($backend, 'query'), $config['base'], $config['scope'], $filter, $attributes,
445 $config["category"], $config["objectbase"]);
446 } else {
447 $result= call_user_func(array($backend, 'query'), $this->base, $this->scope, $filter, $attributes,
448 $this->category, $this->objectStorage, $this->objectBase);
449 }
451 foreach ($result as $entry) {
452 foreach ($attributes as $attribute) {
453 if (is_array($entry[$attribute])) {
454 for ($i= 0; $i<$entry[$attribute]['count']; $i++) {
455 $res[]= $entry[$attribute][$i];
456 }
457 } else {
458 $res[]= $entry[$attribute];
459 }
460 }
461 }
463 return $res;
464 }
467 function processAutocomplete()
468 {
469 global $class_mapping;
470 $result= array();
472 foreach ($this->autocompleters as $tag => $config) {
473 if(isset($_POST[$tag])){
474 $result= $this->getCompletitionList($config, $tag, $_POST[$tag]);
476 echo '<ul>';
477 foreach ($result as $entry) {
478 echo '<li>'.$entry.'</li>';
479 }
481 echo '</ul>';
482 }
484 }
485 }
488 }
490 ?>