Code

Updated listing
[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;
33   // It would be better to get a dn -> [name, description, type] array
34   // to avoid these tasks be done several times. Skipping for the moment.
35   #'dc' => 'plugins/departments/images/domain.png',
36   #'dc' => 'plugins/departments/images/dc.png',
37   #'l' => 'plugins/departments/images/locality.png',
38   #'c' => 'plugins/departments/images/country.png',
39   #'o' => 'plugins/departments/images/organization.png',
40   #'ou' => 'plugins/departments/images/folder.png',
42   function __construct($bases, $base= "")
43   {
44     // Initialize pid
45     $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
47     // Transfer data
48     $this->setBases($bases);
49     $this->setBase($base);
50   }
53   function setSubmitButton($flag)
54   {
55     $this->submitButton= $flag;
56   }
59   function setHeight($value)
60   {
61     $this->height= $value;
62   }
65   function setBase($base)
66   {
67     if (isset($this->pathMapping[$base])) {
68       $this->base= $base;
69       $this->update(true);
70     }
71   }
74   function setBases($bases)
75   {
76     global $config;
78     $this->pathMapping= array();
79     $selected= $this->base == $config->current['BASE']?"Selected":"";
80     $first= true;
81     $last_indent= 2;
83     foreach ($bases as $base => $dummy) {
85       // Build path style display
86       $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
87       $elements= array_reverse($elements, true);
89       $this->pathMapping[$base]= $base == $config->current['BASE']? '/' : ldap::fix(preg_replace('/(^|,)[a-z0-9]+=/i', '/', implode(',', $elements)));
90     }
92     // Save bases to session for autocompletion
93     session::global_set('pathMapping', $this->pathMapping);
94   }
97   function update($force= false)
98   {
99     global $config;
101     // Analyze for base changes if needed
102     $this->action= null;
103     $last_base= $this->base;
104     if(isset($_REQUEST['PID']) && $_REQUEST['PID'] == $this->pid) {
105       if (isset($_REQUEST['REBASE'])) {
106         $new_base= base64_decode($_REQUEST['REBASE']);
107         if (isset($this->pathMapping[$new_base])) {
108           $this->base= $new_base;
109           $this->action= 'rebase';
110         } else {
111           return false;
112         }
113       }
114     } elseif (isset($_POST['bs_input_'.$this->pid])) {
116       // Check if base is available
117       foreach ($this->pathMapping as $key => $path) {
118         if (mb_strtolower($path) == mb_strtolower($_POST['bs_input_'.$this->pid])) {
119           $this->base= $key;
120           break;
121         }
122       }
124     }
126     /* Skip if there's no change */
127     if (($this->tree && $this->base == $last_base) && !$force) {
128       return true;
129     }
131     $link= "href='?plug=".$_GET['plug']."&amp;PID=".$this->pid."&amp;REBASE=".base64_encode($config->current['BASE'])."'";
132     $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='".$this->pathMapping[$this->base]."'>";
135     // Autocompleter
136     $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
137                   "<script type='text/javascript'>".
138                   "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });".
139                   "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });".
140                   "</script>";
142     $selected= $this->base == $config->current['BASE']?"Selected":"";
143     $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>/ ["._("Root")."]</a><ul class='treeList'>\n";
144     $first= true;
145     $last_indent= 2;
147     foreach ($this->pathMapping as $base => $dummy) {
149       // Skip root for tree
150       if ($base == $config->current['BASE']) {
151         continue;
152       }
154       // Build path style display
155       $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
157       $indent= count($elements);
158       if (!$first && ($indent == $last_indent)) {
159         $this->tree.= "</li>\n";
160       }
161       if ($indent > $last_indent) {
162         $this->tree.= "<ul>\n";
163       }
164       if ($indent < $last_indent) {
165         for ($i= 0; $i < ($last_indent-$indent); $i++) {
166           $this->tree.= "</li></ul>\n";
167         }
168         $this->tree.= "</li>\n";
169       }
170       $selected= $this->base == $base?" class='treeListSelected'":"";
171       $link= "href='?plug=".$_GET['plug']."&amp;PID=".$this->pid."&amp;REBASE=".base64_encode($base)."'";
172       $this->tree.= "<li><a$selected $link>".str_replace(' ', '&nbsp;', ldap::fix(preg_replace('/^[a-z0-9]+=([^,]+),.*$/i', '$1', $base)))."</a>";
174       $last_indent= $indent;
175       $first= false;
176     }
178     // Close tree
179     for ($i= 0; $i<$last_indent; $i++) {
180       $this->tree.= "</li></ul>\n";
181     }
182     $this->tree.= "</div>\n";
184     // Draw submitter if required
185     if ($this->submitButton) {
186       $this->tree.= "&nbsp;<input class='center' type='image' src='images/lists/submit.png' align='middle' title='"._("Submit")."' name='submit_base_".$this->pid."' id='submit_base_".$this->pid."' alt='"._("Submit")."'>";
187     }
189     return true;
190   }
192   function render()
193   {
194     return $this->tree;
195   }
198   function getBase()
199   {
200     return $this->base;
201   }
204   function getAction()
205   {
206     // Do not do anything if this is not our PID, or there's even no PID available...
207     if(!isset($_REQUEST['PID']) || $_REQUEST['PID'] != $this->pid) {
208       return;
209     }
211     if ($this->action) {
212       return array("targets" => array($this->base), "action" => $this->action);
213     }
215     return null;
216   }
220 ?>