Code

Added seperate list handler for gotomasses.
[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();
21   var $divlist = NULL;
23   function gotomasses(&$config, $dn= NULL)
24   {
25     /* Include config object */
26     $this->config= &$config;
28     /* Define source file */
29     $this->data_file = CONFIG_DIR."/gotomasses_machines";
30     $file = $this->config->search("gotomasses", "STORAGE_FILE",array('menu'));
31     if(!empty($file)){
32       $this->data_file = $file;
33     }
34     $this->load_gotomasses_data();
35   }
38   function execute()
39   {
40     $smarty = get_smarty();
41     /************
42      * Handle posts 
43      ************/
45     $s_entry = $s_action = "";
46     $arr = array( "/^edit_task_/"=>"edit","/^remove_task_/"=>"remove",
47                   "/^new_task_/"=>"new_task","/^remove_multiple_task_/" => "remove_multiple");
48     foreach($arr as $regex => $action){
49       foreach($_POST as $name => $value){
50         if(preg_match($regex,$name)){
51           $s_action = $action;
52           $s_entry  = preg_replace($regex,"",$name);
53           $s_entry  = preg_replace("/_(x|y)$/","",$s_entry);
54         }
55       }
56     }
58     /* Edit posted from list link */
59     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
60       $s_action = "edit";
61       $s_entry = $_GET['id'];
62     }
64     /************
65      * List posts 
66      ************/
68     /* Remove multiple */
69     if($s_action == "remove_multiple"){
70       if(!$this->acl_is_removeable()){
71         print_red(_("You are not allowed to remove a task."));
72       }else{
73         $this->ids_to_remove = $this->list_get_selected_items();
74         $tmp = "";
75         foreach($this->ids_to_remove as $key => $id){
76           if(isset($this->tasks[$id])){
77             $task = $this->tasks[$id];
78             $tmp.= "\n".$this->target_to_string($task);
79           }else{
80             unset($this->ids_to_remove[$key]);
81           }
82         }
83         $smarty->assign("multiple", TRUE); 
84         $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
85         $this->current = $s_entry;
86         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
87       }
88     }
90     /* Remove specified tasks */
91     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
92       foreach($this->ids_to_remove as $id){
93         if(isset($this->tasks[$id])){
94           unset($this->tasks[$id]);
95         }
96       }
97       $this->save();
98     }
100     /* Remove entry from list */
101     if($s_action == "remove" && isset($this->tasks[$s_entry])){
102       if(!$this->acl_is_removeable()){
103         print_red(_("You are not allowed to remove a task."));
104       }else{
105         $entry = $this->tasks[$s_entry];
106         $this->current = $s_entry;
107         $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),
108                                     "<pre>".$this->target_to_string($entry)."</pre>"));
109         $smarty->assign("multiple", FALSE); 
110         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
111       }
112     }
114     /* Remove entry, remove is confirmed */
115     if($this->current != -1 && isset($_POST['delete_confirm'])){
116       unset($this->tasks[$this->current]);
117       $this->current = -1;
118       $this->save();
119     }
121     /* Remove aborted */
122     if(isset($_POST['delete_cancel'])){
123       $this->ids_to_remove = array();;
124     }
126     /* Edit selected entry */
127     if($s_action == "edit" && isset($this->tasks[$s_entry])){
128       $entry = $this->tasks[$s_entry];
129       $this->dialog = new goto_task($this->config,$this,$entry);
130       $this->current = $s_entry;
131     }
133     /* New entry */
134     if($s_action== "new_task" && $this->acl_is_createable()){
135       $this->dialog = new goto_task($this->config,$this);
136       $this->current = -1;
137     }
138   
139     /* Close dialog */
140     if(isset($_POST['close_goto_task'])){
141       $this->dialog = FALSE;
142       $this->current = -1;
143     }
145     /* Close dialog */
146     if((isset($_POST['save_goto_task']) || isset($_POST['apply_goto_task'])) && is_object($this->dialog) ){
147       $this->dialog->save_object();
148       $msgs = $this->dialog->check();
149       if(count($msgs)){
150         foreach($msgs as $msg){
151           print_red($msg);  
152         }
153       }else{
154         if(isset($this->tasks[$this->current]) && $this->current != -1){
155           $this->tasks[$this->current] = $this->dialog->save();
156         }else{
157           $this->tasks[] = $this->dialog->save();
158         }
159         if(!isset($_POST['apply_goto_task'])){
160           $this->dialog = FALSE;
161           $this->current = -1;
162         }
163         $this->save();
164       }
165     }
167     /* Display dialogs if currently opened */
168     if(is_object($this->dialog)){
169       $this->dialog->save_object();
170       return($this->dialog->execute());
171     }
174     /************
175      * Handle Divlist 
176      ************/
178     $list = new divListMasses($this->config,$this);
179     $list->SetEntries($this->tasks); 
180     return($list->Draw());
181   }
183   
184   function target_to_string($data)
185   {
186     $ret = "";
187     if($data['Action'] == "initial_install"){
188       foreach($data['Initial_Target'] as $target){
189         $ret .= $target['MAC'].", ";
190       } 
191     }else{
192       foreach($data['Target'] as $target){
193         $ret .= $target.", ";
194       } 
195     }
196     return(preg_replace("/, $/","",$ret));
197   }
199   
200   function time_to_string($data)
201   {
202     return($data['Minute']." ".$data['Hour']." ".$data['Day']." ".$data['Month']." ".$data['Weekday']);
203   }
205   
206   function action_to_string($data)
207   {
208     $tmp = $this->get_actions();
209     if(isset($tmp[$data['Action']])){
210       return($tmp[$data['Action']]);
211     }else{
212       return(_("Unknown"));
213     }
214   }
216   
217   function load_gotomasses_data()
218   {
219     $ui = get_userinfo();
221     /* Check file existence */
222     if(!file_exists($this->data_file) || !is_readable($this->data_file)){
223       print_red(sprintf(_("Can't locate or read gotomasses storage file '%s'."),$this->data_file));
224       return(FALSE);
225     }
227     /* check if file is readable */
228     $fp = @fopen($this->data_file,"r");
229     if(!$fp){
230       print_red(sprintf(_("Can't read gotomasses storage file '%s'."),$this->data_file));
231       return(FALSE);
232     }
234     /* Get file contents */
235     $data ="";
236     while(!feof($fp)){
237       $data.= fread($fp,512);
238     }
240     /* Get lines from file */
241     $this->tasks  = array(); 
242     $comment      = "";
243     $rows         = split("\n",$data);
245     /* Walk trough rows and parse data */
246     foreach($rows as $row){
248       /* Skip empty lines */
249       $row = trim($row);
250       if(empty($row)){
251         continue;
252       }
254       /* Get comment, if available */     
255       if(preg_match("/^#/",$row)){
256         $comment = preg_replace("/^#/","",$row);
257         continue;
258       }
260       /* Split row into minutes/ hours ...*/ 
261       $row    = preg_replace('/[\t ]/umi'," ",$row);
262       $row    = preg_replace('/  */umi'," ",$row);
263       $parts  = split(" ",$row);
265       if(count($parts) != 10){
266         print_red(_("Entry broken, skipped."));
267       }else{
269         $entry = array();
270         $entry['Minute']  = $parts[0];  
271         $entry['Hour']    = $parts[1];  
272         $entry['Day']     = $parts[2];  
273         $entry['Month']   = $parts[3];  
274         $entry['Weekday'] = $parts[4];  
275         $entry['Action']  = $parts[5];  
276         $entry['OGroup']  = $parts[6];  
277         $entry['Zone']    = $parts[7];  
278         $entry['Section'] = $parts[8];  
279         if($entry['Action'] == "initial_install"){
280           $tmp2 = split(";",$parts[9]);
281           foreach($tmp2 as $target){
282             $tmp = split(",",$target);
283             $entry['Initial_Target'][]  = array(
284                           "MAC"     => $tmp[0],
285                           "IP"      => $tmp[1],
286                           "NAME"    => $tmp[2]);
287           }
288           $entry['Target']  = array();
289         }else{
290           $entry['Initial_Target']  = array();
291           $entry['Target']  = split(";",$parts[7]);
292         }
293         $entry['Comment'] = $comment;  
294         $this->tasks []   = $entry;
295       }
296     } 
297   }
300   function save_gotomasses_data()
301   {
302     $str = "#GOsa generated file, please just modify if you realy know what you do.";
303     foreach($this->tasks as $task){
304       $str .= "\n#".trim($task['Comment']);
305       $str .= "\n";
306       if($task['Action'] == "initial_install"){
307         $str .= "*     *     *     *     *     ";
308       }else{
309         $str .= str_pad($task['Minute'] ,5," ")." ";
310         $str .= str_pad($task['Hour']   ,5," ")." ";
311         $str .= str_pad($task['Day']    ,5," ")." ";
312         $str .= str_pad($task['Month']  ,5," ")." ";
313         $str .= str_pad($task['Weekday'],5," ")." ";
314       }
315       $str .= str_pad($task['Action'] ,5," ")." ";
316       $str .= str_pad($task['OGroup'] ,5," ")." ";
317       $str .= str_pad($task['Zone']   ,5," ")." ";
318       $str .= str_pad($task['Section'],5," ")." ";
319       if($task['Action'] == "initial_install"){
320         foreach($task['Initial_Target'] as $target){
321           $str .= trim($target['MAC']).",".trim($target['IP']).",".trim($target['NAME']).";";
322         }
323       }else{
324         foreach($task['Target'] as $target){
325           $str .= $target.";";
326         }
327       }
328       $str = preg_replace("/;$/","",$str);
329     }
331     /* check file existence*/
332     $ui = get_userinfo();
333     if(!file_exists($this->data_file) || !is_writeable($this->data_file)){
334       print_red(sprintf(_("Can't locate or write gotomasses storage file '%s'."),$this->data_file));
335       return(FALSE);
336     }
338     /* check permissions */
339     $fp = @fopen($this->data_file,"w");
340     if(!$fp){
341       print_red(sprintf(_("Can't read gotomasses storage file '%s'."),$this->data_file));
342       return(FALSE);
343     }
345     /* Write contents */
346     $str .= "\n";
347     fwrite($fp,$str);
348     fclose($fp);
349   }
352   function save_object()
353   {
354   }
357   /* Return list of object groups */
358   function get_object_groups()
359   {
360     $ret = array();
361     $ldap = $this->config->get_ldap_link();
362     $ldap->cd($this->config->current['BASE']);
363     $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
364     while($attrs = $ldap->fetch()){
365       $ret[$attrs['cn'][0]] = $attrs['cn'][0];
366     }
367     return($ret); 
368   }
370   
371   function save()
372   {
373     $this->save_gotomasses_data();
374   }
377   function list_get_selected_items()
378   {
379     $ids = array();
380     foreach($_POST as $name => $value){
381       if(preg_match("/^item_selected_[0-9]*$/",$name)){
382         $id   = preg_replace("/^item_selected_/","",$name);
383         $ids[$id] = $id;
384       }
385     }
386     return($ids);
387   }
390   function get_actions()
391   {
392     /* Prepare list of available actions */
393     $actions = array(       "reboot"          => _("Reboot"),
394                             "localboot"       => _("Localboot"),
395                             "halt"            => _("Halt system"),
396                             "initial_install" => _("Initial installation"),
397                             "update"          => _("Update"),
398                             "reinstall"       => _("(Re)Install"),
399                             "rescan"          => _("Rescan"),
400                             "wake"            => _("Wake"),
401                             "memcheck"        => _("Memory check"));
402     return($actions);
403   }
406   function plInfo()
407   {
408     return (array(
409         "plShortName"   => _("System mass deployment"),
410         "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
411         "plSelfModify"  => FALSE,
412         "plDepends"     => array(),
413         "plPriority"    => 0,
414         "plSection"     => array("addon"),
415         "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
416         "plProvidedAcls" => array("Comment"   => _("Description"), 
417                                   "Action"    => _("Action"),
418                                   "Day"       => _("Day"),
419                                   "Minute"    => _("Minute"),
420                                   "Hour"      => _("Hour"),
421                                   "Month"     => _("Month"),
422                                   "Weekday"   => _("Week day"),
423                                   "Target"    => _("Target"))
424         ));
425   }
427 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
428 ?>