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->setBases($bases);
44 $this->setBase($base);
45 }
48 function setSubmitButton($flag)
49 {
50 $this->submitButton= $flag;
51 }
54 function setHeight($value)
55 {
56 $this->height= $value;
57 }
60 function setBase($base)
61 {
62 $this->base= $base;
63 if (isset($this->pathMapping[$base])) {
64 $this->update(true);
65 }
66 }
69 function checkBase($base)
70 {
71 return isset($this->pathMapping[$base]);
72 }
75 function checkLastBaseUpdate()
76 {
77 return $this->lastState;
78 }
81 function setBases($bases)
82 {
83 global $config;
84 $this->releaseInfo = array();
85 $this->pathMapping= array();
86 $selected= $this->base == $this->releaseBase?"Selected":"";
87 $first= true;
88 $last_indent= 2;
90 foreach ($bases as $base => $dummy) {
92 // Build path style display
93 $elements= explode(',', substr($base, 0, strlen($base) - strlen($this->releaseBase)));
94 $elements= array_reverse($elements, true);
95 $this->pathMapping[$base]= $base == $this->releaseBase? '/' : ldap::fix(preg_replace('/(^|,)[a-z0-9]+=/i', '/', implode(',', $elements)));
97 $this->releaseInfo[$base]['name'] = ldap::fix(preg_replace('/(^|,)[a-z0-9]+=/i', '', $elements[0]));
98 $this->releaseInfo[$base]['description'] = $dummy;
99 }
101 // Save bases to session for autocompletion
102 session::global_set('pathMapping', $this->pathMapping);
103 session::global_set('department_info', $this->releaseInfo);
104 }
107 function update($force= false)
108 {
109 global $config;
111 // Analyze for base changes if needed
112 $this->action= null;
113 $last_base= $this->base;
114 if(isset($_REQUEST['BPID']) && $_REQUEST['BPID'] == $this->pid) {
115 if (isset($_POST['bs_rebase_'.$this->pid]) && !empty($_POST['bs_rebase_'.$this->pid])) {
116 $new_base= base64_decode(get_post('bs_rebase_'.$this->pid));
117 if (isset($this->pathMapping[$new_base])) {
118 $this->base= $new_base;
119 $this->action= 'rebase';
120 } else {
121 $this->lastState= false;
122 return false;
123 }
124 }else{
125 // Input field set?
126 if (isset($_POST['bs_input_'.$this->pid])) {
128 // Take over input field base
129 if ($this->submitButton && isset($_POST['submit_base_'.$this->pid]) || !$this->submitButton) {
131 // Check if base is available
132 $this->lastState= false;
133 foreach ($this->pathMapping as $key => $path) {
134 if (mb_strtolower($path) == mb_strtolower(get_post('bs_input_'.$this->pid))) {
135 $this->base= $key;
136 $this->lastState= true;
137 break;
138 }
139 }
140 }
141 }
142 }
144 }
146 /* Skip if there's no change */
147 if (($this->tree && $this->base == $last_base) && !$force) {
148 return true;
149 }
151 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($this->releaseBase)."';
152 $('submit_tree_base_".$this->pid."').click();\"";
154 $this->tree= "<input style='width:160px' type='text' size='35'
155 name='bs_input_{$this->pid}' id='bs_input_{$this->pid}'
157 onkeydown=\" \$('bs_{$this->pid}').hide(); \"
158 onfocus=\" \$('bs_{$this->pid}').hide(); \"
159 onmouseover=\" mouseIsStillOver = true;
160 function showIt()
161 {
162 if(mouseIsStillOver){
163 \$('bs_".$this->pid."').show();
164 }
165 };
166 Element.clonePosition(\$('bs_".$this->pid."'),
167 'bs_input_".$this->pid."',
168 {setHeight: false, setWidth: false, offsetTop:(Element.getHeight('bs_input_".$this->pid."'))});
169 rtimer=showIt.delay(0.25); \"
171 onmouseout=\" mouseIsStillOver=false;
172 rtimer=Element.hide.delay(0.25,'bs_".$this->pid."')\"
174 value=\"".preg_replace('/"/','"',$this->pathMapping[$this->base])."\">";
176 // Autocompleter
177 $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
178 "<script type='text/javascript'>".
179 "var mouseIsStillOver = false;".
180 "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });";
181 if ($this->submitButton) {
182 $this->tree.= "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });";
183 }
184 $this->tree.= "</script>";
186 $selected= $this->base == $this->releaseBase?"Selected":"";
187 $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";
188 $first= true;
189 $last_indent= 2;
191 foreach ($this->pathMapping as $base => $dummy) {
193 // Skip root for tree
194 if ($base == $this->releaseBase) {
195 continue;
196 }
198 // Build path style display
199 $elements= explode(',', substr($base, 0, strlen($base) - strlen($this->releaseBase)));
201 $indent= count($elements);
202 if (!$first && ($indent == $last_indent)) {
203 $this->tree.= "</li>\n";
204 }
205 if ($indent > $last_indent) {
206 $this->tree.= "<ul>\n";
207 }
208 if ($indent < $last_indent) {
209 for ($i= 0; $i < ($last_indent-$indent); $i++) {
210 $this->tree.= "</li></ul>\n";
211 }
212 $this->tree.= "</li>\n";
213 }
214 $selected= $this->base == $base?" class='treeListSelected'":"";
215 $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($base)."';$('submit_tree_base_".$this->pid."').click();\"";
217 $this->tree.= "<li>".
218 "<a$selected $link>".
219 $this->gennonbreaks($this->releaseInfo[$base]['name']).
220 ($this->releaseInfo[$base]['description']==''?'':' <span class="informal">['.$this->gennonbreaks($this->releaseInfo[$base]['description']).']</span>').
221 "</a>";
223 $last_indent= $indent;
224 $first= false;
225 }
227 // Close tree
228 for ($i= 1; $i<$last_indent; $i++) {
229 $this->tree.= "</li></ul>\n";
230 }
231 $this->tree.= "</div>\n";
233 // Draw submitter if required
234 if ($this->submitButton) {
235 $this->tree.= image('images/lists/submit.png', "submit_base_".$this->pid, _("Submit"));
236 }
237 $this->tree.= "<input type='submit' style='display:none' name='submit_tree_base_".$this->pid."' id='submit_tree_base_".$this->pid."'>";
238 $this->tree.= "<input type='hidden' name='bs_rebase_".$this->pid."' id='bs_rebase_".$this->pid."'>";
239 $this->tree.= "<input type='hidden' name='BPID' id='BPID' value='".$this->pid."'>";
241 $this->lastState= true;
242 return true;
243 }
246 function gennonbreaks($string)
247 {
248 return str_replace('-', '‑', str_replace(' ', ' ', $string));
249 }
252 function render()
253 {
254 return $this->tree;
255 }
258 function getBase()
259 {
260 return $this->base;
261 }
264 function getAction()
265 {
266 // Do not do anything if this is not our BPID, or there's even no BPID available...
267 if(!isset($_REQUEST['BPID']) || $_REQUEST['BPID'] != $this->pid) {
268 return;
269 }
271 if ($this->action) {
272 return array("targets" => array($this->base), "action" => $this->action);
273 }
275 return null;
276 }
278 }
280 ?>