Code

Updated gotomasses.
[gosa.git] / gosa-plugins / goto / 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 $events         = array();
21   var $sort_by  = "Schedule";
22   var $sort_dir = "down";
23   var $entries  = array();
24   var $range    = 25;
25   var $start    = 0;
27   function gotomasses(&$config, $dn= NULL)
28   {
29     /* Include config object */
30     $this->config= &$config;
31     $this->o_queue = new gosaSupportDaemon(TRUE,10);
32     $this->events  = DaemonEvent::get_event_types();
33   }
36   function execute()
37   {
38     $smarty = get_smarty();
39  
40     /************
41      * Handle posts 
42      ************/
43     
44     $s_entry = $s_action = "";
45     $arr = array( 
46         "/^stop_/"        => "stop",
47         "/^start_/"       => "start",
49         "/^prio_up_/"     => "prio_up",
50         "/^prio_down_/"   => "prio_down",
52         "/^edit_task_/"             =>  "edit",
53         "/^remove_task_/"           =>  "remove",
54         "/^new_task_/"              =>  "new_task");;
57     foreach($arr as $regex => $action){
58       foreach($_POST as $name => $value){
59         if(preg_match($regex,$name)){
60           $s_action = $action;
61           $s_entry  = preg_replace($regex,"",$name);
62           $s_entry  = preg_replace("/_(x|y)$/","",$s_entry);
63         }
64       }
65     }
67     /* Menu actions */
68     if(isset($_POST['menu_action']) && !empty($_POST['menu_action'])){
69       $s_action = $_POST['menu_action'];
70     }
71     
72     /* Edit posted from list link */
73     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
74       $s_action = "edit";
75       $s_entry = $_GET['id'];
76     }
79     /************
80      * Handle Priority modifications  
81      ************/
83     if(preg_match("/^prio_/",$s_action)){
85       switch($s_action){
87         case 'prio_down'  : $this->update_priority($s_entry,"down");break;
88         case 'prio_up'    : $this->update_priority($s_entry,"up");break;
89       }
90     }
92     /************
93      * Handle start/stop modifications  
94      ************/
96     if(preg_match("/^start/",$s_action) || preg_match("/^stop/",$s_action)){
98       switch($s_action){
99         case 'start_all'   : $this->start_queue_entries($this->list_get_selected_items());break;
100         case 'start'       : $this->start_queue_entries(array($s_entry));break; 
101         case 'stop_all'    : $this->stop_queue_entries ($this->list_get_selected_items());break;
102         case 'stop'        : $this->stop_queue_entries (array($s_entry));break; 
103         default : trigger_error("Undefined priority setting used.");
104       }
105       if($this->o_queue->is_error()){
106         msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
107       }
108     }
110     /************
111      * ADD 
112      ************/
113   
114     if(preg_match("/^add_event_/",$s_action)){
115       $type = preg_replace("/^add_event_/","",$s_action);
116       if(isset($this->events['BY_CLASS'][$type])){
117         $e_data = $this->events['BY_CLASS'][$type];
118         $this->dialog = new $e_data['CLASS_NAME']($this->config);
119       }
120     }
122     /************
123      * EDIT
124      ************/
126     if($s_action == "edit"){  
127       $id =  $s_entry;
128       $type = FALSE;
129       if(isset($this->entries[$id])){
130         $event = $this->entries[$s_entry];
131         if(isset($this->events['BY_QUEUED_ACTION'][$event['HEADERTAG']])){
132           $type = $this->events['BY_QUEUED_ACTION'][$event['HEADERTAG']];
133           $this->dialog = new $type['CLASS_NAME']($this->config,$event);
134         }
135       }
136     }
138     /************
139      * REMOVE 
140      ************/
142     /* Remove multiple */
143     if($s_action == "remove_multiple" || $s_action == "remove"){
145       if(!$this->acl_is_removeable()){
146         msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), ERROR_DIALOG);
147       }else{
149         if($s_action == "remove"){
150           $ids = array($s_entry);
151         }else{
152           $ids = $this->list_get_selected_items();
153         }
154         if(count($ids)){
155           $this->ids_to_remove = $ids;
156           $ret = $this->o_queue->ids_exist($this->ids_to_remove);
157           $ret = $this->o_queue->get_entries_by_id($ret);
159           $tmp = "";
160           foreach($ret as $task){
161             if(isset($this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']])){
162               $evt = $this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']];
163               $tmp.= "\n".$task['ID']." - ".$evt['s_Menu_Name']."&nbsp;".$task['MACADDRESS'];
164             }else{
165               $tmp.= "\n".$task['ID']." - ".$task['HEADERTAG']."&nbsp;".$task['MACADDRESS'];
166             }
167           }
168           $smarty->assign("multiple", TRUE); 
169           $smarty->assign("info",sprintf(_("You are about to remove the following actions from the GOsa support Daemon: %s"),"<pre>".$tmp."</pre>"));
170           $this->current = $s_entry;
171           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
172         }
173       }
174     }
176     /* Remove specified tasks */
177     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
178       $this->o_queue->remove_entries($this->ids_to_remove);
179       $this->save();
180     }
182     /* Remove aborted */
183     if(isset($_POST['delete_cancel'])){
184       $this->ids_to_remove = array();;
185     }
188     /************
189      * EDIT 
190      ************/
192     /* Close dialog */
193     if(isset($_POST['save_event_dialog'])){
195       if(is_object($this->dialog)){
197         if($this->dialog->is_new()){
198           $header     = $this->dialog->get_schedule_action();
199           $targets    = $this->dialog->get_targets();
200           $data       = $this->dialog->save();
201           $data['timestamp'] = $this->dialog->get_timestamp(); 
204           foreach($targets as $target){
205             $data['mac'] =  $target;
206             if(!$this->o_queue->send($header,$target,$data,TRUE)){
207               msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
208                     $this->o_queue->get_error()),ERROR_DIALOG);
209             }  
210           }
211           $this->dialog = FALSE; 
212           $this->current = -1;
213         }else{
214           $id                 = $this->dialog->get_id();
215           $data               = $this->dialog->save();
216           $data['timestamp']  = $this->dialog->get_timestamp();
217           if($this->o_queue->update_entries(array($id),$data)){
218             $this->dialog = FALSE;
219             $this->current = -1;
220           }
221         }
222       }
223     }
226     /* Close dialog */
227     if(isset($_POST['abort_event_dialog'])){
228       $this->dialog = FALSE;
229       $this->current = -1;
230     }
232     /* Display dialogs if currently opened */
233     if(is_object($this->dialog)){
234       $this->dialog->save_object();
235       return($this->dialog->execute());
236     }
238     /************
239      * Handle Divlist 
240      ************/
242     $divlist = new MultiSelectWindow($this->config,"gotoMasses",array("gotomasses"));
243     $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa deamon tasks."));
244     $divlist->SetSummary(_("List of queued deamon jobs"));
245     $divlist->EnableCloseButton(FALSE);
246     $divlist->EnableSaveButton(FALSE);
247     $divlist->SetHeadpageMode();
248     $s = ".|"._("Actions")."|\n";
249     $s.= "..|<img src='images/list_new.png' alt='' border='0' class='center'>&nbsp;"._("Create")."\n";
250     foreach($this->events['BY_CLASS'] as $name =>  $event){
251       $s.= "...|".$event['MenuImage']."&nbsp;".$event['s_Menu_Name']."|add_event_".$name."\n";
252     }
253     if($this->acl_is_removeable()){
254       $s.= "..|---|\n";
255       $s.= "..|<img src='images/edittrash.png' alt='' border='0' class='center'>&nbsp;"._("Remove")."|remove_multiple\n";
256     }
257     if(preg_match("/w/",$this->getacl(""))){
258       $s.= "..|---|\n";
259       $s.= "..|<img src='images/status_start_all.png' alt='' border='0' class='center'>&nbsp;"._("Start all")."|start_all\n";
260       $s.= "..|<img src='images/status_stop_all.png' alt='' border='0' class='center'>&nbsp;"._("Stop all")."|stop_all\n";
261     }
263     $divlist->SetDropDownHeaderMenu($s);
265     if($this->sort_dir == "up"){
266       $sort_img = "<img src='images/sort_up.png' alt='/\' border=0>";
267     }else{
268       $sort_img = "<img src='images/sort_down.png' alt='\/' border=0>";
269     }
271     if($this->sort_by == "TargetName"){ $sort_img_1 = $sort_img; } else { $sort_img_1 = "" ;}
272     if($this->sort_by == "TaskID"){ $sort_img_2 = $sort_img; } else { $sort_img_2 = "" ;}
273     if($this->sort_by == "Schedule"){ $sort_img_3 = $sort_img; } else { $sort_img_3 = "" ;}
274     if($this->sort_by == "Action"){ $sort_img_4 = $sort_img; } else { $sort_img_4 = "" ;}
276     /* Create divlist */
277     $divlist->SetListHeader("<input type='image' src='images/list_reload.png' title='"._("Reload")."'>");
279     $plug  = $_GET['plug'];
280     $chk = "<input type='checkbox' id='select_all' name='select_all'
281                onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
283     /* set Page header */
284     $divlist->AddHeader(array("string"=> $chk,          "attach"=>"style='width:20px;'"));
285     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TargetName'>"._("Target").$sort_img_1."</a>"));
286     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TaskID'>"._("Task").$sort_img_2."</a>",
287                                       "attach"=>"style='width:120px;'"));
288     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
289                                       "attach"=>"style='width:100px;'"));
290     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Action'>"._("Status").$sort_img_4."</a>",
291                                       "attach"=>"style='width:80px;'"));
292     $divlist->AddHeader(array("string"=>_("Action"),
293                                       "attach"=>"style='border-right:0px;width:120px;'"));
296     /* Reload the list of entries */
297     $this->reload();
299     foreach($this->entries as $key => $task){
301       $prio_actions = "<input class='center' type='image' src='images/prio_increase.png' name='prio_up_".$key."'>&nbsp;";
302       $prio_actions.= "<input class='center' type='image' src='images/prio_decrease.png' name='prio_down_".$key."'>&nbsp;";
303       $prio_actions.= "<input class='center' type='image' src='images/status_stop_all.png' name='stop_".$key."'>&nbsp;";
304       $prio_actions.= "<input class='center' type='image' src='images/status_start_all.png' name='start_".$key."'>&nbsp;";
306       $action = "<input type='image' src='images/edit.png' name='edit_task_".$key."' 
307         class='center' alt='"._("Edit")."'>";
309       if($this->acl_is_removeable()){
310         $action.= "<input type='image' src='images/edittrash.png' name='remove_task_".$key."' 
311           class='center' alt='"._("Reomve")."'>";
312       }
314       $color = "";
315       $display = $task['MACADDRESS'];
316       $display2= $task['HEADERTAG'];
317      
318       /* Check if this event exists as Daemon class 
319        * In this case, display a more accurate entry.
320        */ 
321       if(isset($this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']]['s_Menu_Name'])){
322         $event_type = $this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']];
323         $display2= $event_type['s_Menu_Name'];
324         if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
325           $display2 = $event_type['ListImage']."&nbsp;".$display2;
326         }
327       } 
329       /* Create each field */
330       $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$key."'>" ,
331                       "attach" => "style='width:20px;".$color."'");
332       $field1 = array("string" => $display,
333                       "attach" => "style='".$color."'");
334       $field1a= array("string" => $display2,
335                       "attach" => "style='".$color.";width:120px;'");
336       $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:100px;'");
337       $field3 = array("string" => $task['STATUS'],"attach" => "style='".$color.";width:80px;'");
338       $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:120px;border-right:0px;'");
339       $divlist->AddElement(array($field0,$field1,$field1a,$field2,$field3,$field4));
340     }
342     $smarty = get_smarty();
343     $smarty->assign("events",$this->events);
344     $smarty->assign("start",$this->start);
345     $smarty->assign("start_real", ($this->start + 1));
346     $smarty->assign("ranges", array("10" => "10",
347                                     "20" => "20",
348                                     "25" => "25",
349                                     "50" => "50",
350                                     "100"=> "100",
351                                     "200"=> "200",
352                                     "9999" => "*"));
354     $count = $this->o_queue->number_of_queued_entries();
355     $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"range"));
356     $smarty->assign("range",$this->range);
357     $smarty->assign("div",$divlist->Draw());
358     return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
359   }
362   /*! \brief  Move an entry up or down in the queue, by updating its execution timestamp  
363       @param  $id     Integer  The ID of the entry which should be updated.
364       @param  $type   String   "up" / "down"
365       @return boolean TRUE in case of success else FALSE
366   */
367   public function update_priority($id,$type = "up")
368   {
369     if($type == "up"){
370       $tmp = $this->o_queue->get_queued_entries(-1,-1,"timestamp DESC");
371     }else{
372       $tmp = $this->o_queue->get_queued_entries(-1,-1,"timestamp ASC");
373     }
374     $last = array();
375     foreach($tmp as $entry){
376       if($entry['ID'] == $id){
377         if(count($last)){
378           $time = strtotime($last['TIMESTAMP']);
379           if($type == "up"){
380             $time ++;
381           }else{
382             $time --;
383           }
384           $time_str = date("YmdHis",$time); 
385           return($this->o_queue->update_entries(array($id),array("timestamp" => $time_str)));
386         }else{
387           return(FALSE);
388         }
389       }
390       $last = $entry;
391     }
392     return(FALSE);
393   }
396   /*! \brief  Force queue job to be done as far as possible.
397    *  @return Boolean TRUE in case of success, else FALSE. 
398    */
399   private function start_queue_entries($ids)
400   {
401     if(!count($ids)){
402       return;
403     }
404   
405     $data = array("timestamp" => date("YmdHis"),
406                   "status"    => "-");
407     if(!$this->o_queue->update_entries($ids,$data)){
408       msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
409       return(FALSE);
410     }
411     return(TRUE);
412   }
415   /*! \brief Stops the specified queue entry from execution.
416    *  @return Boolean TRUE in case of success, else FALSE. 
417    */
418   private function stop_queue_entries($ids)
419   {
420     if(!count($ids)){
421       return;
422     }
423     $data = array("timestamp" => date("YmdHis",(time() + (60*60*24*365))),
424                   "status"    => _("Stoppped"));
425     if(!$this->o_queue->update_entries($ids,$data)){
426       msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
427       return(FALSE);
428     }
429     return(TRUE);
430   }
433   /*! \brief  Request list of queued jobs.
434    *  @return Returns an array of all queued jobs.
435    */
436   function reload()
437   {
438     $map = array(
439         "QueuePosition" => "id",
440         "Action"        => "status",
441         "TaskID"        => "headertag",
442         "TargetName"    => "macaddress",
443         "Schedule"      => "timestamp");
445     if(!isset($map[$this->sort_by])){
446       $sort = "id DESC";
447     }else{
448       $sort   = $map[$this->sort_by]; 
449       if($this->sort_dir == "up"){
450         $sort.= " ASC";
451       }else{
452         $sort.= " DESC";
453       }
454     }
455       
456     $start  = $this->start; 
457     $stop   = $this->range;
459     $entries = $this->o_queue->get_queued_entries($start,$stop,$sort);
460     if ($this->o_queue->is_error()){
462       msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
463     }
465     $this->entries = array();
466     foreach($entries as $entry){
467       $this->entries[$entry['ID']]= $entry;
468     }
469   }
472   /*! \brief  Handle post jobs, like sorting.
473    */
474   function save_object()
475   {
476     $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
477     if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
478       $sort = $_GET['sort'];
479       if($this->sort_by == $sort){
480         if($this->sort_dir == "up"){
481           $this->sort_dir = "down";
482         }else{
483           $this->sort_dir = "up";
484         }
485       }
486       $this->sort_by = $sort;
487     }
488     if(isset($_POST['range']) && is_numeric($_POST['range'])){
489       $this->range = $_POST['range'];
490     }
491     if(isset($_GET['start'])){
492       $start = $_GET['start'];
493       if(is_numeric($start) || $start == 0){
494         $this->start = $start;
495       }
496     }
498     /* Check start stop and reset if necessary */
499     $count = $this->o_queue->number_of_queued_entries();
500     if($this->start >= $count){
501       $this->start = $count -1;
502     }
503     if($this->start < 0){
504       $this->start = 0;
505     }
506   }
509   function save()
510   {
511     // We do not save anything here.
512   }
515   /*! \brief  Return a list of all selected items.
516     @return Array   Returns an array containing all selected item ids.
517    */
518   function list_get_selected_items()
519   {
520     $ids = array();
521     foreach($_POST as $name => $value){
522       if(preg_match("/^item_selected_[0-9]*$/",$name)){
523         $id   = preg_replace("/^item_selected_/","",$name);
524         $ids[$id] = $id;
525       }
526     }
527     return($ids);
528   }
531   static function plInfo()
532   {
533     return (array(
534           "plShortName"   => _("System mass deployment"),
535           "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
536           "plSelfModify"  => FALSE,
537           "plDepends"     => array(),
538           "plPriority"    => 0,
539           "plSection"     => array("addon"),
540           "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
541           "plProvidedAcls" => array("Comment"   => _("Description")) 
542           ));
543   }
545 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
546 ?>