FusionDirectory
 All Data Structures Files Functions Variables
class_template.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) 2014-2016 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 
27 class template
28 {
29  protected $type;
30  protected $dn;
31  protected $needed;
32  protected $attrs;
33  protected $tabObject;
34  protected $attributes;
35 
36  protected $applied = FALSE;
37 
38  static function plInfo()
39  {
40  return array(
41  'plShortName' => _('Template'),
42  'plDescription' => _('Object template, used to create several objects with similar values'),
43  /* Categories for templates are computed in config class */
44  'plCategory' => array(),
45 
46  'plProvidedAcls' => array(
47  'template_cn' => _('Template name')
48  )
49  );
50  }
51 
52  static function getTemplatedTypes()
53  {
54  $result = array();
55  $types = objects::types();
56  foreach ($types as $type) {
57  if (in_array($type, departmentManagement::getDepartmentTypes())) {
58  continue;
59  }
60  $infos = objects::infos($type);
61  if ($infos['templateActive']) {
62  $result[$type] = $infos['name'];
63  }
64  }
65  asort($result);
66  return $result;
67  }
68 
69  function __construct($type, $dn, $targetdn = NULL)
70  {
71  $this->type = $type;
72  $this->dn = $dn;
73  list($this->attrs, $depends) = templateHandling::fetch($this->dn);
74  $this->needed = templateHandling::neededAttrs($this->attrs, $depends);
75  $this->needed[] = 'base';
76  if ($targetdn === NULL) {
77  $this->tabObject = objects::create($this->type);
78  } else {
79  trigger_error("This should not be used for now");
80  $this->tabObject = objects::open($this->dn, $this->type);
81  }
82  $tempTabObject = objects::open($this->dn, $this->type); /* Used to know which tab is activated */
83  $this->attributes = array();
84  foreach ($tempTabObject->by_object as $class => &$plugin) {
85  if ($plugin->is_account || $plugin->ignore_account) {
86  $this->tabObject->by_object[$class]->is_account = $plugin->is_account;
87  $this->attributes[$class] = array();
88  $attrs = array_unique(array_merge($this->tabObject->by_object[$class]->getRequiredAttributes(), $this->needed));
89  foreach (array_keys($this->tabObject->by_object[$class]->attributesAccess) as $attr) {
90  if (!$plugin->showInTemplate($attr, $this->attrs)) {
91  continue;
92  }
93  if (in_array($attr, $attrs)) {
94  $this->attributes[$class][] = $attr;
95  }
96  }
97  }
98  }
99  }
100 
102  function reset()
103  {
104  list($this->attrs, $depends) = templateHandling::fetch($this->dn);
105  // This is needed because it removes %askme% values from attrs
106  $this->needed = templateHandling::neededAttrs($this->attrs, $depends);
107  $this->needed[] = 'base';
108  $this->tabObject = objects::create($this->type);
109  /* Used to know which tab is activated */
110  $tempTabObject = objects::open($this->dn, $this->type);
111  foreach ($tempTabObject->by_object as $class => &$plugin) {
112  if ($plugin->is_account || $plugin->ignore_account) {
113  $this->tabObject->by_object[$class]->is_account = $plugin->is_account;
114  }
115  }
116  $this->applied = FALSE;
117  }
118 
119  function getDn()
120  {
121  return $this->dn;
122  }
123 
124  function getBase()
125  {
126  if (is_object($this->tabObject)) {
127  return $this->tabObject->getBaseObject()->base;
128  } else {
129  $infos = objects::infos($this->type);
130  return dn2base($this->dn, 'ou=templates,'.$infos['ou']);
131  }
132  }
133 
134  function getNeeded()
135  {
136  return $this->attributes;
137  }
138 
139  function serialize()
140  {
141  $attributes = array();
142  foreach ($this->tabObject->by_object as $class => &$plugin) {
143  if (!isset($this->attributes[$class])) {
144  continue;
145  }
146  $attributes[$class] = array('name' => $this->tabObject->by_name[$class], 'attrs' => array());
147  foreach ($this->attributes[$class] as $attr) {
148  $plugin->attributesAccess[$attr]->serializeAttribute($attributes[$class]['attrs'], FALSE);
149  }
150  $attributes[$class]['attrs_order'] = array_keys($attributes[$class]['attrs']);
151  }
152 
153  return $attributes;
154  }
155 
156  function deserialize($values)
157  {
158  foreach ($values as $class => $class_values) {
159  $result = $this->tabObject->by_object[$class]->deserializeValues($class_values);
160  if ($result !== TRUE) {
161  return $result;
162  }
163  }
164  return TRUE;
165  }
166 
167  function save_object()
168  {
169  foreach ($this->tabObject->by_object as $plugin) {
170  $plugin->save_object();
171  }
172  }
173 
174  function execute()
175  {
176  $smarty = get_smarty();
177  $sections = array();
178  $posted = array();
179  $smarty->assign('baseACL', 'rw');
180  foreach ($this->tabObject->by_object as $class => &$plugin) {
181  if (!isset($this->attributes[$class])) {
182  continue;
183  }
184  $attributes = array();
185  foreach ($this->attributes[$class] as $attr) {
186  if ($plugin->attributesAccess[$attr]->getAclInfo() !== FALSE) {
187  // We assign ACLs so that attributes can use them in their template code
188  $smarty->assign($plugin->attributesAccess[$attr]->getAcl().'ACL', $plugin->aclGetPermissions($plugin->attributesAccess[$attr]->getAcl()));
189  }
190  $plugin->attributesAccess[$attr]->renderAttribute($attributes, FALSE);
191  }
192 
193  $smarty->assign('section', $class);
194  $smarty->assign('sectionId', $class);
195  $smarty->assign('sectionClasses', ' fullwidth');
196  $smarty->assign('attributes', $attributes);
197 
198  $posted[] = $class.'_posted';
199  $sections[] = $smarty->fetch(get_template_path('simpleplugin_section.tpl'));
200  }
201  unset($plugin);
202 
203  $smarty->assign('sections', $sections);
204  $smarty->assign('hiddenPostedInput', $posted);
205  $smarty->assign('focusedField', '');
206 
207  return $smarty->fetch(get_template_path('simpleplugin.tpl'));
208  }
209 
210  /* Apply template and current values to an object and returns it for saving or edition
211  * Cannot be called twice! If you need to, call reset between calls */
212  function apply($targetdn = NULL)
213  {
214  if ($this->applied) {
215  trigger_error('Templates can’t be applied twice without calling reset before');
216  return;
217  }
218 
219  if ($targetdn !== NULL) {
220  $this->tabObject = objects::open($targetdn, $this->type);
221  unset($this->attrs['objectClass']['count']);
222  foreach ($this->tabObject->by_object as $class => &$plugin) {
223  if ($plugin->is_account || $plugin->ignore_account) {
224  $this->attrs['objectClass'] = $plugin->mergeObjectClasses($this->attrs['objectClass']);
225  }
226  }
227  }
228 
229  foreach ($this->tabObject->by_object as $class => &$plugin) {
230  if (!isset($this->attributes[$class])) {
231  continue;
232  }
233  foreach ($this->attributes[$class] as $attr) {
234  $plugin->attributesAccess[$attr]->fillLdapValue($this->attrs);
235  }
236  }
237  unset($plugin);
238  foreach ($this->tabObject->by_object as $class => &$plugin) {
239  if (!isset($this->attributes[$class])) {
240  continue;
241  }
242  foreach ($this->attributes[$class] as $attr) {
243  $plugin->attributesAccess[$attr]->fillLdapValueHook($this->attrs);
244  }
245  }
246  unset($plugin);
247  foreach ($this->attrs as &$array) {
248  if (!is_array($array)) {
249  $array = array($array);
250  }
251  if (!isset($array['count'])) {
252  $array['count'] = count($array);
253  }
254  }
255  unset($array);
256 
257  $this->attrs = templateHandling::parseArray($this->attrs);
258  $this->tabObject->adapt_from_template($this->attrs, call_user_func_array('array_merge', $this->attributes));
259 
260  $this->applied = TRUE;
261  return $this->tabObject;
262  }
263 }
Class for applying a template.
get_template_path($filename= '', $plugin=FALSE, $path= '')
Return themed path for specified base file.
Definition: functions.inc:296
reset()
Used when you need to re-apply the same template with different values.
static fetch($dn)
Fetch a template from LDAP and returns its attributes and dependencies information.
& get_smarty()
Get global smarty object.
Definition: functions.inc:934
static neededAttrs(array &$attrs, array $flatdepends)
Return attrs needed before applying template.
dn2base($dn, $ou=NULL)
Return the base of a given DN.
Definition: functions.inc:1295
static open($dn, $type)
Create the tab object for the given dn.
static parseArray(array $attrs)
Parse template masks in an array.