1 <?php
3 class DeviceConfig extends plugin
4 {
6 /* Some plugin management related attributes
7 * See class plugin for details.
8 */
9 public $view_logged = FALSE;
10 public $ignore_account = FALSE;
11 public $parent = NULL;
13 public $parameterList = NULL;
14 public $usedElementsList = NULL;
15 public $assignableElementsList = NULL;
17 public $usedElements = array();
18 public $assignableElements = array();
19 public $parameterValues = array();
20 public $parameterWidgets = array();
22 public $initialized = FALSE;
25 /*! \brief Initialize the plugin and its widgets.
26 */
27 function __construct(&$config, $dn, $parent)
28 {
29 plugin::plugin($config, $dn);
31 // Get the account information from the generic class.
32 $this->is_account = $this->initially_was_account = FALSE;
33 $this->_device = &$parent;
34 $this->deviceUUID = $parent->deviceUUID;
36 // Create assignable Elements list.
37 $this->assignableElementsList = new sortableListing();
38 $this->assignableElementsList->setDeleteable(FALSE);
39 $this->assignableElementsList->setEditable(TRUE);
40 $this->assignableElementsList->setWidth("100%");
41 $this->assignableElementsList->setHeight("200px");
42 $this->assignableElementsList->setHeader(array(_("Name"),_("Description"),_("Add")));
43 $this->assignableElementsList->setColspecs(array('*','*','20'));
44 $this->assignableElementsList->setAcl($this->getacl(''));
46 // Create used Elements list.
47 $this->usedElementsList = new sortableListing();
48 $this->usedElementsList->setDeleteable(TRUE);
49 $this->usedElementsList->setInstantDelete(FALSE);
50 $this->usedElementsList->setEditable(FALSE);
51 $this->usedElementsList->setWidth("100%");
52 $this->usedElementsList->setHeight("200px");
53 $this->usedElementsList->setHeader(array(_("Name"), _("Description"), _("Remove")));
54 $this->usedElementsList->setColspecs(array('*','*','20'));
55 $this->usedElementsList->setAcl($this->getacl(''));
57 // Create parameter Elements list.
58 $this->parameterList = new sortableListing();
59 $this->parameterList->setDeleteable(FALSE);
60 $this->parameterList->setInstantDelete(FALSE);
61 $this->parameterList->setEditable(FALSE);
62 $this->parameterList->setWidth("100%");
63 $this->parameterList->setHeight("200px");
64 $this->parameterList->setHeader(array(_("Name"), _("Description"), _("Value")));
65 $this->parameterList->setColspecs(array('*','*','350px'));
66 $this->parameterList->setAcl($this->getacl(''));
67 }
70 function check()
71 {
72 }
75 /*! \brief Generates the HTML output for the plugin
76 */
77 function execute()
78 {
79 // Initialize to plugin
80 if(!$this->initialized){
82 // Load config settings for the current device.
83 $rpc = $this->config->getRpcHandle();
84 $res = $rpc->systemGetConfigParameters($this->deviceUUID);
85 if(!$rpc->success()){
86 $smarty = get_smarty();
87 $smarty->assign("error", TRUE);
88 $smarty->assign("errorMsg", msgPool::rpcError($rpc->get_error()));
89 return($smarty->fetch(get_template_path('goto/Device/DeviceConfig.tpl', TRUE)));
90 }else{
92 // Initialization completed.
93 $this->initialized = TRUE;
94 $this->is_account = $this->initially_was_account = isset($res['item']);
95 if(isset($res['var'])) $this->parameterValues = $res['var'];
96 if(isset($res['item'])) $this->usedElements = $res['item'];
97 }
98 }
100 // Load assignable elements.
101 $rpc = $this->config->getRpcHandle();
102 $release = $this->parent->by_object['InstallRecipe']->installRelease;
103 $release = preg_replace("/^[^\/]+\//","", $release);
104 $assignableElements = $rpc->listAssignableElements($release);
105 if(!$rpc->success()){
106 $smarty = get_smarty();
107 $smarty->assign("error", TRUE);
108 $smarty->assign("errorMsg", msgPool::rpcError($rpc->get_error()));
109 return($smarty->fetch(get_template_path('goto/Device/DeviceConfig.tpl', TRUE)));
110 }else{
111 $this->assignableElements = $assignableElements;
112 }
114 // Log account access
115 if($this->is_account && !$this->view_logged){
116 $this->view_logged = TRUE;
117 new log("view","Device/".get_class($this),$this->dn);
118 }
120 /***************
121 Handle account state
122 ***************/
124 // Allow to add or remove the distribution list extension
125 if(isset($_POST['modify_state'])){
126 if($this->is_account && $this->acl_is_removeable()){
127 $this->is_account= FALSE;
128 }elseif(!$this->is_account && $this->acl_is_createable()){
129 $this->is_account= TRUE;
130 }
131 }
133 // Show account status-changer
134 $display = "";
135 if ($this->parent !== NULL){
136 if ($this->is_account){
137 $display= $this->show_disable_header(_("Remove config mangement"),
138 msgPool::featuresEnabled(_("Config management")));
139 } else {
140 $display= $this->show_enable_header(_("Add Config management"),
141 msgPool::featuresDisabled(_("Config management")));
142 return ($display);
143 }
144 }
146 /***************
147 Create the HTML content with its lists
148 ***************/
150 // Fill list of assignable elements
151 $data = $lData = array();;
152 foreach($this->assignableElements as $name => $element){
154 // skip already assigned elements
155 if(in_array_strict($name, $this->usedElements)) continue;
157 // Create the list elements.
158 $data[$name] = $element;
159 $desc = (isset($element['description'])) ? $element['description'] : '';
160 $lData[$name] = array('data' => array($name, $desc));
162 }
163 $this->assignableElementsList->setListData($data, $lData);
164 $this->assignableElementsList->update();
166 // Build list of assigned Elements
167 $data = $lData = array();;
168 foreach($this->usedElements as $name){
170 // check if the assgined element is still available, if not mark it in red.
171 if(isset($this->assignableElements[$name])){
172 $element = $this->assignableElements[$name];
173 $data[$name] = $element;
174 $desc = (isset($element['description'])) ? $element['description'] : '';
175 }else{
176 $element = NULL;
177 $data[$name] = $element;
178 $desc = "<font color='red'>"._("Unknown config item")."</font>";
179 }
180 $lData[$name] = array('data' => array($name, $desc));
181 }
182 $this->usedElementsList->setListData($data, $lData);
183 $this->usedElementsList->update();
185 // Build up parameter list
186 $data = $lData = array();;
187 foreach($this->usedElements as $name){
189 // Skip parameters for not existent items.
190 if(!isset($this->assignableElements[$name]['parameter'])) continue;
192 // Build up the parameter list using the 'TemplateWidget_string' class.
193 $parameter = $this->assignableElements[$name]['parameter'];
194 foreach($parameter as $param => $desc){
195 $data[$param] = NULL;
196 $value = (isset($this->parameterValues[$param])) ? $this->parameterValues[$param] : "";
198 // Create template widget on demand.
199 if(!isset($this->parameterWidgets[$param])){
200 $this->parameterWidgets[$param] = new TemplateWidget_stringLong($this->config, $param, $value,
201 $desc, '', FALSE, 'string', $param);
203 $this->parameterWidgets[$param]->setWriteable(preg_match("/w/",$this->getacl('')));
204 $this->parameterWidgets[$param]->setReadable(preg_match("/r/",$this->getacl('')));
205 }
206 $w = $this->parameterWidgets[$param];
207 $lData[$param] = array('data' => array($w->getDisplayName(),$desc, $w->render()));
208 }
209 }
210 $this->parameterList->setListData($data, $lData);
211 $this->parameterList->update();
213 // Build up smarty template.
214 $smarty = get_smarty();
215 $smarty->assign("error", FALSE);
216 $smarty->assign("assignableElementsList", $this->assignableElementsList->render());
217 $smarty->assign("usedElementsList", $this->usedElementsList->render());
218 $smarty->assign("parameterList", $this->parameterList->render());
219 return($display.$smarty->fetch(get_template_path('goto/Device/DeviceConfig.tpl', TRUE)));
220 }
223 /*! \brief Act on posted values.
224 * And keep track of posted values.
225 */
226 function save_object()
227 {
228 // Get posted parameter values
229 foreach($this->parameterWidgets as $name => $w){
230 if($w->isWriteable()){
231 $w->save_object();
232 $this->parameterValues[$name] = $w->getValue();
233 }
234 }
236 if(preg_match("/w/", $this->getacl(''))){
238 // Act on Add-Element actions
239 $this->assignableElementsList->save_object();
240 $action = $this->assignableElementsList->getAction();
241 if($action['action'] == 'edit'){
242 $this->usedElements[] = $this->assignableElementsList->getKey($action['targets'][0]);
243 $this->usedElements = array_unique($this->usedElements);
244 }
246 // Act on Remove-Item actions
247 $this->usedElementsList->save_object();
248 $action = $this->usedElementsList->getAction();
249 if($action['action'] == 'delete'){
250 $name = $this->usedElementsList->getKey($action['targets'][0]);
251 $this->usedElements = array_remove_entries_ics(array($name), $this->usedElements);
252 }
253 }
254 }
257 /*! \brief Save changes back to the GOsa-ng server.
258 */
259 function save()
260 {
261 // If no items are assinged we've to remove the extension.
262 if(!count($this->usedElements)) {
263 $this->remove_from_parent();
264 return;
265 }
267 // Load assignable elements.
268 $rpc = $this->config->getRpcHandle();
269 $release = $this->parent->by_object['InstallRecipe']->installRelease;
270 $release = preg_replace("/^[^\/]+\//","", $release);
271 $assignableElements = $rpc->listAssignableElements($release);
272 if(!$rpc->success()){
273 msg_dialog::display(_("Error"), msgPool::rpcError($rpc->get_error()), ERROR_DIALOG);
274 return(NULL);
275 }else{
276 $this->assignableElements = $assignableElements;
277 }
279 // Collect paramter list.
280 $params = array();
281 foreach($this->usedElements as $name){
282 if(isset($this->assignableElements[$name]['parameter'])){
283 $params = array_merge($params, $this->assignableElements[$name]['parameter']);
284 }
285 }
287 // Decide whoch parameter has to be saved and which has to be removed.
288 $save = array();
289 foreach($this->parameterValues as $name => $value){
290 if(isset($params[$name])){
291 $save[$name] = $value;
292 }
293 }
295 // Save changes back to server.
296 $rpc = $this->config->getRpcHandle();
297 $installConfigManagement = $this->parent->by_object['InstallRecipe']->installConfigManagement;
298 $rpc->systemSetConfigParameters($this->deviceUUID, array('method' => $installConfigManagement,
299 'var' => $save, 'item' => array_values($this->usedElements)));
300 if(!$rpc->success()){
301 msg_dialog::display(_("Error"), msgPool::rpcError($rpc->get_error()), ERROR_DIALOG);
302 return(NULL);
303 }
304 }
307 /*! \brief Removes the account from the device.
308 */
309 function remove_from_parent()
310 {
311 // Save changes back to server.
312 $rpc = $this->config->getRpcHandle();
313 $rpc->removeConfigParameters($this->deviceUUID);
314 if(!$rpc->success()){
315 msg_dialog::display(_("Error"), msgPool::rpcError($rpc->get_error()), ERROR_DIALOG);
316 return(NULL);
317 }
318 }
321 static function plInfo()
322 {
323 return (array(
324 "plShortName" => _("Device config"),
325 "plDescription" => _("Device config"),
326 "plSelfModify" => FALSE,
327 "plDepends" => array(),
328 "plPriority" => 1,
329 "plSection" => array("administration"),
330 "plCategory" => array("Device"),
331 "plProvidedAcls" => array(
332 )));
333 }
334 }
335 ?>