Code

Starting move
[gosa.git] / gosa-core / 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   /* Queue tasks */
14   var $current =false;
15   var $dialog = FALSE;
16   var $ids_to_remove = array();
17   var $divlist = NULL;
19   var $sort_by = "QueuePosition";
20   var $sort_dir= "up";
22   function gotomasses(&$config, $dn= NULL)
23   {
24     /* Include config object */
25     $this->config= &$config;
26     $this->divlist = new divListMasses($this->config,$this);
27     $this->o_queue = new hostActionQueue(&$config);
28   }
31   function execute()
32   {
33     $smarty = get_smarty();
34     /************
35      * Handle posts 
36      ************/
38     $s_entry = $s_action = "";
39     $arr = array( 
40                   "/^prio_top_/"      => "prio_top",
41                   "/^prio_increase_/" => "prio_increase",
42                   "/^prio_decrease_/" => "prio_decrease",
43                   "/^prio_bottom_/"   => "prio_bottom",
45                   "/^multiple_prio_top_/"      => "mprio_top",
46                   "/^multiple_prio_increase_/" => "mprio_increase",
47                   "/^multiple_prio_decrease_/" => "mprio_decrease",
48                   "/^multiple_prio_bottom_/"   => "mprio_bottom",
50                   "/^edit_task_/"=>"edit","/^remove_task_/"=>"remove",
51                   "/^new_task_/"=>"new_task","/^remove_multiple_task_/" => "remove_multiple");
52     foreach($arr as $regex => $action){
53       foreach($_POST as $name => $value){
54         if(preg_match($regex,$name)){
55           $s_action = $action;
56           $s_entry  = preg_replace($regex,"",$name);
57           $s_entry  = preg_replace("/_(x|y)$/","",$s_entry);
58         }
59       }
60     }
62     /* Edit posted from list link */
63     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
64       $s_action = "edit";
65       $s_entry = $_GET['id'];
66     }
68     /************
69      * REMOVE 
70      ************/
71     
72     if(preg_match("/^mprio_/",$s_action) || preg_match("/^prio_/",$s_action)){
74       switch($s_action){
75         case 'mprio_top'       : foreach($this->list_get_selected_items() as $id ){
76                                    $this->o_queue->max_entry_priority($id);
77                                  }
78                                  break;
79         case 'mprio_increase'  : foreach($this->list_get_selected_items() as $id ){
80                                    $this->o_queue->increase_entry_priority($id);
81                                  }
82                                  break;
83         case 'mprio_decrease'  : foreach($this->list_get_selected_items() as $id ){
84                                    $this->o_queue->decrease_entry_priority($id);
85                                  }
86                                  break;
87         case 'mprio_bottom'    : foreach($this->list_get_selected_items() as $id ){
88                                    $this->o_queue->min_entry_priority($id);
89                                  }
90                                  break;
91         case 'prio_top'       : $this->o_queue->max_entry_priority($s_entry);break;
92         case 'prio_increase'  : $this->o_queue->increase_entry_priority($s_entry);break;
93         case 'prio_decrease'  : $this->o_queue->decrease_entry_priority($s_entry);break;
94         case 'prio_bottom'    : $this->o_queue->min_entry_priority($s_entry);break;
95         default : trigger_error("Undefined priority setting used.");
96       }
97       if($this->o_queue->is_error()){
98         print_red($this->o_queue->get_error());
99       }
100     }
102     /************
103      * REMOVE 
104      ************/
106     /* Remove multiple */
107     if($s_action == "remove_multiple" || $s_action == "remove"){
109       if(!$this->acl_is_removeable()){
110         print_red(_("You are not allowed to remove a task."));
111       }else{
113         if($s_action == "remove"){
114           $ids = array($s_entry);
115         }else{
116           $ids = $this->list_get_selected_items();
117         }
118   
120         $this->ids_to_remove = $ids;
121         $tmp = "";
122         foreach($this->ids_to_remove as $key => $id){
123           if($this->o_queue->id_exists($id)){
124             $task = $this->o_queue->get_entry($id);
125             $tmp.= "\n".$this->target_to_string($task);
126           }else{
127             unset($this->ids_to_remove[$key]);
128           }
129         }
130         $smarty->assign("multiple", TRUE); 
131         $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
132         $this->current = $s_entry;
133         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
134       }
135     }
137     /* Remove specified tasks */
138     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
139       foreach($this->ids_to_remove as $id){
140         if($this->o_queue->id_exists($id)){
141           $this->o_queue->remove_entry($id);
142         }
143       }
144       $this->save();
145     }
147     /* Remove aborted */
148     if(isset($_POST['delete_cancel'])){
149       $this->ids_to_remove = array();;
150     }
153     /************
154      * EDIT 
155      ************/
157     /* Edit selected entry */
158     if($s_action == "edit"){
159       $entry = $this->o_queue->get_entry($s_entry);
160       if($entry){
161         $this->dialog = new goto_task($this->config,$this,$entry);
162         $this->current = $s_entry;
163       }
164     }
166     /* New entry */
167     if($s_action== "new_task" && $this->acl_is_createable()){
168       $this->dialog = new goto_task($this->config,$this);
169       $this->current = -1;
170     }
171   
172     /* Close dialog */
173     if(isset($_POST['close_goto_task'])){
174       $this->dialog = FALSE;
175       $this->current = -1;
176     }
178     /* Close dialog */
179     if((isset($_POST['save_goto_task']) || isset($_POST['apply_goto_task'])) && is_object($this->dialog) ){
180       $this->dialog->save_object();
181       $msgs = $this->dialog->check();
182       if(count($msgs)){
183         foreach($msgs as $msg){
184           print_red($msg);  
185         }
186       }else{  
187         
188         if($this->o_queue->id_exists($this->current)){
189           $this->o_queue->update_entry($this->current,$this->dialog->save());
190         }else{
191           $tmp = $this->dialog->save();
192           $tmp2= array();
193           $targets =$tmp['Target'];
194           foreach($targets as $target){
195             $tmp['Target'] = array($target);
196             $tmp2[] = $tmp;
197           }
198           if(!$this->o_queue->add_multiple($tmp2)){
199             print_red($this->o_queue->get_error());
200           }
201         }
202         if(!isset($_POST['apply_goto_task'])){
203           $this->dialog = FALSE;
204           $this->current = -1;
205         }
206         $this->save();
207       }
208     }
210     /* Display dialogs if currently opened */
211     if(is_object($this->dialog)){
212       $this->dialog->save_object();
213       return($this->dialog->execute());
214     }
216     /************
217      * Handle Divlist 
218      ************/
220     $this->divlist->execute();
221     $this->divlist->SetEntries($this->get_queue_entries()); 
222     $str = 
223       "<script type=\"text/javascript\">
224       <!--
225         var time = null
226         function move() {
227           window.location = '?plug=".$_GET['plug']."'
228         }
229         setInterval(\"move()\",20000);
230         //-->
231         </script>";
232     return($str.$this->divlist->Draw());
233   }
235     
236   function get_queue_entries()
237   {
238     if(!$this->o_queue->load()){
239       print_red("ERROR:".$this->o_queue->get_error());
240       return(array());
241     }
243     $queue_pos = 1;
244     $tasks = array();
245     $ret = array();
246     while($entry = $this->o_queue->fetch()){
247       $task = $entry['TASK_ID']; 
248       if(!isset($tasks[$task])){
249         $tasks[$task] = dechex(rand(100,240)).dechex(rand(100,240)).dechex(rand(100,240));
250       }
251       $entry['Queue_Position'] = $queue_pos;
252       $entry['color'] = $tasks[$task]; 
253       $ret[]= $entry;
254       $queue_pos ++;
255     }
256   
257     /* Sort entries */
258     $simple_map = array("QueuePosition" => "Queue_Position",
259                         "Action"        => "Action",
260                         "TaskID"        => "TASK_ID");
261     $sort_arr = array();
262     if(isset($simple_map[$this->sort_by])){
263       foreach($ret as $id => $entry){
264         $sort_arr[$entry[$simple_map[$this->sort_by]]][] = $id;
265       }
266     }
267     if($this->sort_by == "TargetName"){
268       foreach($ret as $id => $entry){
269         $name = $this->target_to_string($entry);
270         $sort_arr[$name][] = $id;
271       }
272     }
273     if($this->sort_by == "Schedule"){
274       foreach($ret as $id => $entry){
275         $name = $entry['Minute'].$entry['Hour'].$entry['Day'].$entry['Month'].$entry['Weekday'];
276         $sort_arr[$name][] = $id;
277       }
278     }
279     if($this->sort_dir == "up"){
280       ksort($sort_arr);
281     }else{
282       krsort($sort_arr);
283     }
284     $ret2 = array();
285     foreach($sort_arr as $key => $entries){
286       foreach($entries as $entry_id){
287         $ret2[] = $ret[$entry_id];
288       }
289     }
290     return($ret2);
291   }
294   function get_additional_entry_informations($data)
295   {
296     $i = rand(0,3);
297     if($i == 0){
298       return("<div style='width:80px;background-color:#999999;'><div style='background-color:#00FF00;width:40%'>&nbsp;</div></div>");
299     }
300     if($i == 1){
301       return("<font style='color: #FF0000;'>Failed</font>");
302     }
303     if($i == 1){
304       return("<font style='color: #00FF00;'>OK</font>");
305     }
306     return("-");
307   }
310   function target_to_string($data)
311   {
312     $ret = "";
313     if($data['Action'] == "initial_install"){
314       foreach($data['Initial_Target'] as $target){
315         $ret .= $target['MAC'].", ";
316       } 
317     }else{
318       foreach($data['Target'] as $target){
319         $ret .= $target.", ";
320       } 
321     }
322     return(preg_replace("/, $/","",$ret));
323   }
325   
326   function time_to_string($data)
327   {
328     return($data['Minute']." ".$data['Hour']." ".$data['Day']." ".$data['Month']." ".$data['Weekday']);
329   }
331   
332   function action_to_string($data)
333   {
334     $tmp = $this->get_actions();
335     if(isset($tmp[$data['Action']])){
336       return($tmp[$data['Action']]);
337     }else{
338       return(_("Unknown"));
339     }
340   }
342   
343   function save_object()
344   {
345     $this->divlist->save_object();
346     $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
347     if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
348       $sort = $_GET['sort'];
349       if($this->sort_by == $sort){
350         if($this->sort_dir == "up"){
351            $this->sort_dir = "down";
352         }else{
353            $this->sort_dir = "up";
354         }
355       }
356       $this->sort_by = $sort;
357     }
358   }
361   /* Return list of object groups */
362   function get_object_groups()
363   {
364     $ret = array();
365     $ldap = $this->config->get_ldap_link();
366     $ldap->cd($this->config->current['BASE']);
367     $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
368     while($attrs = $ldap->fetch()){
369       $ret[$attrs['cn'][0]] = $attrs['cn'][0];
370     }
371     return($ret); 
372   }
374   
375   function save()
376   {
377   }
380   function list_get_selected_items()
381   {
382     $ids = array();
383     foreach($_POST as $name => $value){
384       if(preg_match("/^item_selected_[0-9]*$/",$name)){
385         $id   = preg_replace("/^item_selected_/","",$name);
386         $ids[$id] = $id;
387       }
388     }
389     return($ids);
390   }
393   function get_actions()
394   {
395     /* Prepare list of available actions */
396     $actions = array(       "reboot"          => _("Reboot"),
397                             "localboot"       => _("Localboot"),
398                             "halt"            => _("Halt system"),
399                             "initial_install" => _("Initial installation"),
400                             "update"          => _("Update"),
401                             "reinstall"       => _("(Re)Install"),
402                             "rescan"          => _("Rescan"),
403                             "wake"            => _("Wake"),
404                             "memcheck"        => _("Memory check"));
405     return($actions);
406   }
409   static function plInfo()
410   {
411     return (array(
412         "plShortName"   => _("System mass deployment"),
413         "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
414         "plSelfModify"  => FALSE,
415         "plDepends"     => array(),
416         "plPriority"    => 0,
417         "plSection"     => array("addon"),
418         "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
419         "plProvidedAcls" => array("Comment"   => _("Description"), 
420                                   "Action"    => _("Action"),
421                                   "Day"       => _("Day"),
422                                   "Minute"    => _("Minute"),
423                                   "Hour"      => _("Hour"),
424                                   "Month"     => _("Month"),
425                                   "Weekday"   => _("Week day"),
426                                   "Target"    => _("Target"))
427         ));
428   }
430 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
431 ?>