Code

Updated listing classes
[gosa.git] / gosa-core / include / class_ItemSelector.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 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]= preg_replace("/^".preg_quote($this->releaseBase,'/')."\/?/i", "/", $path);
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));
141                 if($new_base){
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                 }
150             }else{
151                 // Input field set?
152                 if (isset($_POST['bs_input_'.$this->pid])) {
154                     // Take over input field base
155                     if ($this->submitButton && isset($_POST['submit_base_'.$this->pid]) || !$this->submitButton) {
157                         // Check if base is available
158                         $this->lastState= false;
159                         foreach ($this->pathMapping as $key => $path) {
160                             if (mb_strtolower($path) == mb_strtolower(get_post('bs_input_'.$this->pid))) {
161                                 $this->base= $key;
162                                 $this->lastState= true;
163                                 break;
164                             }
165                         }
166                     }
167                 }
168             } 
170         }
172         /* Skip if there's no change */
173         if (($this->tree && $this->base == $last_base) && !$force) {
174             return true;
175         }
177         $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($this->releaseBase)."';  
178             $('submit_tree_base_".$this->pid."').click();\"";
180         $initialValue = "";
181         if(isset($this->pathMapping[$this->base])){
182             $initialValue = $this->pathMapping[$this->base];
183         }
185         $this->tree= "
186         
187             <input style='width:160px' type='text' size='35' 
188             name='bs_input_{$this->pid}' 
189             id='bs_input_{$this->pid}'
191             onkeydown=\"    \$('bs_{$this->pid}').hide(); \"
192             onfocus=\"      \$('bs_{$this->pid}').hide(); \"
193             onmouseover=\"  
194                 mouseIsStillOver = true; 
195                 function showIt() 
196                 {
197                     if(mouseIsStillOver){
198                         \$('bs_".$this->pid."').show();  
199                     }
200                 };
202                 Element.clonePosition(
203                     \$('bs_".$this->pid."'),
204                     'bs_input_".$this->pid."', 
205                     {
206                         setHeight: false, 
207                         setWidth: false, 
208                         offsetTop:(Element.getHeight('bs_input_".$this->pid."'))
209                     });
210                 rtimer=showIt.delay(0.25); \" 
212             onmouseout=\"   
213                 mouseIsStillOver=false; 
214                 rtimer=Element.hide.delay(0.25,'bs_".$this->pid."')\"
216             value=\"".preg_replace('/"/','&quot;',$initialValue)."\">";
218         // Autocompleter
219         $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
220             "<script type='text/javascript'>".
221             "var mouseIsStillOver = false;".
222             "new Ajax.Autocompleter(
223                 'bs_input_".$this->pid."', 
224                 'autocomplete_".$this->pid."', 
225                 'autocomplete.php?type=base&pid={$this->pid}', 
226                 { minChars: 3, frequency: 0.5 });";
228         if ($this->submitButton) {
229             $this->tree.= "$('bs_input_".$this->pid."').observe('keypress', 
230                     function(event) 
231                     { 
232                         if(event.keyCode == Event.KEY_RETURN) { 
233                             $('submit_base_".$this->pid."').click();          
234                         } 
235                     });";
236         }
237         $this->tree.= "</script>";
238         $selected= $this->base == $this->releaseBase?"Selected":"";
239         $this->tree.= "<div class='treeList' 
240                             style=' display:none;
241                                     max-height:".$this->height."px' 
242                             id='bs_".$this->pid."' 
243                             onmouseover=\"
244                                     window.clearTimeout(rtimer);\" 
245                             onmouseout=\"
246                                     rtimer= Element.hide.delay(0.25, 'bs_".$this->pid."')\">
247                         <a                     class='treeList$selected' $link>/&nbsp;["._("Root")."]</a>
248                         <ul class='treeList'>\n";
250         $first= true;
251         $last_indent= 1;
253         $baseDepth = 0;
254         foreach ($this->pathMapping as $base => $dummy) {
256             // Do not render the base element
257             if($base == $this->releaseBase){
258                 $baseDepth = 1;
259                 continue;
260             }
262             // Build path style display
263             $elements= explode('/', substr($base, strlen($this->releaseBase), strlen($base)));
264             $indent= count($elements) - $baseDepth ;
265         
266             if (!$first && ($indent == $last_indent)) {
267                 $this->tree.= "</li>\n";
268             }
269             if ($indent > $last_indent) {
270                 $this->tree.= "<ul>\n";
271             }
272             if ($indent < $last_indent) {
273                 for ($i= 0; $i < ($last_indent-$indent); $i++) {
274                     $this->tree.= "</li></ul>\n";
275                 }
276                 $this->tree.= "</li>\n";
277             }
278             $selected= $this->base == $base?" class='treeListSelected'":"";
279             $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($base)."';$('submit_tree_base_".$this->pid."').click();\"";
281             $this->tree.= "<li>".
282                 "<a$selected $link>".
283                 $this->gennonbreaks($this->releaseInfo[$base]['name']).
284                 ($this->releaseInfo[$base]['description']==''?'':'&nbsp;<span class="informal">['.$this->gennonbreaks($this->releaseInfo[$base]['description']).']</span>').
285                 "</a>";
287             $last_indent= $indent;
288             $first= false;
289         }
291         // Close tree
292         for ($i= 1; $i<$last_indent; $i++) {
293             $this->tree.= "</li></ul>\n";
294         }
295         $this->tree.= "</div>\n";
297         // Draw submitter if required
298         if ($this->submitButton) {
299             $this->tree.= image('images/lists/submit.png', "submit_base_".$this->pid, _("Submit"));
300         }
301         $this->tree.= "<input type='submit' style='display:none' name='submit_tree_base_".$this->pid."' id='submit_tree_base_".$this->pid."'>";
302         $this->tree.= "<input type='hidden' name='bs_rebase_".$this->pid."' id='bs_rebase_".$this->pid."'>";
303         $this->tree.= "<input type='hidden' name='BPID_{$this->pid}' id='BPID_{$this->pid}' value='".$this->pid."'>";
305         $this->lastState= true;
306         return true;
307     }
310     function gennonbreaks($string)
311     {
312         return str_replace('-', '&#8209;', str_replace(' ', '&nbsp;', $string));
313     }
316     function render()
317     {
318         return $this->tree;
319     }
322     function getBase()
323     {
324         return $this->base;
325     }
328     function getAction()
329     {
330         // Do not do anything if this is not our BPID, or there's even no BPID available...
331         if(!isset($_REQUEST["BPID_{$this->pid}"]) || $_REQUEST["BPID_{$this->pid}"] != $this->pid) {
332             return;
333         }
335         if ($this->action) {
336             return array("targets" => array($this->base), "action" => $this->action);
337         }
339         return null;
340     }
344 ?>