f3eca46910af2515bac978b4e09c88a33c43b767
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 ItemSelector {
25 public $base;
26 public $pid;
27 public $action;
28 public $height= 500;
29 public $submitButton= true;
30 public $tree;
31 public $pathMapping;
32 public $lastState;
33 public $releaseInfo;
34 public $releaseBase;
36 function __construct($bases = array(), $base = "", $releaseBase = "")
37 {
38 // Initialize pid
39 $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
41 // Get list of used IDs
42 if(!session::is_set('releaseSelector_USED_IDS')){
43 session::set('releaseSelector_USED_IDS',array());
44 }
45 $usedIds = session::get('releaseSelector_USED_IDS');
47 // Generate instance wide unique ID
48 $pid = "";
49 while($pid == "" || in_array_strict($pid, $usedIds)){
51 // Wait 1 msec to ensure that we definately get a new id
52 if($pid != "") usleep(1);
53 $tmp= gettimeofday();
54 $pid = 'l'.md5(microtime().$tmp['sec']);
55 }
57 // Only keep the last 10 list IDsi
58 $usedIds = array_slice($usedIds, count($usedIds) -10, 10);
59 $usedIds[] = $pid;
60 session::set('releaseSelector_USED_IDS',$usedIds);
61 $this->pid = $pid;
63 // Transfer data
64 $this->releaseBase = $releaseBase;
65 $this->setBases($bases);
66 $this->setBase($base);
67 }
70 function setSubmitButton($flag)
71 {
72 $this->submitButton= $flag;
73 }
76 function setHeight($value)
77 {
78 $this->height= $value;
79 }
82 function setBase($base)
83 {
84 $this->base= $base;
85 if (isset($this->pathMapping[$base])) {
86 $this->update(true);
87 }
88 }
90 function setRootBase($base)
91 {
92 $this->releaseBase = $base;
93 }
96 function checkBase($base)
97 {
98 return isset($this->pathMapping[$base]);
99 }
102 function checkLastBaseUpdate()
103 {
104 return $this->lastState;
105 }
108 function setBases($bases)
109 {
110 global $config;
111 $this->releaseInfo = array();
112 $this->pathMapping= array();
113 $selected= $this->base == $this->releaseBase?"Selected":"";
114 $first= true;
115 $last_indent= 2;
117 foreach ($bases as $path => $data) {
119 // Build path style display
120 $this->pathMapping[$path]= $path == $this->releaseBase? '/' : $data['name'];
121 $this->releaseInfo[$path]['name'] = $data['name'];
122 $this->releaseInfo[$path]['description'] = $data['desc'];
123 }
125 // Save bases to session for autocompletion
126 session::global_set("pathMapping_{$this->pid}", $this->pathMapping);
127 session::global_set("department_info_{$this->pid}", $this->releaseInfo);
128 }
131 function update($force= false)
132 {
133 global $config;
135 // Analyze for base changes if needed
136 $this->action= null;
137 $last_base= $this->base;
138 if(isset($_REQUEST["BPID_{$this->pid}"]) && $_REQUEST["BPID_{$this->pid}"] == $this->pid) {
139 if (isset($_POST['bs_rebase_'.$this->pid])) {
140 $new_base= base64_decode(get_post('bs_rebase_'.$this->pid));
142 if (isset($this->pathMapping[$new_base])) {
143 $this->base= $new_base;
144 $this->action= 'rebase';
145 } else {
146 $this->lastState= false;
147 return false;
148 }
149 }else{
150 // Input field set?
151 if (isset($_POST['bs_input_'.$this->pid])) {
153 // Take over input field base
154 if ($this->submitButton && isset($_POST['submit_base_'.$this->pid]) || !$this->submitButton) {
156 // Check if base is available
157 $this->lastState= false;
158 foreach ($this->pathMapping as $key => $path) {
159 if (mb_strtolower($path) == mb_strtolower(get_post('bs_input_'.$this->pid))) {
160 $this->base= $key;
161 $this->lastState= true;
162 break;
163 }
164 }
165 }
166 }
167 }
169 }
171 /* Skip if there's no change */
172 if (($this->tree && $this->base == $last_base) && !$force) {
173 return true;
174 }
176 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($this->releaseBase)."';
177 $('submit_tree_base_".$this->pid."').click();\"";
179 $initialValue = "";
180 if(isset($this->pathMapping[$this->base])){
181 $initialValue = $this->pathMapping[$this->base];
182 }
184 $this->tree= "
186 <input style='width:160px' type='text' size='35'
187 name='bs_input_{$this->pid}'
188 id='bs_input_{$this->pid}'
190 onkeydown=\" \$('bs_{$this->pid}').hide(); \"
191 onfocus=\" \$('bs_{$this->pid}').hide(); \"
192 onmouseover=\"
193 mouseIsStillOver = true;
194 function showIt()
195 {
196 if(mouseIsStillOver){
197 \$('bs_".$this->pid."').show();
198 }
199 };
201 Element.clonePosition(
202 \$('bs_".$this->pid."'),
203 'bs_input_".$this->pid."',
204 {
205 setHeight: false,
206 setWidth: false,
207 offsetTop:(Element.getHeight('bs_input_".$this->pid."'))
208 });
209 rtimer=showIt.delay(0.25); \"
211 onmouseout=\"
212 mouseIsStillOver=false;
213 rtimer=Element.hide.delay(0.25,'bs_".$this->pid."')\"
215 value=\"".preg_replace('/"/','"',$initialValue)."\">";
217 // Autocompleter
218 $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
219 "<script type='text/javascript'>".
220 "var mouseIsStillOver = false;".
221 "new Ajax.Autocompleter(
222 'bs_input_".$this->pid."',
223 'autocomplete_".$this->pid."',
224 'autocomplete.php?type=base&pid={$this->pid}',
225 { minChars: 3, frequency: 0.5 });";
227 if ($this->submitButton) {
228 $this->tree.= "$('bs_input_".$this->pid."').observe('keypress',
229 function(event)
230 {
231 if(event.keyCode == Event.KEY_RETURN) {
232 $('submit_base_".$this->pid."').click();
233 }
234 });";
235 }
236 $this->tree.= "</script>";
237 $this->tree.= "<div class='treeList'
238 style=' display:none;
239 max-height:".$this->height."px'
240 id='bs_".$this->pid."'
241 onmouseover=\"
242 window.clearTimeout(rtimer);\"
243 onmouseout=\"
244 rtimer= Element.hide.delay(0.25, 'bs_".$this->pid."')\">
245 <ul class='treeList'>\n";
247 $first= true;
248 $last_indent= 1;
250 foreach ($this->pathMapping as $base => $dummy) {
252 // Build path style display
253 $elements= explode('/', substr($base, strlen($this->releaseBase), strlen($base)));
254 $indent= count($elements) ;
256 if (!$first && ($indent == $last_indent)) {
257 $this->tree.= "</li>\n";
258 }
259 if ($indent > $last_indent) {
260 $this->tree.= "<ul>\n";
261 }
262 if ($indent < $last_indent) {
263 for ($i= 0; $i < ($last_indent-$indent); $i++) {
264 $this->tree.= "</li></ul>\n";
265 }
266 $this->tree.= "</li>\n";
267 }
268 $selected= $this->base == $base?" class='treeListSelected'":"";
269 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($base)."';$('submit_tree_base_".$this->pid."').click();\"";
271 $this->tree.= "<li>".
272 "<a$selected $link>".
273 $this->gennonbreaks($this->releaseInfo[$base]['name']).
274 ($this->releaseInfo[$base]['description']==''?'':' <span class="informal">['.$this->gennonbreaks($this->releaseInfo[$base]['description']).']</span>').
275 "</a>";
277 $last_indent= $indent;
278 $first= false;
279 }
281 // Close tree
282 for ($i= 1; $i<$last_indent; $i++) {
283 $this->tree.= "</li></ul>\n";
284 }
285 $this->tree.= "</div>\n";
287 // Draw submitter if required
288 if ($this->submitButton) {
289 $this->tree.= image('images/lists/submit.png', "submit_base_".$this->pid, _("Submit"));
290 }
291 $this->tree.= "<input type='submit' style='display:none' name='submit_tree_base_".$this->pid."' id='submit_tree_base_".$this->pid."'>";
292 $this->tree.= "<input type='hidden' name='bs_rebase_".$this->pid."' id='bs_rebase_".$this->pid."'>";
293 $this->tree.= "<input type='hidden' name='BPID_{$this->pid}' id='BPID_{$this->pid}' value='".$this->pid."'>";
295 $this->lastState= true;
296 return true;
297 }
300 function gennonbreaks($string)
301 {
302 return str_replace('-', '‑', str_replace(' ', ' ', $string));
303 }
306 function render()
307 {
308 return $this->tree;
309 }
312 function getBase()
313 {
314 return $this->base;
315 }
318 function getAction()
319 {
320 // Do not do anything if this is not our BPID, or there's even no BPID available...
321 if(!isset($_REQUEST["BPID_{$this->pid}"]) || $_REQUEST["BPID_{$this->pid}"] != $this->pid) {
322 return;
323 }
325 if ($this->action) {
326 return array("targets" => array($this->base), "action" => $this->action);
327 }
329 return null;
330 }
332 }
334 ?>