Code

8c5e7d46b1ce3c3a2f14a84672689b5f9523d051
[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'] = 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($_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($_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)."';  $('submit_tree_base_".$this->pid."').click();\"";
152     $this->tree= "<input style='width:160px' 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])."\">";
154     // Autocompleter
155     $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
156                   "<script type='text/javascript'>".
157                   "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });";
158     if ($this->submitButton) {
159       $this->tree.= "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });";
160     }
161     $this->tree.= "</script>";
163     $selected= $this->base == $this->releaseBase?"Selected":"";
164     $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";
165     $first= true;
166     $last_indent= 2;
168     foreach ($this->pathMapping as $base => $dummy) {
170       // Skip root for tree
171       if ($base == $this->releaseBase) {
172         continue;
173       }
175       // Build path style display
176       $elements= explode(',', substr($base, 0, strlen($base) - strlen($this->releaseBase)));
178       $indent= count($elements);
179       if (!$first && ($indent == $last_indent)) {
180         $this->tree.= "</li>\n";
181       }
182       if ($indent > $last_indent) {
183         $this->tree.= "<ul>\n";
184       }
185       if ($indent < $last_indent) {
186         for ($i= 0; $i < ($last_indent-$indent); $i++) {
187           $this->tree.= "</li></ul>\n";
188         }
189         $this->tree.= "</li>\n";
190       }
191       $selected= $this->base == $base?" class='treeListSelected'":"";
192       $link= "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($base)."';$('submit_tree_base_".$this->pid."').click();\"";
194       $this->tree.= "<li>".
195                     "<a$selected $link>".
196                     $this->gennonbreaks($this->releaseInfo[$base]['name']).
197                     ($this->releaseInfo[$base]['description']==''?'':'&nbsp;<span class="informal">['.$this->gennonbreaks($this->releaseInfo[$base]['description']).']</span>').
198                     "</a>";
200       $last_indent= $indent;
201       $first= false;
202     }
204     // Close tree
205     for ($i= 1; $i<$last_indent; $i++) {
206       $this->tree.= "</li></ul>\n";
207     }
208     $this->tree.= "</div>\n";
210     // Draw submitter if required
211     if ($this->submitButton) {
212       $this->tree.= image('images/lists/submit.png', "submit_base_".$this->pid, _("Submit"));
213     }
214     $this->tree.= "<input type='submit' style='display:none' name='submit_tree_base_".$this->pid."' id='submit_tree_base_".$this->pid."'>";
215     $this->tree.= "<input type='hidden' name='bs_rebase_".$this->pid."' id='bs_rebase_".$this->pid."'>";
216     $this->tree.= "<input type='hidden' name='BPID' id='BPID' value='".$this->pid."'>";
218     $this->lastState= true;
219     return true;
220   }
223   function gennonbreaks($string)
224   {
225     return str_replace('-', '&#8209;', str_replace(' ', '&nbsp;', $string));
226   }
229   function render()
230   {
231     return $this->tree;
232   }
235   function getBase()
236   {
237     return $this->base;
238   }
241   function getAction()
242   {
243     // Do not do anything if this is not our BPID, or there's even no BPID available...
244     if(!isset($_REQUEST['BPID']) || $_REQUEST['BPID'] != $this->pid) {
245       return;
246     }
248     if ($this->action) {
249       return array("targets" => array($this->base), "action" => $this->action);
250     }
252     return null;
253   }
257 ?>