Code

0de55ac3293bfe7fe1bdaba67dfa349568d1375f
[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 $submitButton= true;
29   protected $tree;
30   protected $pathMapping;
32   // It would be better to get a dn -> [name, description, type] array
33   // to avoid these tasks be done several times. Skipping for the moment.
34   #'dc' => 'plugins/departments/images/domain.png',
35   #'dc' => 'plugins/departments/images/dc.png',
36   #'l' => 'plugins/departments/images/locality.png',
37   #'c' => 'plugins/departments/images/country.png',
38   #'o' => 'plugins/departments/images/organization.png',
39   #'ou' => 'plugins/departments/images/folder.png',
41   function __construct($bases, $base= "")
42   {
43     // Initialize pid
44     $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
46     // Transfer data
47     $this->setBases($bases);
48     $this->setBase($base);
49   }
52   function setSubmitButton($flag)
53   {
54     $this->submitButton= $flag;
55   }
58   function setBase($base)
59   {
60     if (isset($this->pathMapping[$base])) {
61       $this->base= $base;
62       $this->update(true);
63     }
64   }
67   function setBases($bases)
68   {
69     global $config;
71     $this->pathMapping= array();
72     $selected= $this->base == $config->current['BASE']?"Selected":"";
73     $first= true;
74     $last_indent= 2;
76     foreach ($bases as $base => $dummy) {
78       // Build path style display
79       $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
80       $elements= array_reverse($elements, true);
82       $this->pathMapping[$base]= $base == $config->current['BASE']? '/' : ldap::fix(preg_replace('/(^|,)[a-z0-9]+=/i', '/', implode(',', $elements)));
83     }
85     // Save bases to session for autocompletion
86     session::global_set('pathMapping', $this->pathMapping);
87   }
90   function update($force= false)
91   {
92     global $config;
94     // Analyze for base changes if needed
95     $this->action= null;
96     $last_base= $this->base;
97     if(isset($_REQUEST['PID']) && $_REQUEST['PID'] == $this->pid) {
98       if (isset($_REQUEST['REBASE'])) {
99         $new_base= base64_decode($_REQUEST['REBASE']);
100         if (isset($this->pathMapping[$new_base])) {
101           $this->base= $new_base;
102           $this->action= 'rebase';
103         } else {
104           die ("Base mismatch!");
105         }
106       }
107     } elseif (isset($_POST['bs_input_'.$this->pid])) {
109       // Check if base is available
110       foreach ($this->pathMapping as $key => $path) {
111         if (mb_strtolower($path) == mb_strtolower($_POST['bs_input_'.$this->pid])) {
112           $this->base= $key;
113           break;
114         }
115       }
117     }
119     /* Skip if there's no change */
120     if (($this->tree && $this->base == $last_base) && !$force) {
121       return;
122     }
124     $link= "href='?plug=".$_GET['plug']."&amp;PID=".$this->pid."&amp;REBASE=".base64_encode($config->current['BASE'])."'";
125     $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]."'>";
128     // Autocompleter
129     $this->tree.= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
130                   "<script type='text/javascript'>".
131                   "new Ajax.Autocompleter('bs_input_".$this->pid."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });".
132                   "$('bs_input_".$this->pid."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });".
133                   "</script>";
135     $selected= $this->base == $config->current['BASE']?"Selected":"";
136     $this->tree.= "<div class='treeList' style='display:none' 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";
137     $first= true;
138     $last_indent= 2;
140     foreach ($this->pathMapping as $base => $dummy) {
142       // Skip root for tree
143       if ($base == $config->current['BASE']) {
144         continue;
145       }
147       // Build path style display
148       $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
150       $indent= count($elements);
151       if (!$first && ($indent == $last_indent)) {
152         $this->tree.= "</li>\n";
153       }
154       if ($indent > $last_indent) {
155         $this->tree.= "<ul>\n";
156       }
157       if ($indent < $last_indent) {
158         for ($i= 0; $i < ($last_indent-$indent); $i++) {
159           $this->tree.= "</li></ul>\n";
160         }
161         $this->tree.= "</li>\n";
162       }
163       $selected= $this->base == $base?" class='treeListSelected'":"";
164       $link= "href='?plug=".$_GET['plug']."&amp;PID=".$this->pid."&amp;REBASE=".base64_encode($base)."'";
165       $this->tree.= "<li><a$selected $link>".str_replace(' ', '&nbsp;', ldap::fix(preg_replace('/^[a-z0-9]+=([^,]+),.*$/i', '$1', $base)))."</a>";
167       $last_indent= $indent;
168       $first= false;
169     }
171     // Close tree
172     for ($i= 0; $i<$last_indent; $i++) {
173       $this->tree.= "</li></ul>\n";
174     }
175     $this->tree.= "</div>\n";
177     // Draw submitter if required
178     if ($this->submitButton) {
179       $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")."'>";
180     }
182   }
184   function render()
185   {
186     return $this->tree;
187   }
190   function getBase()
191   {
192     return $this->base;
193   }
196   function getAction()
197   {
198     // Do not do anything if this is not our PID, or there's even no PID available...
199     if(!isset($_REQUEST['PID']) || $_REQUEST['PID'] != $this->pid) {
200       return;
201     }
203     if ($this->action) {
204       return array("targets" => array($this->base), "action" => $this->action);
205     }
207     return null;
208   }
212 ?>