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 setBases($bases)
75 {
76 global $config;
78 $this->pathMapping= array();
79 $selected= $this->base == $config->current['BASE']?"Selected":"";
80 $first= true;
81 $last_indent= 2;
83 foreach ($bases as $base => $dummy) {
85 // Build path style display
86 $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
87 $elements= array_reverse($elements, true);
89 $this->pathMapping[$base]= $base == $config->current['BASE']? '/' : ldap::fix(preg_replace('/(^|,)[a-z0-9]+=/i', '/', implode(',', $elements)));
90 }
92 // Save bases to session for autocompletion
93 session::global_set('pathMapping', $this->pathMapping);
94 }
97 function update($force= false)
98 {
99 global $config;
101 // Analyze for base changes if needed
102 $this->action= null;
103 $last_base= $this->base;
104 if(isset($_REQUEST['PID']) && $_REQUEST['PID'] == $this->pid) {
105 if (isset($_REQUEST['REBASE'])) {
106 $new_base= base64_decode($_REQUEST['REBASE']);
107 if (isset($this->pathMapping[$new_base])) {
108 $this->base= $new_base;
109 $this->action= 'rebase';
110 } else {
111 return false;
112 }
113 }
114 } elseif (isset($_POST['bs_input_'.$this->pid])) {
116 // Take over input field base
117 if ($this->submitButton && isset($_POST['submit_base_'.$this->pid.'_x']) || !$this->submitButton) {
119 // Check if base is available
120 foreach ($this->pathMapping as $key => $path) {
121 if (mb_strtolower($path) == mb_strtolower($_POST['bs_input_'.$this->pid])) {
122 $this->base= $key;
123 break;
124 }
125 }
126 }
128 }
130 /* Skip if there's no change */
131 if (($this->tree && $this->base == $last_base) && !$force) {
132 return true;
133 }
135 $link= "href='?plug=".$_GET['plug']."&PID=".$this->pid."&REBASE=".base64_encode($config->current['BASE'])."'";
136 $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='".$this->pathMapping[$this->base]."'>";
139 // Autocompleter
140 $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
141 "<script type='text/javascript'>".
142 "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });".
143 "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });".
144 "</script>";
146 $selected= $this->base == $config->current['BASE']?"Selected":"";
147 $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";
148 $first= true;
149 $last_indent= 2;
151 foreach ($this->pathMapping as $base => $dummy) {
153 // Skip root for tree
154 if ($base == $config->current['BASE']) {
155 continue;
156 }
158 // Build path style display
159 $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
161 $indent= count($elements);
162 if (!$first && ($indent == $last_indent)) {
163 $this->tree.= "</li>\n";
164 }
165 if ($indent > $last_indent) {
166 $this->tree.= "<ul>\n";
167 }
168 if ($indent < $last_indent) {
169 for ($i= 0; $i < ($last_indent-$indent); $i++) {
170 $this->tree.= "</li></ul>\n";
171 }
172 $this->tree.= "</li>\n";
173 }
174 $selected= $this->base == $base?" class='treeListSelected'":"";
175 $link= "href='?plug=".$_GET['plug']."&PID=".$this->pid."&REBASE=".base64_encode($base)."'";
176 $this->tree.= "<li><a$selected $link>".str_replace(' ', ' ', ldap::fix(preg_replace('/^[a-z0-9]+=([^,]+),.*$/i', '$1', $base)))."</a>";
178 $last_indent= $indent;
179 $first= false;
180 }
182 // Close tree
183 for ($i= 0; $i<$last_indent; $i++) {
184 $this->tree.= "</li></ul>\n";
185 }
186 $this->tree.= "</div>\n";
188 // Draw submitter if required
189 if ($this->submitButton) {
190 $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")."'>";
191 }
193 return true;
194 }
196 function render()
197 {
198 return $this->tree;
199 }
202 function getBase()
203 {
204 return $this->base;
205 }
208 function getAction()
209 {
210 // Do not do anything if this is not our PID, or there's even no PID available...
211 if(!isset($_REQUEST['PID']) || $_REQUEST['PID'] != $this->pid) {
212 return;
213 }
215 if ($this->action) {
216 return array("targets" => array($this->base), "action" => $this->action);
217 }
219 return null;
220 }
222 }
224 ?>