1 <?php
3 /*! \brief A GOsa plugin which generates a device configuration dialog
4 */
5 class DeviceConfig extends plugin
6 {
7 private $TemplateEngine = NULL;
8 private $idToName = array();
9 private $currentItemName = "";
10 private $currentItemValues = array();
11 private $currentItem = array();
14 /*! \brief Constructs the device configuration plugin
15 * @param Config The GOsa configuration object.
16 */
17 function __construct(&$config, $dn)
18 {
19 // Load the template engine and tell her what template
20 // to use for the HTML it produces.
21 $this->TemplateEngine = new TemplateEngine($config);
22 $this->TemplateEngine->setTemplate('puppet.tpl');
23 $this->config = $config;
25 // CREATE Dummy entry
27 $str = '{
28 "PuppetModule": {
29 "options": {
30 "dependency": {
31 "description": "Modules that are needed to be installed for this module",
32 "required": false,
33 "value": "0x002",
34 "syntax": "^[a-zA-Z0-9_+\\\\./-]+(\\\\[[<=>]+[a-zA-Z0-9_+\\\\.-]+\\\\])?$",
35 "type": "file",
36 "display": "Module dependencies"
37 },
38 "version": {
39 "description": "The version of the puppet module",
40 "required": true,
41 "value": "",
42 "syntax": "^[a-zA-Z0-9_+.-]+$",
43 "type": "string",
44 "display": "Module version"
45 },
46 "name": {
47 "description": "The name of the puppet module",
48 "required": true,
49 "value": "",
50 "syntax": "^[a-zA-Z0-9_+.-]+$",
51 "type": "string",
52 "display": "Module name"
53 },
54 "description": {
55 "required": false,
56 "type": "string",
57 "display": "Module description",
58 "value": "",
59 "description": "Text briefly describing the module contents"
60 }
61 },
62 "container": [
63 "PuppetManifest",
64 "PuppetFile",
65 "PuppetTemplate"
66 ],
67 "name": "Module",
68 "description": "Puppet module"
69 },
70 "root": {
71 "options": {
73 },
74 "container": [
75 "PuppetModule"
76 ],
77 "name": "Root",
78 "description": "The root item"
79 }
80 }';
82 // Load the item-configuration description to populate the
83 // the available modules.
84 $this->itemConfig = json_decode($str, TRUE);
85 $this->TemplateEngine->load($this->itemConfig);
89 // CREATE Dummy entry
91 // Set current item to 'root'.
92 $this->addItem('root','root',array());
93 $this->setCurrentItem('root');
94 $this->addItem('PuppetModule','test1',
95 array(
96 'dependency' => '',
97 'version' => '2.4-f',
98 'name' => 'Thundebird',
99 'description' => 'Mozilla mail client')
100 );
101 $this->addItem('PuppetModule','test2',
102 array(
103 'version' => 1,
104 'name' => 'Firefox',
105 'description' => 'Test Module')
106 );
107 $this->setCurrentItem('test1');
108 $this->addItem('PuppetTemplate','temp1',
109 array(
110 'name' => 'temp1',
111 'file' => 'kekse.tpl')
112 );
114 $this->setCurrentItem('root');
115 }
118 /*! \brief Renders a navigation to allow to switch between the
119 * active mopdules.
120 * This method recursivly collects all module entries.
121 * @return HTML content which represents the navigation
122 */
123 function renderNavigator($array = NULL)
124 {
125 $array = ($array == NULL)? $this->currentItemValues['root']: $array;
126 $str = "<ul>";
127 $plug = $_GET['plug'];
129 $id = array_search($array['name'],$this->idToName);
130 $str .= "<a href='?plug={$plug}&item={$id}'>";
131 if($this->currentItemName == $array['name']){
132 $str .= "<b>".$array['name']."</b>";
133 }else{
134 $str .= $array['name'];
135 }
136 $str .= " <i>(".$array['type'].")</i>";
137 $str .= "</a>";
139 if(count($array['children'])){
140 foreach($array['children'] as $subItem){
141 $str .= $this->renderNavigator($subItem);
142 }
143 }
144 $str .= "</ul>";
145 return($str);
146 }
149 /*! \brief Add a new child-item to the currently selected one.
150 *
151 * @param String type The 'type' of the new object, eg. 'KickstartTemplate'
152 * @param String name The 'name' of the new object.
153 * @param Array values The initial values for this object.
154 * @return
155 */
156 function addItem($type,$name, $values)
157 {
158 $current = &$this->currentItem;
159 $this->idToName[] = $name;
160 $new = array(
161 'children' => array(),
162 'type' => $type,
163 'name' => $name,
164 'values' => $values);
165 $this->currentItemValues[$name] = $new;
166 $current['children'][$name] = &$this->currentItemValues[$name];
167 }
170 /*! \brief Selects an item as active and takes care
171 * of required post/get handling.
172 * @param String The name of the item we want to select.
173 * @return
174 */
175 function setCurrentItem($item)
176 {
177 // Do nothing if we're already where we wanted to switch to.
178 if($this->currentItemName == $item) return;
180 // Save eventually changed values
181 if($this->currentItem){
182 foreach($this->TemplateEngine->getWidgets() as $widget){
183 $this->currentItem['values'][$widget->getName()] = $widget->getValue();
184 }
185 }
187 // Set the new item info.
188 $this->currentItemName = $item;
189 $this->currentItem = &$this->currentItemValues[$item];
190 $this->currentItemType = $this->currentItem['type'];
191 $this->currentItemDescriptor =&$this->itemConfig[$this->currentItem['type']];
193 // Update the template engine to use another type of item and
194 // some other values.
195 $this->TemplateEngine->setType($this->currentItemType);
196 $this->TemplateEngine->setValues($this->currentItem['values']);
197 }
200 /*! \brief Renders the HTML content for the device-config plugin.
201 * @return String The generated HTML code.
202 */
203 function execute()
204 {
205 $smarty = get_smarty();
207 // Assign the navigation bar.
208 $smarty->assign('navigator', $this->renderNavigator());
210 // Assign possible sub-container objects.
211 $smarty->assign('subModule', $this->currentItemDescriptor['container']);
213 // Assign current item info
214 $smarty->assign('containerName', $this->currentItemDescriptor['name']);
215 $smarty->assign('containerDescription', $this->currentItemDescriptor['description']);
217 // Assign the generated HTML of Widgets.
218 $smarty->assign('template',$this->TemplateEngine->render());
219 return($smarty->fetch(get_template_path('goto/Config/DeviceConfig.tpl', TRUE)));
220 }
223 /*! \brief Keep track of posted values, some may be interesting for us.
224 * Tell the template engine to take care of posted values too.
225 * @param String
226 * @return
227 */
228 function save_object()
229 {
230 $this->TemplateEngine->save_object();
232 // Add sub-module requested.
233 if(isset($_POST['addSubModule']) && isset($_POST['subModule'])){
234 $sub = get_post('subModule');
235 if(in_array($sub, $this->currentItemDescriptor['container'])){
237 }
238 }
240 // Module switched
241 if(isset($_GET['item'])){
242 $name = $this->idToName[$_GET['item']];
243 $this->setCurrentItem($name);
244 }
245 }
246 }
247 ?>