FusionDirectory
 All Data Structures Files Functions Variables
class_baseSelector.inc
Go to the documentation of this file.
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2003-2010 Cajus Pollmeier
5  Copyright (C) 2011-2017 FusionDirectory
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21 
31 {
32  private $base;
33  protected $pid;
34  private $action;
35  private $height = 500;
36  private $submitButton = TRUE;
37  protected $tree;
38  protected $pathMapping;
39  protected $lastState;
40 
48  function __construct($bases, $base = "")
49  {
50  // Initialize pid
51  $this->pid = preg_replace("/[^0-9]/", "", microtime(TRUE));
52 
53  // Transfer data
54  $this->setBases($bases);
55  $this->setBase($base);
56  }
57 
61  function getInputHtmlId()
62  {
63  return 'bs_input_'.$this->pid;
64  }
65 
71  function setSubmitButton($flag)
72  {
73  $this->submitButton = $flag;
74  }
75 
81  function setHeight($value)
82  {
83  $this->height = $value;
84  }
85 
91  function setBase($base)
92  {
93  if (isset($this->pathMapping[$base])) {
94  $this->base = $base;
95  $this->lastState = TRUE;
96  return $this->update(TRUE);
97  } else {
98  $this->lastState = FALSE;
99  return FALSE;
100  }
101  }
102 
109  {
110  return $this->lastState;
111  }
112 
118  function setBases($bases)
119  {
120  global $config;
121 
122  $this->pathMapping = array();
123 
124  foreach ($bases as $base => $dummy) {
125  // Build path style display
126  $elements = explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
127  $elements = array_reverse($elements, TRUE);
128 
129  $this->pathMapping[$base] = (($base == $config->current['BASE']) ? '/' : preg_replace('/(^|,)[a-z0-9]+=/i', '/', implode(',', $elements)));
130  }
131 
132  // Save bases to session for autocompletion
133  session::global_set('pathMapping', $this->pathMapping);
134  }
135 
141  function update($force = FALSE)
142  {
143  if (!isset($this->base) || ($this->base == '')) {
144  $this->lastState = FALSE;
145  return FALSE;
146  }
147 
148  // Analyze for base changes if needed
149  $this->action = NULL;
150  $last_base = $this->base;
151  if (isset($_REQUEST['BPID']) && $_REQUEST['BPID'] == $this->pid) {
152  if (!empty($_POST['bs_rebase_'.$this->pid])) {
153  $new_base = base64_decode($_POST['bs_rebase_'.$this->pid]);
154  if (isset($this->pathMapping[$new_base])) {
155  $this->base = $new_base;
156  $this->action = 'rebase';
157  } else {
158  $this->lastState = FALSE;
159  return FALSE;
160  }
161  } elseif (isset($_POST[$this->getInputHtmlId()])) {
162  // Take over input field base
163  if (($this->submitButton && isset($_POST['submit_base_'.$this->pid.'_x'])) || !$this->submitButton) {
164 
165  // Check if base is available
166  $this->lastState = FALSE;
167  foreach ($this->pathMapping as $key => $path) {
168  if (mb_strtolower($path) == mb_strtolower($_POST[$this->getInputHtmlId()])) {
169  $this->base = $key;
170  $this->lastState = TRUE;
171  break;
172  }
173  }
174  }
175  }
176  }
177 
178  /* Skip if there's no change */
179  if ($this->tree && ($this->base == $last_base) && !$force) {
180  $this->lastState = TRUE;
181  return TRUE;
182  }
183 
184  $this->renderTree();
185 
186  $this->lastState = TRUE;
187  return TRUE;
188  }
189 
190  protected function renderTree()
191  {
192  global $config;
193  $link = "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($config->current['BASE'])."'; $('submit_tree_base_".$this->pid."').click();\"";
194  $this->tree = '<input class="base-selector" type="text" name="'.$this->getInputHtmlId().'"'.
195  ' id="'.$this->getInputHtmlId().'"'.
196  ' onkeydown="$(\'bs_'.$this->pid.'\').hide()"'.
197  ' onfocus="$(\'bs_'.$this->pid.'\').hide()"'.
198  ' onmouseover="Element.clonePosition($(\'bs_'.$this->pid.'\'), \''.$this->getInputHtmlId().'\', {setHeight: false, setWidth: false, offsetTop:(Element.getHeight(\''.$this->getInputHtmlId().'\'))});$(\'bs_'.$this->pid.'\').show();"'.
199  ' onmouseout="rtimer= Element.hide.delay(0.25, \'bs_'.$this->pid.'\')"'.
200  ' value="'.htmlentities($this->pathMapping[$this->base], ENT_COMPAT, 'UTF-8').'"/>';
201 
202  // Autocompleter
203  $this->tree .= "<div id='autocomplete_".$this->pid."' class='autocomplete'></div>".
204  "<script type='text/javascript'>".
205  "new Ajax.Autocompleter('".$this->getInputHtmlId()."', 'autocomplete_".$this->pid."', 'autocomplete.php?type=base', { minChars: 3, frequency: 0.5 });";
206  if ($this->submitButton) {
207  $this->tree .= "$('".$this->getInputHtmlId()."').observe('keypress', function(event) { if(event.keyCode == Event.KEY_RETURN) { $('submit_base_".$this->pid."').click(); } });";
208  }
209  $this->tree .= "</script>";
210 
211  $selected = ($this->base == $config->current['BASE'] ? 'Selected' : '');
212  $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";
213  $first = TRUE;
214  $last_indent = 2;
215 
216  foreach ($this->pathMapping as $base => $dummy) {
217  // Skip root for tree
218  if ($base == $config->current['BASE']) {
219  continue;
220  }
221 
222  // Build path style display
223  $elements = explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
224 
225  $indent = count($elements);
226  if (!$first && ($indent == $last_indent)) {
227  $this->tree .= "</li>\n";
228  } elseif ($indent > $last_indent) {
229  $this->tree .= "<ul>\n";
230  } elseif ($indent < $last_indent) {
231  for ($i = 0; $i < ($last_indent - $indent); $i++) {
232  $this->tree .= "</li></ul>\n";
233  }
234  $this->tree .= "</li>\n";
235  }
236  $selected = ($this->base == $base ? ' class="treeListSelected"' : '');
237  $link = "onclick=\"\$('bs_rebase_".$this->pid."').value='".base64_encode($base)."';$('submit_tree_base_".$this->pid."').click();\"";
238  $this->tree .= "<li><a$selected $link>".
239  '<img class="center" '.
240  'src="'.htmlentities($config->department_info[$base]['img'], ENT_COMPAT, 'UTF-8').'" '.
241  'alt="'.htmlentities($config->department_info[$base]['name'], ENT_COMPAT, 'UTF-8').'"/>&nbsp;'.
242  $this->escape($config->department_info[$base]['name']).
243  (($config->department_info[$base]['description'] == '') ? '' : '&nbsp;<span class="informal">['.$this->escape($config->department_info[$base]['description']).']</span>').
244  '</a>';
245 
246  $last_indent = $indent;
247  $first = FALSE;
248  }
249 
250  // Close tree
251  for ($i = 1; $i < $last_indent; $i++) {
252  if (($i > 1) || (!$first)) {
253  $this->tree .= "</li>";
254  }
255  $this->tree .= "</ul>\n";
256  }
257  $this->tree .= "</div>\n";
258 
259  // Draw submitter if required
260  if ($this->submitButton) {
261  $this->tree .= "&nbsp;<input class='center' type='image' src='geticon.php?context=actions&amp;icon=submit&amp;size=16' title='"._("Submit")."' name='submit_base_".$this->pid."' id='submit_base_".$this->pid."' alt='"._("Submit")."'>";
262  }
263  $this->tree .= '<input type="submit" style="display:none" name="submit_tree_base_'.$this->pid.'" id="submit_tree_base_'.$this->pid.'"/>';
264  $this->tree .= '<input type="hidden" name="bs_rebase_'.$this->pid.'" id="bs_rebase_'.$this->pid.'"/>';
265  $this->tree .= '<input type="hidden" name="BPID" id="BPID" value="'.$this->pid.'"/>';
266  }
267 
268 
274  function escape($string)
275  {
276  return str_replace(' ', '&nbsp;', htmlentities($string, ENT_COMPAT, 'UTF-8'));
277  }
278 
284  function render()
285  {
286  return $this->tree;
287  }
288 
294  function getBase()
295  {
296  return $this->base;
297  }
298 
304  function getBases()
305  {
306  return $this->pathMapping;
307  }
308 }
309 ?>
render()
Accessor of the member tree.
setSubmitButton($flag)
Set a new flag to the submit button.
update($force=FALSE)
Update the base.
setBase($base)
Set a new value of the member base.
escape($string)
Replace all space of the string by non-breaking space and escapes HTML.
Class Base Selector.
checkLastBaseUpdate()
Check the last base value updated.
static global_set($name, $value)
Set a value in a session.
setHeight($value)
Set a new value of the member height.
getBase()
Accessor of the base.
__construct($bases, $base="")
baseSelector contructor
getInputHtmlId()
Returns id of the html field.
getBases()
Accessor of the bases.
setBases($bases)
Set new bases.