Code

Added more translations, fixed layout
[gosa.git] / plugins / addons / gotomasses / class_gotomasses.inc
1 <?php
3 class gotomasses extends plugin
4 {
5   /* Definitions */
6   var $plHeadline     = "System deployment";
7   var $plDescription  = "This does something";
9   /* attribute list for save action */
10   var $attributes= array();
11   var $objectclasses= array();
13   /* Source file that contains the gotomasses data */
14   var $data_file = "Undefined"; #Set in constructor 
16   /* Queue tasks */
17   var $tasks = array();
18   var $current =false;
19   var $dialog = FALSE;
20   var $ids_to_remove = array();
22   function gotomasses($config, $dn= NULL)
23   {
24     /* Define source file */
25     $this->data_file = CONFIG_DIR."/gotomasses_machines";
26   
27     /* Include config object */
28     $this->config= $config;
29     $this->load_gotomasses_data();
30   }
33   function execute()
34   {
35     $smarty = get_smarty();
36     /************
37      * Handle posts 
38      ************/
40     $s_entry = $s_action = "";
41     $arr = array( "/^edit_task_/"=>"edit","/^remove_task_/"=>"remove",
42                   "/^new_task_/"=>"new_task","/^remove_multiple_task_/" => "remove_multiple");
43     foreach($arr as $regex => $action){
44       foreach($_POST as $name => $value){
45         if(preg_match($regex,$name)){
46           $s_action = $action;
47           $s_entry  = preg_replace($regex,"",$name);
48           $s_entry  = preg_replace("/_(x|y)$/","",$s_entry);
49         }
50       }
51     }
53     /* Edit posted from list link */
54     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
55       $s_action = "edit";
56       $s_entry = $_GET['id'];
57     }
59     /************
60      * List posts 
61      ************/
63     /* Remove multiple */
64     if($s_action == "remove_multiple"){
65       if(!$this->acl_is_removeable()){
66         print_red(_("You are not allowed to remove a task."));
67       }else{
68         $this->ids_to_remove = $this->list_get_selected_items();
69         $tmp = "";
70         foreach($this->ids_to_remove as $id){
71           $task = $this->tasks[$id];
72           $tmp.= "\n".$this->target_to_string($task);
73         }
74         $smarty->assign("multiple", TRUE); 
75         $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
76         $this->current = $s_entry;
77         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
78       }
79     }
81     /* Remove specified tasks */
82     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
83       foreach($this->ids_to_remove as $id){
84         if(isset($this->tasks[$id])){
85           unset($this->tasks[$id]);
86         }
87       }
88       $this->save();
89     }
91     /* Remove entry from list */
92     if($s_action == "remove" && isset($this->tasks[$s_entry])){
93       if(!$this->acl_is_removeable()){
94         print_red(_("You are not allowed to remove a task."));
95       }else{
96         $entry = $this->tasks[$s_entry];
97         $this->current = $s_entry;
98         $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),
99                                     "<pre>".$this->target_to_string($entry)."</pre>"));
100         $smarty->assign("multiple", FALSE); 
101         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
102       }
103     }
105     /* Remove entry, remove is confirmed */
106     if($this->current != -1 && isset($_POST['delete_confirm'])){
107       unset($this->tasks[$this->current]);
108       $this->current = -1;
109       $this->save();
110     }
112     /* Remove aborted */
113     if(isset($_POST['delete_cancel'])){
114       $this->ids_to_remove = array();;
115     }
117     /* Edit selected entry */
118     if($s_action == "edit" && isset($this->tasks[$s_entry])){
119       $entry = $this->tasks[$s_entry];
120       $this->dialog = new goto_task($this->config,$this,$entry);
121       $this->current = $s_entry;
122     }
124     /* New entry */
125     if($s_action== "new_task" && $this->acl_is_createable()){
126       $this->dialog = new goto_task($this->config,$this);
127       $this->current = -1;
128     }
129   
130     /* Close dialog */
131     if(isset($_POST['close_goto_task'])){
132       $this->dialog = FALSE;
133       $this->current = -1;
134     }
136     /* Close dialog */
137     if((isset($_POST['save_goto_task']) || isset($_POST['apply_goto_task'])) && is_object($this->dialog) ){
138       $this->dialog->save_object();
139       $msgs = $this->dialog->check();
140       if(count($msgs)){
141         foreach($msgs as $msg){
142           print_red($msg);  
143         }
144       }else{
145         if(isset($this->tasks[$this->current]) && $this->current != -1){
146           $this->tasks[$this->current] = $this->dialog->save();
147         }else{
148           $this->tasks[] = $this->dialog->save();
149         }
150         if(!isset($_POST['apply_goto_task'])){
151           $this->dialog = FALSE;
152           $this->current = -1;
153         }
154         $this->save();
155       }
156     }
158     /* Display dialogs if currently opened */
159     if(is_object($this->dialog)){
160       $this->dialog->save_object();
161       return($this->dialog->execute());
162     }
165     /************
166      * Handle Divlist 
167      ************/
169     $plug = $_GET['plug'];
170     $divlist = new MultiSelectWindow($this->config,"GotoMasses","gotomassses");
171     $divlist->SetSummary(_("List of system deployment tasks"));
172     $divlist->SetHeadpageMode();
173     $divlist->EnableCloseButton(FALSE);
174     $divlist->EnableSaveButton(FALSE);
175     $divlist->SetInformation(_("This menu allows you to add, remove and change the properties of system deployment tasks."));
176  
177     /* Display add button if allowed */ 
178     $header = "<div style='padding:5px'>";
179     if($this->acl_is_createable()){ 
180       $header .= "<input type='image' src='images/gotomasses_new_task.png' name='new_task' class='center'>&nbsp;";
181     }
183     /* Display add button if allowed */ 
184     if($this->acl_is_removeable()){ 
185       $header .= "<input type='image' src='images/edittrash.png' name='remove_multiple_task' class='center'>&nbsp;";
186     }
187     $header .= "</div>";
189     /* Get Permissions */ 
190     $ui = get_userinfo();
191     $acls = $this->getacl("");
192     foreach($ui->get_module_departments("gotomasses") as $dep){
193       $acls .= $ui->get_category_permissions($dep,"gotomasses");
194     }
195  
196     /* Create divlist */
197     $divlist->SetListHeader($header);
198  /* Toggle all selected / deselected */
199     $chk = "<input type='checkbox' id='select_all' name='select_all'
200                onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
202     /* set Page header */
203     $divlist->AddHeader(array("string"=> $chk,          "attach"=>"style='width:20px;'"));
205     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=object'>"._("Target")."</a>"));
206     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=schedule'>"._("Schedule")."</a>",
207                                       "attach"=>"style='width:100px;'"));
208     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=action'>"._("Type")."</a>",
209                                       "attach"=>"style='width:80px;'"));
210     $divlist->AddHeader(array("string"=>_("Action"),
211                                       "attach"=>"style='border-right:0px;width:40px;'"));
213     /* Create edit link */
214     $edit_link = "<div style='width:100%;overflow:hidden;'><nobr><a href='?plug=".$_GET['plug']."&act=edit&id=%key%'>%str%</nobr></div>";
215   
216     if(!empty($acls)){
217       foreach($this->tasks as $key => $task){
218         $action = "<input type='image' src='images/edit.png' name='edit_task_".$key."' class='center' alt='"._("Edit")."'>";
219         if($this->acl_is_removeable()){
220           $action.= "<input type='image' src='images/edittrash.png' name='remove_task_".$key."' class='center' alt='"._("Reomve")."'>";
221         }
222         /* Create each field */
223         $field0 = array("string" => "<input type='checkbox' id='item_selected_".$key."' name='item_selected_".$key."'>" ,
224                         "attach" => "style='width:20px;'");
226         $field1 = array("string" => preg_replace(array("/%key%/","/%str%/"),array($key,$this->target_to_string($task)),$edit_link));
227         $field2 = array("string" => $this->time_to_string($task),"attach" => "style='width:100px;'");
228         $field3 = array("string" => $this->action_to_string($task),"attach" => "style='width:80px;'");
229         $field4 = array("string" => $action,"attach" => "style='text-align:right;width:40px;border-right:0px;'");
230         $divlist->AddElement(array($field0,$field1,$field2,$field3,$field4));
231       }
232     }
233  
234     return($divlist->Draw());
235   }
237   
238   function target_to_string($data)
239   {
240     $ret = "";
241     if($data['Action'] == "initial_install"){
242       foreach($data['Initial_Target'] as $target){
243         $ret .= $target['MAC'].", ";
244       } 
245     }else{
246       foreach($data['Target'] as $target){
247         $ret .= $target.", ";
248       } 
249     }
250     return(preg_replace("/, $/","",$ret));
251   }
253   
254   function time_to_string($data)
255   {
256     return($data['Minute']." ".$data['Hour']." ".$data['Day']." ".$data['Month']." ".$data['Weekday']);
257   }
259   
260   function action_to_string($data)
261   {
262     $tmp = $this->get_actions();
263     if(isset($tmp[$data['Action']])){
264       return($tmp[$data['Action']]);
265     }else{
266       return(_("Unknown"));
267     }
268   }
270   
271   function load_gotomasses_data()
272   {
273     $ui = get_userinfo();
275     /* Check file existence */
276     if(!file_exists($this->data_file) || !is_readable($this->data_file)){
277       print_red(sprintf(_("Can't locate or read gotomasses storage file '%s'."),$this->data_file));
278       return(FALSE);
279     }
281     /* check if file is readable */
282     $fp = @fopen($this->data_file,"r");
283     if(!$fp){
284       print_red(sprintf(_("Can't read gotomasses storage file '%s'."),$this->data_file));
285       return(FALSE);
286     }
288     /* Get file contents */
289     $data ="";
290     while(!feof($fp)){
291       $data.= fread($fp,512);
292     }
294     /* Get lines from file */
295     $this->tasks  = array(); 
296     $comment      = "";
297     $rows         = split("\n",$data);
299     /* Walk trough rows and parse data */
300     foreach($rows as $row){
302       /* Skip empty lines */
303       $row = trim($row);
304       if(empty($row)){
305         continue;
306       }
308       /* Get comment, if available */     
309       if(preg_match("/^#/",$row)){
310         $comment = preg_replace("/^#/","",$row);
311         continue;
312       }
314       /* Split row into minutes/ hours ...*/ 
315       $row    = preg_replace('/[\t ]/umi'," ",$row);
316       $row    = preg_replace('/  */umi'," ",$row);
317       $parts  = split(" ",$row);
319       if(count($parts) != 8){
320         print_red(_("Entry broken, skipped."));
321       }else{
323         $entry = array();
324         $entry['Minute']  = $parts[0];  
325         $entry['Hour']    = $parts[1];  
326         $entry['Day']     = $parts[2];  
327         $entry['Month']   = $parts[3];  
328         $entry['Weekday'] = $parts[4];  
329         $entry['Action']  = $parts[5];  
330         $entry['OGroup']  = $parts[6];  
331         if($entry['Action'] == "initial_install"){
332           $tmp2 = split(";",$parts[7]);
333           foreach($tmp2 as $target){
334             $tmp = split(",",$target);
335             $entry['Initial_Target'][]  = array("MAC" => $tmp[0],"IP"=>$tmp[1],"NAME" => $tmp[2]);
336           }
337           $entry['Target']  = array();
338         }else{
339           $entry['Initial_Target']  = array();
340           $entry['Target']  = split(";",$parts[7]);
341         }
342         $entry['Comment'] = $comment;  
343         $this->tasks []   = $entry;
344       }
345     } 
346   }
349   function save_gotomasses_data()
350   {
351     $str = "#GOsa generated file, please just modify if you realy know what you do.";
352     foreach($this->tasks as $task){
353       $str .= "\n#".trim($task['Comment']);
354       $str .= "\n";
355       if($task['Action'] == "initial_install"){
356         $str .= "*     *     *     *     *     ";
357       }else{
358         $str .= str_pad($task['Minute'] ,5," ")." ";
359         $str .= str_pad($task['Hour']   ,5," ")." ";
360         $str .= str_pad($task['Day']    ,5," ")." ";
361         $str .= str_pad($task['Month']  ,5," ")." ";
362         $str .= str_pad($task['Weekday'],5," ")." ";
363       }
364       $str .= str_pad($task['Action'] ,5," ")." ";
365       $str .= str_pad($task['OGroup'] ,5," ")." ";
366       if($task['Action'] == "initial_install"){
367         foreach($task['Initial_Target'] as $target){
368           $str .= $target['MAC'].",".$target['IP'].",".$target['NAME'].";";
369         }
370       }else{
371         foreach($task['Target'] as $target){
372           $str .= $target.";";
373         }
374       }
375       $str = preg_replace("/;$/","",$str);
376     }
378     /* check file existence*/
379     $ui = get_userinfo();
380     if(!file_exists($this->data_file) || !is_writeable($this->data_file)){
381       print_red(sprintf(_("Can't locate or write gotomasses storage file '%s'."),$this->data_file));
382       return(FALSE);
383     }
385     /* check permissions */
386     $fp = @fopen($this->data_file,"w");
387     if(!$fp){
388       print_red(sprintf(_("Can't read gotomasses storage file '%s'."),$this->data_file));
389       return(FALSE);
390     }
392     /* Write contents */
393     $str .= "\n";
394     fwrite($fp,$str);
395     fclose($fp);
396   }
399   function save_object()
400   {
401   }
404   /* Return list of object groups */
405   function get_object_groups()
406   {
407     $ret = array();
408     $ldap = $this->config->get_ldap_link();
409     $ldap->cd($this->config->current['BASE']);
410     $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
411     while($attrs = $ldap->fetch()){
412       $ret[$attrs['cn'][0]] = $attrs['cn'][0];
413     }
414     return($ret); 
415   }
417   
418   function save()
419   {
420     $this->save_gotomasses_data();
421   }
424   function list_get_selected_items()
425   {
426     $ids = array();
427     foreach($_POST as $name => $value){
428       if(preg_match("/^item_selected_[0-9]*$/",$name)){
429         $id   = preg_replace("/^item_selected_/","",$name);
430         $ids[$id] = $id;
431       }
432     }
433     return($ids);
434   }
437   function get_actions()
438   {
439     /* Prepare list of available actions */
440     $actions = array(       "reboot"          => _("Reboot"),
441                             "localboot"       => _("Localboot"),
442                             "halt"            => _("Halt system"),
443                             "initial_install" => _("Initial installation"),
444                             "update"          => _("Update"),
445                             "reinstall"       => _("(Re)Install"),
446                             "rescan"          => _("Rescan"),
447                             "wake"            => _("Wake"),
448                             "memcheck"        => _("Memory check"));
449     return($actions);
450   }
453   function plInfo()
454   {
455     return (array(
456         "plShortName"   => _("System mass deployment"),
457         "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
458         "plSelfModify"  => FALSE,
459         "plDepends"     => array(),
460         "plPriority"    => 0,
461         "plSection"     => array("addon"),
462         "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
463         "plProvidedAcls" => array("Comment"   => _("Description"), 
464                                   "Action"    => _("Action"),
465                                   "Day"       => _("Day"),
466                                   "Minute"    => _("Minute"),
467                                   "Hour"      => _("Hour"),
468                                   "Month"     => _("Month"),
469                                   "Weekday"   => _("Week day"),
470                                   "Target"    => _("Target"))
471         ));
472   }
474 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
475 ?>