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 session::global_set('department_info', $config->department_info);
100 }
103 function update($force= false)
104 {
105 global $config;
107 // Analyze for base changes if needed
108 $this->action= null;
109 $last_base= $this->base;
110 if(isset($_REQUEST['BPID']) && $_REQUEST['BPID'] == $this->pid) {
111 if (isset($_POST['bs_rebase_'.$this->pid]) && !empty($_POST['bs_rebase_'.$this->pid])) {
112 $new_base= base64_decode($_POST['bs_rebase_'.$this->pid]);
113 if (isset($this->pathMapping[$new_base])) {
114 $this->base= $new_base;
115 $this->action= 'rebase';
116 } else {
117 $this->lastState= false;
118 return false;
119 }
120 }else{
121 // Input field set?
122 if (isset($_POST['bs_input_'.$this->pid])) {
124 // Take over input field base
125 if ($this->submitButton && isset($_POST['submit_base_'.$this->pid]) || !$this->submitButton) {
127 // Check if base is available
128 $this->lastState= false;
129 foreach ($this->pathMapping as $key => $path) {
130 if (mb_strtolower($path) == mb_strtolower($_POST['bs_input_'.$this->pid])) {
131 $this->base= $key;
132 $this->lastState= true;
133 break;
134 }
135 }
136 }
137 }
138 }
140 }
142 /* Skip if there's no change */
143 if (($this->tree && $this->base == $last_base) && !$force) {
144 return true;
145 }
147 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($config->current['BASE'])."'; $('submit_tree_base_".$this->pid."').click();\"";
148 $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])."\">";
150 // Autocompleter
151 $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
152 "<script type='text/javascript'>".
153 "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });";
154 if ($this->submitButton) {
155 $this->tree.= "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });";
156 }
157 $this->tree.= "</script>";
159 $selected= $this->base == $config->current['BASE']?"Selected":"";
160 $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";
161 $first= true;
162 $last_indent= 2;
164 foreach ($this->pathMapping as $base => $dummy) {
166 // Skip root for tree
167 if ($base == $config->current['BASE']) {
168 continue;
169 }
171 // Build path style display
172 $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
174 $indent= count($elements);
175 if (!$first && ($indent == $last_indent)) {
176 $this->tree.= "</li>\n";
177 }
178 if ($indent > $last_indent) {
179 $this->tree.= "<ul>\n";
180 }
181 if ($indent < $last_indent) {
182 for ($i= 0; $i < ($last_indent-$indent); $i++) {
183 $this->tree.= "</li></ul>\n";
184 }
185 $this->tree.= "</li>\n";
186 }
187 $selected= $this->base == $base?" class='treeListSelected'":"";
188 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($base)."';$('submit_tree_base_".$this->pid."').click();\"";
189 $this->tree.= "<li>".
190 image($config->department_info[$base]['img'])." <a$selected $link>".
191 $this->gennonbreaks($config->department_info[$base]['name']).
192 ($config->department_info[$base]['description']==''?'':' <span class="informal">['.$this->gennonbreaks($config->department_info[$base]['description']).']</span>').
193 "</a>";
195 $last_indent= $indent;
196 $first= false;
197 }
199 // Close tree
200 for ($i= 1; $i<$last_indent; $i++) {
201 $this->tree.= "</li></ul>\n";
202 }
203 $this->tree.= "</div>\n";
205 // Draw submitter if required
206 if ($this->submitButton) {
207 $this->tree.= image('images/lists/submit.png', "submit_base_".$this->pid, _("Submit"));
208 }
209 $this->tree.= "<input type='submit' style='display:none' name='submit_tree_base_".$this->pid."' id='submit_tree_base_".$this->pid."'>";
210 $this->tree.= "<input type='hidden' name='bs_rebase_".$this->pid."' id='bs_rebase_".$this->pid."'>";
211 $this->tree.= "<input type='hidden' name='BPID' id='BPID' value='".$this->pid."'>";
213 $this->lastState= true;
214 return true;
215 }
218 function gennonbreaks($string)
219 {
220 return str_replace('-', '‑', str_replace(' ', ' ', $string));
221 }
224 function render()
225 {
226 return $this->tree;
227 }
230 function getBase()
231 {
232 return $this->base;
233 }
236 function getAction()
237 {
238 // Do not do anything if this is not our BPID, or there's even no BPID available...
239 if(!isset($_REQUEST['BPID']) || $_REQUEST['BPID'] != $this->pid) {
240 return;
241 }
243 if ($this->action) {
244 return array("targets" => array($this->base), "action" => $this->action);
245 }
247 return null;
248 }
250 }
252 ?>