Code

375fe207ac3ed59c683c87c47632529850113b51
[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   protected $tree;
29   protected $pathMapping;
31   // It would be better to get a dn -> [name, description, type] array
32   // to avoid these tasks be done several times. Skipping for the moment.
33   #'dc' => 'plugins/departments/images/domain.png',
34   #'dc' => 'plugins/departments/images/dc.png',
35   #'l' => 'plugins/departments/images/locality.png',
36   #'c' => 'plugins/departments/images/country.png',
37   #'o' => 'plugins/departments/images/organization.png',
38   #'ou' => 'plugins/departments/images/folder.png',
40   function __construct($bases, $base= "")
41   {
42     // Initialize pid
43     $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
45     // Transfer data
46     $this->setBases($bases);
47     $this->setBase($base);
48   }
51   function setBase($base)
52   {
53     if (isset($this->pathMapping[$base])) {
54       $this->base= $base;
55       $this->update(true);
56     } else {
57       die("Invalid base selected");
58     }
59   }
62   function setBases($bases)
63   {
64     global $config;
66     $this->pathMapping= array();
67     $selected= $this->base == $config->current['BASE']?"Selected":"";
68     $first= true;
69     $last_indent= 2;
71     foreach ($bases as $base => $dummy) {
73       // Build path style display
74       $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
75       $elements= array_reverse($elements, true);
77       $this->pathMapping[$base]= $base == $config->current['BASE']? '/' : ldap::fix(preg_replace('/(^|,)[a-z0-9]+=/i', '/', implode(',', $elements)));
78     }
80     // Save bases to session for autocompletion
81     session::global_set('pathMapping', $this->pathMapping);
82   }
85   function update($force= false)
86   {
87     global $config;
89     // Analyze for base changes if needed
90     $this->action= null;
91     $last_base= $this->base;
92     if(isset($_REQUEST['PID']) && $_REQUEST['PID'] == $this->pid) {
93       if (isset($_REQUEST['REBASE'])) {
94         $new_base= base64_decode($_REQUEST['REBASE']);
95         if (isset($this->pathMapping[$new_base])) {
96           $this->base= $new_base;
97           $this->action= 'rebase';
98         } else {
99           die ("Base mismatch!");
100         }
101       }
102     }
104     /* Skip if there's no change */
105     if (($this->tree && $this->base == $last_base) && !$force) {
106       return;
107     }
109     $link= "href='?plug=".$_GET['plug']."&amp;PID=".$this->pid."&amp;REBASE=".base64_encode($config->current['BASE'])."'";
110     $this->tree= "<input type='text' size='35' name='bs_input_".$this->pid."' id='bs_input_".$this->pid."' 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]."'>";
112     $selected= $this->base == $config->current['BASE']?"Selected":"";
113     $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";
114     $first= true;
115     $last_indent= 2;
117     foreach ($this->pathMapping as $base => $dummy) {
119       // Skip root for tree
120       if ($base == $config->current['BASE']) {
121         continue;
122       }
124       // Build path style display
125       $elements= explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
127       $indent= count($elements);
128       if (!$first && ($indent == $last_indent)) {
129         $this->tree.= "</li>\n";
130       }
131       if ($indent > $last_indent) {
132         $this->tree.= "<ul>\n";
133       }
134       if ($indent < $last_indent) {
135         for ($i= 0; $i < ($last_indent-$indent); $i++) {
136           $this->tree.= "</li></ul>\n";
137         }
138         $this->tree.= "</li>\n";
139       }
140       $selected= $this->base == $base?" class='treeListSelected'":"";
141       $link= "href='?plug=".$_GET['plug']."&amp;PID=".$this->pid."&amp;REBASE=".base64_encode($base)."'";
142       $this->tree.= "<li><a$selected $link>".str_replace(' ', '&nbsp;', ldap::fix(preg_replace('/^[a-z0-9]+=([^,]+),.*$/i', '$1', $base)))."</a>";
144       $last_indent= $indent;
145       $first= false;
146     }
148     // Close tree
149     for ($i= 0; $i<$last_indent; $i++) {
150       $this->tree.= "</li></ul>\n";
151     }
152     $this->tree.= "</div>\n";
153   }
155   function render()
156   {
157     return $this->tree;
158   }
161   function getBase()
162   {
163     return $this->base;
164   }
167   function getAction()
168   {
169     // Do not do anything if this is not our PID, or there's even no PID available...
170     if(!isset($_REQUEST['PID']) || $_REQUEST['PID'] != $this->pid) {
171       return;
172     }
174     if ($this->action) {
175       return array("targets" => array($this->base), "action" => $this->action);
176     }
178     return null;
179   }
183 ?>