1 <?php
3 class DeviceConfig extends plugin
4 {
5 private $TemplateEngine = NULL;
7 private $idToName = array();
8 private $currentItemName = "";
10 function __construct(&$config, $dn)
11 {
12 $this->TemplateEngine = new TemplateEngine($config);
13 $this->TemplateEngine->setTemplate('puppet.tpl');
14 $this->config = $config;
16 $str = '{
17 "PuppetModule": {
18 "options": {
19 "dependency": {
20 "description": "Modules that are needed to be installed for this module",
21 "required": false,
22 "value": [
24 ],
25 "syntax": "^[a-zA-Z0-9_+\\\\./-]+(\\\\[[<=>]+[a-zA-Z0-9_+\\\\.-]+\\\\])?$",
26 "type": "list",
27 "display": "Module dependencies"
28 },
29 "version": {
30 "description": "The version of the puppet module",
31 "required": true,
32 "value": "",
33 "syntax": "^[a-zA-Z0-9_+.-]+$",
34 "type": "string",
35 "display": "Module version"
36 },
37 "name": {
38 "description": "The name of the puppet module",
39 "required": true,
40 "value": "",
41 "syntax": "^[a-zA-Z0-9_+.-]+$",
42 "type": "string",
43 "display": "Module name"
44 },
45 "description": {
46 "required": false,
47 "type": "string",
48 "display": "Module description",
49 "value": "",
50 "description": "Text briefly describing the module contents"
51 }
52 },
53 "container": [
54 "PuppetManifest",
55 "PuppetFile",
56 "PuppetTemplate"
57 ],
58 "name": "Module",
59 "description": "Puppet module"
60 },
61 "root": {
62 "options": {
64 },
65 "container": [
66 "PuppetModule"
67 ],
68 "name": "Root",
69 "description": "The root item"
70 }
71 }';
73 $this->itemConfig = json_decode($str, TRUE);
74 $this->TemplateEngine->load($this->itemConfig);
76 // Check for the root object
77 if(!isset($this->itemConfig['root'])){
78 echo 'No root!';
79 }
81 // Set current item to 'root'.
82 $this->currentItemDescriptor = &$this->itemConfig['root'];
83 $this->currentItemValues = array();
84 $this->currentItem = array();
86 $this->addItem('root','root',array());
87 $this->setCurrentItem('root');
88 $this->addItem('PuppetModule','test1',
89 array(
90 'dependency' => array('mozilla-2','toaster-2.0'),
91 'version' => '2.4-f',
92 'name' => 'Thundebird',
93 'description' => 'Mozilla mail client')
94 );
95 $this->addItem('PuppetModule','test2',
96 array(
97 'version' => 1,
98 'name' => 'Firefox',
99 'description' => 'Test Module')
100 );
101 $this->setCurrentItem('test1');
102 $this->addItem('PuppetTemplate','temp1',array('name' => 'temp1', 'file' => 'kekse.tpl'));
104 $this->setCurrentItem('root');
105 }
108 function renderNavigator($array = NULL)
109 {
110 $array = ($array == NULL)? $this->currentItemValues['root']: $array;
111 $str = "<ul>";
112 $plug = $_GET['plug'];
114 $id = array_search($array['name'],$this->idToName);
115 $str .= "<a href='?plug={$plug}&item={$id}'>";
116 if($this->currentItemName == $array['name']){
117 $str .= "<b>".$array['name']."</b>";
118 }else{
119 $str .= $array['name'];
120 }
121 $str .= " <i>(".$array['type'].")</i>";
122 $str .= "</a>";
124 if(count($array['children'])){
125 foreach($array['children'] as $subItem){
126 $str .= $this->renderNavigator($subItem);
127 }
128 }
129 $str .= "</ul>";
130 return($str);
131 }
133 function addItem($type,$name, $values)
134 {
135 $current = &$this->currentItem;
136 $this->idToName[] = $name;
137 $new = array(
138 'children' => array(),
139 'type' => $type,
140 'name' => $name,
141 'values' => $values);
142 $this->currentItemValues[$name] = $new;
143 $current['children'][$name] = &$this->currentItemValues[$name];
144 }
146 function setCurrentItem($item)
147 {
148 if($this->currentItemName == $item) return;
150 // Save eventually changed values
151 if($this->currentItem){
152 foreach($this->TemplateEngine->getWidgets() as $widget){
153 $this->currentItem['values'][$widget->getName()] = $widget->getValue();
154 }
155 }
158 $this->currentItemName = $item;
159 $this->currentItem = &$this->currentItemValues[$item];
160 $this->currentItemType = $this->currentItem['type'];
161 $this->currentItemDescriptor =&$this->itemConfig[$this->currentItem['type']];
162 $this->TemplateEngine->setType($this->currentItemType);
163 $this->TemplateEngine->setValues($this->currentItem['values']);
164 }
167 function execute()
168 {
169 $smarty = get_smarty();
171 // Assign possible sub-container objects.
172 $smarty->assign('subModule', $this->currentItemDescriptor['container']);
173 $smarty->assign('containerName', $this->currentItemDescriptor['name']);
174 $smarty->assign('containerDescription', $this->currentItemDescriptor['description']);
176 $smarty->assign('navigator', $this->renderNavigator());
180 $smarty->assign('template',$this->TemplateEngine->render());
181 return($smarty->fetch(get_template_path('goto/Config/DeviceConfig.tpl', TRUE)));
182 }
184 function save_object()
185 {
186 $this->TemplateEngine->save_object();
188 if(isset($_POST['addSubModule']) && isset($_POST['subModule'])){
189 $sub = get_post('subModule');
190 if(in_array($sub, $this->currentItemDescriptor['container'])){
192 }
193 }
195 if(isset($_GET['item'])){
196 $name = $this->idToName[$_GET['item']];
197 $this->setCurrentItem($name);
198 }
199 }
200 }
201 ?>