Code

Add gotomasses remove template
[gosa.git] / plugins / addons / gotomasses / class_gotomasses.inc
1 <?php
3 class gotomasses extends plugin
4 {
5   /* Definitions */
6   var $plHeadline     = "System mass 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;
21   function gotomasses($config, $dn= NULL)
22   {
23     /* Define source file */
24     $this->data_file = CONFIG_DIR."/gotomasses_machines";
25   
26     /* Include config object */
27     $this->config= $config;
29     $this->load_gotomasses_data();
30   }
34   function execute()
35   {
36     $smarty = get_smarty();
37     /************
38      * Handle posts 
39      ************/
41     $s_entry = $s_action = "";
42     $arr = array("/^edit_task_/"=>"edit","/^remove_task_/"=>"remove","/^new_task_/"=>"new_task");
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     /************
54      * List posts 
55      ************/
57     /* Remove entry from list */
58     if($s_action == "remove" && isset($this->tasks[$s_entry])){
59       $smarty->assign("info",_("Your are about to delete a gotomasses task."));
60       $this->current = $s_entry;
61       return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
62     }
64     /* Remove entry, remove is confirmed */
65     if($this->current != -1 && isset($_POST['delete_confirm'])){
66       unset($this->tasks[$this->current]);
67       $this->current = -1;
68       $this->save();
69     }
71     /* Edit selected entry */
72     if($s_action == "edit" && isset($this->tasks[$s_entry])){
73       $entry = $this->tasks[$s_entry];
74       $this->dialog = new goto_task($this->config,$this,$entry);
75       $this->current = $s_entry;
76     }
78     /* New entry */
79     if($s_action== "new_task"){
80       $this->dialog = new goto_task($this->config,$this);
81       $this->current = -1;
82     }
83   
84     /* Close dialog */
85     if(isset($_POST['close_goto_task'])){
86       $this->dialog = FALSE;
87       $this->current = -1;
88     }
90     /* Close dialog */
91     if(isset($_POST['save_goto_task']) && is_object($this->dialog)){
92       $this->dialog->save_object();
93       $msgs = $this->dialog->check();
94       if(count($msgs)){
95         foreach($msgs as $msg){
96           print_red($msg);  
97         }
98       }else{
99         if(isset($this->tasks[$this->current]) && $this->current != -1){
100           $this->tasks[$this->current] = $this->dialog->save();
101         }else{
102           $this->tasks[] = $this->dialog->save();
103         }
104         $this->dialog = FALSE;
105         $this->current = -1;
106         $this->save();
107       }
108     }
109  
110     /* Display dialogs if currently opened */
111     if(is_object($this->dialog)){
112       $this->dialog->save_object();
113       return($this->dialog->execute());
114     }
117     /************
118      * Handle Divlist 
119      ************/
121     $plug = $_GET['plug'];
122     $divlist = new MultiSelectWindow($this->config,"GotoMasses","gotomassses");
123     $divlist->SetSummary(_("Gotomasses tasks"));
124     $divlist->SetHeadpageMode();
125     $divlist->EnableCloseButton(FALSE);
126     $divlist->EnableSaveButton(FALSE);
127     $divlist->SetInformation(_("This menu allows you to add, remove and change the properties of gotomasses tasks."));
128     
129     $header = "<div style='padding:5px'>
130                 <input type='image' src='images/gotomasses_new_task.png' name='new_task' class='center'>
131                </div>";
132   
133     $divlist->SetListHeader($header);
134     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=object'>"._("Target")."</a>"));
135     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=schedule'>"._("Schedule")."</a>",
136                                       "attach"=>"style='width:100px;'"));
137     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=action'>"._("Type")."</a>",
138                                       "attach"=>"style='width:60px;'"));
139     $divlist->AddHeader(array("string"=>_("Action"),
140                                       "attach"=>"style='border-right:0px;width:40px;'"));
141     foreach($this->tasks as $key => $task){
142   
143       $action = "<input type='image' src='images/edit.png' name='edit_task_".$key."' class='center' alt='"._("Edit")."'>";
144       $action.= "<input type='image' src='images/edittrash.png' name='remove_task_".$key."' class='center' alt='"._("Reomve")."'>";
146       $field1 = array("string" => "<div style='width:100%;overflow:hidden;'><nobr>".$this->target_to_string($task)."</nobr></div>");
147       $field2 = array("string" => $this->time_to_string($task),"attach" => "style='width:100px;'");
148       $field3 = array("string" => $this->action_to_string($task),"attach" => "style='width:60px;'");
149       $field4 = array("string" => $action,"attach" => "style='width:40px;border-right:0px;'");
150       
151  
152       $divlist->AddElement(array($field1,$field2,$field3,$field4));
153     }
154  
155     return($divlist->Draw());
156   }
158   
159   function target_to_string($data)
160   {
161     $ret = "";
162     foreach($data['Target'] as $target){
163       $ret .= preg_replace("/^[^:]+:/i","",$target).", ";
164     } 
165     return(preg_replace("/, $/","",$ret));
166   }
168   
169   function time_to_string($data)
170   {
171     return($data['Minute']." ".$data['Hour']." ".$data['Day']." ".$data['Month']." ".$data['Weekday']);
172   }
174   
175   function action_to_string($data)
176   {
177     switch($data['Action']){
179       case 'reinstall' : return("Reinstall");break;
180       case 'install' : return("Install");break;
181       case 'reboot' : return("Restart");break;
182       case 'update' : return("Update");break;
183       default : return("Unknown");
184     }
185   }
187   
188   function load_gotomasses_data()
189   {
190     $ui = get_userinfo();
192     
193     if(!file_exists($this->data_file) || !is_readable($this->data_file)){
194       print_red(sprintf(_("Can't locate or read gotomasses storage file '%s'."),$this->data_file));
195       return(FALSE);
196     }
198     $fp = @fopen($this->data_file,"r");
199     if(!$fp){
200       print_red(sprintf(_("Can't read gotomasses storage file '%s'."),$this->data_file));
201       return(FALSE);
202     }
204     /* Get file contents */
205     $data ="";
206     while(!feof($fp)){
207       $data.= fread($fp,512);
208     }
210     /* Get lines from file */
211     $this->tasks  = array(); 
212     $comment      = "";
213     $rows         = split("\n",$data);
215     /* Walk trough rows and parse data */
216     foreach($rows as $row){
218       /* Skip empty lines */
219       $row = trim($row);
220       if(empty($row)){
221         continue;
222       }
224       /* Get comment, if available */     
225       if(preg_match("/^#/",$row)){
226         $comment = preg_replace("/^#/","",$row);
227         continue;
228       }
230       /* Split row into minutes/ hours ...*/ 
231       $row    = preg_replace('/[\t ]/umi'," ",$row);
232       $row    = preg_replace('/  */umi'," ",$row);
233       $parts  = split(" ",$row);
235       if(count($parts) != 8){
236         print_red(_("Entry broken, skipped."));
237       }else{
239         $entry = array();
240         $entry['Minute']  = $parts[0];  
241         $entry['Hour']    = $parts[1];  
242         $entry['Day']     = $parts[2];  
243         $entry['Month']   = $parts[3];  
244         $entry['Weekday'] = $parts[4];  
245         $entry['Action']  = $parts[5];  
246         $entry['OGroup']  = $parts[6];  
247         $entry['Target']  = split(",",$parts[7]);  
248         $entry['Comment'] = $comment;  
249         $this->tasks []   = $entry;
250       }
251     } 
252   }
255   function save_gotomasses_data()
256   {
257     $str = "#GOsa generated file, please just modify if you realy know what you do.";
258   
259     foreach($this->tasks as $task){
260       $str .= "\n#".trim($task['Comment']);
262       $str .= "\n";
263       $str .= str_pad($task['Minute'] ,5," ")." ";
264       $str .= str_pad($task['Hour']   ,5," ")." ";
265       $str .= str_pad($task['Day']    ,5," ")." ";
266       $str .= str_pad($task['Month']  ,5," ")." ";
267       $str .= str_pad($task['Weekday'],5," ")." ";
268       $str .= str_pad($task['Action'] ,5," ")." ";
269       $str .= str_pad($task['OGroup'] ,5," ")." ";
270     
271       foreach($task['Target'] as $target){
272         $str .= $target.",";
273       }
274       $str = preg_replace("/,$/","",$str);
275     }
277     $ui = get_userinfo();
279     if(!file_exists($this->data_file) || !is_writeable($this->data_file)){
280       print_red(sprintf(_("Can't locate or write gotomasses storage file '%s'."),$this->data_file));
281       return(FALSE);
282     }
284     $fp = @fopen($this->data_file,"w");
285     if(!$fp){
286       print_red(sprintf(_("Can't read gotomasses storage file '%s'."),$this->data_file));
287       return(FALSE);
288     }
290     $str .= "\n";
291     fwrite($fp,$str);
292     fclose($fp);
293   }
296   function save_object()
297   {
298     if(isset($_POST['gotomasses'])){
299     }
300   }
303   /* Return list of object groups */
304   function get_object_groups()
305   {
306     $ret = array();
307     $ldap = $this->config->get_ldap_link();
308     $ldap->cd($this->config->current['BASE']);
309     $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
310     while($attrs = $ldap->fetch()){
311       $ret[$attrs['cn'][0]] = $attrs['cn'][0];
312     }
313     return($ret); 
314   }
316   
317   function save()
318   {
319     $this->save_gotomasses_data();
320   }
323   function get_actions()
324   {
325     /* Prepare list of available actions */
326     $actions = array( "reboot"          => _("Reboot"),
327                             "localboot"       => _("Localboot"),
328                             "halt"            => _("Halt system"),
329                             "update"          => _("Update"),
330                             "reinstall"       => _("(Re)Install"),
331                             "rescan"          => _("Rescan"),
332                             "memcheck"        => _("Memory check"));
333     return($actions);
334   }
337   function plInfo()
338   {
339     return (array(
340         "plShortName"   => _("System mass deployment"),
341         "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
342         "plSelfModify"  => FALSE,
343         "plDepends"     => array(),
344         "plPriority"    => 0,
345         "plSection"     => array("addon"),
346         "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
347         "plProvidedAcls" => array("Comment"   => _("Description"), 
348                                   "Action"    => _("Action"),
349                                   "Day"       => _("Day"),
350                                   "Minute"    => _("Minute"),
351                                   "Hour"      => _("Hour"),
352                                   "Month"     => _("Month"),
353                                   "Weekday"   => _("Week day"),
354                                   "Target"    => _("Target"))
355         ));
356   }
358 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
359 ?>