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 }
56 /*! \brief Sets the current item type we want to render
57 * E.g. 'KickstartTemplate' and the corresponding values.
58 *
59 * @param String The name of the item we want to render now.
60 * @param Array The initial value.
61 */
62 function setValues($name, $values)
63 {
64 // Set the current item type and reset the widget list.
65 $this->itemType = $name;
66 $this->widgets = array();
68 // Do nothing if something seems to be wrong.
69 if(!isset($this->data[$this->itemType])){
70 echo "Undefined item type '{$name}'!<br>";
71 return;
72 }
74 // Get the options provided by the item and create widget for them.
75 $data = $this->data[$this->itemType];
76 if(isset($data['options']) && count($data['options'])){
77 foreach($data['options'] as $name => $item){
78 $widgetClassName = "TemplateWidget_{$item['type']}";
80 // Check if the widget is available, if it is not, use a default (string).
81 if(!class_available($widgetClassName)){
82 echo "Unknown widget class {$widgetClassName}! Falling back to default widget.<br>";
83 $widgetClassName = "TemplateWidget_string";
84 }
86 // Prepare the value for the widget
87 $value = $values[$name];
88 $syntax = (isset($item['syntax']))? $item['syntax']: "";
89 $providedValues = (isset($item['values']))? $item['values']: array();
91 // Create the new widget.
92 $this->widgets[$name] = new $widgetClassName($this->config, $name,
93 $value,
94 $item['description'],
95 $syntax,
96 $item['required'],
97 $item['type'],
98 $item['display'],
99 $providedValues);
100 }
101 }
102 }
105 function execute(){
106 return($this->render());
107 }
110 /*! \brief Creates the HTML content for the given list of widgets
111 * @return String The HTML content.
112 */
113 function render()
114 {
115 $smarty = get_smarty();
116 $smarty->assign("type", $this->itemType);
118 // Tell smarty the HTML-content for each widget and the name that should be
119 // displayed.
120 foreach($this->widgets as $widget){
121 $smarty->assign($widget->getName(), $widget->render());
122 $smarty->assign($widget->getName()."Name", $widget->getDisplayName());
123 }
124 $template = $smarty->fetch(get_template_path("goto/Config/{$this->template}", TRUE));
126 $smarty->assign('template', $template);
127 return($smarty->fetch(get_template_path("goto/Config/TemplateEngine.tpl", TRUE)));
128 }
131 /*! \brief Keep track of posted values.
132 */
133 function save_object()
134 {
135 foreach($this->widgets as $widget){
136 $widget->save_object();
137 }
138 }
141 /*! \brief Check widget values and return a list of errors.
142 */
143 function check()
144 {
145 $msgs = array();
146 foreach($this->widgets as $widget){
147 $msgs = array_merge($msgs, $widget->check());
148 }
149 return($msgs);
150 }
151 }
154 ?>