Code

Readded plain name to gotomasses closes #419
[gosa.git] / gosa-plugins / goto / addons / goto / class_gotomasses.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class gotomasses extends plugin
24 {
25   /* Definitions */
26   var $plHeadline     = "Deployment status";
27   var $plDescription  = "System deployment status";
29   /* attribute list for save action */
30   var $attributes= array();
31   var $objectclasses= array();
33   /* Queue tasks */
34   var $current        = FALSE;
35   var $dialog         = FALSE;
36   var $ids_to_remove  = array();
37   var $divlist        = NULL;
39   var $events         = array();
40   var $event_tags     = array();
42   var $sort_by  = "Schedule";
43   var $sort_dir = "down";
44   var $entries  = array();
45   var $range    = 25;
46   var $start    = 0;
48   function gotomasses(&$config, $dn= NULL)
49   {
50     /* Include config object */
51     $this->config= &$config;
52     $this->o_queue = new gosaSupportDaemon(TRUE,5);
53     $this->events  = DaemonEvent::get_event_types( SYSTEM_EVENT);
55     /* Get tags that will be used in queue searches */
56     $this->event_tags = array("none");
57     foreach($this->events['SCHEDULED'] as $evt){
58       $this->event_tags[] = $evt['s_Queued_Action'];
59     }
60   }
63   function execute()
64   {
65     $smarty = get_smarty();
66  
67     /************
68      * Handle posts 
69      ************/
70     
71     $s_entry = $s_action = "";
72     $arr = array( 
74         "/^pause_/"           => "pause",
75         "/^resume_/"          => "resume",
76         "/^execute_process_/" => "execute_process",
77         "/^abort_process_/"   => "abort_process",
79         "/^prio_up_/"     => "prio_up",
80         "/^prio_down_/"   => "prio_down",
82         "/^edit_task_/"             =>  "edit",
83         "/^remove_task_/"           =>  "remove",
84         "/^new_task_/"              =>  "new_task");;
87     foreach($arr as $regex => $action){
88       foreach($_POST as $name => $value){
89         if(preg_match($regex,$name)){
90           $s_action = $action;
91           $s_entry  = preg_replace($regex,"",$name);
92           $s_entry  = preg_replace("/_(x|y)$/","",$s_entry);
93         }
94       }
95     }
97     /* Menu actions */
98     if(isset($_POST['menu_action']) && !empty($_POST['menu_action'])){
99       $s_action = $_POST['menu_action'];
100     }
101     
102     /* Edit posted from list link */
103     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
104       $s_action = "edit";
105       $s_entry = $_GET['id'];
106     }
109     /************
110      * Handle Priority modifications  
111      ************/
113     if(preg_match("/^prio_/",$s_action)){
114       switch($s_action){
115         case 'prio_down'  : $this->update_priority($s_entry,"down");break;
116         case 'prio_up'    : $this->update_priority($s_entry,"up");break;
117       }
118     }
120     /************
121      * Handle pause/resume/execute modifications  
122      ************/
124     if(preg_match("/^resume/",$s_action) || 
125        preg_match("/^pause/",$s_action) || 
126        preg_match("/^abort_process/",$s_action) || 
127        preg_match("/^execute_process/",$s_action)){
129       switch($s_action){
130         case 'resume'         : $this->resume_queue_entries   (array($s_entry));break; 
131         case 'pause'          : $this->pause_queue_entries    (array($s_entry));break; 
132         case 'execute_process': $this->execute_queue_entries  (array($s_entry));break; 
133         case 'abort_process'  : $this->abort_queue_entries    (array($s_entry));break; 
134         case 'resume_all'         : $this->resume_queue_entries   ($this->list_get_selected_items());break; 
135         case 'pause_all'          : $this->pause_queue_entries    ($this->list_get_selected_items());break; 
136         case 'execute_process_all': $this->execute_queue_entries  ($this->list_get_selected_items());break; 
137         case 'abort_process_all'  : $this->abort_queue_entries    ($this->list_get_selected_items());break; 
139         default : trigger_error("Undefined action setting used (".$s_action.").");
140       }
141       if($this->o_queue->is_error()){
142         msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
143       }
144     }
146     /************
147      * ADD 
148      ************/
149   
150     if(preg_match("/^add_event_/",$s_action)){
151       $type = preg_replace("/^add_event_/","",$s_action);
152       if(isset($this->events['BY_CLASS'][$type])){
153         $e_data = $this->events['BY_CLASS'][$type];
154         $this->dialog = new $e_data['CLASS_NAME']($this->config);
155       }
156     }
158     /************
159      * EDIT
160      ************/
162     if($s_action == "edit"){  
163       $id =  $s_entry;
164       $type = FALSE;
165       if(isset($this->entries[$id])){
166         $event = $this->entries[$s_entry];
167         if($event['STATUS'] == "waiting" && isset($this->events['QUEUED'][$event['HEADERTAG']])){
168           $evt_name = $this->events['QUEUED'][$event['HEADERTAG']];
169           $type = $this->events['BY_CLASS'][$evt_name];
170           $this->dialog = new $type['CLASS_NAME']($this->config,$event);
171         }
172       }
173     }
175     /************
176      * REMOVE 
177      ************/
179     /* Remove multiple */
180     if($s_action == "remove_multiple" || $s_action == "remove"){
182       if(!$this->acl_is_removeable()){
183         msg_dialog::display(_("Permission"), msgPool::permDelete(), ERROR_DIALOG);
184       }else{
186         if($s_action == "remove"){
187           $ids = array($s_entry);
188         }else{
189           $ids = $this->list_get_selected_items();
190         }
192         $this->ids_to_remove = array();
194         if(count($ids)){
195           $ret = $this->o_queue->ids_exist($ids);
196           $ret = $this->o_queue->get_entries_by_id($ret);
197           $tmp = "";
198           foreach($ret as $task){
200             /* Only remove WAITING or ERROR entries */
201             if(in_array($task['STATUS'],array("waiting","error","processed")) || 
202                ($task['STATUS'] == "processing" && !preg_match("/install/",$task['HEADERTAG'])) ){
203               $this->ids_to_remove[] = $task['ID'];
204               if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
205                 $evt_name = $this->events['QUEUED'][$task['HEADERTAG']];
206                 $evt = $this->events['BY_CLASS'][$evt_name];
207                 $tmp.= "\n".$task['ID']." - ".$evt['s_Menu_Name']."&nbsp;".$task['MACADDRESS'];
208               }else{
209                 $tmp.= "\n".$task['ID']." - ".$task['HEADERTAG']."&nbsp;".$task['MACADDRESS'];
210               }
211             }
212           }
213           $smarty->assign("multiple", TRUE); 
214           $smarty->assign("info",msgPool::deleteInfo("<pre>".$tmp."</pre>"));
215           $this->current = $s_entry;
216           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
217         }
218       }
219     }
221     /* Remove specified tasks */
222     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
224       /* Reboot hosts with not yet startet installations and timestamps in the past 
225        */
226       timezone::get_default_timezone();
227       foreach($this->ids_to_remove as $id){
228         $entry = $this->o_queue->get_entries_by_id(array($id));
229         if(isset($entry['ANSWER1'])){
230           $entry = $entry['ANSWER1'];
231           if( $entry['STATUS'] == "waiting" && 
232               $entry['HEADERTAG'] == "trigger_action_reinstall"){
233             $evt = new DaemonEvent_reinstall($this->config,$entry);
234             if($evt->get_timestamp(FALSE)  < time()){
235               $r_evt = new DaemonEvent_localboot($this->config);
236               $r_evt->add_targets(array($entry['MACADDRESS']));
237               $r_evt->set_type(TRIGGERED_EVENT);
238               $this->o_queue->append($r_evt);
239             }
240           }
241         }
242       }
244       $this->o_queue->remove_entries($this->ids_to_remove);
245       $this->save();
246     }
248     /* Remove aborted */
249     if(isset($_POST['delete_cancel'])){
250       $this->ids_to_remove = array();;
251     }
254     /************
255      * EDIT 
256      ************/
258     /* Close dialog */
259     if(isset($_POST['save_event_dialog'])){
260       if(is_object($this->dialog)){
261         $this->dialog->save_object();
262         if(!$this->o_queue->append($this->dialog)){
263           msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->o_queue->get_error()),ERROR_DIALOG);
264         }else{
265           $this->dialog = FALSE; 
266           $this->current = -1;
267         } 
268       }
269     }
272     /* Close dialog */
273     if(isset($_POST['abort_event_dialog'])){
274       $this->dialog = FALSE;
275       $this->current = -1;
276     }
278     /* Display dialogs if currently opened */
279     if(is_object($this->dialog)){
280       $this->dialog->save_object();
281       return($this->dialog->execute());
282     }
284     /************
285      * Handle Divlist 
286      ************/
288     $divlist = new MultiSelectWindow($this->config,"gotoMasses",array("gotomasses"));
289     $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa tasks."));
290     $divlist->SetSummary(_("List of queued jobs"));
291     $divlist->EnableCloseButton(FALSE);
292     $divlist->EnableSaveButton(FALSE);
293     $divlist->SetHeadpageMode();
294     $s = ".|"._("Actions")."|\n";
295     $s.= "..|<img src='images/lists/new.png' alt='' border='0' class='center'>&nbsp;"._("Create")."\n";
297     foreach($this->events['SCHEDULED'] as $name =>  $event){
298       $s.= "...|".$event['MenuImage']."&nbsp;".$event['s_Menu_Name']."|add_event_".$name."\n";
299     }
300     if($this->acl_is_removeable()){
301       $s.= "..|---|\n";
302       $s.= "..|<img src='images/lists/trash.png' alt='' border='0' class='center'>&nbsp;"._("Remove")."|remove_multiple\n";
303     }
304     if(preg_match("/w/",$this->getacl(""))){
305       $s.= "..|---|\n";
306       $s.= "..|<img src='images/status_start.png' alt='' border='0' class='center'>&nbsp;"._("Resume")."|resume_all\n";
307       $s.= "..|<img src='images/status_pause.png' alt='' border='0' class='center'>&nbsp;"._("Pause")."|pause_all\n";
308       $s.= "..|<img src='images/small_error.png'  alt='' border='0' class='center'>&nbsp;"._("Abort")."|abort_process_all\n";
309       $s.= "..|<img src='images/rocket.png'       alt='' border='0' class='center'>&nbsp;"._("Execute")."|execute_process_all\n";
310     }
312     $divlist->SetDropDownHeaderMenu($s);
314     if($this->sort_dir == "up"){
315       $sort_img = "<img src='images/lists/sort-up.png' alt='/\' border=0>";
316     }else{
317       $sort_img = "<img src='images/lists/sort-down.png' alt='\/' border=0>";
318     }
320     if($this->sort_by == "TargetName"){ $sort_img_1 = $sort_img; } else { $sort_img_1 = "" ;}
321     if($this->sort_by == "TaskID"){ $sort_img_2 = $sort_img; } else { $sort_img_2 = "" ;}
322     if($this->sort_by == "Schedule"){ $sort_img_3 = $sort_img; } else { $sort_img_3 = "" ;}
323     if($this->sort_by == "Action"){ $sort_img_4 = $sort_img; } else { $sort_img_4 = "" ;}
325     /* Create divlist */
326     $divlist->SetListHeader("<input type='image' src='images/lists/reload.png' title='"._("Reload")."'>");
328     $plug  = $_GET['plug'];
329     $chk = "<input type='checkbox' id='select_all' name='select_all'
330                onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
332     /* set Page header */
333     $divlist->AddHeader(array("string"=> $chk,          "attach"=>"style='width:20px;'"));
334     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TargetName'>"._("Target").$sort_img_1."</a>"));
335     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TaskID'>"._("Task").$sort_img_2."</a>",
336                                       "attach"=>"style='width:120px;'"));
337     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
338                                       "attach"=>"style='width:100px;'"));
339     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Action'>"._("Status").$sort_img_4."</a>",
340                                       "attach"=>"style='width:80px;'"));
341     $divlist->AddHeader(array("string"=>_("Action"),
342                                       "attach"=>"style='border-right:0px;width:120px;'"));
345     /* Reload the list of entries */
346     $this->reload();
348     foreach($this->entries as $key => $task){
350       $prio_actions="";
351       $action = "";
353       /* If WAITING add priority action
354        */  
355       if(in_array($task['STATUS'],array("waiting"))){
356         $prio_actions.= "<input class='center' type='image' src='images/prio_increase.png' 
357           title='"._("Move up in execution queue")."' name='prio_up_".$key."'>&nbsp;";
358         $prio_actions.= "<input class='center' type='image' src='images/prio_decrease.png' 
359           title='"._("Move down in execution queue")."' name='prio_down_".$key."'>&nbsp;";
360       }
361     
362       /* If WAITING add pause action
363        */  
364       if(in_array($task['STATUS'],array("waiting"))){
365         $prio_actions.= "<input class='center' type='image' src='images/status_pause.png' 
366           title='"._("Pause job")."' name='pause_".$key."'>&nbsp;";
367       }
369       /* If PAUSED add resume action
370        */  
371       if(in_array($task['STATUS'],array("paused"))){
372         $prio_actions.= "<input class='center' type='image' src='images/status_start.png' 
373           title='"._("Resume job")."' name='resume_".$key."'>&nbsp;";
374       }
376       /* If PROCESSING add abort action
377        */  
378       if(in_array($task['STATUS'],array("processing")) && preg_match("/install/",$task['HEADERTAG'])){
379         $prio_actions.= "<input class='center' type='image' src='images/small_error.png' 
380           title='"._("Abort execution")."' name='abort_process_".$key."'>";
381       }
383       /* If PAUSED or WAITING add execution action
384        */  
385       if(in_array($task['STATUS'],array("paused","waiting"))){
386         $prio_actions.= "<input class='center' type='image' src='images/rocket.png' 
387           title='"._("Force execution now!")."' name='execute_process_".$key."'>&nbsp;";
388       }
390       /* If PAUSED or WAITING add edit action
391        */  
392       if(in_array($task['STATUS'],array("waiting"))){
393         $action.= "<input type='image' src='images/lists/edit.png' name='edit_task_".$key."' 
394           class='center' alt='"._("Edit")."'>";
395       }
397       /* If WAITING or ERROR add remove action
398        */  
399       if( $this->acl_is_removeable() && in_array($task['STATUS'],array("waiting","error","processed"))){
400         $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."' 
401           class='center' alt='"._("Remove")."'>";
402       }
403       if(in_array($task['STATUS'],array("processing")) && !preg_match("/install/",$task['HEADERTAG'])){
404         $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."' 
405           class='center' alt='"._("Remove")."'>";
406       }
408       /* Create entry display name and tooltip */
409       $color = "";
410       $display = $task['MACADDRESS'];
411       $tooltip = "";
412       if(isset($task['PLAINNAME'])){
413         $display = $task['PLAINNAME'];
414         $tooltip = " title='".$task['MACADDRESS']."' ";
415       }
416       $display2= $task['HEADERTAG'];
417      
418       /* Check if this event exists as Daemon class 
419        * In this case, display a more accurate entry.
420        */ 
421       if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
422         $evt_name   = $this->events['QUEUED'][$task['HEADERTAG']];
423         $event_type = $this->events['BY_CLASS'][$evt_name];
424         $display2   = $event_type['s_Menu_Name'];
426         if(strlen($display2) > 20){
427           $display2 = substr($display2,0,18)."...";
428         }
430         if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
431           $display2 = $event_type['ListImage']."&nbsp;".$display2;
432         }
433       } 
435       $status = $task['STATUS'];
436   
437       if($status == "waiting"){
438         $status = "<img class='center' src='images/clock.png' alt=''>&nbsp;"._("Waiting");
439       }
440       if($status == "error"){
441         $status = "<img class='center' src='images/false.png' alt=''>&nbsp;"._("Error");
442       }
443       if($status == "processed"){
444         $status = "<img class='center' src='images/true.png' alt=''>&nbsp;"._("Processed");
445       }
447       /* Special handling for all entries that have 
448           STATUS == "processing" && PROGRESS == NUMERIC
449        */
450       if($status == "processing" && isset($task['PROGRESS'])){
451         $percent = $task['PROGRESS'];
453         /* Show activation? */
454         if ($percent == "goto-activation"){
455           $status = "<img class='center' src='images/lists/off.png' alt=''>&nbsp;"._("Locked");
457         /* Show hardware detect? */
458         } elseif ($percent == "goto-hardware-detection") {
459           $status = "<img class='center' src='images/hardware.png' alt=''>&nbsp;"._("Detection");
461         /* Real percent */
462         } else {
463          if (preg_match('/install/', $task['HEADERTAG'])){
464             $status = "<img src='progress.php?x=80&amp;y=13&amp;p=".$percent."' alt='".$percent."&nbsp;%'>";
465           } else {
466             $status = preg_replace('/ /', '&nbsp;', _("in progress"));
467           }
468         }
469       }
471       /* Create each field */
472       $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$key."'>" ,
473                       "attach" => "style='width:20px;".$color."'");
474       $field1 = array("string" => $display,
475                       "attach" => $tooltip."style='".$color."'");
476       $field1a= array("string" => $display2,
477                       "attach" => "style='".$color.";width:120px;'");
478       $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:100px;'");
479       $field3 = array("string" => $status,"attach" => "style='".$color.";width:80px;'");
480       $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:120px;border-right:0px;'");
481       $divlist->AddElement(array($field0,$field1,$field1a,$field2,$field3,$field4));
482     }
484     $smarty = get_smarty();
485     $smarty->assign("events",$this->events);
486     $smarty->assign("start",$this->start);
487     $smarty->assign("start_real", ($this->start + 1));
488     $smarty->assign("ranges", array("10" => "10",
489                                     "20" => "20",
490                                     "25" => "25",
491                                     "50" => "50",
492                                     "100"=> "100",
493                                     "200"=> "200",
494                                     "9999" => "*"));
496     $count = $this->o_queue->number_of_queued_entries($this->event_tags);
497     if(!$count) $count = $this->range;
498     $divlist->SetListFooter(range_selector($count, $this->start, $this->range,"range"));
499     $smarty->assign("range",$this->range);
500     $smarty->assign("div",$divlist->Draw());
501     return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
502   }
505   /*! \brief  Move an entry up or down in the queue, by updating its execution timestamp  
506       @param  $id     Integer  The ID of the entry which should be updated.
507       @param  $type   String   "up" / "down"
508       @return boolean TRUE in case of success else FALSE
509   */
510   public function update_priority($id,$type = "up")
511   {
512     if($type == "up"){
513       $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp DESC");
514     }else{
515       $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp ASC");
516     }
517     $last = array();
518     foreach($tmp as $entry){
519       if($entry['ID'] == $id){
520         if(count($last)){
521           $time = strtotime($last['TIMESTAMP']);
522           if($type == "up"){
523             $time ++;
524           }else{
525             $time --;
526           }
527           $time_str = date("YmdHis",$time); 
528           return($this->o_queue->update_entries(array($id),array("timestamp" => $time_str)));
529         }else{
530           return(FALSE);
531         }
532       }
533       $last = $entry;
534     }
535     return(FALSE);
536   }
539   /*! \brief  Resumes to status 'waiting'.
540    *  @return Boolean TRUE in case of success, else FALSE. 
541    */
542   private function resume_queue_entries($ids)
543   {
544     if(!count($ids)){
545       return;
546     }
548     /* Entries are resumed by setting the status to 
549      *  'waiting'
550      */
551     $data = array("status"    => "waiting");
552   
553     /* Check if given ids are valid and check if the status
554      *  allows resuming.
555      */
556     $update_ids = array();
557     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
558       if(isset($entry['STATUS']) && preg_match("/paused/",$entry['STATUS'])){
559         $update_ids[] = $entry['ID'];
560       }
561     }
563     /* Tell the daemon that we have entries to update.
564      */
565     if(count($update_ids)){
566       if(!$this->o_queue->update_entries($update_ids,$data)){
567         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
568         return(FALSE);
569       }
570     }
571     return(TRUE);
572   }
575   /*! \brief  Force queue job to be done as far as possible.
576    *  @return Boolean TRUE in case of success, else FALSE.
577    */
578   private function execute_queue_entries($ids)
579   {
580     if(!count($ids)){
581       return;
582     }
584     /* Execution is forced by updating the status to 
585      *  waiting and setting the timestamp to current time.
586      */
587     $data = array(  "timestamp" => date("YmdHis",time()), 
588                     "status"    => "waiting");
590     /* Only allow execution of paused or waiting entries 
591      */
592     $update_ids = array();
593     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
594       if(in_array($entry['STATUS'],array("paused","waiting"))){
595         $update_ids[] = $entry['ID'];
596       }
597     }
599     /* Tell the daemon that we want to update some entries
600      */
601     if(count($update_ids)){
602       if(!$this->o_queue->update_entries($update_ids,$data)){
603         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entries.")) , ERROR_DIALOG);
604         return(FALSE);
605       }
606     }
607     return(TRUE);
608   }
611   /*! \brief  Force queue job to be done as far as possible.
612    *  @return Boolean TRUE in case of success, else FALSE.
613    */
614   private function abort_queue_entries($ids)
615   {
616     if(!count($ids)){
617       return;
618     }
620     /* Entries are paused by setting the status to
621      *  something different from 'waiting'.
622      * We simply use 'paused'.
623      */
624     $data = array("status"    => "paused");
626     /* Detect if the ids we got are valid and
627      *  check if the status allows pausing.
628      */
629     $update_ids = array();
630     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
631       if(isset($entry['STATUS']) && preg_match("/processing/",$entry['STATUS'])){
632         if(isset($entry['MACADDRESS'])){
633           $update_ids[] = $entry['MACADDRESS'];
634         }else{
635           trigger_error("No mac address found in event.");
636         }
637       }
638     }
640     if(class_available("DaemonEvent_faireboot")){
641       $tmp = new DaemonEvent_faireboot($this->config);
642       $tmp->add_targets($update_ids);
643       $tmp->set_type(TRIGGERED_EVENT);
644       if(!$this->o_queue->append($tmp)){
645         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
646         return(FALSE);
647       }
648     }else{
649       msg_dialog::display(_("Error"),
650           sprintf(_("The job could not be aborted, the required class '%s' was not found."),
651             "DaemonEvent_faireboot") , ERROR_DIALOG);
652     }
653   }
656   /*! \brief Pauses the specified queue entry from execution.
657    *  @return Boolean TRUE in case of success, else FALSE. 
658    */
659   private function pause_queue_entries($ids)
660   {
661     if(!count($ids)){
662       return;
663     }
665     /* Entries are paused by setting the status to 
666      *  something different from 'waiting'.
667      * We simply use 'paused'.
668      */   
669     $data = array("status"    => "paused");
671     /* Detect if the ids we got are valid and
672      *  check if the status allows pausing.
673      */ 
674     $update_ids = array();
675     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
676       if(isset($entry['STATUS']) && preg_match("/waiting/",$entry['STATUS'])){
677         $update_ids[] = $entry['ID'];
678       }
679     }
681     /* Tell the daemon that we want to update some entries
682      */
683     if(count($update_ids)){
684       if(!$this->o_queue->update_entries($update_ids,$data)){
685         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
686         return(FALSE);
687       }
688     }
689     return(TRUE);
690   }
693   /*! \brief  Request list of queued jobs.
694    *  @return Returns an array of all queued jobs.
695    */
696   function reload()
697   {
699     /* Sort map   html-post-name => daemon-col-name
700      */
701     $map = array(
702         "QueuePosition" => "id",
703         "Action"        => "status",
704         "TaskID"        => "headertag",
705         "TargetName"    => "macaddress",
706         "Schedule"      => "timestamp");
708     /* Create sort header 
709      */
710     if(!isset($map[$this->sort_by])){
711       $sort = "id DESC";
712     }else{
713       $sort   = $map[$this->sort_by]; 
714       if($this->sort_dir == "up"){
715         $sort.= " ASC";
716       }else{
717         $sort.= " DESC";
718       }
719     }
720      
721     /* Get entries. */ 
722     $start  = $this->start; 
723     $stop   = $this->range;
724     $entries = $this->o_queue->get_queued_entries($this->event_tags,$start,$stop,$sort);
725     if ($this->o_queue->is_error()){
726       msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
727     }
729     /* Assign entries by id.
730      */
731     $this->entries = array();
732     
733     foreach($entries as $entry){
734       $this->entries[$entry['ID']]= $entry;
735     }
736   }
739   /*! \brief  Handle post jobs, like sorting.
740    */
741   function save_object()
742   {
743     /* Check for sorting changes 
744      */
745     $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
746     if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
747       $sort = $_GET['sort'];
748       if($this->sort_by == $sort){
749         if($this->sort_dir == "up"){
750           $this->sort_dir = "down";
751         }else{
752           $this->sort_dir = "up";
753         }
754       }
755       $this->sort_by = $sort;
756     }
758     /* Range selection used? */
759     if(isset($_POST['range']) && is_numeric($_POST['range'])){
760       $this->range = $_POST['range'];
761     }
762     
763     /* Page changed. */
764     if(isset($_GET['start'])){
765       $start = $_GET['start'];
766       if(is_numeric($start) || $start == 0){
767         $this->start = $start;
768       }
769     }
771     /* Check start stop and reset if necessary */
772     $count = $this->o_queue->number_of_queued_entries($this->event_tags);
773     if($this->start >= $count){
774       $this->start = $count -1;
775     }
776     if($this->start < 0){
777       $this->start = 0;
778     }
779   }
782   function save()
783   {
784     // We do not save anything here.
785   }
788   /*! \brief  Return a list of all selected items.
789     @return Array   Returns an array containing all selected item ids.
790    */
791   function list_get_selected_items()
792   {
793     $ids = array();
794     foreach($_POST as $name => $value){
795       if(preg_match("/^item_selected_[0-9]*$/",$name)){
796         $id   = preg_replace("/^item_selected_/","",$name);
797         $ids[$id] = $id;
798       }
799     }
800     return($ids);
801   }
804   static function plInfo()
805   {
806     return (array(
807           "plShortName"   => _("System mass deployment"),
808           "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
809           "plSelfModify"  => FALSE,
810           "plDepends"     => array(),
811           "plPriority"    => 0,
812           "plSection"     => array("addon"),
813           "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
814           "plProvidedAcls" => array("Comment"   => _("Description")) 
815           ));
816   }
818 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
819 ?>