c50493162af27cc60504fe9c1d257aee30ca348b
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();\"";
151 $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."'))});
152 Element.show.delay(0.25,\$('bs_".$this->pid."'));\" onmouseout=\"rtimer= Element.hide.delay(0.25, 'bs_".$this->pid."')\" value=\"".preg_replace('/"/', '"',$this->pathMapping[$this->base])."\">";
154 // Autocompleter
155 $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
156 "<script type='text/javascript'>".
157 "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });";
158 if ($this->submitButton) {
159 $this->tree.= "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });";
160 }
161 $this->tree.= "</script>";
163 $selected= $this->base == $config->current['BASE']?"Selected":"";
164 $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";
165 $first= true;
166 $last_indent= 2;
168 foreach ($this->pathMapping as $base => $dummy) {
170 // Skip root for tree
171 if ($base == $config->current['BASE']) {
172 continue;
173 }
175 // Build path style display
176 $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
178 $indent= count($elements);
179 if (!$first && ($indent == $last_indent)) {
180 $this->tree.= "</li>\n";
181 }
182 if ($indent > $last_indent) {
183 $this->tree.= "<ul>\n";
184 }
185 if ($indent < $last_indent) {
186 for ($i= 0; $i < ($last_indent-$indent); $i++) {
187 $this->tree.= "</li></ul>\n";
188 }
189 $this->tree.= "</li>\n";
190 }
191 $selected= $this->base == $base?" class='treeListSelected'":"";
192 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($base)."';$('submit_tree_base_".$this->pid."').click();\"";
193 $this->tree.= "<li>".
194 image($config->department_info[$base]['img'])." <a$selected $link>".
195 $this->gennonbreaks($config->department_info[$base]['name']).
196 ($config->department_info[$base]['description']==''?'':' <span class="informal">['.$this->gennonbreaks($config->department_info[$base]['description']).']</span>').
197 "</a>";
199 $last_indent= $indent;
200 $first= false;
201 }
203 // Close tree
204 for ($i= 1; $i<$last_indent; $i++) {
205 $this->tree.= "</li></ul>\n";
206 }
207 $this->tree.= "</div>\n";
209 // Draw submitter if required
210 if ($this->submitButton) {
211 $this->tree.= image('images/lists/submit.png', "submit_base_".$this->pid, _("Submit"));
212 }
213 $this->tree.= "<input type='submit' style='display:none' name='submit_tree_base_".$this->pid."' id='submit_tree_base_".$this->pid."'>";
214 $this->tree.= "<input type='hidden' name='bs_rebase_".$this->pid."' id='bs_rebase_".$this->pid."'>";
215 $this->tree.= "<input type='hidden' name='BPID' id='BPID' value='".$this->pid."'>";
217 $this->lastState= true;
218 return true;
219 }
222 function gennonbreaks($string)
223 {
224 return str_replace('-', '‑', str_replace(' ', ' ', $string));
225 }
228 function render()
229 {
230 return $this->tree;
231 }
234 function getBase()
235 {
236 return $this->base;
237 }
240 function getAction()
241 {
242 // Do not do anything if this is not our BPID, or there's even no BPID available...
243 if(!isset($_REQUEST['BPID']) || $_REQUEST['BPID'] != $this->pid) {
244 return;
245 }
247 if ($this->action) {
248 return array("targets" => array($this->base), "action" => $this->action);
249 }
251 return null;
252 }
254 }
256 ?>