b05ff5b211608d0287e2eaba5ed5963efec68d9b
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;
34 // It would be better to get a dn -> [name, description, type] array
35 // to avoid these tasks be done several times. Skipping for the moment.
36 #'dc' => 'plugins/departments/images/domain.png',
37 #'dc' => 'plugins/departments/images/dc.png',
38 #'l' => 'plugins/departments/images/locality.png',
39 #'c' => 'plugins/departments/images/country.png',
40 #'o' => 'plugins/departments/images/organization.png',
41 #'ou' => 'plugins/departments/images/folder.png',
43 function __construct($bases, $base= "")
44 {
45 // Initialize pid
46 $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
48 // Transfer data
49 $this->setBases($bases);
50 $this->setBase($base);
51 }
54 function setSubmitButton($flag)
55 {
56 $this->submitButton= $flag;
57 }
60 function setHeight($value)
61 {
62 $this->height= $value;
63 }
66 function setBase($base)
67 {
68 if (isset($this->pathMapping[$base])) {
69 $this->base= $base;
70 $this->update(true);
71 }
72 }
75 function checkBase($base)
76 {
77 return isset($this->pathMapping[$base]);
78 }
81 function checkLastBaseUpdate()
82 {
83 return $this->lastState;
84 }
87 function setBases($bases)
88 {
89 global $config;
91 $this->pathMapping= array();
92 $selected= $this->base == $config->current['BASE']?"Selected":"";
93 $first= true;
94 $last_indent= 2;
96 foreach ($bases as $base => $dummy) {
98 // Build path style display
99 $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
100 $elements= array_reverse($elements, true);
102 $this->pathMapping[$base]= $base == $config->current['BASE']? '/' : ldap::fix(preg_replace('/(^|,)[a-z0-9]+=/i', '/', implode(',', $elements)));
103 }
105 // Save bases to session for autocompletion
106 session::global_set('pathMapping', $this->pathMapping);
107 }
110 function update($force= false)
111 {
112 global $config;
114 // Analyze for base changes if needed
115 $this->action= null;
116 $last_base= $this->base;
117 if(isset($_REQUEST['BPID']) && $_REQUEST['BPID'] == $this->pid) {
118 if (isset($_POST['bs_rebase_'.$this->pid]) && !empty($_POST['bs_rebase_'.$this->pid])) {
119 $new_base= base64_decode($_POST['bs_rebase_'.$this->pid]);
120 if (isset($this->pathMapping[$new_base])) {
121 $this->base= $new_base;
122 $this->action= 'rebase';
123 } else {
124 $this->lastState= false;
125 return false;
126 }
127 }
129 // Input field set?
130 if (isset($_POST['bs_input_'.$this->pid])) {
132 // Take over input field base
133 if ($this->submitButton && isset($_POST['submit_base_'.$this->pid.'_x']) || !$this->submitButton) {
135 // Check if base is available
136 $this->lastState= false;
137 foreach ($this->pathMapping as $key => $path) {
138 if (mb_strtolower($path) == mb_strtolower($_POST['bs_input_'.$this->pid])) {
139 $this->base= $key;
140 $this->lastState= true;
141 break;
142 }
143 }
144 }
145 }
147 }
149 /* Skip if there's no change */
150 if (($this->tree && $this->base == $last_base) && !$force) {
151 return true;
152 }
154 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($config->current['BASE'])."'; $('submit_tree_base_".$this->pid."').click();\"";
155 $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])."\">";
157 // Autocompleter
158 $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
159 "<script type='text/javascript'>".
160 "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });";
161 if ($this->submitButton) {
162 $this->tree.= "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });";
163 }
164 $this->tree.= "</script>";
166 $selected= $this->base == $config->current['BASE']?"Selected":"";
167 $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";
168 $first= true;
169 $last_indent= 2;
171 foreach ($this->pathMapping as $base => $dummy) {
173 // Skip root for tree
174 if ($base == $config->current['BASE']) {
175 continue;
176 }
178 // Build path style display
179 $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
181 $indent= count($elements);
182 if (!$first && ($indent == $last_indent)) {
183 $this->tree.= "</li>\n";
184 }
185 if ($indent > $last_indent) {
186 $this->tree.= "<ul>\n";
187 }
188 if ($indent < $last_indent) {
189 for ($i= 0; $i < ($last_indent-$indent); $i++) {
190 $this->tree.= "</li></ul>\n";
191 }
192 $this->tree.= "</li>\n";
193 }
194 $selected= $this->base == $base?" class='treeListSelected'":"";
195 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($base)."';$('submit_tree_base_".$this->pid."').click();\"";
196 $this->tree.= "<li><a$selected $link>".str_replace(' ', ' ', ldap::fix(preg_replace('/^[a-z0-9]+=([^,]+),.*$/i', '$1', $base)))."</a>";
198 $last_indent= $indent;
199 $first= false;
200 }
202 // Close tree
203 for ($i= 0; $i<$last_indent; $i++) {
204 $this->tree.= "</li></ul>\n";
205 }
206 $this->tree.= "</div>\n";
208 // Draw submitter if required
209 if ($this->submitButton) {
210 $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")."'>";
211 }
212 $this->tree.= "<input type='submit' style='display:none' name='submit_tree_base_".$this->pid."' id='submit_tree_base_".$this->pid."'>";
213 $this->tree.= "<input type='hidden' name='bs_rebase_".$this->pid."' id='bs_rebase_".$this->pid."'>";
214 $this->tree.= "<input type='hidden' name='BPID' id='BPID' value='".$this->pid."'>";
216 $this->lastState= true;
217 return true;
218 }
220 function render()
221 {
222 return $this->tree;
223 }
226 function getBase()
227 {
228 return $this->base;
229 }
232 function getAction()
233 {
234 // Do not do anything if this is not our BPID, or there's even no BPID available...
235 if(!isset($_REQUEST['BPID']) || $_REQUEST['BPID'] != $this->pid) {
236 return;
237 }
239 if ($this->action) {
240 return array("targets" => array($this->base), "action" => $this->action);
241 }
243 return null;
244 }
246 }
248 ?>