class_template.inc 7.81 KB
Newer Older
1
2
3
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4
  Copyright (C) 2014-2016  FusionDirectory
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/

21
22
23
24
25
26
/*!
 * \file class_template.inc
 * Source code for the class template
 */

/*! \brief Class for applying a template */
27
28
29
30
31
32
33
34
35
class template
{
  protected $type;
  protected $dn;
  protected $needed;
  protected $attrs;
  protected $tabObject;
  protected $attributes;

36
37
  protected $applied = FALSE;

38
  static function plInfo()
39
40
41
42
43
44
45
46
47
48
49
50
  {
    return array(
      'plShortName'   => _('Template'),
      'plDescription' => _('Object template, used to create several objects with similar values'),
      'plCategory'    => array('user'),

      'plProvidedAcls' => array(
        'template_cn' => _('Template name')
      )
    );
  }

51
  function __construct($type, $dn, $targetdn = NULL)
52
53
54
  {
    $this->type = $type;
    $this->dn   = $dn;
55
56
    list($this->attrs, $depends) = templateHandling::fetch($this->dn);
    $this->needed     = templateHandling::neededAttrs($this->attrs, $depends);
57
    $this->needed[]   = 'base';
58
59
60
61
62
63
    if ($targetdn === NULL) {
      $this->tabObject  = objects::create($this->type);
    } else {
      trigger_error("This should not be used for now");
      $this->tabObject  = objects::open($this->dn, $this->type);
    }
64
    $tempTabObject    = objects::open($this->dn, $this->type); /* Used to know which tab is activated */
65
    $this->attributes = array();
66
    foreach ($tempTabObject->by_object as $class => &$plugin) {
67
      if ($plugin->is_account || $plugin->ignore_account) {
68
        $this->tabObject->by_object[$class]->is_account = $plugin->is_account;
69
        $this->attributes[$class] = array();
70
        $attrs = array_unique(array_merge($this->tabObject->by_object[$class]->getRequiredAttributes(), $this->needed));
71
        foreach (array_keys($this->tabObject->by_object[$class]->attributesAccess) as $attr) {
72
          if (!$plugin->showInTemplate($attr, $this->attrs)) {
73
74
            continue;
          }
75
          if (in_array($attr, $attrs)) {
76
77
            $this->attributes[$class][] = $attr;
          }
78
79
80
81
82
        }
      }
    }
  }

83
  /*! \brief Used when you need to re-apply the same template with different values */
84
85
  function reset()
  {
86
    list($this->attrs, $depends) = templateHandling::fetch($this->dn);
87
    // This is needed because it removes %askme% values from attrs
88
    $this->needed     = templateHandling::neededAttrs($this->attrs, $depends);
89
    $this->needed[]   = 'base';
90
    $this->tabObject  = objects::create($this->type);
91
92
    /* Used to know which tab is activated */
    $tempTabObject    = objects::open($this->dn, $this->type);
93
94
95
96
97
98
99
100
    foreach ($tempTabObject->by_object as $class => &$plugin) {
      if ($plugin->is_account || $plugin->ignore_account) {
        $this->tabObject->by_object[$class]->is_account = $plugin->is_account;
      }
    }
    $this->applied = FALSE;
  }

101
102
103
104
105
106
107
  function getDn()
  {
    return $this->dn;
  }

  function getBase()
  {
108
109
110
111
112
113
    if (is_object($this->tabObject)) {
      return $this->tabObject->getBaseObject()->base;
    } else {
      $infos = objects::infos($this->type);
      return dn2base($this->dn, 'ou=templates,'.$infos['ou']);
    }
114
115
  }

116
117
118
119
120
  function getNeeded()
  {
    return $this->attributes;
  }

121
  function serialize()
122
123
124
  {
    $attributes = array();
    foreach ($this->tabObject->by_object as $class => &$plugin) {
125
126
127
128
      if (!isset($this->attributes[$class])) {
        continue;
      }
      $attributes[$class] = array('name' => $this->tabObject->by_name[$class], 'attrs' => array());
129
      foreach ($this->attributes[$class] as $attr) {
130
        $plugin->attributesAccess[$attr]->serializeAttribute($attributes[$class]['attrs'], FALSE);
131
      }
132
      $attributes[$class]['attrs_order'] = array_keys($attributes[$class]['attrs']);
133
134
135
136
137
    }

    return $attributes;
  }

138
139
140
  function deserialize($values)
  {
    foreach ($values as $class => $class_values) {
141
      $result = $this->tabObject->by_object[$class]->deserializeValues($class_values);
142
      if ($result !== TRUE) {
143
144
        return $result;
      }
145
    }
146
    return TRUE;
147
148
  }

149
150
  function save_object()
  {
151
    foreach ($this->tabObject->by_object as $plugin) {
152
153
      $plugin->save_object();
    }
154
155
156
157
158
159
160
  }

  function execute()
  {
    $smarty   = get_smarty();
    $sections = array();
    $posted   = array();
161
    $smarty->assign('baseACL', 'rw');
162
    foreach ($this->tabObject->by_object as $class => &$plugin) {
163
164
165
      if (!isset($this->attributes[$class])) {
        continue;
      }
166
167
168
169
      $attributes = array();
      foreach ($this->attributes[$class] as $attr) {
        if ($plugin->attributesAccess[$attr]->getAclInfo() !== FALSE) {
          // We assign ACLs so that attributes can use them in their template code
170
          $smarty->assign($plugin->attributesAccess[$attr]->getAcl().'ACL', $plugin->aclGetPermissions($plugin->attributesAccess[$attr]->getAcl()));
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
        }
        $plugin->attributesAccess[$attr]->renderAttribute($attributes, FALSE);
      }

      $smarty->assign('section', $class);
      $smarty->assign('sectionId', $class);
      $smarty->assign('sectionClasses', ' fullwidth');
      $smarty->assign('attributes', $attributes);

      $posted[]   = $class.'_posted';
      $sections[] = $smarty->fetch(get_template_path('simpleplugin_section.tpl'));
    }
    unset($plugin);

    $smarty->assign('sections', $sections);
    $smarty->assign('hiddenPostedInput', $posted);
    $smarty->assign('focusedField', '');

    return $smarty->fetch(get_template_path('simpleplugin.tpl'));
  }

192
193
  /* Apply template and current values to an object and returns it for saving or edition
   * Cannot be called twice! If you need to, call reset between calls */
194
  function apply($targetdn = NULL)
195
  {
196
197
    if ($targetdn !== NULL) {
      $this->tabObject = objects::open($targetdn, $this->type);
198
      unset($this->attrs['objectClass']['count']);
199
200
201
202
203
      foreach ($this->tabObject->by_object as $class => &$plugin) {
        if ($plugin->is_account || $plugin->ignore_account) {
          $this->attrs['objectClass'] = $plugin->mergeObjectClasses($this->attrs['objectClass']);
        }
      }
204
205
206
    } elseif ($this->applied) {
      trigger_error('Templates can’t be applied twice without calling reset before');
      return;
207
208
    }

209
    foreach ($this->tabObject->by_object as $class => &$plugin) {
210
211
212
      if (!isset($this->attributes[$class])) {
        continue;
      }
213
214
215
216
217
218
      foreach ($this->attributes[$class] as $attr) {
        $plugin->attributesAccess[$attr]->fillLdapValue($this->attrs);
      }
    }
    unset($plugin);
    foreach ($this->tabObject->by_object as $class => &$plugin) {
219
220
221
      if (!isset($this->attributes[$class])) {
        continue;
      }
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
      foreach ($this->attributes[$class] as $attr) {
        $plugin->attributesAccess[$attr]->fillLdapValueHook($this->attrs);
      }
    }
    unset($plugin);
    foreach ($this->attrs as &$array) {
      if (!is_array($array)) {
        $array = array($array);
      }
      if (!isset($array['count'])) {
        $array['count'] = count($array);
      }
    }
    unset($array);

237
    $this->attrs = templateHandling::parseArray($this->attrs);
238
    $this->tabObject->adapt_from_template($this->attrs, call_user_func_array('array_merge', $this->attributes));
239
240

    $this->applied = TRUE;
241
242
243
    return $this->tabObject;
  }
}