Code

8a800559825e6a9c644520f3e9a4f8876f54ff65
[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( 
47         "/^pause_/"           => "pause",
48         "/^resume_/"          => "resume",
49         "/^execute_process_/" => "execute_process",
50         "/^abort_process_/"   => "abort_process",
52         "/^prio_up_/"     => "prio_up",
53         "/^prio_down_/"   => "prio_down",
55         "/^edit_task_/"             =>  "edit",
56         "/^remove_task_/"           =>  "remove",
57         "/^new_task_/"              =>  "new_task");;
60     foreach($arr as $regex => $action){
61       foreach($_POST as $name => $value){
62         if(preg_match($regex,$name)){
63           $s_action = $action;
64           $s_entry  = preg_replace($regex,"",$name);
65           $s_entry  = preg_replace("/_(x|y)$/","",$s_entry);
66         }
67       }
68     }
70     /* Menu actions */
71     if(isset($_POST['menu_action']) && !empty($_POST['menu_action'])){
72       $s_action = $_POST['menu_action'];
73     }
74     
75     /* Edit posted from list link */
76     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
77       $s_action = "edit";
78       $s_entry = $_GET['id'];
79     }
82     /************
83      * Handle Priority modifications  
84      ************/
86     if(preg_match("/^prio_/",$s_action)){
88       switch($s_action){
90         case 'prio_down'  : $this->update_priority($s_entry,"down");break;
91         case 'prio_up'    : $this->update_priority($s_entry,"up");break;
92       }
93     }
95     /************
96      * Handle pause/resume/execute modifications  
97      ************/
99     if(preg_match("/^resume/",$s_action) || 
100        preg_match("/^pause/",$s_action) || 
101        preg_match("/^abort_process/",$s_action) || 
102        preg_match("/^execute_process/",$s_action)){
104       switch($s_action){
105         case 'resume'         : $this->resume_queue_entries   (array($s_entry));break; 
106         case 'pause'          : $this->pause_queue_entries    (array($s_entry));break; 
107         case 'execute_process': $this->execute_queue_entries  (array($s_entry));break; 
108         case 'abort_process'  : $this->abort_queue_entries    (array($s_entry));break; 
110         case 'resume_all'         : $this->resume_queue_entries   ($this->list_get_selected_items());break; 
111         case 'pause_all'          : $this->pause_queue_entries    ($this->list_get_selected_items());break; 
112         case 'execute_process_all': $this->execute_queue_entries  ($this->list_get_selected_items());break; 
113         case 'abort_process_all'  : $this->abort_queue_entries    ($this->list_get_selected_items());break; 
115         default : trigger_error("Undefined action setting used (".$s_action.").");
116       }
117       if($this->o_queue->is_error()){
118         msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
119       }
120     }
122     /************
123      * ADD 
124      ************/
125   
126     if(preg_match("/^add_event_/",$s_action)){
127       $type = preg_replace("/^add_event_/","",$s_action);
128       if(isset($this->events['BY_CLASS'][$type])){
129         $e_data = $this->events['BY_CLASS'][$type];
130         $this->dialog = new $e_data['CLASS_NAME']($this->config);
131       }
132     }
134     /************
135      * EDIT
136      ************/
138     if($s_action == "edit"){  
139       $id =  $s_entry;
140       $type = FALSE;
141       if(isset($this->entries[$id])){
142         $event = $this->entries[$s_entry];
143         if(isset($this->events['BY_QUEUED_ACTION'][$event['HEADERTAG']])){
144           $type = $this->events['BY_QUEUED_ACTION'][$event['HEADERTAG']];
145           $this->dialog = new $type['CLASS_NAME']($this->config,$event);
146         }
147       }
148     }
150     /************
151      * REMOVE 
152      ************/
154     /* Remove multiple */
155     if($s_action == "remove_multiple" || $s_action == "remove"){
157       if(!$this->acl_is_removeable()){
158         msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), ERROR_DIALOG);
159       }else{
161         if($s_action == "remove"){
162           $ids = array($s_entry);
163         }else{
164           $ids = $this->list_get_selected_items();
165         }
166         if(count($ids)){
167           $this->ids_to_remove = $ids;
168           $ret = $this->o_queue->ids_exist($this->ids_to_remove);
169           $ret = $this->o_queue->get_entries_by_id($ret);
171           $tmp = "";
172           foreach($ret as $task){
173             if(isset($this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']])){
174               $evt = $this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']];
175               $tmp.= "\n".$task['ID']." - ".$evt['s_Menu_Name']."&nbsp;".$task['MACADDRESS'];
176             }else{
177               $tmp.= "\n".$task['ID']." - ".$task['HEADERTAG']."&nbsp;".$task['MACADDRESS'];
178             }
179           }
180           $smarty->assign("multiple", TRUE); 
181           $smarty->assign("info",sprintf(_("You are about to remove the following actions from the GOsa support Daemon: %s"),"<pre>".$tmp."</pre>"));
182           $this->current = $s_entry;
183           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
184         }
185       }
186     }
188     /* Remove specified tasks */
189     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
190       $this->o_queue->remove_entries($this->ids_to_remove);
191       $this->save();
192     }
194     /* Remove aborted */
195     if(isset($_POST['delete_cancel'])){
196       $this->ids_to_remove = array();;
197     }
200     /************
201      * EDIT 
202      ************/
204     /* Close dialog */
205     if(isset($_POST['save_event_dialog'])){
206       if(is_object($this->dialog)){
207         $this->dialog->save_object();
208         if($this->dialog->is_new()){
209           $header     = $this->dialog->get_schedule_action();
210           $targets    = $this->dialog->get_targets();
211           $data       = $this->dialog->save();
213           foreach($targets as $target){
214             $data['macaddress'] =  $target;
215             $this->o_queue->send_data($header,$target,$data,TRUE);
216             if($this->o_queue->is_error()){
217               msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
218                     $this->o_queue->get_error()),ERROR_DIALOG);
219             }else{
220               $this->dialog = FALSE; 
221               $this->current = -1;
222             } 
223           }
224         }else{
225           $id                 = $this->dialog->get_id();
226           $data               = $this->dialog->save();
227           if($this->o_queue->update_entries(array($id),$data)){
228             $this->dialog = FALSE;
229             $this->current = -1;
230           }else{
231             msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
232               $this->o_queue->get_error()),ERROR_DIALOG);
233           }
234         }
235       }
236     }
239     /* Close dialog */
240     if(isset($_POST['abort_event_dialog'])){
241       $this->dialog = FALSE;
242       $this->current = -1;
243     }
245     /* Display dialogs if currently opened */
246     if(is_object($this->dialog)){
247       $this->dialog->save_object();
248       return($this->dialog->execute());
249     }
251     /************
252      * Handle Divlist 
253      ************/
255     $divlist = new MultiSelectWindow($this->config,"gotoMasses",array("gotomasses"));
256     $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa deamon tasks."));
257     $divlist->SetSummary(_("List of queued deamon jobs"));
258     $divlist->EnableCloseButton(FALSE);
259     $divlist->EnableSaveButton(FALSE);
260     $divlist->SetHeadpageMode();
261     $s = ".|"._("Actions")."|\n";
262     $s.= "..|<img src='images/list_new.png' alt='' border='0' class='center'>&nbsp;"._("Create")."\n";
263     foreach($this->events['BY_CLASS'] as $name =>  $event){
264       $s.= "...|".$event['MenuImage']."&nbsp;".$event['s_Menu_Name']."|add_event_".$name."\n";
265     }
266     if($this->acl_is_removeable()){
267       $s.= "..|---|\n";
268       $s.= "..|<img src='images/edittrash.png' alt='' border='0' class='center'>&nbsp;"._("Remove")."|remove_multiple\n";
269     }
270     if(preg_match("/w/",$this->getacl(""))){
271       $s.= "..|---|\n";
272       $s.= "..|<img src='images/status_start.png' alt='' border='0' class='center'>&nbsp;"._("Resume all")."|resume_all\n";
273       $s.= "..|<img src='images/status_pause.png' alt='' border='0' class='center'>&nbsp;"._("Pause all")."|pause_all\n";
274       $s.= "..|<img src='images/small_error.png'  alt='' border='0' class='center'>&nbsp;"._("Aboer all")."|abort_process_all\n";
275       $s.= "..|<img src='images/rocket.png'       alt='' border='0' class='center'>&nbsp;"._("Execute all")."|execute_process_all\n";
276     }
278     $divlist->SetDropDownHeaderMenu($s);
280     if($this->sort_dir == "up"){
281       $sort_img = "<img src='images/sort_up.png' alt='/\' border=0>";
282     }else{
283       $sort_img = "<img src='images/sort_down.png' alt='\/' border=0>";
284     }
286     if($this->sort_by == "TargetName"){ $sort_img_1 = $sort_img; } else { $sort_img_1 = "" ;}
287     if($this->sort_by == "TaskID"){ $sort_img_2 = $sort_img; } else { $sort_img_2 = "" ;}
288     if($this->sort_by == "Schedule"){ $sort_img_3 = $sort_img; } else { $sort_img_3 = "" ;}
289     if($this->sort_by == "Action"){ $sort_img_4 = $sort_img; } else { $sort_img_4 = "" ;}
291     /* Create divlist */
292     $divlist->SetListHeader("<input type='image' src='images/list_reload.png' title='"._("Reload")."'>");
294     $plug  = $_GET['plug'];
295     $chk = "<input type='checkbox' id='select_all' name='select_all'
296                onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
298     /* set Page header */
299     $divlist->AddHeader(array("string"=> $chk,          "attach"=>"style='width:20px;'"));
300     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TargetName'>"._("Target").$sort_img_1."</a>"));
301     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TaskID'>"._("Task").$sort_img_2."</a>",
302                                       "attach"=>"style='width:120px;'"));
303     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
304                                       "attach"=>"style='width:100px;'"));
305     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Action'>"._("Status").$sort_img_4."</a>",
306                                       "attach"=>"style='width:80px;'"));
307     $divlist->AddHeader(array("string"=>_("Action"),
308                                       "attach"=>"style='border-right:0px;width:120px;'"));
311     /* Reload the list of entries */
312     $this->reload();
314     foreach($this->entries as $key => $task){
316       $prio_actions="";
317       if(isset($task['STATUS']) && preg_match("/waiting/",$task['STATUS'])){
318         $prio_actions.= "<input class='center' type='image' src='images/prio_increase.png' 
319           title='"._("Move up in execution queue")."' name='prio_up_".$key."'>&nbsp;";
320         $prio_actions.= "<input class='center' type='image' src='images/prio_decrease.png' 
321           title='"._("Move down in execution queue")."' name='prio_down_".$key."'>&nbsp;";
322       }
323      
324       if(isset($task['STATUS']) && preg_match("/waiting/",$task['STATUS'])){
325         $prio_actions.= "<input class='center' type='image' src='images/status_pause.png' 
326           title='"._("Pause job")."' name='pause_".$key."'>&nbsp;";
327       }
328       if(isset($task['STATUS']) && preg_match("/paused/",$task['STATUS'])){
329         $prio_actions.= "<input class='center' type='image' src='images/status_start.png' 
330           title='"._("Resume job")."' name='resume_".$key."'>&nbsp;";
331       }
333       if(isset($task['STATUS']) && preg_match("/processing/",$task['STATUS'])){
334         $prio_actions.= "<input class='center' type='image' src='images/small_error.png' 
335           title='"._("Abort execution")."' name='abort_process_".$key."'>&nbsp;";
336       }
338       if(isset($task['STATUS']) && 
339           (preg_match("/paused/",$task['STATUS']) || preg_match("/waiting/",$task['STATUS']))){
340         $prio_actions.= "<input class='center' type='image' src='images/rocket.png' 
341           title='"._("Force execution now!")."' name='execute_process_".$key."'>&nbsp;";
342       }
344       $action = "<input type='image' src='images/edit.png' name='edit_task_".$key."' 
345         class='center' alt='"._("Edit")."'>";
347       if($this->acl_is_removeable()){
348         $action.= "<input type='image' src='images/edittrash.png' name='remove_task_".$key."' 
349           class='center' alt='"._("Reomve")."'>";
350       }
352       $color = "";
353       $display = $task['MACADDRESS'];
354       $display2= $task['HEADERTAG'];
355      
356       /* Check if this event exists as Daemon class 
357        * In this case, display a more accurate entry.
358        */ 
359       if(isset($this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']]['s_Menu_Name'])){
360         $event_type = $this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']];
361         $display2= $event_type['s_Menu_Name'];
362         if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
363           $display2 = $event_type['ListImage']."&nbsp;".$display2;
364         }
365       } 
367       $status = $task['STATUS'];
369       /* Special handling for all entries that have 
370           STATUS == "processing" && PROGRESS == NUMERIC
371        */
372       if($status == "processing" && isset($task['PROGRESS'])){
373         $percent = $task['PROGRESS'];
374         $status = "<img src='progress.php?x=80&amp;y=13&amp;p=".$percent."' alt='".$percent."&nbsp;%'>";
375       }
378       /* Create each field */
379       $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$key."'>" ,
380                       "attach" => "style='width:20px;".$color."'");
381       $field1 = array("string" => $display,
382                       "attach" => "style='".$color."'");
383       $field1a= array("string" => $display2,
384                       "attach" => "style='".$color.";width:120px;'");
385       $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:100px;'");
386       $field3 = array("string" => $status,"attach" => "style='".$color.";width:80px;'");
387       $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:120px;border-right:0px;'");
388       $divlist->AddElement(array($field0,$field1,$field1a,$field2,$field3,$field4));
389     }
391     $smarty = get_smarty();
392     $smarty->assign("events",$this->events);
393     $smarty->assign("start",$this->start);
394     $smarty->assign("start_real", ($this->start + 1));
395     $smarty->assign("ranges", array("10" => "10",
396                                     "20" => "20",
397                                     "25" => "25",
398                                     "50" => "50",
399                                     "100"=> "100",
400                                     "200"=> "200",
401                                     "9999" => "*"));
403     $count = $this->o_queue->number_of_queued_entries();
404     $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"range"));
405     $smarty->assign("range",$this->range);
406     $smarty->assign("div",$divlist->Draw());
407     return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
408   }
411   /*! \brief  Move an entry up or down in the queue, by updating its execution timestamp  
412       @param  $id     Integer  The ID of the entry which should be updated.
413       @param  $type   String   "up" / "down"
414       @return boolean TRUE in case of success else FALSE
415   */
416   public function update_priority($id,$type = "up")
417   {
418     if($type == "up"){
419       $tmp = $this->o_queue->get_queued_entries(-1,-1,"timestamp DESC");
420     }else{
421       $tmp = $this->o_queue->get_queued_entries(-1,-1,"timestamp ASC");
422     }
423     $last = array();
424     foreach($tmp as $entry){
425       if($entry['ID'] == $id){
426         if(count($last)){
427           $time = strtotime($last['TIMESTAMP']);
428           if($type == "up"){
429             $time ++;
430           }else{
431             $time --;
432           }
433           $time_str = date("YmdHis",$time); 
434           return($this->o_queue->update_entries(array($id),array("timestamp" => $time_str)));
435         }else{
436           return(FALSE);
437         }
438       }
439       $last = $entry;
440     }
441     return(FALSE);
442   }
445   /*! \brief  Resumes to status 'waiting'.
446    *  @return Boolean TRUE in case of success, else FALSE. 
447    */
448   private function resume_queue_entries($ids)
449   {
450     if(!count($ids)){
451       return;
452     }
453   
454     $data = array("status"    => "waiting");
455     if(!$this->o_queue->update_entries($ids,$data)){
456       msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
457       return(FALSE);
458     }
459     return(TRUE);
460   }
463   /*! \brief  Force queue job to be done as far as possible.
464    *  @return Boolean TRUE in case of success, else FALSE.
465    */
466   private function execute_queue_entries($ids)
467   {
468     if(!count($ids)){
469       return;
470     }
472     $data = array(  "timestamp" => date("YmdHis",time()), 
473                     "status"    => "waiting");
474     if(!$this->o_queue->update_entries($ids,$data)){
475       msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
476       return(FALSE);
477     }
478     return(TRUE);
479   }
482   /*! \brief  Force queue job to be done as far as possible.
483    *  @return Boolean TRUE in case of success, else FALSE.
484    */
485   private function abort_queue_entries($ids)
486   {
487     if(!count($ids)){
488       return;
489     }
490     print_red(_("Not implemented."));
491     return(TRUE);
492   }
495   /*! \brief Pauses the specified queue entry from execution.
496    *  @return Boolean TRUE in case of success, else FALSE. 
497    */
498   private function pause_queue_entries($ids)
499   {
500     if(!count($ids)){
501       return;
502     }
503     $data = array("status"    => "paused");
504     if(!$this->o_queue->update_entries($ids,$data)){
505       msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
506       return(FALSE);
507     }
508     return(TRUE);
509   }
512   /*! \brief  Request list of queued jobs.
513    *  @return Returns an array of all queued jobs.
514    */
515   function reload()
516   {
517     $map = array(
518         "QueuePosition" => "id",
519         "Action"        => "status",
520         "TaskID"        => "headertag",
521         "TargetName"    => "macaddress",
522         "Schedule"      => "timestamp");
524     if(!isset($map[$this->sort_by])){
525       $sort = "id DESC";
526     }else{
527       $sort   = $map[$this->sort_by]; 
528       if($this->sort_dir == "up"){
529         $sort.= " ASC";
530       }else{
531         $sort.= " DESC";
532       }
533     }
534       
535     $start  = $this->start; 
536     $stop   = $this->range;
538     $entries = $this->o_queue->get_queued_entries($start,$stop,$sort);
539     if ($this->o_queue->is_error()){
541       msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
542     }
544     $this->entries = array();
545     foreach($entries as $entry){
546       $this->entries[$entry['ID']]= $entry;
547     }
548   }
551   /*! \brief  Handle post jobs, like sorting.
552    */
553   function save_object()
554   {
555     $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
556     if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
557       $sort = $_GET['sort'];
558       if($this->sort_by == $sort){
559         if($this->sort_dir == "up"){
560           $this->sort_dir = "down";
561         }else{
562           $this->sort_dir = "up";
563         }
564       }
565       $this->sort_by = $sort;
566     }
567     if(isset($_POST['range']) && is_numeric($_POST['range'])){
568       $this->range = $_POST['range'];
569     }
570     if(isset($_GET['start'])){
571       $start = $_GET['start'];
572       if(is_numeric($start) || $start == 0){
573         $this->start = $start;
574       }
575     }
577     /* Check start stop and reset if necessary */
578     $count = $this->o_queue->number_of_queued_entries();
579     if($this->start >= $count){
580       $this->start = $count -1;
581     }
582     if($this->start < 0){
583       $this->start = 0;
584     }
585   }
588   function save()
589   {
590     // We do not save anything here.
591   }
594   /*! \brief  Return a list of all selected items.
595     @return Array   Returns an array containing all selected item ids.
596    */
597   function list_get_selected_items()
598   {
599     $ids = array();
600     foreach($_POST as $name => $value){
601       if(preg_match("/^item_selected_[0-9]*$/",$name)){
602         $id   = preg_replace("/^item_selected_/","",$name);
603         $ids[$id] = $id;
604       }
605     }
606     return($ids);
607   }
610   static function plInfo()
611   {
612     return (array(
613           "plShortName"   => _("System mass deployment"),
614           "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
615           "plSelfModify"  => FALSE,
616           "plDepends"     => array(),
617           "plPriority"    => 0,
618           "plSection"     => array("addon"),
619           "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
620           "plProvidedAcls" => array("Comment"   => _("Description")) 
621           ));
622   }
624 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
625 ?>