Code

- Adapt actions to be more consistent overall
[gosa.git] / trunk / gosa-plugins / goto / admin / ogroups / goto / class_termgroup.inc
1 <?php
3 class termgroup extends plugin
4 {
5   /* attribute list for save action */
6   var $members= array();
8   /* Stores a list of the member dn's */
9   var $member_dn = array();
11   var $gotoMode           = "locked";
12   var $gotoSyslogServer   = "";
13   var $gotoNtpServer      = array();
14   var $gotoTerminalPath   = array();
15   var $gotoSwapServer     = array();
16   var $modes              = array();
17   var $inheritTimeServer  = true;
18   var $is_account                     = true; 
19   var $orig_dn            = "";
20   var $didAction          = FALSE;
21   var $mapActions   = array("halt"            => "halt",
22                             "reboot"          => "reboot",
23                             "update"          => "update",
24                             "reinstall"       => "reinstall",
25                             "rescan"          => "rescan",
26                             "wake"            => "wakeup",
27                             "localboot"       => "localboot"
28                            # These are currently not supported by the tftp daemon
29                            # "memcheck"        => "memcheck",
30                            # "sysinfo"         => "sysinfo"
31                            );
33   var $attributes     = array("gotoMode","gotoSyslogServer", "gotoNtpServer", "gotoTerminalPath", "gotoSwapServer");
34   var $objectclasses  = array("gotoWorkstationTemplate");
35   var $CopyPasteVars  = array("gotoNtpServers","modes","inheritTimeServer","members");
36   var $view_logged    = FALSE;
37   var $nfsservers     = array();
38   var $swapservers    = array();
39   var $member_of_ogroup= false;
40   var $members_inherit_from_group = true;
42   function termgroup (&$config, $dn= NULL, $parent= NULL)
43   {
44     /***************
45       Some  initialisations
46      ***************/
48     plugin::plugin($config, $dn, $parent);
49     $ldap= $config->get_ldap_link();
51     $this->is_account = true;
52     $this->modes["active"]= _("Activated");
53     $this->modes["locked"]= _("Locked");
54 //    $this->modes["memcheck"]= _("Memory test");
55 //    $this->modes["sysinfo"]= _("System analysis");
57     $this->orig_dn =    $this->dn;
59     /*************** 
60       Get mac addresses from member objects  
61      ***************/
63     /* We're only interested in the terminal members here, evaluate
64        these... */
65     if(isset($this->attrs['member'])){
66       for ($i= 0; $i<$this->attrs['member']['count']; $i++){
67         $member= $this->attrs['member'][$i];
68         array_push($this->member_dn, $member);
69         $ldap->cat($member, array('objectClass', 'macAddress', 'cn'));
70         if ($ldap->success()){
71           $attrs = $ldap->fetch();
72           if (in_array("gotoTerminal", $attrs['objectClass']) ||
73               in_array("gotoWorkstation", $attrs['objectClass'])){
74             if (isset($attrs['macAddress'])){
75               $this->members[$attrs['cn'][0]]= $attrs['macAddress'][0];
76             } else {
77               $this->members[$attrs['cn'][0]]= "";
78             }
79           }
80         }
81       }
82     }
84     /*************** 
85       Perpare NTP settings 
86      ***************/
88     /* Create used ntp server array */
89     $this->gotoNtpServer= array();
90     if(isset($this->attrs['gotoNtpServer'])){
91       $this->inheritTimeServer = false;
92       unset($this->attrs['gotoNtpServer']['count']);
93       foreach($this->attrs['gotoNtpServer'] as $server){
94         $this->gotoNtpServer[$server] = $server;
95       }
96     }
98     /* Get Share servers */
99     $tmp2 = array();
100     $tmp2['!']= _("Local swap");
101     foreach($this->config->data['SERVERS']['NBD'] as $server){
102       if($server != "default"){
103         $tmp2[$server]= $server;
104       }else{
105         if($this->member_of_ogroup){
106           $tmp2[$server]="["._("inherited")."]";
107         }
108       }
109     }
110     $this->swapservers= $tmp2;
112     $tmp2 = array();
113     foreach($this->config->data['SERVERS']['NFS'] as $server){
114       if($server != "default"){
115         $tmp2[$server]= $server;
116       }else{
117         if($this->member_of_ogroup){
118           $tmp2[$server]="["._("inherited")."]";
119         }
120       }
121     }
122     $this->nfsservers= $tmp2;
124     /* Set inherit checkbox state */
125     if(in_array("default",$this->gotoNtpServer)){
126       $this->inheritTimeServer = true;
127       $this->gotoNtpServer=array();
128     }
130     /* Create available ntp options */
131     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
132     foreach($this->gotoNtpServers as $key => $server){
133       if($server == "default"){
134         unset($this->gotoNtpServers[$key]);
135       }
136     }
137   }
139   function check()
140   {
141      /* Call common method to give check the hook */
142     $message= plugin::check();
144     if (!$this->acl_is_createable() && $this->dn == "new"){
145       $message[]= msgPool::permCreate();
146     }
148     /* Check for valid ntpServer selection */
149     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
150       $message[]= msgPool::required(_("NTP server"));
151     }
152     return($message);
153   }
155   function remove_from_parent()
156   {
157     /* Workstation startup is using gotoWorkstationTemplate too,
158         if we remove this oc all other not manged attributes will cause errors */
159     if(isset($this->attrs['gotoKernelParameters'])){
160       $this->objectclasses = array();
161     }
163     /* Remove acc */
164     plugin::remove_from_parent();
165     $ldap = $this->config->get_ldap_link();
166     $ldap->cd($this->orig_dn);
167     $ldap->modify($this->attrs);
168     $this->handle_post_events("remove");
169     new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
170   }
174   function execute()
175   {
176     /* Call parent execute */
177     plugin::execute();
179     if(!$this->view_logged){
180       $this->view_logged = TRUE;
181       new log("view","ogroups/".get_class($this),$this->dn);
182     }
185     /*************** 
186       Handle requested action
187      ***************/
189     /* Handle the inherit to members button */
190     if (isset($_POST['inheritToMembers'])) {
191       $this->members_inherit_from_group = true;
192     }
194     /* Watch for events */
195     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
197       /* Check if we have an DaemonEvent for this action */
198       $action = $this->mapActions[$_POST['saction']];
199       if(class_available("DaemonEvent")){
200         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
201         $macaddresses= array();
202         foreach ($this->members as $cn => $macAddress){
203           $macaddresses[]= $macAddress;
204         }
206         if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
207           $evt = $events['TRIGGERED']["DaemonEvent_".$action];
208           $tmp = new $evt['CLASS_NAME']($this->config);
209           $tmp->add_targets($macaddresses);
210           $tmp->set_type(TRIGGERED_EVENT);
211           $o_queue = new gosaSupportDaemon();
212           if(!$o_queue->append($tmp)){
213             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
214           }
215         }
216       } else {
217         msg_dialog::display(_("Event error"),
218             sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
219       }
220     }
223     /*************** 
224       Add remove NTP server
225      ***************/
227     /* Add new ntp Server to our list */
228     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
229       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
230     }
232     /* Delete selected NtpServer for list of used servers  */
233     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
234       foreach($_POST['gotoNtpServerSelected'] as $name){
235         unset($this->gotoNtpServer[$name]);
236       }
237     }
240     /*************** 
241       Prepare smarty 
242      ***************/
244     /* Set government mode */
245     $smarty= get_smarty();
247     if (isset($this->parent->by_name['termstartup'])){
248         $smarty->assign("is_termgroup", "1");
249     } else {
250         $smarty->assign("is_termgroup", "0");
251     }
253     $tmp = $this->plInfo();
254     foreach($tmp['plProvidedAcls'] as $name => $translated) {
255       $smarty->assign($name."ACL",$this->getacl($name));
256     }
258     foreach($this->attributes as $attr){
259       $smarty->assign($attr,      $this->$attr);
260     }
262     /* Variables */
263     foreach(array("gotoMode","gotoNtpServer") as $val){
264       $smarty->assign($val."_select", $this->$val);
265     }
267     $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
268           "update" => _("Software update"),
269           "wake" => _("Wake"),
270           "reinstall" => _("Reinstall"),
271           "rescan" => _("Rescan hardware"),
272           "localboot" => _("Force localboot")
273 #          "memcheck" => _("Memory test"),
274 #          "sysinfo"  => _("System analysis")
275         ));
277     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
278     $smarty->assign("modes", $this->modes);
280     $tmp = array();
281     foreach($this->gotoNtpServers as $server){
282       if(!in_array($server,$this->gotoNtpServer)){
283         $tmp[$server] = $server;
284       }
285     }
287     $smarty->assign("gotoNtpServers",$tmp); 
289     $smarty->assign("nfsservers",     $this->nfsservers);
290     $smarty->assign("swapservers",    $this->swapservers);
292     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']); 
293     $smarty->assign("gotoSyslogServer_select", $this->gotoSyslogServer); 
295     /* Variables */
296     foreach(array("gotoTerminalPath", "gotoSwapServer") as $val){
297             $smarty->assign($val."_select", $this->$val);
298     }
301     /* Show main page */
302     return ($smarty->fetch (get_template_path('termgroup.tpl', TRUE, dirname(__FILE__))));
303   }
306   function save_object()
307   {
308     plugin::save_object();  
309     /* Set inherit mode */
310     if(isset($_POST['workgeneric_posted'])){
311       if(isset($_POST["inheritTimeServer"])){
312         $this->inheritTimeServer = true;
313       }else{
314         $this->inheritTimeServer = false;
315       }
316     }
317   }
319   /* Save to LDAP */
320   function save()
321   {
322     if (isset($this->parent->by_name['termstartup'])){
323         $this->objectclasses= array("gotoTerminalTemplate");
324     } else {
325         $this->objectclasses= array("gotoWorkstationTemplate");
326     }
328     plugin::save();
330     /* Tell members to inherit all attributes if the flag is set  */
331     $this->inherit_attributes_to_members();
333     /***************
334       Prepare special vars 
335      ***************/
337     /* Unset some special vars ... */
338     foreach (array("gotoSyslogServer") as $val){
339       if ($this->attrs[$val] == "default"){
340         $this->attrs[$val]= array();
341       }
342     }
344     /* Update ntp server settings */
345     if($this->inheritTimeServer){
346       $this->attrs['gotoNtpServer'] = "default";
347     }else{
348       /* Set ntpServers */
349       $this->attrs['gotoNtpServer'] = array();
350       foreach($this->gotoNtpServer as $server){
351         $this->attrs['gotoNtpServer'][] = $server;
352       }
353     }
356     /***************
357       Write to ldap 
358      ***************/
360     /* Write back to ldap */
361     $ldap= $this->config->get_ldap_link();
362     $ldap->cd($this->dn);
363     $this->cleanup();
364     $ldap->modify ($this->attrs);
366     if($this->initially_was_account){
367       new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
368     }else{
369       new log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
370     }
372     if(!$this->didAction){
373       $this->handle_post_events("modify");
374     }
375     if (!$ldap->success()){
376       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
377     }
378   }
380   
381   static function plInfo()
382   {
383     return (array(
384           "plShortName"   => _("System"),
385           "plDescription" => _("System group"),
386           "plSelfModify"  => FALSE,
387           "plDepends"     => array(),
388           "plPriority"    => 5,
389           "plSection"     => array("administration"),
390           "plCategory"    => array("ogroups"),
391           "plProvidedAcls"=> array(
392             "gotoMode"          => _("Mode"),     
393             "gotoSyslogServer"  => _("Syslog server"), 
394             "FAIstate"          => _("Action flag"), 
395             "gotoNtpServer"     => _("Ntp server"),
396             "gotoTerminalPath"    => _("Root server"),
397             "gotoSwapServer"      => _("Swap server"))
398           ));
399   }
401   function inherit_attributes_to_members()
402   {
403     if ($this->members_inherit_from_group) {
404       foreach ($this->member_dn as $dn) {
405         $member_obj = new worktabs($this->config, $this->config->data['TABS']['WORKTABS'], $dn);
406         $member_obj->by_object['workgeneric']->set_everything_to_inherited();
407         $member_obj->save();
408       }
409     }
410   }
412   function PrepareForCopyPaste($source)
413   {
414     /* Create used ntp server array */
415     $this->gotoNtpServer= array();
417     if(isset($source['gotoNtpServer'])){
418       $this->inheritTimeServer = false;
419       unset($source['gotoNtpServer']['count']);
420       foreach($source['gotoNtpServer'] as $server){
421         $this->gotoNtpServer[$server] = $server;
422       }
423     }
425     /* Set inherit checkbox state */
426     if(in_array("default",$this->gotoNtpServer)){
427       $this->inheritTimeServer = true;
428       $this->gotoNtpServer=array();
429     }
431     /* Create available ntp options */
432     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
433     foreach($this->gotoNtpServers as $key => $server){
434       if($server == "default"){
435         unset($this->gotoNtpServers[$key]);
436       }
437     }
438   }
441 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
442 ?>