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;
33 // It would be better to get a dn -> [name, description, type] array
34 // to avoid these tasks be done several times. Skipping for the moment.
35 #'dc' => 'plugins/departments/images/domain.png',
36 #'dc' => 'plugins/departments/images/dc.png',
37 #'l' => 'plugins/departments/images/locality.png',
38 #'c' => 'plugins/departments/images/country.png',
39 #'o' => 'plugins/departments/images/organization.png',
40 #'ou' => 'plugins/departments/images/folder.png',
42 function __construct($bases, $base= "")
43 {
44 // Initialize pid
45 $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
47 // Transfer data
48 $this->setBases($bases);
49 $this->setBase($base);
50 }
53 function setSubmitButton($flag)
54 {
55 $this->submitButton= $flag;
56 }
59 function setHeight($value)
60 {
61 $this->height= $value;
62 }
65 function setBase($base)
66 {
67 if (isset($this->pathMapping[$base])) {
68 $this->base= $base;
69 $this->update(true);
70 }
71 }
74 function checkBase($base)
75 {
76 return isset($this->pathMapping[$base]);
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[$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 }
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 return false;
118 }
119 }
120 } elseif (isset($_POST['bs_input_'.$this->pid])) {
122 // Take over input field base
123 if ($this->submitButton && isset($_POST['submit_base_'.$this->pid.'_x']) || !$this->submitButton) {
125 // Check if base is available
126 foreach ($this->pathMapping as $key => $path) {
127 if (mb_strtolower($path) == mb_strtolower($_POST['bs_input_'.$this->pid])) {
128 $this->base= $key;
129 break;
130 }
131 }
132 }
134 }
136 /* Skip if there's no change */
137 if (($this->tree && $this->base == $last_base) && !$force) {
138 return true;
139 }
141 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($config->current['BASE'])."'; $('submit_tree_base_".$this->pid."').click();\"";
142 $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])."\">";
144 // Autocompleter
145 $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
146 "<script type='text/javascript'>".
147 "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });";
148 if ($this->submitButton) {
149 $this->tree.= "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });";
150 }
151 $this->tree.= "</script>";
153 $selected= $this->base == $config->current['BASE']?"Selected":"";
154 $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";
155 $first= true;
156 $last_indent= 2;
158 foreach ($this->pathMapping as $base => $dummy) {
160 // Skip root for tree
161 if ($base == $config->current['BASE']) {
162 continue;
163 }
165 // Build path style display
166 $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
168 $indent= count($elements);
169 if (!$first && ($indent == $last_indent)) {
170 $this->tree.= "</li>\n";
171 }
172 if ($indent > $last_indent) {
173 $this->tree.= "<ul>\n";
174 }
175 if ($indent < $last_indent) {
176 for ($i= 0; $i < ($last_indent-$indent); $i++) {
177 $this->tree.= "</li></ul>\n";
178 }
179 $this->tree.= "</li>\n";
180 }
181 $selected= $this->base == $base?" class='treeListSelected'":"";
182 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($base)."';$('submit_tree_base_".$this->pid."').click();\"";
183 $this->tree.= "<li><a$selected $link>".str_replace(' ', ' ', ldap::fix(preg_replace('/^[a-z0-9]+=([^,]+),.*$/i', '$1', $base)))."</a>";
185 $last_indent= $indent;
186 $first= false;
187 }
189 // Close tree
190 for ($i= 0; $i<$last_indent; $i++) {
191 $this->tree.= "</li></ul>\n";
192 }
193 $this->tree.= "</div>\n";
195 // Draw submitter if required
196 if ($this->submitButton) {
197 $this->tree.= " <input class='center' type='image' src='images/lists/submit.png' align='middle' title='"._("Submit")."' name='submit_base_".$this->pid."' id='submit_base_".$this->pid."' alt='"._("Submit")."'>";
198 }
199 $this->tree.= "<input type='submit' style='display:none' name='submit_tree_base_".$this->pid."' id='submit_tree_base_".$this->pid."'>";
200 $this->tree.= "<input type='hidden' name='bs_rebase_".$this->pid."' id='bs_rebase_".$this->pid."'>";
201 $this->tree.= "<input type='hidden' name='BPID' id='BPID' value='".$this->pid."'>";
203 return true;
204 }
206 function render()
207 {
208 return $this->tree;
209 }
212 function getBase()
213 {
214 return $this->base;
215 }
218 function getAction()
219 {
220 // Do not do anything if this is not our BPID, or there's even no BPID available...
221 if(!isset($_REQUEST['BPID']) || $_REQUEST['BPID'] != $this->pid) {
222 return;
223 }
225 if ($this->action) {
226 return array("targets" => array($this->base), "action" => $this->action);
227 }
229 return null;
230 }
232 }
234 ?>