Code

cf73a43db2dddc43a996981371fca4e30cfd619c
[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 $inherit_confirmation;
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->inherit_confirmation = new msg_dialog(_("Pass on all attributes to group members"),  _("This action will overwrite all attributes of the group members with the values specified in this object group. Do you want to proceed?"),CONFIRM_DIALOG);
192     }
193  
194     if (is_object($this->inherit_confirmation) && ($this->inherit_confirmation->is_confirmed())) {
195         $this->pass_attributes_to_members();
196     }
198     /* Watch for events */
199     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
201       /* Check if we have an DaemonEvent for this action */
202       $action = $this->mapActions[$_POST['saction']];
203       if(class_available("DaemonEvent")){
204         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
205         $macaddresses= array();
206         foreach ($this->members as $cn => $macAddress){
207           $macaddresses[]= $macAddress;
208         }
210         if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
211           $evt = $events['TRIGGERED']["DaemonEvent_".$action];
212           $tmp = new $evt['CLASS_NAME']($this->config);
213           $tmp->add_targets($macaddresses);
214           $tmp->set_type(TRIGGERED_EVENT);
215           $o_queue = new gosaSupportDaemon();
216           if(!$o_queue->append($tmp)){
217             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
218           }
219         }
220       } else {
221         msg_dialog::display(_("Event error"),
222             sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
223       }
224     }
227     /*************** 
228       Add remove NTP server
229      ***************/
231     /* Add new ntp Server to our list */
232     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
233       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
234     }
236     /* Delete selected NtpServer for list of used servers  */
237     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
238       foreach($_POST['gotoNtpServerSelected'] as $name){
239         unset($this->gotoNtpServer[$name]);
240       }
241     }
244     /*************** 
245       Prepare smarty 
246      ***************/
248     /* Set government mode */
249     $smarty= get_smarty();
251     if (isset($this->parent->by_name['termstartup'])){
252         $smarty->assign("is_termgroup", "1");
253     } else {
254         $smarty->assign("is_termgroup", "0");
255     }
257     $tmp = $this->plInfo();
258     foreach($tmp['plProvidedAcls'] as $name => $translated) {
259       $smarty->assign($name."ACL",$this->getacl($name));
260     }
262     foreach($this->attributes as $attr){
263       $smarty->assign($attr,      $this->$attr);
264     }
266     /* Variables */
267     foreach(array("gotoMode","gotoNtpServer") as $val){
268       $smarty->assign($val."_select", $this->$val);
269     }
271     $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
272           "update" => _("Software update"),
273           "wake" => _("Wake"),
274           "reinstall" => _("Reinstall"),
275           "rescan" => _("Rescan hardware"),
276           "localboot" => _("Force localboot")
277 #          "memcheck" => _("Memory test"),
278 #          "sysinfo"  => _("System analysis")
279         ));
281     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
282     $smarty->assign("modes", $this->modes);
284     $tmp = array();
285     foreach($this->gotoNtpServers as $server){
286       if(!in_array($server,$this->gotoNtpServer)){
287         $tmp[$server] = $server;
288       }
289     }
291     $smarty->assign("gotoNtpServers",$tmp); 
293     $smarty->assign("nfsservers",     $this->nfsservers);
294     $smarty->assign("swapservers",    $this->swapservers);
296     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']); 
297     $smarty->assign("gotoSyslogServer_select", $this->gotoSyslogServer); 
299     /* Variables */
300     foreach(array("gotoTerminalPath", "gotoSwapServer") as $val){
301             $smarty->assign($val."_select", $this->$val);
302     }
305     /* Show main page */
306     return ($smarty->fetch (get_template_path('termgroup.tpl', TRUE, dirname(__FILE__))));
307   }
310   function save_object()
311   {
312     plugin::save_object();  
313     /* Set inherit mode */
314     if(isset($_POST['workgeneric_posted'])){
315       if(isset($_POST["inheritTimeServer"])){
316         $this->inheritTimeServer = true;
317       }else{
318         $this->inheritTimeServer = false;
319       }
320     }
321   }
323   /* Save to LDAP */
324   function save()
325   {
326     if (isset($this->parent->by_name['termstartup'])){
327         $this->objectclasses= array("gotoTerminalTemplate");
328     } else {
329         $this->objectclasses= array("gotoWorkstationTemplate");
330     }
332     plugin::save();
334     /***************
335       Prepare special vars 
336      ***************/
338     /* Unset some special vars ... */
339     foreach (array("gotoSyslogServer") as $val){
340       if ($this->attrs[$val] == "default"){
341         $this->attrs[$val]= array();
342       }
343     }
345     /* Update ntp server settings */
346     if($this->inheritTimeServer){
347       $this->attrs['gotoNtpServer'] = "default";
348     }else{
349       /* Set ntpServers */
350       $this->attrs['gotoNtpServer'] = array();
351       foreach($this->gotoNtpServer as $server){
352         $this->attrs['gotoNtpServer'][] = $server;
353       }
354     }
357     /***************
358       Write to ldap 
359      ***************/
361     /* Write back to ldap */
362     $ldap= $this->config->get_ldap_link();
363     $ldap->cd($this->dn);
364     $this->cleanup();
365     $ldap->modify ($this->attrs);
367     if($this->initially_was_account){
368       new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
369     }else{
370       new log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
371     }
373     if(!$this->didAction){
374       $this->handle_post_events("modify");
375     }
376     if (!$ldap->success()){
377       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
378     }
379   }
381   
382   static function plInfo()
383   {
384     return (array(
385           "plShortName"   => _("System"),
386           "plDescription" => _("System group"),
387           "plSelfModify"  => FALSE,
388           "plDepends"     => array(),
389           "plPriority"    => 5,
390           "plSection"     => array("administration"),
391           "plCategory"    => array("ogroups"),
392           "plProvidedAcls"=> array(
393             "gotoMode"          => _("Mode"),     
394             "gotoSyslogServer"  => _("Syslog server"), 
395             "FAIstate"          => _("Action flag"), 
396             "gotoNtpServer"     => _("Ntp server"),
397             "gotoTerminalPath"    => _("Root server"),
398             "gotoSwapServer"      => _("Swap server"))
399           ));
400   }
402   function pass_attributes_to_members()
403   {
404     foreach ($this->member_dn as $dn) {
405       $member_obj = new worktabs($this->config, $this->config->data['TABS']['WORKTABS'], $dn, 'workstation');
406       $member_obj->by_object['workgeneric']->set_everything_to_inherited();
407       $member_obj->save();
408     }
409   }
411   function PrepareForCopyPaste($source)
412   {
413     /* Create used ntp server array */
414     $this->gotoNtpServer= array();
416     if(isset($source['gotoNtpServer'])){
417       $this->inheritTimeServer = false;
418       unset($source['gotoNtpServer']['count']);
419       foreach($source['gotoNtpServer'] as $server){
420         $this->gotoNtpServer[$server] = $server;
421       }
422     }
424     /* Set inherit checkbox state */
425     if(in_array("default",$this->gotoNtpServer)){
426       $this->inheritTimeServer = true;
427       $this->gotoNtpServer=array();
428     }
430     /* Create available ntp options */
431     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
432     foreach($this->gotoNtpServers as $key => $server){
433       if($server == "default"){
434         unset($this->gotoNtpServers[$key]);
435       }
436     }
437   }
440 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
441 ?>