1 <?php
3 class TemplateEngine
4 {
5 private $config;
6 private $data = array();
7 private $template = "";
9 private $itemType = '';
10 private $widgets = array();
12 function __construct($config)
13 {
14 $this->config = &$config;
15 }
17 function setValues($values)
18 {
19 foreach($values as $name => $value){
20 if(!isset($this->widgets[$name])){
21 $class = get_class();
22 echo "Unknown option '{$name}' for {$class}!)}<br>";
23 continue;
24 }
25 $this->widgets[$name]->setValue($value);
26 }
27 }
29 function setType($name)
30 {
31 $this->itemType = $name;
32 $this->widgets = array();
34 if(!isset($this->data[$this->itemType])){
35 echo "Undefined item type '{$name}'!<br>";
36 return;
37 }
38 $data = $this->data[$this->itemType];
39 if(isset($data['options']) && count($data['options'])){
40 foreach($data['options'] as $name => $item){
42 $widgetClassName = "TemplateWidget_{$item['type']}";
43 if(!class_available($widgetClassName)){
44 echo "Unknown widget class {$widgetClassName}! Falling back to default widget.<br>";
45 $widgetClassName = "TemplateWidget_string";
46 }
48 $this->widgets[$name] = new $widgetClassName($this->config, $name,
49 $item['value'],
50 $item['description'],
51 $item['required'],
52 $item['type'],
53 $item['display']);
54 }
55 }
56 }
58 function load($array)
59 {
60 $this->data = $array;
61 }
63 function setTemplate($tmpl)
64 {
65 $this->template = $tmpl;
66 }
68 function getWidgets()
69 {
70 return($this->widgets);
71 }
73 function render()
74 {
75 $smarty = get_smarty();
76 $smarty->assign("type", $this->itemType);
77 foreach($this->widgets as $widget){
78 $smarty->assign($widget->getName(), $widget->render());
79 }
80 return($smarty->fetch(get_template_path("goto/Config/{$this->template}", TRUE)));
81 }
83 function save_object()
84 {
85 foreach($this->widgets as $widget){
86 $widget->save_object();
87 }
88 }
89 }
92 ?>