Code

Added html entities
[gosa.git] / gosa-core / include / class_baseSelector.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2010 GONICUS GmbH
5  *
6  * ID: $$Id: class_listing.inc 15296 2010-01-26 08:27:39Z cajus $$
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 baseSelector {
25   private $base;
26   private $pid;
27   private $action;
28   private $height= 500;
29   private $submitButton= true;
30   protected $tree;
31   protected $pathMapping;
33   // It would be better to get a dn -> [name, description, type] array
34   // to avoid these tasks be done several times. Skipping for the moment.
35   #'dc' => 'plugins/departments/images/domain.png',
36   #'dc' => 'plugins/departments/images/dc.png',
37   #'l' => 'plugins/departments/images/locality.png',
38   #'c' => 'plugins/departments/images/country.png',
39   #'o' => 'plugins/departments/images/organization.png',
40   #'ou' => 'plugins/departments/images/folder.png',
42   function __construct($bases, $base= "")
43   {
44     // Initialize pid
45     $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
47     // Transfer data
48     $this->setBases($bases);
49     $this->setBase($base);
50   }
53   function setSubmitButton($flag)
54   {
55     $this->submitButton= $flag;
56   }
59   function setHeight($value)
60   {
61     $this->height= $value;
62   }
65   function setBase($base)
66   {
67     if (isset($this->pathMapping[$base])) {
68       $this->base= $base;
69       $this->update(true);
70     }
71   }
74   function setBases($bases)
75   {
76     global $config;
78     $this->pathMapping= array();
79     $selected= $this->base == $config->current['BASE']?"Selected":"";
80     $first= true;
81     $last_indent= 2;
83     foreach ($bases as $base => $dummy) {
85       // Build path style display
86       $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
87       $elements= array_reverse($elements, true);
89       $this->pathMapping[$base]= $base == $config->current['BASE']? '/' : ldap::fix(preg_replace('/(^|,)[a-z0-9]+=/i', '/', implode(',', $elements)));
90     }
92     // Save bases to session for autocompletion
93     session::global_set('pathMapping', $this->pathMapping);
94   }
97   function update($force= false)
98   {
99     global $config;
101     // Analyze for base changes if needed
102     $this->action= null;
103     $last_base= $this->base;
104     if(isset($_REQUEST['PID']) && $_REQUEST['PID'] == $this->pid) {
105       if (isset($_REQUEST['REBASE'])) {
106         $new_base= base64_decode($_REQUEST['REBASE']);
107         if (isset($this->pathMapping[$new_base])) {
108           $this->base= $new_base;
109           $this->action= 'rebase';
110         } else {
111           return false;
112         }
113       }
114     } elseif (isset($_POST['bs_input_'.$this->pid])) {
116       // Take over input field base
117       if ($this->submitButton && isset($_POST['submit_base_'.$this->pid.'_x']) || !$this->submitButton) {
119         // Check if base is available
120         foreach ($this->pathMapping as $key => $path) {
121           if (mb_strtolower($path) == mb_strtolower($_POST['bs_input_'.$this->pid])) {
122             $this->base= $key;
123             break;
124           }
125         }
126       }
128     }
130     /* Skip if there's no change */
131     if (($this->tree && $this->base == $last_base) && !$force) {
132       return true;
133     }
135     $link= "href='?plug=".$_GET['plug']."&amp;PID=".$this->pid."&amp;REBASE=".base64_encode($config->current['BASE'])."'";
136     $this->tree= "<input type='text' size='35' name='bs_input_".$this->pid."' id='bs_input_".$this->pid."' onkeydown=\"\$('bs_".$this->pid."').hide()\" onfocus=\"\$('bs_".$this->pid."').hide()\" onmouseover=\"Element.clonePosition(\$('bs_".$this->pid."'), 'bs_input_".$this->pid."', {setHeight: false, setWidth: false, offsetTop:(Element.getHeight('bs_input_".$this->pid."'))});\$('bs_".$this->pid."').show();\" onmouseout=\"rtimer= Element.hide.delay(0.25, 'bs_".$this->pid."')\" value=\"".htmlentities($this->pathMapping[$this->base])."\">";
139     // Autocompleter
140     $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
141                   "<script type='text/javascript'>".
142                   "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });";
143     if ($this->submitButton) {
144       $this->tree.= "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });";
145     }
146     $this->tree.= "</script>";
148     $selected= $this->base == $config->current['BASE']?"Selected":"";
149     $this->tree.= "<div class='treeList' style='display:none;max-height:".$this->height."px' id='bs_".$this->pid."' onmouseover=\"window.clearTimeout(rtimer);\" onmouseout=\"rtimer= Element.hide.delay(0.25, 'bs_".$this->pid."')\"><a class='treeList$selected' $link>/ ["._("Root")."]</a><ul class='treeList'>\n";
150     $first= true;
151     $last_indent= 2;
153     foreach ($this->pathMapping as $base => $dummy) {
155       // Skip root for tree
156       if ($base == $config->current['BASE']) {
157         continue;
158       }
160       // Build path style display
161       $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
163       $indent= count($elements);
164       if (!$first && ($indent == $last_indent)) {
165         $this->tree.= "</li>\n";
166       }
167       if ($indent > $last_indent) {
168         $this->tree.= "<ul>\n";
169       }
170       if ($indent < $last_indent) {
171         for ($i= 0; $i < ($last_indent-$indent); $i++) {
172           $this->tree.= "</li></ul>\n";
173         }
174         $this->tree.= "</li>\n";
175       }
176       $selected= $this->base == $base?" class='treeListSelected'":"";
177       $link= "href='?plug=".$_GET['plug']."&amp;PID=".$this->pid."&amp;REBASE=".base64_encode($base)."'";
178       $this->tree.= "<li><a$selected $link>".str_replace(' ', '&nbsp;', ldap::fix(preg_replace('/^[a-z0-9]+=([^,]+),.*$/i', '$1', $base)))."</a>";
180       $last_indent= $indent;
181       $first= false;
182     }
184     // Close tree
185     for ($i= 0; $i<$last_indent; $i++) {
186       $this->tree.= "</li></ul>\n";
187     }
188     $this->tree.= "</div>\n";
190     // Draw submitter if required
191     if ($this->submitButton) {
192       $this->tree.= "&nbsp;<input class='center' type='image' src='images/lists/submit.png' align='middle' title='"._("Submit")."' name='submit_base_".$this->pid."' id='submit_base_".$this->pid."' alt='"._("Submit")."'>";
193     }
195     return true;
196   }
198   function render()
199   {
200     return $this->tree;
201   }
204   function getBase()
205   {
206     return $this->base;
207   }
210   function getAction()
211   {
212     // Do not do anything if this is not our PID, or there's even no PID available...
213     if(!isset($_REQUEST['PID']) || $_REQUEST['PID'] != $this->pid) {
214       return;
215     }
217     if ($this->action) {
218       return array("targets" => array($this->base), "action" => $this->action);
219     }
221     return null;
222   }
226 ?>