Code

Updated baseSelector
[gosa.git] / gosa-core / include / class_baseSelector.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 baseSelector {
25   private $base;
26   private $pid;
27   private $action;
28   private $height= 500;
29   private $submitButton= true;
30   protected $tree;
31   protected $pathMapping;
32   protected $lastState;
35   function __construct($bases, $base= "")
36   {
37     // Initialize pid
38     $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
40     // Transfer data
41     $this->setBases($bases);
42     $this->setBase($base);
43   }
46   function setSubmitButton($flag)
47   {
48     $this->submitButton= $flag;
49   }
52   function setHeight($value)
53   {
54     $this->height= $value;
55   }
58   function setBase($base)
59   {
60     if (isset($this->pathMapping[$base])) {
61       $this->base= $base;
62       $this->update(true);
63     }
64   }
67   function checkBase($base)
68   {
69     return isset($this->pathMapping[$base]);
70   }
73   function checkLastBaseUpdate()
74   {
75     return $this->lastState;
76   }
79   function setBases($bases)
80   {
81     global $config;
83     $this->pathMapping= array();
84     $selected= $this->base == $config->current['BASE']?"Selected":"";
85     $first= true;
86     $last_indent= 2;
88     foreach ($bases as $base => $dummy) {
90       // Build path style display
91       $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
92       $elements= array_reverse($elements, true);
94       $this->pathMapping[$base]= $base == $config->current['BASE']? '/' : ldap::fix(preg_replace('/(^|,)[a-z0-9]+=/i', '/', implode(',', $elements)));
95     }
97     // Save bases to session for autocompletion
98     session::global_set('pathMapping', $this->pathMapping);
99     session::global_set('department_info', $config->department_info);
100   }
103   function update($force= false)
104   {
105     global $config;
107     // Analyze for base changes if needed
108     $this->action= null;
109     $last_base= $this->base;
110     if(isset($_REQUEST['BPID']) && $_REQUEST['BPID'] == $this->pid) {
111       if (isset($_POST['bs_rebase_'.$this->pid]) && !empty($_POST['bs_rebase_'.$this->pid])) {
112         $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{
123         // Input field set?
124         if (isset($_POST['bs_input_'.$this->pid])) {
126           // Take over input field base
127           if ($this->submitButton && isset($_POST['submit_base_'.$this->pid]) || !$this->submitButton) {
129             // Check if base is available
130             $this->lastState= false;
131             foreach ($this->pathMapping as $key => $path) {
132               if (mb_strtolower($path) == mb_strtolower(get_post('bs_input_'.$this->pid))) {
133                 $this->base= $key;
134                 $this->lastState= true;
135                 break;
136               }
137             }
138           }
139         }
140       } 
142     }
144     /* Skip if there's no change */
145     if (($this->tree && $this->base == $last_base) && !$force) {
146       return true;
147     }
149     $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($config->current['BASE'])."';  $('submit_tree_base_".$this->pid."').click();\"";
150     $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('/"/', '&quot;',$this->pathMapping[$this->base])."\">";
152     // Autocompleter
153     $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
154                   "<script type='text/javascript'>".
155                   "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });";
156     if ($this->submitButton) {
157       $this->tree.= "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });";
158     }
159     $this->tree.= "</script>";
161     $selected= $this->base == $config->current['BASE']?"Selected":"";
162     $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";
163     $first= true;
164     $last_indent= 2;
166     foreach ($this->pathMapping as $base => $dummy) {
168       // Skip root for tree
169       if ($base == $config->current['BASE']) {
170         continue;
171       }
173       // Build path style display
174       $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
176       $indent= count($elements);
177       if (!$first && ($indent == $last_indent)) {
178         $this->tree.= "</li>\n";
179       }
180       if ($indent > $last_indent) {
181         $this->tree.= "<ul>\n";
182       }
183       if ($indent < $last_indent) {
184         for ($i= 0; $i < ($last_indent-$indent); $i++) {
185           $this->tree.= "</li></ul>\n";
186         }
187         $this->tree.= "</li>\n";
188       }
189       $selected= $this->base == $base?" class='treeListSelected'":"";
190       $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($base)."';$('submit_tree_base_".$this->pid."').click();\"";
191       $this->tree.= "<li>".
192                     image($config->department_info[$base]['img'])."&nbsp;<a$selected $link>".
193                     $this->gennonbreaks($config->department_info[$base]['name']).
194                     ($config->department_info[$base]['description']==''?'':'&nbsp;<span class="informal">['.$this->gennonbreaks($config->department_info[$base]['description']).']</span>').
195                     "</a>";
197       $last_indent= $indent;
198       $first= false;
199     }
201     // Close tree
202     for ($i= 1; $i<$last_indent; $i++) {
203       $this->tree.= "</li></ul>\n";
204     }
205     $this->tree.= "</div>\n";
207     // Draw submitter if required
208     if ($this->submitButton) {
209       $this->tree.= image('images/lists/submit.png', "submit_base_".$this->pid, _("Submit"));
210     }
211     $this->tree.= "<input type='submit' style='display:none' name='submit_tree_base_".$this->pid."' id='submit_tree_base_".$this->pid."'>";
212     $this->tree.= "<input type='hidden' name='bs_rebase_".$this->pid."' id='bs_rebase_".$this->pid."'>";
213     $this->tree.= "<input type='hidden' name='BPID' id='BPID' value='".$this->pid."'>";
215     $this->lastState= true;
216     return true;
217   }
220   function gennonbreaks($string)
221   {
222     return str_replace('-', '&#8209;', str_replace(' ', '&nbsp;', $string));
223   }
226   function render()
227   {
228     return $this->tree;
229   }
232   function getBase()
233   {
234     return $this->base;
235   }
238   function getAction()
239   {
240     // Do not do anything if this is not our BPID, or there's even no BPID available...
241     if(!isset($_REQUEST['BPID']) || $_REQUEST['BPID'] != $this->pid) {
242       return;
243     }
245     if ($this->action) {
246       return array("targets" => array($this->base), "action" => $this->action);
247     }
249     return null;
250   }
254 ?>