Code

Updated release selector
[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     // 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'] = $this->pathMapping[$base];
98     }
100     // Save bases to session for autocompletion
101     session::global_set('pathMapping', $this->pathMapping);
102     session::global_set('department_info', $this->releaseInfo);
103   }
106   function update($force= false)
107   {
108     global $config;
110     // Analyze for base changes if needed
111     $this->action= null;
112     $last_base= $this->base;
113     if(isset($_REQUEST['BPID']) && $_REQUEST['BPID'] == $this->pid) {
114       if (isset($_POST['bs_rebase_'.$this->pid]) && !empty($_POST['bs_rebase_'.$this->pid])) {
115         $new_base= base64_decode($_POST['bs_rebase_'.$this->pid]);
116         if (isset($this->pathMapping[$new_base])) {
117           $this->base= $new_base;
118           $this->action= 'rebase';
119         } else {
120           $this->lastState= false;
121           return false;
122         }
123       }else{
124         // Input field set?
125         if (isset($_POST['bs_input_'.$this->pid])) {
127           // Take over input field base
128           if ($this->submitButton && isset($_POST['submit_base_'.$this->pid]) || !$this->submitButton) {
130             // Check if base is available
131             $this->lastState= false;
132             foreach ($this->pathMapping as $key => $path) {
133               if (mb_strtolower($path) == mb_strtolower($_POST['bs_input_'.$this->pid])) {
134                 $this->base= $key;
135                 $this->lastState= true;
136                 break;
137               }
138             }
139           }
140         }
141       } 
143     }
145     /* Skip if there's no change */
146     if (($this->tree && $this->base == $last_base) && !$force) {
147       return true;
148     }
150     $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($this->releaseBase)."';  $('submit_tree_base_".$this->pid."').click();\"";
151     $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])."\">";
153     // Autocompleter
154     $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
155                   "<script type='text/javascript'>".
156                   "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });";
157     if ($this->submitButton) {
158       $this->tree.= "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });";
159     }
160     $this->tree.= "</script>";
162     $selected= $this->base == $this->releaseBase?"Selected":"";
163     $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";
164     $first= true;
165     $last_indent= 2;
167     foreach ($this->pathMapping as $base => $dummy) {
169       // Skip root for tree
170       if ($base == $this->releaseBase) {
171         continue;
172       }
174       // Build path style display
175       $elements= explode(',', substr($base, 0, strlen($base) - strlen($this->releaseBase)));
177       $indent= count($elements);
178       if (!$first && ($indent == $last_indent)) {
179         $this->tree.= "</li>\n";
180       }
181       if ($indent > $last_indent) {
182         $this->tree.= "<ul>\n";
183       }
184       if ($indent < $last_indent) {
185         for ($i= 0; $i < ($last_indent-$indent); $i++) {
186           $this->tree.= "</li></ul>\n";
187         }
188         $this->tree.= "</li>\n";
189       }
190       $selected= $this->base == $base?" class='treeListSelected'":"";
191       $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($base)."';$('submit_tree_base_".$this->pid."').click();\"";
192       $this->tree.= "<li>".
193                     "<a$selected $link>".
194                     $this->gennonbreaks($this->releaseInfo[$base]['name']).
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 ?>