FusionDirectory
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 
5  Copyright (C) 2003-2010 Cajus Pollmeier
6  Copyright (C) 2011-2020 FusionDirectory
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22 
32 {
33  private $base = '';
34  protected $pid;
35  private $action;
36  private $height = 500;
37  private $submitButton = TRUE;
38  protected $tree = NULL;
39  protected $pathMapping = [];
40  protected $lastState;
41 
49  function __construct (array $bases, string $base = '')
50  {
51  // Initialize pid
52  $this->pid = preg_replace("/[^0-9]/", "", microtime(TRUE));
53 
54  // Transfer data
55  $this->setBases($bases);
56  $this->setBase($base);
57  }
58 
62  function getInputHtmlId (): string
63  {
64  return 'bs_input_'.$this->pid;
65  }
66 
72  function setSubmitButton ($flag)
73  {
74  $this->submitButton = $flag;
75  }
76 
82  function setHeight ($value)
83  {
84  $this->height = $value;
85  }
86 
92  function setBase (string $base): bool
93  {
94  if (isset($this->pathMapping[$base])) {
95  $this->base = $base;
96  $this->lastState = TRUE;
97  return $this->update(TRUE);
98  } else {
99  $this->lastState = FALSE;
100  return FALSE;
101  }
102  }
103 
109  function checkLastBaseUpdate (): bool
110  {
111  return $this->lastState;
112  }
113 
119  function setBases (array $bases)
120  {
121  global $config;
122 
123  $this->pathMapping = [];
124 
125  foreach ($bases as $base => $dummy) {
126  // Build path style display
127  $elements = explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
128  $elements = array_reverse($elements, TRUE);
129 
130  $this->pathMapping[$base] = (($base == $config->current['BASE']) ? '/' : preg_replace('/(^|,)[a-z0-9]+=/i', '/', implode(',', $elements)));
131  }
132 
133  // Save bases to session for autocompletion
134  session::set('pathMapping', $this->pathMapping);
135  }
136 
142  function update (bool $force = FALSE): bool
143  {
144  if (!isset($this->base) || ($this->base == '')) {
145  $this->lastState = FALSE;
146  return FALSE;
147  }
148 
149  // Analyze for base changes if needed
150  $this->action = NULL;
151  $last_base = $this->base;
152  if (isset($_REQUEST['BPID']) && $_REQUEST['BPID'] == $this->pid) {
153  if (!empty($_POST['bs_rebase_'.$this->pid])) {
154  $new_base = base64_decode($_POST['bs_rebase_'.$this->pid]);
155  if (isset($this->pathMapping[$new_base])) {
156  $this->base = $new_base;
157  $this->action = 'rebase';
158  } else {
159  $this->lastState = FALSE;
160  return FALSE;
161  }
162  } elseif (isset($_POST[$this->getInputHtmlId()])) {
163  // Take over input field base
164  if (($this->submitButton && isset($_POST['submit_base_'.$this->pid.'_x'])) || !$this->submitButton) {
165 
166  // Check if base is available
167  $this->lastState = FALSE;
168  foreach ($this->pathMapping as $key => $path) {
169  if (mb_strtolower($path) == mb_strtolower($_POST[$this->getInputHtmlId()])) {
170  $this->base = $key;
171  $this->lastState = TRUE;
172  break;
173  }
174  }
175  }
176  }
177  }
178 
179  /* Skip if there's no change */
180  if ($this->tree && ($this->base == $last_base) && !$force) {
181  $this->lastState = TRUE;
182  return TRUE;
183  }
184 
185  /* Force tree render next time render() is called */
186  $this->tree = NULL;
187 
188  $this->lastState = TRUE;
189  return TRUE;
190  }
191 
192  protected function renderTree ()
193  {
194  global $config;
195 
196  /* Build tree */
197  $departmentInfo = $config->getDepartmentInfo();
198  $tree = [];
199  foreach (array_keys($this->pathMapping) as $base) {
200  if ($base == $config->current['BASE']) {
201  /* Skip root */
202  continue;
203  }
204 
205  $elements = explode(',', substr($base, 0, strlen($base) - strlen($config->current['BASE'])));
206  /* Remove last one */
207  array_pop($elements);
208  /* Remove first one */
209  array_shift($elements);
210 
211  $array =& $tree;
212  $elementBase = $config->current['BASE'];
213  foreach (array_reverse($elements) as $element) {
214  $elementBase = $element.','.$elementBase;
215  if (!isset($array[$elementBase])) {
216  /* Our parent is missing, add it but without link */
217  $array[$elementBase] = [
218  'tree' => [],
219  'selected' => FALSE,
220  'link' => FALSE,
221  'img' => $departmentInfo[$elementBase]['img'],
222  'name' => $departmentInfo[$elementBase]['name'],
223  'description' => $departmentInfo[$elementBase]['description'],
224  ];
225  }
226  /* Go down one level */
227  $array =& $array[$elementBase]['tree'];
228  }
229 
230  $array[$base] = [
231  'tree' => [],
232  'selected' => ($this->base == $base),
233  'link' => TRUE,
234  'img' => $departmentInfo[$base]['img'],
235  'name' => $departmentInfo[$base]['name'],
236  'description' => $departmentInfo[$base]['description'],
237  ];
238  }
239 
240  $smarty = get_smarty();
241  $smarty->assign('htmlid', $this->getInputHtmlId());
242  $smarty->assign('pid', $this->pid);
243  $smarty->assign('currentValue', $this->pathMapping[$this->base]);
244  $smarty->assign('submitButton', $this->submitButton);
245  $smarty->assign('height', $this->height);
246  $smarty->assign('selected', ($this->base == $config->current['BASE']));
247  $smarty->assign('rootBase', $config->current['BASE']);
248  $smarty->assign('tree', $tree);
249 
250  $this->tree = $smarty->fetch(get_template_path('baseselector.tpl'));
251  }
252 
258  function render (): string
259  {
260  if (!isset($this->tree)) {
261  $this->renderTree();
262  }
263 
264  return $this->tree;
265  }
266 
272  function getBase (): string
273  {
274  return $this->base;
275  }
276 
282  function getBases (): array
283  {
284  return $this->pathMapping;
285  }
286 }
render()
Accessor of the member tree.
setSubmitButton($flag)
Set a new flag to the submit button.
get_template_path($filename='', $plugin=FALSE, $path='')
Return themed path for specified base file.
Definition: functions.inc:174
__construct(array $bases, string $base='')
baseSelector contructor
update(bool $force=FALSE)
Update the base.
static set($name, $value)
Set a value in a session.
Class Base Selector.
checkLastBaseUpdate()
Check the last base value updated.
& get_smarty()
Get global smarty object.
Definition: functions.inc:324
setHeight($value)
Set a new value of the member height.
getBase()
Accessor of the base.
getInputHtmlId()
Returns id of the html field.
getBases()
Accessor of the bases.
setBases(array $bases)
Set new bases.
setBase(string $base)
Set a new value of the member base.