diff --git a/include/class_templateHandling.inc b/include/class_templateHandling.inc index 4ec050d51929ec5cae5913d415c6008826b1e294..ebc623c277dc464b0853ebcf34fee803d9ab2e44 100644 --- a/include/class_templateHandling.inc +++ b/include/class_templateHandling.inc @@ -118,7 +118,7 @@ class templateHandling } /*! \brief Parse a mask (without surrounding %) using $attrs attributes, apply modifiers and returns an array containing possible results */ - public static function parseMask ($mask, array $attrs) + public static function parseMask (string $mask, array $attrs): array { if ($mask == '|') { return ['%']; @@ -137,7 +137,7 @@ class templateHandling $result = [$result]; } } elseif (($mask != '') && !preg_match('/c/', $modifiers)) { - trigger_error("'$mask' was not found in attributes"); + throw new FusionDirectoryException(sprintf(_('"%s" was not found in attributes'), $mask)); } $len = strlen($modifiers); for ($i = 0; $i < $len; ++$i) { @@ -211,7 +211,7 @@ class templateHandling * * \return string the string with patterns replaced by their values */ - public static function parseString ($string, array $attrs, $escapeMethod = NULL, $unique = NULL, $target = NULL) + public static function parseString (string $string, array $attrs, $escapeMethod = NULL, string $unique = NULL, string $target = NULL): string { global $config; @@ -261,7 +261,7 @@ class templateHandling * \param array $variables The possible values for each mask with its position and length * \param callable $escapeMethod Method to call to escape mask result */ - protected static function iteratePossibleValues ($rule, array $variables, $escapeMethod = NULL) + protected static function iteratePossibleValues (string $rule, array $variables, $escapeMethod = NULL) { if (!count($variables)) { yield $rule; @@ -466,7 +466,7 @@ class templateHandling * * \return array an array of possible values * */ - protected static function applyModifier ($m, array $args, $str) + protected static function applyModifier (string $m, array $args, $str): array { mb_internal_encoding('UTF-8'); mb_regex_encoding('UTF-8'); diff --git a/include/management/class_management.inc b/include/management/class_management.inc index d858a3dc70078a9b3e8ec26339b3f27a343cc63d..289fc914d7feacf5d42a0e880691e455c5a2a672 100644 --- a/include/management/class_management.inc +++ b/include/management/class_management.inc @@ -458,7 +458,12 @@ class management if ($this->tabObject instanceOf simpleTabs) { $this->tabObject->save_object(); } elseif (is_object($this->dialogObject) && method_exists($this->dialogObject, 'save_object')) { - $this->dialogObject->save_object(); + try { + $this->dialogObject->save_object(); + } catch (FusionDirectoryException $e) { + msg_dialog::display(_('Error'), $e->getMessage(), ERROR_DIALOG); + $this->closeDialogs(); + } } /* Display tab object */ diff --git a/include/management/class_templateDialog.inc b/include/management/class_templateDialog.inc new file mode 100644 index 0000000000000000000000000000000000000000..00be48579eca005a963be81f6cd8ffde65b07fd2 --- /dev/null +++ b/include/management/class_templateDialog.inc @@ -0,0 +1,125 @@ +<?php +/* + This code is part of FusionDirectory (http://www.fusiondirectory.org/) + Copyright (C) 2013-2019 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. +*/ + +/*! + * \brief Template dialog handling + */ +class templateDialog +{ + protected $simpleManagement; + protected $type; + protected $template = NULL; + protected $templates; + protected $target = NULL; + + protected $tabObject; + + protected $post_finish = 'template_continue'; + protected $post_cancel = 'template_cancel'; + + function __construct ($simpleManagement, $type, $dn = NULL, $target = NULL) + { + $this->simpleManagement = $simpleManagement; + $this->type = $type; + $this->templates = objects::getTemplates($this->type); + if ($dn !== NULL) { + if (isset($this->templates[$dn])) { + $this->template = new template($this->type, $dn); + } else { + trigger_error('Unknown template "'.$dn.'"'); + } + } + $this->target = $target; + } + + function save_object () + { + if (isset($_POST[$this->post_cancel])) { + return $this->handle_cancel(); + } + + if (($this->target === NULL) && + (isset($_POST[$this->post_finish]) || isset($_GET[$this->post_finish])) && + is_object($this->template)) { + $this->template->save_object(); + return $this->handle_finish(); + } + + if ( + isset($_POST['template']) && + isset($this->templates[$_POST['template']]) + ) { + if (is_object($this->template)) { + trigger_error('redefining template object'); + } + $this->template = new template($this->type, $_POST['template']); + /* This method can loop if there are several targets */ + unset($_POST['template']); + } + if (is_object($this->template)) { + if ($this->target !== NULL) { + $this->simpleManagement->openTabObject($this->template->apply($this->target)); + $this->simpleManagement->handleTemplateApply(); + return FALSE; + } else { + $this->template->save_object(); + } + } + + return TRUE; + } + + function setNextTarget ($target) + { + $this->target = $target; + $this->template->reset(); + } + + function execute () + { + $smarty = get_smarty(); + if (is_object($this->template)) { + $templateOutput = $this->template->execute(); + if ($this->template->dialogOpened()) { + return $templateOutput; + } else { + $smarty->assign('template_dialog', $templateOutput); + } + } else { + $smarty->assign('templates', $this->templates); + } + $display = $smarty->fetch(get_template_path('template.tpl')); + return $display; + } + + function handle_finish () + { + $this->simpleManagement->closeDialogs(); + $this->simpleManagement->openTabObject($this->template->apply()); + return FALSE; + } + + function handle_cancel () + { + $this->simpleManagement->remove_lock(); + $this->simpleManagement->closeDialogs(); + return FALSE; + } +} diff --git a/include/simpleplugin/class_simpleManagement.inc b/include/simpleplugin/class_simpleManagement.inc index 7a6eb47e7b251d19c808cb2ba8b8de93ce6045fe..c61471a725533976d62b222f3815c7b10ffa31f2 100644 --- a/include/simpleplugin/class_simpleManagement.inc +++ b/include/simpleplugin/class_simpleManagement.inc @@ -18,112 +18,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ -/*! - * \brief Template dialog handling - */ -class templateDialog -{ - protected $simpleManagement; - protected $type; - protected $template = NULL; - protected $templates; - protected $target = NULL; - - protected $tabObject; - - protected $post_finish = 'template_continue'; - protected $post_cancel = 'template_cancel'; - - function __construct ($simpleManagement, $type, $dn = NULL, $target = NULL) - { - $this->simpleManagement = $simpleManagement; - $this->type = $type; - $this->templates = objects::getTemplates($this->type); - if ($dn !== NULL) { - if (isset($this->templates[$dn])) { - $this->template = new template($this->type, $dn); - } else { - trigger_error('Unknown template "'.$dn.'"'); - } - } - $this->target = $target; - } - - function save_object () - { - if (isset($_POST[$this->post_cancel])) { - return $this->handle_cancel(); - } - - if (($this->target === NULL) && - (isset($_POST[$this->post_finish]) || isset($_GET[$this->post_finish])) && - is_object($this->template)) { - $this->template->save_object(); - return $this->handle_finish(); - } - - if ( - isset($_POST['template']) && - isset($this->templates[$_POST['template']]) - ) { - if (is_object($this->template)) { - trigger_error('redefining template object'); - } - $this->template = new template($this->type, $_POST['template']); - /* This method can loop if there are several targets */ - unset($_POST['template']); - } - if (is_object($this->template)) { - if ($this->target !== NULL) { - $this->simpleManagement->openTabObject($this->template->apply($this->target)); - $this->simpleManagement->handleTemplateApply(); - return FALSE; - } else { - $this->template->save_object(); - } - } - - return TRUE; - } - - function setNextTarget ($target) - { - $this->target = $target; - $this->template->reset(); - } - - function execute () - { - $smarty = get_smarty(); - if (is_object($this->template)) { - $templateOutput = $this->template->execute(); - if ($this->template->dialogOpened()) { - return $templateOutput; - } else { - $smarty->assign('template_dialog', $templateOutput); - } - } else { - $smarty->assign('templates', $this->templates); - } - $display = $smarty->fetch(get_template_path('template.tpl')); - return $display; - } - - function handle_finish () - { - $this->simpleManagement->closeDialogs(); - $this->simpleManagement->openTabObject($this->template->apply()); - return FALSE; - } - - function handle_cancel () - { - $this->simpleManagement->remove_lock(); - $this->simpleManagement->closeDialogs(); - return FALSE; - } -} - /*! * \brief Management base class */