Code

Updated partition handling
[gosa.git] / gosa-plugins / goto / admin / ConfigManagement / class_TemplateEngine.inc
1 <?php
3 /*! \brief  A template engine used to render dynamic template
4  *           for the GOsa-devices.
5  */
6 class TemplateEngine
7 {
8     private $config;
9     private $data = array();    
10     private $template = "";
11     private $itemType = '';
12     private $widgets = array();
15     /*! \brief  Constructs the template engine. 
16      *  @param  Config  The GOsa configuration object.
17      */
18     function __construct($config)
19     {
20         $this->config = &$config;
21     }
24     /*! \brief  Load/Sets the instruction-set to use for the current
25      *           device configuration.
26      *          A device configruation tells us what options 
27      *           an item can have and what children.
28      *  @param  Array   The instruction set to use.
29      */
30     function load($array)
31     {
32         $this->data = $array;
33     }
36     /*! \brief  Set the template which will be used to generate 
37      *           the HTML content for this configuration session.
38      *  @param  String  A template filename.
39      */
40     function setTemplate($tmpl)
41     {
42         $this->template = $tmpl;
43     }
46     /*! \brief  Returns the list of widgets which are currently used 
47      *           by the template engine to render the plugin.
48      *  @return Array  A list of widgets.
49      */
50     function getWidgets()
51     {
52         return($this->widgets);
53     }     
55     function getItemType()
56     {
57         return($this->itemType);
58     }
59     
61     /*! \brief  Sets the current item type we want to render
62      *          E.g.  'KickstartTemplate' and the corresponding values.
63      *
64      *  @param  String  The name of the item we want to render now.
65      *  @param  Array   The initial value.
66      */
67     function setValues($name, $values)
68     {
69         // Set the current item type and reset the widget list.
70         $this->itemType = $name;
71         $this->widgets = array();
73         // Do nothing if something seems to be wrong. 
74         if(!isset($this->data[$this->itemType])){
75             echo "Undefined item type '{$name}'!<br>";
76             return;
77         }
79         // Get the options provided by the item and create widget for them.
80         $data = $this->data[$this->itemType];
81         if(isset($data['options']) && count($data['options'])){
82             foreach($data['options'] as $name => $item){
83                 $widgetClassName = "TemplateWidget_{$item['type']}";
85                 // Check if the widget is available, if it is not, use a default (string).
86                 if(!class_available($widgetClassName)){
87                     echo "Unknown widget class {$widgetClassName}! Falling back to default widget.<br>";
88                     $widgetClassName = "TemplateWidget_string";
89                 }
91                 // Prepare the value for the widget 
92                 if(!isset($values[$name])) $values[$name] = $item['default'];
94                 $value = $values[$name];
95                 $syntax = (isset($item['syntax']))? $item['syntax']: "";
96                 $providedValues = (isset($item['values']))? $item['values']: array();
97    
98                 // Create the new widget.
99                 $this->widgets[$name] = new $widgetClassName($this->config, $name, 
100                         $value,
101                         $item['description'],
102                         $syntax,
103                         $item['required'],
104                         $item['type'],
105                         $item['display'],
106                         $providedValues);
107             }
108         }
109     }
111     
112     function execute(){
113         return($this->render());
114     }
115     
117     /*! \brief  Creates the HTML content for the given list of widgets
118      *  @return String  The HTML content.
119      */
120     function render()
121     {
122         $smarty = get_smarty();
123         $smarty->assign("type", $this->itemType);
125         // Tell smarty the HTML-content for each widget and the name that should be
126         //  displayed.
127         foreach($this->widgets as $widget){
128             $smarty->assign($widget->getName(), $widget->render());
129             $smarty->assign($widget->getName()."Name", $widget->getDisplayName());
130         }
131         $template = $smarty->fetch(get_template_path("goto/Config/{$this->template}", TRUE));
133         $smarty->assign('template', $template);
134         return($smarty->fetch(get_template_path("goto/Config/TemplateEngine.tpl", TRUE)));
135     }
138     /*! \brief  Keep track of posted values.
139      */
140     function save_object()
141     {
142         foreach($this->widgets as $widget){
143             $widget->save_object();
144         }
145     }
148     /*! \brief  Check widget values and return a list of errors.
149      */
150     function check()
151     {
152         $msgs = array();
153         foreach($this->widgets as $widget){
154             $msgs = array_merge($msgs, $widget->check());
155         }
156         return($msgs);
157     }
161 ?>