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 $this->pathMapping[$base]= stripslashes( $this->pathMapping[$base]);
96 }
98 // Save bases to session for autocompletion
99 session::global_set('pathMapping', $this->pathMapping);
100 session::global_set('department_info', $config->department_info);
101 }
104 function update($force= false)
105 {
106 global $config;
108 // Analyze for base changes if needed
109 $this->action= null;
110 $last_base= $this->base;
111 if(isset($_REQUEST['BPID']) && $_REQUEST['BPID'] == $this->pid) {
112 if (isset($_POST['bs_rebase_'.$this->pid]) && !empty($_POST['bs_rebase_'.$this->pid])) {
113 $new_base= base64_decode($_POST['bs_rebase_'.$this->pid]);
115 if (isset($this->pathMapping[$new_base])) {
116 $this->base= $new_base;
117 $this->action= 'rebase';
118 } else {
119 $this->lastState= false;
120 return false;
121 }
122 }else{
124 // Input field set?
125 if (isset($_POST['bs_input_'.$this->pid])) {
127 // Take over input field base
128 if ($this->submitButton && isset($_POST['submit_base_'.$this->pid]) || !$this->submitButton) {
130 // Check if base is available
131 $this->lastState= false;
132 foreach ($this->pathMapping as $key => $path) {
133 if (mb_strtolower($path) == mb_strtolower(get_post('bs_input_'.$this->pid))) {
134 $this->base= $key;
135 $this->lastState= true;
136 break;
137 }
138 }
139 }
140 }
141 }
143 }
145 /* Skip if there's no change */
146 if (($this->tree && $this->base == $last_base) && !$force) {
147 return true;
148 }
150 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($config->current['BASE'])."'; $('submit_tree_base_".$this->pid."').click();\"";
153 $this->tree= "<input style='width:160px' type='text' size='35'
154 name='bs_input_{$this->pid}' id='bs_input_{$this->pid}'
156 onkeydown=\" \$('bs_{$this->pid}').hide(); \"
157 onfocus=\" \$('bs_{$this->pid}').hide(); \"
158 onmouseover=\" mouseIsStillOver = true;
159 function showIt()
160 {
161 if(mouseIsStillOver){
162 \$('bs_".$this->pid."').show();
163 }
164 };
165 Element.clonePosition(\$('bs_".$this->pid."'),
166 'bs_input_".$this->pid."',
167 {setHeight: false, setWidth: false, offsetTop:(Element.getHeight('bs_input_".$this->pid."'))});
168 rtimer=showIt.delay(0.25); \"
170 onmouseout=\" mouseIsStillOver=false;
171 rtimer=Element.hide.delay(0.25,'bs_".$this->pid."')\"
173 value=\"".preg_replace('/"/','"',$this->pathMapping[$this->base])."\">";
176 // Autocompleter
177 $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
178 "<script type='text/javascript'>".
179 "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });";
180 if ($this->submitButton) {
181 $this->tree.= "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });";
182 }
183 $this->tree.= "</script>";
185 $selected= $this->base == $config->current['BASE']?"Selected":"";
186 $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";
187 $first= true;
188 $last_indent= 2;
190 foreach ($this->pathMapping as $base => $dummy) {
192 // Skip root for tree
193 if ($base == $config->current['BASE']) {
194 continue;
195 }
197 // Build path style display
198 $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
200 $indent= count($elements);
201 if (!$first && ($indent == $last_indent)) {
202 $this->tree.= "</li>\n";
203 }
204 if ($indent > $last_indent) {
205 $this->tree.= "<ul>\n";
206 }
207 if ($indent < $last_indent) {
208 for ($i= 0; $i < ($last_indent-$indent); $i++) {
209 $this->tree.= "</li></ul>\n";
210 }
211 $this->tree.= "</li>\n";
212 }
213 $selected= $this->base == $base?" class='treeListSelected'":"";
214 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($base)."';$('submit_tree_base_".$this->pid."').click();\"";
215 $this->tree.= "<li>".
216 image($config->department_info[$base]['img'])." <a$selected $link>".
217 $this->gennonbreaks($config->department_info[$base]['name']).
218 ($config->department_info[$base]['description']==''?'':' <span class="informal">['.$this->gennonbreaks($config->department_info[$base]['description']).']</span>').
219 "</a>";
221 $last_indent= $indent;
222 $first= false;
223 }
225 // Close tree
226 for ($i= 1; $i<$last_indent; $i++) {
227 $this->tree.= "</li></ul>\n";
228 }
229 $this->tree.= "</div>\n";
231 // Draw submitter if required
232 if ($this->submitButton) {
233 $this->tree.= image('images/lists/submit.png', "submit_base_".$this->pid, _("Submit"));
234 }
235 $this->tree.= "<input type='submit' style='display:none' name='submit_tree_base_".$this->pid."' id='submit_tree_base_".$this->pid."'>";
236 $this->tree.= "<input type='hidden' name='bs_rebase_".$this->pid."' id='bs_rebase_".$this->pid."'>";
237 $this->tree.= "<input type='hidden' name='BPID' id='BPID' value='".$this->pid."'>";
239 $this->lastState= true;
240 return true;
241 }
244 function gennonbreaks($string)
245 {
246 return str_replace('-', '‑', str_replace(' ', ' ', $string));
247 }
250 function render()
251 {
252 return $this->tree;
253 }
256 function getBase()
257 {
258 return $this->base;
259 }
262 function getAction()
263 {
264 // Do not do anything if this is not our BPID, or there's even no BPID available...
265 if(!isset($_REQUEST['BPID']) || $_REQUEST['BPID'] != $this->pid) {
266 return;
267 }
269 if ($this->action) {
270 return array("targets" => array($this->base), "action" => $this->action);
271 }
273 return null;
274 }
276 }
278 ?>