FusionDirectory
class_templateDialog.inc
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2013-2020 FusionDirectory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
26 {
27  protected $management;
28  protected $type;
29  protected $template = NULL;
30  protected $templates;
31  protected $target = NULL;
32  protected $closed = FALSE;
33 
34  protected $tabObject;
35 
36  protected $post_finish = 'template_continue';
37  protected $post_cancel = 'template_cancel';
38 
39  function __construct ($management, $type, $dn = NULL, $target = NULL)
40  {
41  $this->management = $management;
42  $this->type = $type;
43  $this->templates = objects::getTemplates($this->type);
44  if ($dn !== NULL) {
45  if (isset($this->templates[$dn])) {
46  $this->template = new template($this->type, $dn);
47  } else {
48  trigger_error('Unknown template "' . $dn . '"');
49  }
50  }
51  $this->target = $target;
52  }
53 
54  function readPost ()
55  {
56  if (isset($_POST[$this->post_cancel])) {
57  $this->handleCancel();
58  return;
59  }
60 
61  if (($this->target === NULL) &&
62  is_object($this->template) &&
63  (isset($_POST[$this->post_finish]) || isset($_GET[$this->post_finish]))
64  ) {
65  $this->template->readPost();
66  $this->template->update();
67  $this->handleFinish();
68  return;
69  }
70 
71  if (
72  isset($_POST['template']) &&
73  isset($this->templates[$_POST['template']])
74  ) {
75  if (is_object($this->template)) {
76  trigger_error('redefining template object');
77  }
78  $this->template = new template($this->type, $_POST['template']);
79  /* This method can loop if there are several targets */
80  unset($_POST['template']);
81  }
82  if (is_object($this->template)) {
83  $this->template->readPost();
84  $this->template->update();
85  if ($this->target !== NULL) {
86  $this->management->openTabObject($this->template->apply($this->target));
87  $this->management->handleTemplateApply();
88  return;
89  } else {
90  if (empty($this->template->getNeeded())) {
91  $this->handleFinish();
92  return;
93  }
94  }
95  }
96  }
97 
98  public function update (): bool
99  {
100  return !$this->closed;
101  }
102 
103  function setNextTarget ($target)
104  {
105  $this->target = $target;
106  $this->template->reset();
107  }
108 
109  public function render (): string
110  {
111  $smarty = get_smarty();
112  if (is_object($this->template)) {
113  $templateOutput = $this->template->render();
114  if ($this->template->dialogOpened()) {
115  return $templateOutput;
116  } else {
117  $smarty->assign('template_dialog', $templateOutput);
118  }
119  } else {
120  $smarty->assign('templates', $this->templates);
121  }
122  $display = $smarty->fetch(get_template_path('template.tpl'));
123  return $display;
124  }
125 
131  protected function handleFinish ()
132  {
133  $this->management->closeDialogs();
134  $tab = $this->template->apply();
135  $errors = $tab->save();
136 
137  if (!empty($errors)) {
138  $this->management->openTabObject($tab);
139  msg_dialog::displayChecks($errors);
140  }
141  }
142 
143 
144  protected function handleCancel ()
145  {
146  $this->management->removeLocks();
147  $this->closed = TRUE;
148  }
149 }
Class for applying a template.
get_template_path($filename='', $plugin=FALSE, $path='')
Return themed path for specified base file.
Definition: functions.inc:174
This interface should be implemented by all dialog classes in FusionDirectory.
& get_smarty()
Get global smarty object.
Definition: functions.inc:324
update()
Update state and return FALSE if the dialog was closed.
readPost()
Interpret POST content.
removeLocks()
Removes ldap object locks created by this class. Whenever an object is edited, we create locks to avo...
Template dialog handling.
Management base class.
closeDialogs()
This method closes dialogs and cleans up the cached object info and the ui.
render()
Render the dialog and returns the HTML code.