Code

Added the template engine
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Mon, 11 Apr 2011 13:44:51 +0000 (13:44 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Mon, 11 Apr 2011 13:44:51 +0000 (13:44 +0000)
git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@20691 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateEngine.inc [new file with mode: 0644]
gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget.inc [new file with mode: 0644]
gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_checkbox.inc [new file with mode: 0644]
gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_combobox.inc [new file with mode: 0644]
gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_file.inc [new file with mode: 0644]
gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_fixedList.inc [new file with mode: 0644]
gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_list.inc [new file with mode: 0644]
gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_string.inc [new file with mode: 0644]
gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_textEditor.inc [new file with mode: 0644]
gosa-plugins/goto-ng/admin/newConfigManagement/TemplateEngine/class_TemplateWidget_textarea.inc [new file with mode: 0644]

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 (file)
index 0000000..9de9653
--- /dev/null
@@ -0,0 +1,161 @@
+<?php
+
+/*! \brief  A template engine used to render dynamic template
+ *           for the GOsa-devices.
+ */
+class TemplateEngine
+{
+    private $config;
+    private $data = array();    
+    private $template = "";
+    private $itemType = '';
+    private $widgets = array();
+
+
+    /*! \brief  Constructs the template engine. 
+     *  @param  Config  The GOsa configuration object.
+     */
+    function __construct($config)
+    {
+        $this->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}'!<br>";
+            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.<br>";
+                    $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 (file)
index 0000000..45f4fae
--- /dev/null
@@ -0,0 +1,122 @@
+<?php
+
+/*! \brief  The base class for all template-widgets used by the 
+ *           device-configuration class.
+ */
+class TemplateWidget
+{
+    protected $name = "unnamed";
+    protected $value = "";
+
+    protected $description = "";
+    protected $required = "";
+    protected $type = "";
+    protected $display = "";
+    protected $syntax = "";
+    protected $values = "";
+
+    /*! \brief  Constructs the template widget and sets the default values.
+     *  @param  Config  The GOsa configuration object.
+     *  @param  String  A name for the widget.
+     *  @param  String  The initial value.
+     *  @param  String  A description.
+     *  @param  String  True/False  Must-Value/Optional-Value.
+     *  @param  String  The widget type. 
+     *  @param  String  A display name for the widget.
+     */
+    function __construct(&$config, $name, $value, $description,$syntax,$required,$type,$display, $values=array())
+    {
+        $this->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)?"<span class='required'>*</span>":"";
+        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 (file)
index 0000000..cd6cb1b
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+
+class TemplateWidget_checkbox extends TemplateWidget
+{
+    function render()
+    {
+        $str = "";
+        $str .= "<input type='checkbox' value='1' name='{$this->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 (file)
index 0000000..d0a3069
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+
+class TemplateWidget_combobox extends TemplateWidget
+{
+    function render()
+    {
+        $str = "";
+
+        // Build up list data
+        $str .= "<select size='1' name='{$this->postName}'>";
+        foreach($this->values as $name => $value){
+            if($name == $this->value){
+                $str .= "<option selected value=\"{$name}\">{$value}</option>\n";
+            }else{
+                $str .= "<option value=\"{$name}\">{$value}</option>\n";
+            }
+        }
+        $str .= "</select>";
+        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 (file)
index 0000000..1e41cd9
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+
+class TemplateWidget_file extends TemplateWidget
+{
+    function render()
+    {
+        $title = set_post($this->description);
+        if(mb_strlen($this->value) == 0){   
+            $ret = "<i>"._("No file uploaded yet")."</i>";
+            $ret.= "<br> <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"2000000\">
+                <input id=\"{$this->postName}\" 
+                name=\"{$this->postName}\" 
+                type=\"file\" 
+                size=\"20\" 
+                maxlength=\"255\" 
+                accept=\"*.*\">&nbsp;";
+            $ret.= "<button name='{$this->postName}_Upload'>"._("Upload")."</button>";
+        }else{
+            $ret = "<i>"._("File uploaded").": ".mb_strlen($this->value)." "._("Bytes");
+            $ret.= "&nbsp;<button name='{$this->postName}_Remove'>".msgPool::delButton()."</button>";
+        }
+        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 (file)
index 0000000..0f98097
--- /dev/null
@@ -0,0 +1,86 @@
+<?php
+
+class TemplateWidget_fixedList extends TemplateWidget
+{
+    protected $value = array();
+    
+    function __construct(&$config, $name, $value, $description,$syntax,$required,$type,$display,$values)
+    {
+        parent:: __construct($config, $name, $value, $description,$syntax,$required,$type,$display,$values);
+
+        $this->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 .= "<select size='1' name='{$this->postName}_Input'>";
+        foreach($this->getAvailableOptions() as $name => $value){
+            $str .= "<option value=\"{$name}\">{$value}</option>\n";
+        }
+        $str .= "</select>";
+        $str .= "<button name='{$this->postName}_Add'>".msgPool::addButton()."</button>";
+        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 (file)
index 0000000..d95da2c
--- /dev/null
@@ -0,0 +1,64 @@
+<?php
+
+class TemplateWidget_list extends TemplateWidget
+{
+    function __construct(&$config, $name, $value, $description,$syntax,$required,$type,$display,$values)
+    {
+        parent:: __construct($config, $name, $value, $description,$syntax,$required,$type,$display,$values);
+
+        $this->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 .= "<input type='text' name='{$this->postName}_Input'>";
+        $str .= "<button name='{$this->postName}_Add'>".msgPool::addButton()."</button>";
+        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 (file)
index 0000000..2bf3ccc
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+
+class TemplateWidget_string extends TemplateWidget
+{
+    function render()
+    {
+        $desc = set_post($this->description);
+        $value = set_post($this->value);
+
+        $name = " name=\"{$this->postName}\" ";
+        $value = " value=\"{$value}\" ";
+        $title = (empty($this->description))?"": " title=\"{$desc}\"";
+
+        return("<input type='text' {$title} {$name} {$value }>");
+    }
+}
+
+?>
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 (file)
index 0000000..7c824f4
--- /dev/null
@@ -0,0 +1,75 @@
+<?php
+
+class TemplateWidget_textEditor extends TemplateWidget
+{
+    protected $value = array();
+    protected $write_protect = FALSE;
+    
+    function __construct(&$config, $name, $value, $description,$syntax,$required,$type,$display,$values)
+    {
+        parent:: __construct($config, $name, $value, $description,$syntax,$required,$type,$display,$values);
+
+        // Keep an eye on dangerous encodings, we may break scripts while editing.
+        $this->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 (file)
index 0000000..030dd11
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+
+class TemplateWidget_textarea extends TemplateWidget
+{
+    function render()
+    {
+        $title = set_post($this->description);
+        return("<textarea   title=\"{$title}\"
+                            style='width:100%' 
+                            rows=4 type='text' 
+                            name=\"{$this->postName}\">".set_post($this->value)."</textarea>");
+    }
+
+}
+
+?>