Code

fe62b530e3f04569c119237a92929c8968091186
[gosa.git] / gosa-core / include / class_releaseSelector.inc
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         // 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($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 $base => $dummy) {
119             // Build path style display
120             $elements= explode(',', substr($base, 0, strlen($base) - strlen($this->releaseBase)));
121             $elements= array_reverse($elements, true);
122             $this->pathMapping[$base]= $base == $this->releaseBase? '/' : ldap::fix(preg_replace('/(^|,)[a-z0-9]+=/i', '/', implode(',', $elements)));
124             $this->releaseInfo[$base]['name'] = ldap::fix(preg_replace('/(^|,)[a-z0-9]+=/i', '', $elements[0]));
125             $this->releaseInfo[$base]['description'] = $dummy;
126         }
128         // Save bases to session for autocompletion
129         session::global_set('pathMapping', $this->pathMapping);
130         session::global_set('department_info', $this->releaseInfo);
131     }
134     function update($force= false)
135     {
136         global $config;
138         // Analyze for base changes if needed
139         $this->action= null;
140         $last_base= $this->base;
141         if(isset($_REQUEST["BPID_{$this->pid}"]) && $_REQUEST["BPID_{$this->pid}"] == $this->pid) {
142             if (isset($_POST['bs_rebase_'.$this->pid]) && !empty($_POST['bs_rebase_'.$this->pid])) {
143                 $new_base= base64_decode(get_post('bs_rebase_'.$this->pid));
144                 if (isset($this->pathMapping[$new_base])) {
145                     $this->base= $new_base;
146                     $this->action= 'rebase';
147                 } else {
148                     $this->lastState= false;
149                     return false;
150                 }
151             }else{
152                 // Input field set?
153                 if (isset($_POST['bs_input_'.$this->pid])) {
155                     // Take over input field base
156                     if ($this->submitButton && isset($_POST['submit_base_'.$this->pid]) || !$this->submitButton) {
158                         // Check if base is available
159                         $this->lastState= false;
160                         foreach ($this->pathMapping as $key => $path) {
161                             if (mb_strtolower($path) == mb_strtolower(get_post('bs_input_'.$this->pid))) {
162                                 $this->base= $key;
163                                 $this->lastState= true;
164                                 break;
165                             }
166                         }
167                     }
168                 }
169             } 
171         }
173         /* Skip if there's no change */
174         if (($this->tree && $this->base == $last_base) && !$force) {
175             return true;
176         }
178         $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($this->releaseBase)."';  
179             $('submit_tree_base_".$this->pid."').click();\"";
181         $this->tree= "<input style='width:160px' type='text' size='35' 
182             name='bs_input_{$this->pid}' id='bs_input_{$this->pid}'
184             onkeydown=\"    \$('bs_{$this->pid}').hide(); \"
185             onfocus=\"      \$('bs_{$this->pid}').hide(); \"
186             onmouseover=\"  mouseIsStillOver = true; 
187             function showIt() 
188             {
189                 if(mouseIsStillOver){
190                     \$('bs_".$this->pid."').show();  
191                 }
192             };
193         Element.clonePosition(\$('bs_".$this->pid."'),
194                 'bs_input_".$this->pid."', 
195                 {setHeight: false, setWidth: false, offsetTop:(Element.getHeight('bs_input_".$this->pid."'))});
196         rtimer=showIt.delay(0.25); \" 
198             onmouseout=\"   mouseIsStillOver=false; 
199             rtimer=Element.hide.delay(0.25,'bs_".$this->pid."')\"
201             value=\"".preg_replace('/"/','&quot;',$this->pathMapping[$this->base])."\">";
203         // Autocompleter
204         $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
205             "<script type='text/javascript'>".
206             "var mouseIsStillOver = false;".
207             "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });";
208         if ($this->submitButton) {
209             $this->tree.= "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });";
210         }
211         $this->tree.= "</script>";
213         $selected= $this->base == $this->releaseBase?"Selected":"";
214         $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>/&nbsp;["._("Root")."]</a><ul class='treeList'>\n";
215         $first= true;
216         $last_indent= 2;
218         foreach ($this->pathMapping as $base => $dummy) {
220             // Skip root for tree
221             if ($base == $this->releaseBase) {
222                 continue;
223             }
225             // Build path style display
226             $elements= explode(',', substr($base, 0, strlen($base) - strlen($this->releaseBase)));
228             $indent= count($elements);
229             if (!$first && ($indent == $last_indent)) {
230                 $this->tree.= "</li>\n";
231             }
232             if ($indent > $last_indent) {
233                 $this->tree.= "<ul>\n";
234             }
235             if ($indent < $last_indent) {
236                 for ($i= 0; $i < ($last_indent-$indent); $i++) {
237                     $this->tree.= "</li></ul>\n";
238                 }
239                 $this->tree.= "</li>\n";
240             }
241             $selected= $this->base == $base?" class='treeListSelected'":"";
242             $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($base)."';$('submit_tree_base_".$this->pid."').click();\"";
244             $this->tree.= "<li>".
245                 "<a$selected $link>".
246                 $this->gennonbreaks($this->releaseInfo[$base]['name']).
247                 ($this->releaseInfo[$base]['description']==''?'':'&nbsp;<span class="informal">['.$this->gennonbreaks($this->releaseInfo[$base]['description']).']</span>').
248                 "</a>";
250             $last_indent= $indent;
251             $first= false;
252         }
254         // Close tree
255         for ($i= 1; $i<$last_indent; $i++) {
256             $this->tree.= "</li></ul>\n";
257         }
258         $this->tree.= "</div>\n";
260         // Draw submitter if required
261         if ($this->submitButton) {
262             $this->tree.= image('images/lists/submit.png', "submit_base_".$this->pid, _("Submit"));
263         }
264         $this->tree.= "<input type='submit' style='display:none' name='submit_tree_base_".$this->pid."' id='submit_tree_base_".$this->pid."'>";
265         $this->tree.= "<input type='hidden' name='bs_rebase_".$this->pid."' id='bs_rebase_".$this->pid."'>";
266         $this->tree.= "<input type='hidden' name='BPID_{$this->pid}' id='BPID_{$this->pid}' value='".$this->pid."'>";
268         $this->lastState= true;
269         return true;
270     }
273     function gennonbreaks($string)
274     {
275         return str_replace('-', '&#8209;', str_replace(' ', '&nbsp;', $string));
276     }
279     function render()
280     {
281         return $this->tree;
282     }
285     function getBase()
286     {
287         return $this->base;
288     }
291     function getAction()
292     {
293         // Do not do anything if this is not our BPID, or there's even no BPID available...
294         if(!isset($_REQUEST["BPID_{$this->pid}"]) || $_REQUEST["BPID_{$this->pid}"] != $this->pid) {
295             return;
296         }
298         if ($this->action) {
299             return array("targets" => array($this->base), "action" => $this->action);
300         }
302         return null;
303     }
307 ?>