From 015e646a449c1fbd14e939f625c94a3ccf036ef0 Mon Sep 17 00:00:00 2001 From: hickert Date: Mon, 11 Apr 2011 13:44:51 +0000 Subject: [PATCH] Added the template engine git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@20691 594d385d-05f5-0310-b6e9-bd551577e9d8 --- .../TemplateEngine/class_TemplateEngine.inc | 161 ++++++++++++++++++ .../TemplateEngine/class_TemplateWidget.inc | 122 +++++++++++++ .../class_TemplateWidget_checkbox.inc | 19 +++ .../class_TemplateWidget_combobox.inc | 22 +++ .../class_TemplateWidget_file.inc | 34 ++++ .../class_TemplateWidget_fixedList.inc | 86 ++++++++++ .../class_TemplateWidget_list.inc | 64 +++++++ .../class_TemplateWidget_string.inc | 18 ++ .../class_TemplateWidget_textEditor.inc | 75 ++++++++ .../class_TemplateWidget_textarea.inc | 16 ++ 10 files changed, 617 insertions(+) create mode 100644 gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateEngine.inc create mode 100644 gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget.inc create mode 100644 gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_checkbox.inc create mode 100644 gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_combobox.inc create mode 100644 gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_file.inc create mode 100644 gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_fixedList.inc create mode 100644 gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_list.inc create mode 100644 gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_string.inc create mode 100644 gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_textEditor.inc create mode 100644 gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_textarea.inc diff --git a/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateEngine.inc b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateEngine.inc new file mode 100644 index 000000000..9de96538f --- /dev/null +++ b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateEngine.inc @@ -0,0 +1,161 @@ +config = &$config; + } + + + /*! \brief Load/Sets the instruction-set to use for the current + * device configuration. + * A device configruation tells us what options + * an item can have and what children. + * @param Array The instruction set to use. + */ + function load($array) + { + $this->data = $array; + } + + + /*! \brief Set the template which will be used to generate + * the HTML content for this configuration session. + * @param String A template filename. + */ + function setTemplate($tmpl) + { + $this->template = $tmpl; + } + + + /*! \brief Returns the list of widgets which are currently used + * by the template engine to render the plugin. + * @return Array A list of widgets. + */ + function getWidgets() + { + return($this->widgets); + } + + function getItemType() + { + return($this->itemType); + } + + + /*! \brief Sets the current item type we want to render + * E.g. 'KickstartTemplate' and the corresponding values. + * + * @param String The name of the item we want to render now. + * @param Array The initial value. + */ + function setValues($name, $values) + { + // Set the current item type and reset the widget list. + $this->itemType = $name; + $this->widgets = array(); + + // Do nothing if something seems to be wrong. + if(!isset($this->data[$this->itemType])){ + echo "Undefined item type '{$name}'!
"; + return; + } + + // Get the options provided by the item and create widget for them. + $data = $this->data[$this->itemType]; + if(isset($data['options']) && count($data['options'])){ + foreach($data['options'] as $name => $item){ + $widgetClassName = "TemplateWidget_{$item['type']}"; + + // Check if the widget is available, if it is not, use a default (string). + if(!class_available($widgetClassName)){ + echo "Unknown widget class {$widgetClassName}! Falling back to default widget.
"; + $widgetClassName = "TemplateWidget_string"; + } + + // Prepare the value for the widget + if(!isset($values[$name])) $values[$name] = $item['default']; + + $value = $values[$name]; + $syntax = (isset($item['syntax']))? $item['syntax']: ""; + $providedValues = (isset($item['values']))? $item['values']: array(); + + // Create the new widget. + $this->widgets[$name] = new $widgetClassName($this->config, $name, + $value, + $item['description'], + $syntax, + $item['required'], + $item['type'], + $item['display'], + $providedValues); + } + } + } + + + function execute(){ + return($this->render()); + } + + + /*! \brief Creates the HTML content for the given list of widgets + * @return String The HTML content. + */ + function render() + { + $smarty = get_smarty(); + $smarty->assign("type", $this->itemType); + + // Tell smarty the HTML-content for each widget and the name that should be + // displayed. + foreach($this->widgets as $widget){ + $smarty->assign($widget->getName(), $widget->render()); + $smarty->assign($widget->getName()."Name", $widget->getDisplayName()); + } + $template = $smarty->fetch(get_template_path("goto/Config/{$this->template}", TRUE)); + + $smarty->assign('template', $template); + return($smarty->fetch(get_template_path("goto/Config/TemplateEngine.tpl", TRUE))); + } + + + /*! \brief Keep track of posted values. + */ + function save_object() + { + foreach($this->widgets as $widget){ + $widget->save_object(); + } + } + + + /*! \brief Check widget values and return a list of errors. + */ + function check() + { + $msgs = array(); + foreach($this->widgets as $widget){ + $msgs = array_merge($msgs, $widget->check()); + } + return($msgs); + } +} + + +?> diff --git a/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget.inc b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget.inc new file mode 100644 index 000000000..45f4fae03 --- /dev/null +++ b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget.inc @@ -0,0 +1,122 @@ +config = &$config; + $this->name = $name; + $this->value = $value; + $this->description = $description; + $this->required = $required; + $this->type = $type; + $this->syntax = $syntax; + $this->values = $values; + $this->display = $display; + $class = get_class(); + $this->postName = "{$class}_{$this->name}"; + } + + + /*! \brief Returns the display-name for the current widget. + * @return String The display-name for the widget, this + * name will usually be rendered infront of input fields. + */ + function getDisplayName() + { + $must = ($this->required)?"*":""; + return($this->display.$must); + } + + + /*! \brief Returns the description for the widget. + */ + function getDescription() + { + return($this->description); + } + + + /*! \brief Generates the HTML code for the widget. + * @return The HTML content for the widget. + */ + function render() + { + return(""); + } + + + /*! \brief Keep track of posted values. + */ + function save_object() + { + if(isset($_POST[$this->postName])){ + $this->value = get_post($this->postName); + } + } + + + /*! \brief Returns the current value. + * @return Mixed The widgets value. + */ + function getValue() + { + return($this->value); + } + + + /*! \brief Returns the name of the widget. + * @param String The widgets name. + */ + function getName() + { + return($this->name); + } + + + /*! \brief Sets a new value for the widget. + * @param String The new value. + */ + function setValue($value) + { + $this->value = $value; + } + + + /*! \brief Check the value entry using the provieded syntax. + * @return Array Returns a list of errors + */ + function check() + { + if($this->required && empty($this->value)){ + return(array(msgPool::required($this->display))); + } + if(!empty($this->value) && !empty($this->syntax) && !preg_match("/".$this->syntax."/", $this->value)){ + return(array(msgPool::invalid($this->display, $this->value, "/".$this->syntax."/"))); + } + return(array()); + } +} +?> diff --git a/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_checkbox.inc b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_checkbox.inc new file mode 100644 index 000000000..cd6cb1b0f --- /dev/null +++ b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_checkbox.inc @@ -0,0 +1,19 @@ +postName}' "; + if($this->value) $str .= " checked "; + $str .= ">"; + return($str); + } + + function save_object() + { + $this->value = isset($_POST[$this->postName]); + } +} +?> diff --git a/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_combobox.inc b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_combobox.inc new file mode 100644 index 000000000..d0a306926 --- /dev/null +++ b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_combobox.inc @@ -0,0 +1,22 @@ +postName}'>"; + foreach($this->values as $name => $value){ + if($name == $this->value){ + $str .= "\n"; + }else{ + $str .= "\n"; + } + } + $str .= ""; + return($str); + } +} +?> diff --git a/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_file.inc b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_file.inc new file mode 100644 index 000000000..1e41cd9be --- /dev/null +++ b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_file.inc @@ -0,0 +1,34 @@ +description); + if(mb_strlen($this->value) == 0){ + $ret = ""._("No file uploaded yet").""; + $ret.= "
+ postName}\" + name=\"{$this->postName}\" + type=\"file\" + size=\"20\" + maxlength=\"255\" + accept=\"*.*\"> "; + $ret.= ""; + }else{ + $ret = ""._("File uploaded").": ".mb_strlen($this->value)." "._("Bytes"); + $ret.= " "; + } + return($ret); + } + + function save_object() + { + if(isset($_POST["{$this->postName}_Upload"]) && isset($_FILES[$this->postName]['tmp_name'])){ + $this->value = file_get_contents(gosa_file_name($_FILES[$this->postName]['tmp_name'])); + } + if(isset($_POST["{$this->postName}_Remove"])) $this->value =""; + } +} + +?> diff --git a/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_fixedList.inc b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_fixedList.inc new file mode 100644 index 000000000..0f9809728 --- /dev/null +++ b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_fixedList.inc @@ -0,0 +1,86 @@ +listWidget= new sortableListing($this->value); + $this->listWidget->setEditable(false); + $this->listWidget->setDeleteable(true); + $this->listWidget->setColspecs(array('*')); + $this->listWidget->setWidth("100%"); + $this->listWidget->setHeight("70px"); + $this->listWidget->setAcl("rwcdm"); + } + + function getAvailableOptions() + { + $tmp = array(); + foreach($this->values as $key => $name){ + if(!in_array($key, $this->value)){ + $tmp[$key]=$name; + } + } + return($tmp); + } + + function render() + { + $str = ""; + + // Build up list data + $data = $this->value; + foreach($this->value as $key => $name){ + $lData[$key] = array('data' => array($this->values[$name])); + } + $this->listWidget->setListData($data, $lData); + $this->listWidget->update(); + $str .= $this->listWidget->render(); + $str .= ""; + $str .= ""; + return($str); + } + + function save_object() + { + $this->listWidget->save_object(); + $action = $this->listWidget->getAction(); + if($action['action'] == 'delete'){ + $id = $this->listWidget->getKey($action['targets'][0]); + unset($this->value[$id]); + $this->value = array_values($this->value); + } + + if(isset($_POST["{$this->postName}_Add"]) && isset($_POST["{$this->postName}_Input"])){ + $input = get_post("{$this->postName}_Input"); + + if(!empty($input) && !empty($this->syntax) && !preg_match("/".$this->syntax."/", $input)){ + msg_dialog::displayChecks(array(msgPool::invalid($this->display, $input, "/".$this->syntax."/"))); + }elseif(!empty($input)){ + $this->value[] = $input; + } + } + } + + /*! \brief Check the value entry using the provieded syntax. + * @return Array Returns a list of errors + */ + function check() + { + if($this->required && empty($this->value)){ + return(array(msgPool::required($this->display))); + } + return(array()); + } +} + + +?> diff --git a/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_list.inc b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_list.inc new file mode 100644 index 000000000..d95da2c57 --- /dev/null +++ b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_list.inc @@ -0,0 +1,64 @@ +listWidget= new sortableListing($this->value); + $this->listWidget->setEditable(false); + $this->listWidget->setDeleteable(true); + $this->listWidget->setColspecs(array('*')); + $this->listWidget->setWidth("100%"); + $this->listWidget->setHeight("70px"); + $this->listWidget->setAcl("rwcdm"); + } + + + function render() + { + $str = ""; + $this->listWidget->setListData($this->value); + $this->listWidget->update(); + $str .= $this->listWidget->render(); + $str .= ""; + $str .= ""; + return($str); + } + + function save_object() + { + $this->listWidget->save_object(); + $action = $this->listWidget->getAction(); + if($action['action'] == 'delete'){ + $id = $this->listWidget->getKey($action['targets'][0]); + unset($this->value[$id]); + $this->value = array_values($this->value); + } + + if(isset($_POST["{$this->postName}_Add"]) && isset($_POST["{$this->postName}_Input"])){ + $input = get_post("{$this->postName}_Input"); + + if(!empty($input) && !empty($this->syntax) && !preg_match("/".$this->syntax."/", $input)){ + msg_dialog::displayChecks(array(msgPool::invalid($this->display, $input, "/".$this->syntax."/"))); + }elseif(!empty($input)){ + $this->value[] = $input; + } + } + } + + /*! \brief Check the value entry using the provieded syntax. + * @return Array Returns a list of errors + */ + function check() + { + if($this->required && empty($this->value)){ + return(array(msgPool::required($this->display))); + } + return(array()); + } +} + + +?> diff --git a/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_string.inc b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_string.inc new file mode 100644 index 000000000..2bf3ccca0 --- /dev/null +++ b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_string.inc @@ -0,0 +1,18 @@ +description); + $value = set_post($this->value); + + $name = " name=\"{$this->postName}\" "; + $value = " value=\"{$value}\" "; + $title = (empty($this->description))?"": " title=\"{$desc}\""; + + return(""); + } +} + +?> diff --git a/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_textEditor.inc b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_textEditor.inc new file mode 100644 index 000000000..7c824f4f1 --- /dev/null +++ b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_textEditor.inc @@ -0,0 +1,75 @@ +mb_extension = function_exists("mb_detect_encoding"); + if($this->mb_extension){ + $this->enc_before_edit = mb_detect_encoding($this->value); + if($this->enc_before_edit != "ASCII"){ + $this->write_protect = TRUE; + } + } + } + + + function render() + { + $smarty = get_smarty(); + $smarty->assign("postName", set_post($this->postName)); + $smarty->assign("write_protect", set_post($this->write_protect)); + $smarty->assign("value", set_post($this->value)); + return($smarty->fetch(get_template_path("TemplateWidget_textEditor.tpl", TRUE, dirname(__FILE__)))); + } + + + function save_object() + { + TemplateWidget::save_object(); + if(isset($_POST['editAnyway'])) $this->write_protect = FALSE; + + if(isset($_POST['ImportUpload'])){ + if(($_FILES['ImportFile']['error']!=0)){ + msg_dialog::display(_("Error"), msgPool::incorrectUpload(), ERROR_DIALOG); + }elseif(($_FILES['ImportFile']['size']==0)){ + msg_dialog::display(_("Error"), msgPool::incorrectUpload(_("file is empty")), ERROR_DIALOG); + }else{ + $str = file_get_contents(gosa_file_name($_FILES['ImportFile']['tmp_name'])); + $this->value = $str; + + // Check encoding again + if($this->mb_extension){ + $this->enc_before_edit = mb_detect_encoding($this->value); + if($this->enc_before_edit != "ASCII"){ + $this->write_protect = TRUE; + } + } + } + } + $this->enc_after_edit = mb_detect_encoding($this->value); + } + + + /*! \brief Check the value entry using the provieded syntax. + * @return Array Returns a list of errors + */ + function check() + { + $msgs = TemplateWidget::check(); + if($this->mb_extension && !$this->write_protect && $this->enc_after_edit !== $this->enc_before_edit ){ + $msg = sprintf(_("The text encoding has changed from %s to %s. Do you really want to save?"), + bold($this->enc_before_edit),bold($this->enc_after_edit)); + $msgs[] = $msg; + $this->enc_before_edit = $this->enc_after_edit; + } + return($msgs); + } +} +?> diff --git a/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_textarea.inc b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_textarea.inc new file mode 100644 index 000000000..030dd1156 --- /dev/null +++ b/gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_textarea.inc @@ -0,0 +1,16 @@ +description); + return(""); + } + +} + +?> -- 2.30.2