Code

Added file upload widget
[gosa.git] / gosa-plugins / goto / admin / systems / goto / Config / class_DeviceConfig.inc
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": "0x002",
23                             "syntax": "^[a-zA-Z0-9_+\\\\./-]+(\\\\[[<=>]+[a-zA-Z0-9_+\\\\.-]+\\\\])?$",
24                             "type": "file",
25                             "display": "Module dependencies"
26                     },
27                         "version": {
28                             "description": "The version of the puppet module",
29                             "required": true,
30                             "value": "",
31                             "syntax": "^[a-zA-Z0-9_+.-]+$",
32                             "type": "string",
33                             "display": "Module  version"
34                         },
35                         "name": {
36                             "description": "The name of the puppet module",
37                             "required": true,
38                             "value": "",
39                             "syntax": "^[a-zA-Z0-9_+.-]+$",
40                             "type": "string",
41                             "display": "Module name"
42                         },
43                         "description": {
44                             "required": false,
45                             "type": "string",
46                             "display": "Module description",
47                             "value": "",
48                             "description": "Text       briefly describing the module contents"
49                         }
50                 },
51                     "container": [
52                         "PuppetManifest",
53                     "PuppetFile",
54                     "PuppetTemplate"
55                         ],
56                     "name": "Module",
57                     "description": "Puppet     module"
58             },
59                 "root": {
60                     "options": {
62                     },
63                     "container": [
64                         "PuppetModule"
65                         ],
66                     "name": "Root",
67                     "description": "The root item"
68                 }
69         }';        
71         $this->itemConfig = json_decode($str, TRUE);
72         $this->TemplateEngine->load($this->itemConfig);
73         
74         // Check for the root object
75         if(!isset($this->itemConfig['root'])){
76             echo 'No root!';
77         }
79         // Set current item to 'root'.
80         $this->currentItemDescriptor = &$this->itemConfig['root'];
81         $this->currentItemValues = array();
82         $this->currentItem = array();
84         $this->addItem('root','root',array());
85         $this->setCurrentItem('root');
86         $this->addItem('PuppetModule','test1',
87                 array(
88                     'dependency' => '',
89                     'version' => '2.4-f',
90                     'name'  => 'Thundebird',
91                     'description' => 'Mozilla mail client')
92                 );
93         $this->addItem('PuppetModule','test2',
94                 array(
95                     'version' => 1,
96                     'name'  => 'Firefox',
97                     'description' => 'Test Module')
98                 );
99         $this->setCurrentItem('test1');
100         $this->addItem('PuppetTemplate','temp1',array('name' => 'temp1', 'file' => 'kekse.tpl'));
102         $this->setCurrentItem('root');
103     }
106     function renderNavigator($array = NULL)
107     {
108         $array = ($array == NULL)? $this->currentItemValues['root']: $array;
109         $str = "<ul>";
110         $plug = $_GET['plug'];
112         $id = array_search($array['name'],$this->idToName);
113         $str .= "<a href='?plug={$plug}&amp;item={$id}'>";
114         if($this->currentItemName == $array['name']){
115             $str .= "<b>".$array['name']."</b>";
116         }else{
117             $str .= $array['name'];
118         }
119         $str .= "&nbsp;<i>(".$array['type'].")</i>";
120         $str .= "</a>";
122         if(count($array['children'])){
123             foreach($array['children'] as $subItem){
124                 $str .= $this->renderNavigator($subItem);
125             } 
126         }
127         $str  .= "</ul>";
128         return($str);
129     }
131     function addItem($type,$name, $values)
132     {
133         $current = &$this->currentItem; 
134         $this->idToName[] = $name;
135         $new = array(
136                 'children' => array(),
137                 'type' => $type, 
138                 'name' => $name, 
139                 'values' => $values);
140         $this->currentItemValues[$name] = $new;
141         $current['children'][$name] = &$this->currentItemValues[$name];
142     }
144     function setCurrentItem($item)
145     {
146         if($this->currentItemName == $item) return;
148         // Save eventually changed values
149         if($this->currentItem){
150             foreach($this->TemplateEngine->getWidgets() as $widget){
151                 $this->currentItem['values'][$widget->getName()] = $widget->getValue();
152             }
153         }
156         $this->currentItemName = $item;
157         $this->currentItem = &$this->currentItemValues[$item];
158         $this->currentItemType = $this->currentItem['type'];
159         $this->currentItemDescriptor =&$this->itemConfig[$this->currentItem['type']];
160         $this->TemplateEngine->setType($this->currentItemType);
161         $this->TemplateEngine->setValues($this->currentItem['values']);
162     }
165     function execute()
166     {
167         $smarty = get_smarty();
168         
169         // Assign possible sub-container objects.
170         $smarty->assign('subModule', $this->currentItemDescriptor['container']);
171         $smarty->assign('containerName', $this->currentItemDescriptor['name']);
172         $smarty->assign('containerDescription', $this->currentItemDescriptor['description']);
174         $smarty->assign('navigator', $this->renderNavigator());
175         
178         $smarty->assign('template',$this->TemplateEngine->render());
179         return($smarty->fetch(get_template_path('goto/Config/DeviceConfig.tpl', TRUE)));
180     }
182     function save_object()
183     {
184         $this->TemplateEngine->save_object();
186         if(isset($_POST['addSubModule']) && isset($_POST['subModule'])){
187             $sub = get_post('subModule');
188             if(in_array($sub, $this->currentItemDescriptor['container'])){
189                 
190             }
191         }
192     
193         if(isset($_GET['item'])){
194             $name = $this->idToName[$_GET['item']];
195             $this->setCurrentItem($name);
196         }
197     }
199 ?>