Commit 1339d15a authored by Côme Bernigaud's avatar Côme Bernigaud Committed by Benoit Mortier
Browse files

Fixes #1887 Added proper template handling

parent adfdb74c
<h1>
{t}Creating a new object using templates{/t}
</h1>
<hr/>
{if isset($template_dialog)}
{$template_dialog}
{else}
<table border="0">
<tr>
<td><label for="template">{t}Template{/t}</label></td>
<td>
<select name="template" id="template">
{html_options options=$templates}
</select>
</td>
</tr>
</table>
{/if}
<br/>
<p class="plugbottom">
<input type="submit" name="template_continue" value="{t}Continue{/t}"/>
&nbsp;
<input type="submit" name="template_cancel" value="{msgPool type=cancelButton}"/>
</p>
......@@ -406,9 +406,10 @@ class management
// Delete the object
$this->dn = $dn;
$this->tabObject = new $tabClass($this->config, $this->config->data['TABS'][$tabType], $this->dn, $aclCategory);
$this->tabObject->set_acl_base($this->dn);
$this->tabObject->parent = &$this;
$this->openTabObject(
new $tabClass($this->config, $this->config->data['TABS'][$tabType], $this->dn, $aclCategory),
$this->dn
);
$this->tabObject->delete ();
// Remove the lock for the current object.
......@@ -656,9 +657,10 @@ class management
// No tab type defined
} else {
if (isset($this->config->data['TABS'][$tabType])) {
$this->tabObject = new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory);
$this->tabObject->set_acl_base($this->headpage->getBase());
$this->tabObject->parent = &$this;
$this->openTabObject(
new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory),
$this->headpage->getBase()
);
@DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $this->dn, "Create new entry initiated!");
} else {
msg_dialog::display(_("Error"), sprintf(_("No tab declaration for '%s' found in your configuration file. Cannot create plugin instance!"), $tabType), ERROR_DIALOG);
......@@ -737,10 +739,11 @@ class management
trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
} else {
$tab = $tabClass;
$this->tabObject = new $tab($this->config, $this->config->data['TABS'][$tabType], $this->dn, $aclCategory);
$this->openTabObject(
new $tab($this->config, $this->config->data['TABS'][$tabType], $this->dn, $aclCategory),
$this->dn
);
@DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $this->dn, "Edit entry initiated!");
$this->tabObject->set_acl_base($this->dn);
$this->tabObject->parent = &$this;
}
}
}
......@@ -809,7 +812,7 @@ class management
* \brief This method closes dialogs
* and cleans up the cached object info and the ui.
*/
protected function closeDialogs()
public function closeDialogs()
{
$this->last_dn = $this->dn;
$this->dn = "";
......@@ -975,5 +978,12 @@ class management
{
return (is_object($this->tabObject) || is_object($this->dialogObject));
}
function openTabObject($object, $base)
{
$this->tabObject = $object;
$this->tabObject->set_acl_base($base);
$this->tabObject->parent = &$this;
}
}
?>
......@@ -241,6 +241,32 @@ class objects
global $config;
return array_keys($config->data['OBJECTS']);
}
/* !\brief This method returns a list of all available templates for the given type
*/
static function getTemplates($type)
{
global $config;
$infos = self::infos($type);
$templates = array();
$ldap = $config->get_ldap_link();
foreach ($config->departments as $key => $value) {
// Search all templates from the current dn.
$ldap->cd($infos['ou'].$value);
// We could also adapt object filter to match templates
$ldap->search('(objectClass=fdTemplate)', array('cn'));
if ($ldap->count() != 0) {
while ($attrs = $ldap->fetch()) {
$templates[$ldap->getDN()] = $attrs['cn'][0]." - ".LDAP::fix($key);
}
}
}
natcasesort($templates);
reset($templates);
return $templates;
}
}
?>
......@@ -316,7 +316,6 @@ class plugin
}
}
sort($template_attrs['fdTemplateField']);
print_r($template_attrs);
return $template_attrs;
}
......@@ -581,6 +580,11 @@ class plugin
{
}
function setTemplate ($bool)
{
$this->is_template = $bool;
}
static function tpl_fetch_template($dn)
{
global $config;
......@@ -677,8 +681,8 @@ class plugin
static function tpl_parse_mask($mask, $attrs)
{
if ($mask == "|") {
return array("%");
if ($mask == '|') {
return array('%');
}
$modifiers = '';
if (preg_match('/^([^|]+)\|/', $mask, $m)) {
......
......@@ -429,7 +429,7 @@ class tabs
$this->getBaseObject()->_template_cn = $cn;
foreach ($this->by_object as &$obj) {
$obj->is_template = TRUE;
$obj->setTemplate(TRUE);
}
unset($obj);
}
......
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2014 FusionDirectory
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.
*/
/* Class for applying a template */
class template
{
protected $type;
protected $dn;
protected $needed;
protected $attrs;
protected $tabObject;
protected $attributes;
function __construct($type, $dn)
{
$this->type = $type;
$this->dn = $dn;
list($this->attrs, $depends) = plugin::tpl_fetch_template($this->dn);
$this->needed = plugin::tpl_needed_attrs($this->attrs, $depends);
$this->tabObject = objects::create($this->type);
$this->attributes = array();
foreach ($this->tabObject->by_object as $class => &$plugin) {
$this->attributes[$class] = array();
$attrs = array_unique(array_merge($plugin->getRequiredAttributes(), $this->needed));
foreach ($attrs as $attr) {
if (isset($this->attrs[$attr])) {
continue;
}
if (isset($plugin->attributesAccess[$attr])) {
$this->attributes[$class][] = $attr;
}
}
}
}
function getDn()
{
return $this->dn;
}
function getBase()
{
$infos = objects::infos($this->type);
return dn2base($this->dn, 'ou=templates,'.$infos['ou']);
}
function save_object()
{
$this->tabObject->save_object();
}
function execute()
{
$smarty = get_smarty();
$sections = array();
$posted = array();
foreach ($this->tabObject->by_object as $class => &$plugin) {
$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
$smarty->assign($plugin->attributesAccess[$attr]->getAcl()."ACL", $plugin->getacl($plugin->attributesAccess[$attr]->getAcl()));
}
$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'));
}
/* Apply template and current values to an object and returns it for saving or edition */
function apply()
{
foreach ($this->tabObject->by_object as $class => &$plugin) {
foreach ($this->attributes[$class] as $attr) {
$plugin->attributesAccess[$attr]->fillLdapValue($this->attrs);
}
}
unset($plugin);
foreach ($this->tabObject->by_object as $class => &$plugin) {
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);
$this->attrs = plugin::tpl_parse_attrs($this->attrs);
$this->tabObject->adapt_from_template($this->attrs, call_user_func_array('array_merge', $this->attributes));
$this->tabObject->getBaseObject()->base = $this->getBase();
return $this->tabObject;
}
}
......@@ -156,11 +156,6 @@ class aclRole extends simplePlugin
$this->attributesAccess['cn']->setUnique(TRUE);
}
function compute_dn()
{
return 'cn='.$this->cn.",".get_ou('aclRoleRDN').$this->base;
}
}
class acl_createedit extends acl
......
......@@ -174,7 +174,7 @@ class userManagement extends management
function newTemplate($action, $entry)
{
$this->newEntry();
$this->tabObject->setTemplateMode('template_tmp'); // FIXME : this needs to be chosen by the user
$this->tabObject->setTemplateMode('template_tmp');
}
......
......@@ -187,7 +187,7 @@
<action>
<name>newfromtpl</name>
<type>entry</type>
<image>geticon.php?context=action&amp;icon=document-new&amp;size=16</image>
<image>geticon.php?context=actions&amp;icon=document-new&amp;size=16</image>
<objectclass>fdTemplate</objectclass>
<label>New user from template</label>
</action>
......
......@@ -140,6 +140,8 @@ class user extends simplePlugin
return array(
'plShortName' => _('Generic'),
'plDescription' => _('Generic user settings'),
'plIcon' => 'geticon.php?context=applications&amp;icon=user-info&amp;size=48',
'plSmallIcon' => 'geticon.php?context=applications&amp;icon=user-info&amp;size=16',
'plSelfModify' => TRUE,
'plObjectType' => array('user' => array(
'description' => _('Users'),
......
......@@ -167,7 +167,6 @@ class StepMigrateCheck
$this->run();
}
return $res;
/* TODO rerun depending tests? (done by hand for now) */
}
public function dialog_refresh()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment