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 $base = LDAP::fix($base);
61 if (isset($this->pathMapping[$base])) {
62 $this->base= $base;
63 $this->update(true);
64 }
65 }
68 function checkBase($base)
69 {
70 return isset($this->pathMapping[$base]);
71 }
74 function checkLastBaseUpdate()
75 {
76 return $this->lastState;
77 }
80 function setBases($bases)
81 {
82 global $config;
84 $this->pathMapping= array();
85 $selected= $this->base == $config->current['BASE']?"Selected":"";
86 $first= true;
87 $last_indent= 2;
89 foreach ($bases as $base => $dummy) {
91 // Build path style display
92 $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
93 $elements= array_reverse($elements, true);
95 $this->pathMapping[LDAP::fix($base)]= $base == $config->current['BASE']? '/' : ldap::fix(preg_replace('/(^|,)[a-z0-9]+=/i', '/', implode(',', $elements)));
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]);
114 if (isset($this->pathMapping[$new_base])) {
115 $this->base= $new_base;
116 $this->action= 'rebase';
117 } else {
118 $this->lastState= false;
119 return false;
120 }
121 }else{
122 // Input field set?
123 if (isset($_POST['bs_input_'.$this->pid])) {
125 // Take over input field base
126 if ($this->submitButton && isset($_POST['submit_base_'.$this->pid]) || !$this->submitButton) {
128 // Check if base is available
129 $this->lastState= false;
130 foreach ($this->pathMapping as $key => $path) {
131 if (mb_strtolower($path) == mb_strtolower($_POST['bs_input_'.$this->pid])) {
132 $this->base= $key;
133 $this->lastState= true;
134 break;
135 }
136 }
137 }
138 }
139 }
141 }
143 /* Skip if there's no change */
144 if (($this->tree && $this->base == $last_base) && !$force) {
145 return true;
146 }
148 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($config->current['BASE'])."'; $('submit_tree_base_".$this->pid."').click();\"";
149 $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])."\">";
151 // Autocompleter
152 $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
153 "<script type='text/javascript'>".
154 "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });";
155 if ($this->submitButton) {
156 $this->tree.= "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });";
157 }
158 $this->tree.= "</script>";
160 $selected= $this->base == $config->current['BASE']?"Selected":"";
161 $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";
162 $first= true;
163 $last_indent= 2;
165 foreach ($this->pathMapping as $base => $dummy) {
167 // Skip root for tree
168 if ($base == $config->current['BASE']) {
169 continue;
170 }
172 // Build path style display
173 $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
175 $indent= count($elements);
176 if (!$first && ($indent == $last_indent)) {
177 $this->tree.= "</li>\n";
178 }
179 if ($indent > $last_indent) {
180 $this->tree.= "<ul>\n";
181 }
182 if ($indent < $last_indent) {
183 for ($i= 0; $i < ($last_indent-$indent); $i++) {
184 $this->tree.= "</li></ul>\n";
185 }
186 $this->tree.= "</li>\n";
187 }
188 $selected= $this->base == $base?" class='treeListSelected'":"";
189 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($base)."';$('submit_tree_base_".$this->pid."').click();\"";
190 $this->tree.= "<li>".
191 image($config->department_info[$base]['img'])." <a$selected $link>".
192 ($this->gennonbreaks($config->department_info[LDAP::fix($base)]['name'])).
193 ($config->department_info[LDAP::fix($base)]['description']==''?'':' <span class="informal">['.$this->gennonbreaks($config->department_info[LDAP::fix($base)]['description']).']</span>').
194 "</a>";
196 $last_indent= $indent;
197 $first= false;
198 }
200 // Close tree
201 for ($i= 1; $i<$last_indent; $i++) {
202 $this->tree.= "</li></ul>\n";
203 }
204 $this->tree.= "</div>\n";
206 // Draw submitter if required
207 if ($this->submitButton) {
208 $this->tree.= image('images/lists/submit.png', "submit_base_".$this->pid, _("Submit"));
209 }
210 $this->tree.= "<input type='submit' style='display:none' name='submit_tree_base_".$this->pid."' id='submit_tree_base_".$this->pid."'>";
211 $this->tree.= "<input type='hidden' name='bs_rebase_".$this->pid."' id='bs_rebase_".$this->pid."'>";
212 $this->tree.= "<input type='hidden' name='BPID' id='BPID' value='".$this->pid."'>";
214 $this->lastState= true;
215 return true;
216 }
219 function gennonbreaks($string)
220 {
221 return str_replace('-', '‑', str_replace(' ', ' ', $string));
222 }
225 function render()
226 {
227 return $this->tree;
228 }
231 function getBase()
232 {
233 return $this->base;
234 }
237 function getAction()
238 {
239 // Do not do anything if this is not our BPID, or there's even no BPID available...
240 if(!isset($_REQUEST['BPID']) || $_REQUEST['BPID'] != $this->pid) {
241 return;
242 }
244 if ($this->action) {
245 return array("targets" => array($this->base), "action" => $this->action);
246 }
248 return null;
249 }
251 }
253 ?>