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 releaseSelector {
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, $base, $releaseBase)
37 {
38 // Initialize pid
39 $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
41 // Transfer data
42 $this->releaseBase = $releaseBase;
43 $this->releaseInfo = $bases;
44 $this->setBases($bases);
45 $this->setBase($base);
46 }
49 function setSubmitButton($flag)
50 {
51 $this->submitButton= $flag;
52 }
55 function setHeight($value)
56 {
57 $this->height= $value;
58 }
61 function setBase($base)
62 {
63 if (isset($this->pathMapping[$base])) {
64 $this->base= $base;
65 $this->update(true);
66 }
67 }
70 function checkBase($base)
71 {
72 return isset($this->pathMapping[$base]);
73 }
76 function checkLastBaseUpdate()
77 {
78 return $this->lastState;
79 }
82 function setBases($bases)
83 {
84 global $config;
86 $this->pathMapping= array();
87 $selected= $this->base == $this->releaseBase?"Selected":"";
88 $first= true;
89 $last_indent= 2;
91 foreach ($bases as $base => $dummy) {
93 // Build path style display
94 $elements= explode(',', substr($base, 0, strlen($base) - strlen($this->releaseBase)));
95 $elements= array_reverse($elements, true);
96 $this->pathMapping[$base]= $base == $this->releaseBase? '/' : ldap::fix(preg_replace('/(^|,)[a-z0-9]+=/i', '/', implode(',', $elements)));
97 }
99 // Save bases to session for autocompletion
100 session::global_set('pathMapping', $this->pathMapping);
101 }
104 function update($force= false)
105 {
106 global $config;
108 // Analyze for base changes if needed
109 $this->action= null;
110 $last_base= $this->base;
111 if(isset($_REQUEST['BPID']) && $_REQUEST['BPID'] == $this->pid) {
112 if (isset($_POST['bs_rebase_'.$this->pid]) && !empty($_POST['bs_rebase_'.$this->pid])) {
113 $new_base= base64_decode($_POST['bs_rebase_'.$this->pid]);
114 if (isset($this->pathMapping[$new_base])) {
115 $this->base= $new_base;
116 $this->action= 'rebase';
117 } else {
118 $this->lastState= false;
119 return false;
120 }
121 }else{
122 // Input field set?
123 if (isset($_POST['bs_input_'.$this->pid])) {
125 // Take over input field base
126 if ($this->submitButton && isset($_POST['submit_base_'.$this->pid.'_x']) || !$this->submitButton) {
128 // Check if base is available
129 $this->lastState= false;
130 foreach ($this->pathMapping as $key => $path) {
131 if (mb_strtolower($path) == mb_strtolower($_POST['bs_input_'.$this->pid])) {
132 $this->base= $key;
133 $this->lastState= true;
134 break;
135 }
136 }
137 }
138 }
139 }
141 }
143 /* Skip if there's no change */
144 if (($this->tree && $this->base == $last_base) && !$force) {
145 return true;
146 }
148 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($this->releaseBase)."'; $('submit_tree_base_".$this->pid."').click();\"";
149 $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])."\">";
151 // Autocompleter
152 $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
153 "<script type='text/javascript'>".
154 "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });";
155 if ($this->submitButton) {
156 $this->tree.= "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });";
157 }
158 $this->tree.= "</script>";
160 $selected= $this->base == $this->releaseBase?"Selected":"";
161 $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";
162 $first= true;
163 $last_indent= 2;
165 foreach ($this->pathMapping as $base => $dummy) {
167 // Skip root for tree
168 if ($base == $this->releaseBase) {
169 continue;
170 }
172 // Build path style display
173 $elements= explode(',', substr($base, 0, strlen($base) - strlen($this->releaseBase)));
175 $indent= count($elements);
176 if (!$first && ($indent == $last_indent)) {
177 $this->tree.= "</li>\n";
178 }
179 if ($indent > $last_indent) {
180 $this->tree.= "<ul>\n";
181 }
182 if ($indent < $last_indent) {
183 for ($i= 0; $i < ($last_indent-$indent); $i++) {
184 $this->tree.= "</li></ul>\n";
185 }
186 $this->tree.= "</li>\n";
187 }
188 $selected= $this->base == $base?" class='treeListSelected'":"";
189 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($base)."';$('submit_tree_base_".$this->pid."').click();\"";
190 $this->tree.= "<li>".
191 "<a$selected $link>".
192 $this->gennonbreaks($this->releaseInfo[$base]).
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 ?>