f2a852abe0d163750bc309078dfc8417943804b5
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;
32 protected $lastState;
35 function __construct($bases, $base= "")
36 {
37 // Initialize pid
38 $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
40 // Transfer data
41 $this->setBases($bases);
42 $this->setBase($base);
43 }
46 function setSubmitButton($flag)
47 {
48 $this->submitButton= $flag;
49 }
52 function setHeight($value)
53 {
54 $this->height= $value;
55 }
58 function setBase($base)
59 {
60 if (isset($this->pathMapping[$base])) {
61 $this->base= $base;
62 $this->update(true);
63 }
64 }
67 function checkBase($base)
68 {
69 return isset($this->pathMapping[$base]);
70 }
73 function checkLastBaseUpdate()
74 {
75 return $this->lastState;
76 }
79 function setBases($bases)
80 {
81 global $config;
83 $this->pathMapping= array();
84 $selected= $this->base == $config->current['BASE']?"Selected":"";
85 $first= true;
86 $last_indent= 2;
88 foreach ($bases as $base => $dummy) {
90 // Build path style display
91 $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
92 $elements= array_reverse($elements, true);
94 $this->pathMapping[$base]= $base == $config->current['BASE']? '/' : ldap::fix(preg_replace('/(^|,)[a-z0-9]+=/i', '/', implode(',', $elements)));
95 }
97 // Save bases to session for autocompletion
98 session::global_set('pathMapping', $this->pathMapping);
99 }
102 function update($force= false)
103 {
104 global $config;
106 // Analyze for base changes if needed
107 $this->action= null;
108 $last_base= $this->base;
109 if(isset($_REQUEST['BPID']) && $_REQUEST['BPID'] == $this->pid) {
110 if (isset($_POST['bs_rebase_'.$this->pid]) && !empty($_POST['bs_rebase_'.$this->pid])) {
111 $new_base= base64_decode($_POST['bs_rebase_'.$this->pid]);
112 if (isset($this->pathMapping[$new_base])) {
113 $this->base= $new_base;
114 $this->action= 'rebase';
115 } else {
116 $this->lastState= false;
117 return false;
118 }
119 }else{
120 // Input field set?
121 if (isset($_POST['bs_input_'.$this->pid])) {
123 // Take over input field base
124 if ($this->submitButton && isset($_POST['submit_base_'.$this->pid.'_x']) || !$this->submitButton) {
126 // Check if base is available
127 $this->lastState= false;
128 foreach ($this->pathMapping as $key => $path) {
129 if (mb_strtolower($path) == mb_strtolower($_POST['bs_input_'.$this->pid])) {
130 $this->base= $key;
131 $this->lastState= true;
132 break;
133 }
134 }
135 }
136 }
137 }
139 }
141 /* Skip if there's no change */
142 if (($this->tree && $this->base == $last_base) && !$force) {
143 return true;
144 }
146 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($config->current['BASE'])."'; $('submit_tree_base_".$this->pid."').click();\"";
147 $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=\"".preg_replace('/"/', '"',$this->pathMapping[$this->base])."\">";
149 // Autocompleter
150 $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
151 "<script type='text/javascript'>".
152 "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });";
153 if ($this->submitButton) {
154 $this->tree.= "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });";
155 }
156 $this->tree.= "</script>";
158 $selected= $this->base == $config->current['BASE']?"Selected":"";
159 $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";
160 $first= true;
161 $last_indent= 2;
163 foreach ($this->pathMapping as $base => $dummy) {
165 // Skip root for tree
166 if ($base == $config->current['BASE']) {
167 continue;
168 }
170 // Build path style display
171 $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
173 $indent= count($elements);
174 if (!$first && ($indent == $last_indent)) {
175 $this->tree.= "</li>\n";
176 }
177 if ($indent > $last_indent) {
178 $this->tree.= "<ul>\n";
179 }
180 if ($indent < $last_indent) {
181 for ($i= 0; $i < ($last_indent-$indent); $i++) {
182 $this->tree.= "</li></ul>\n";
183 }
184 $this->tree.= "</li>\n";
185 }
186 $selected= $this->base == $base?" class='treeListSelected'":"";
187 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($base)."';$('submit_tree_base_".$this->pid."').click();\"";
188 $this->tree.= "<li>".
189 image($config->department_info[$base]['img'])." <a$selected $link>".
190 $this->gennonbreaks($config->department_info[$base]['name']).
191 ($config->department_info[$base]['description']==''?'':' <span class="informal">['.$this->gennonbreaks($config->department_info[$base]['description']).']</span>').
192 "</a>";
194 $last_indent= $indent;
195 $first= false;
196 }
198 // Close tree
199 for ($i= 0; $i<$last_indent; $i++) {
200 $this->tree.= "</li></ul>\n";
201 }
202 $this->tree.= "</div>\n";
204 // Draw submitter if required
205 if ($this->submitButton) {
206 $this->tree.= image('images/lists/submit.png', null, "submit_base_".$this->pid, _("Submit"));
207 }
208 $this->tree.= "<input type='submit' style='display:none' name='submit_tree_base_".$this->pid."' id='submit_tree_base_".$this->pid."'>";
209 $this->tree.= "<input type='hidden' name='bs_rebase_".$this->pid."' id='bs_rebase_".$this->pid."'>";
210 $this->tree.= "<input type='hidden' name='BPID' id='BPID' value='".$this->pid."'>";
212 $this->lastState= true;
213 return true;
214 }
217 function gennonbreaks($string)
218 {
219 return str_replace('-', '‑', str_replace(' ', ' ', $string));
220 }
223 function render()
224 {
225 return $this->tree;
226 }
229 function getBase()
230 {
231 return $this->base;
232 }
235 function getAction()
236 {
237 // Do not do anything if this is not our BPID, or there's even no BPID available...
238 if(!isset($_REQUEST['BPID']) || $_REQUEST['BPID'] != $this->pid) {
239 return;
240 }
242 if ($this->action) {
243 return array("targets" => array($this->base), "action" => $this->action);
244 }
246 return null;
247 }
249 }
251 ?>