Code

Removed reference
[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 = "up";
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     }
61     /* Load filter settings */
62     if(!session::is_set("gotomasses_filter")){
63       $gotomasses_filter = 
64         array(
65             "range" => $this->range,
66             "sort_by" => $this->sort_by,
67             "sort_dir" => $this->sort_dir);
68       session::set("gotomasses_filter",$gotomasses_filter);
69     }
70     $gotomasses_filter = session::get("gotomasses_filter");
71     foreach(array("range","sort_by","sort_dir") as $attr) {
72       $this->$attr = $gotomasses_filter[$attr];
73     }
74   }
77   function execute()
78   {
79     $smarty = get_smarty();
80  
81     /************
82      * Handle posts 
83      ************/
84     
85     $s_entry = $s_action = "";
86     $arr = array( 
88         "/^pause_/"           => "pause",
89         "/^resume_/"          => "resume",
90         "/^execute_process_/" => "execute_process",
91         "/^abort_process_/"   => "abort_process",
93         "/^prio_up_/"     => "prio_up",
94         "/^prio_down_/"   => "prio_down",
96         "/^edit_task_/"             =>  "edit",
97         "/^log_view_/"              =>  "logview",
98         "/^remove_task_/"           =>  "remove",
99         "/^new_task_/"              =>  "new_task");;
102     foreach($arr as $regex => $action){
103       foreach($_POST as $name => $value){
104         if(preg_match($regex,$name)){
105           $s_action = $action;
106           $s_entry  = preg_replace($regex,"",$name);
107           $s_entry  = preg_replace("/_(x|y)$/","",$s_entry);
108         }
109       }
110     }
112     /* Menu actions */
113     if(isset($_POST['menu_action']) && !empty($_POST['menu_action'])){
114       $s_action = $_POST['menu_action'];
115     }
116     
117     /* Edit posted from list link */
118     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
119       $s_action = "edit";
120       $s_entry = $_GET['id'];
121     }
124     /************
125      * Handle Priority modifications  
126      ************/
128     if(preg_match("/^prio_/",$s_action)){
129       switch($s_action){
130         case 'prio_down'  : $this->update_priority($s_entry,"down");break;
131         case 'prio_up'    : $this->update_priority($s_entry,"up");break;
132       }
133     }
135     /************
136      * Handle pause/resume/execute modifications  
137      ************/
139     if(preg_match("/^resume/",$s_action) || 
140        preg_match("/^pause/",$s_action) || 
141        preg_match("/^abort_process/",$s_action) || 
142        preg_match("/^execute_process/",$s_action)){
144       switch($s_action){
145         case 'resume'         : $this->resume_queue_entries   (array($s_entry));break; 
146         case 'pause'          : $this->pause_queue_entries    (array($s_entry));break; 
147         case 'execute_process': $this->execute_queue_entries  (array($s_entry));break; 
148         case 'abort_process'  : $this->abort_queue_entries    (array($s_entry));break; 
149         case 'resume_all'         : $this->resume_queue_entries   ($this->list_get_selected_items());break; 
150         case 'pause_all'          : $this->pause_queue_entries    ($this->list_get_selected_items());break; 
151         case 'execute_process_all': $this->execute_queue_entries  ($this->list_get_selected_items());break; 
152         case 'abort_process_all'  : $this->abort_queue_entries    ($this->list_get_selected_items());break; 
154         default : trigger_error("Undefined action setting used (".$s_action.").");
155       }
156       if($this->o_queue->is_error()){
157         msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
158       }
159     }
161     /************
162      * ADD 
163      ************/
164   
165     if(preg_match("/^add_event_/",$s_action)){
166       $type = preg_replace("/^add_event_/","",$s_action);
167       if(isset($this->events['BY_CLASS'][$type])){
168         $e_data = $this->events['BY_CLASS'][$type];
169         $this->dialog = new $e_data['CLASS_NAME']($this->config);
170       }
171     }
173     /************
174      * EDIT
175      ************/
177     if($s_action == "edit"){  
178       $id =  $s_entry;
179       $type = FALSE;
180       if(isset($this->entries[$id])){
181         $event = $this->entries[$s_entry];
182         if($event['STATUS'] == "waiting" && isset($this->events['QUEUED'][$event['HEADERTAG']])){
183           $evt_name = $this->events['QUEUED'][$event['HEADERTAG']];
184           $type = $this->events['BY_CLASS'][$evt_name];
185           $this->dialog = new $type['CLASS_NAME']($this->config,$event);
186         }
187       }
188     }
190     
191     /************
192      * LOG VIEW
193      ************/
195     if($s_action == "logview"){  
196       $id =  $s_entry;
197       $type = FALSE;
198       if(isset($this->entries[$id])){
199         $event = $this->entries[$s_entry];
200         $this->dialog = new goto_log_view($this->config,$event,$this);
201       }
202     }
205     /************
206      * REMOVE 
207      ************/
209     /* Remove multiple */
210     if($s_action == "remove_multiple" || $s_action == "remove"){
212       if(!$this->acl_is_removeable()){
213         msg_dialog::display(_("Permission"), msgPool::permDelete(), ERROR_DIALOG);
214       }else{
216         if($s_action == "remove"){
217           $ids = array($s_entry);
218         }else{
219           $ids = $this->list_get_selected_items();
220         }
222         $this->ids_to_remove = array();
224         if(count($ids)){
225           $ret = $this->o_queue->ids_exist($ids);
226           $ret = $this->o_queue->get_entries_by_id($ret);
227           $tmp = "";
228           foreach($ret as $task){
230             /* Only remove WAITING or ERROR entries */
231             if(in_array($task['STATUS'],array("waiting","error","processed")) || 
232                ($task['STATUS'] == "processing" && !preg_match("/install/",$task['HEADERTAG'])) ){
233               $this->ids_to_remove[] = $task['ID'];
234               if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
235                 $evt_name = $this->events['QUEUED'][$task['HEADERTAG']];
236                 $evt = $this->events['BY_CLASS'][$evt_name];
237                 $tmp.= "\n".$task['ID']." - ".$evt['s_Menu_Name']."&nbsp;".$task['MACADDRESS'];
238               }else{
239                 $tmp.= "\n".$task['ID']." - ".$task['HEADERTAG']."&nbsp;".$task['MACADDRESS'];
240               }
241             }
242           }
243           $smarty->assign("multiple", TRUE); 
244           $smarty->assign("info",msgPool::deleteInfo("<pre>".$tmp."</pre>"));
245           $this->current = $s_entry;
246           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
247         }
248       }
249     }
251     /* Remove specified tasks */
252     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
254       /* Reboot hosts with not yet startet installations and timestamps in the past 
255        */
256       timezone::get_default_timezone();
257       foreach($this->ids_to_remove as $id){
258         $entry = $this->o_queue->get_entries_by_id(array($id));
259         if(isset($entry['ANSWER1'])){
260           $entry = $entry['ANSWER1'];
261           if( $entry['STATUS'] == "waiting" && 
262               $entry['HEADERTAG'] == "trigger_action_reinstall"){
263             $evt = new DaemonEvent_reinstall($this->config,$entry);
264             if($evt->get_timestamp(FALSE)  < time()){
265               $r_evt = new DaemonEvent_localboot($this->config);
266               $r_evt->add_targets(array($entry['MACADDRESS']));
267               $r_evt->set_type(TRIGGERED_EVENT);
268               $this->o_queue->append($r_evt);
269             }
270           }
271         }
272       }
274       $this->o_queue->remove_entries($this->ids_to_remove);
275       $this->save();
276     }
278     /* Remove aborted */
279     if(isset($_POST['delete_cancel'])){
280       $this->ids_to_remove = array();;
281     }
284     /************
285      * EDIT 
286      ************/
288     /* Close dialog */
289     if(isset($_POST['save_event_dialog'])){
290       if(is_object($this->dialog)){
291         $this->dialog->save_object();
292         if(!$this->o_queue->append($this->dialog)){
293           msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->o_queue->get_error()),ERROR_DIALOG);
294         }else{
295           $this->dialog = FALSE; 
296           $this->current = -1;
297         } 
298       }
299     }
302     /* Close dialog */
303     if(isset($_POST['abort_event_dialog'])){
304       $this->dialog = FALSE;
305       $this->current = -1;
306     }
308     /* Display dialogs if currently opened */
309     if(is_object($this->dialog)){
310       $this->dialog->save_object();
311       return($this->dialog->execute());
312     }
314     /************
315      * Handle Divlist 
316      ************/
318     $divlist = new MultiSelectWindow($this->config,"gotoMasses",array("gotomasses"));
319     $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa tasks."));
320     $divlist->SetSummary(_("List of queued jobs"));
321     $divlist->EnableCloseButton(FALSE);
322     $divlist->EnableSaveButton(FALSE);
323     $divlist->SetHeadpageMode();
324     $s = ".|"._("Actions")."|\n";
325     $s.= "..|<img src='images/lists/new.png' alt='' border='0' class='center'>&nbsp;"._("Create")."\n";
327     foreach($this->events['SCHEDULED'] as $name =>  $event){
328       $s.= "...|".$event['MenuImage']."&nbsp;".$event['s_Menu_Name']."|add_event_".$name."\n";
329     }
330     if($this->acl_is_removeable()){
331       $s.= "..|---|\n";
332       $s.= "..|<img src='images/lists/trash.png' alt='' border='0' class='center'>&nbsp;"._("Remove")."|remove_multiple\n";
333     }
334     if(preg_match("/w/",$this->getacl(""))){
335       $s.= "..|---|\n";
336       $s.= "..|<img src='images/status_start.png' alt='' border='0' class='center'>&nbsp;"._("Resume")."|resume_all\n";
337       $s.= "..|<img src='images/status_pause.png' alt='' border='0' class='center'>&nbsp;"._("Pause")."|pause_all\n";
338       $s.= "..|<img src='images/small_error.png'  alt='' border='0' class='center'>&nbsp;"._("Abort")."|abort_process_all\n";
339       $s.= "..|<img src='images/rocket.png'       alt='' border='0' class='center'>&nbsp;"._("Execute")."|execute_process_all\n";
340     }
342     $divlist->SetDropDownHeaderMenu($s);
344     if($this->sort_dir == "up"){
345       $sort_img = "<img src='images/lists/sort-up.png' alt='/\' border=0>";
346     }else{
347       $sort_img = "<img src='images/lists/sort-down.png' alt='\/' border=0>";
348     }
350     if($this->sort_by == "TargetName"){ $sort_img_1 = $sort_img; } else { $sort_img_1 = "" ;}
351     if($this->sort_by == "TaskID"){ $sort_img_2 = $sort_img; } else { $sort_img_2 = "" ;}
352     if($this->sort_by == "Schedule"){ $sort_img_3 = $sort_img; } else { $sort_img_3 = "" ;}
353     if($this->sort_by == "Action"){ $sort_img_4 = $sort_img; } else { $sort_img_4 = "" ;}
355     /* Create divlist */
356     $divlist->SetListHeader("<input type='image' src='images/lists/reload.png' title='"._("Reload")."'>");
358     $plug  = $_GET['plug'];
359     $chk = "<input type='checkbox' id='select_all' name='select_all'
360                onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
362     /* set Page header */
363     $divlist->AddHeader(array("string"=> $chk,          "attach"=>"style='width:20px;'"));
364     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TargetName'>"._("Target").$sort_img_1."</a>"));
365     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TaskID'>"._("Task").$sort_img_2."</a>",
366                                       "attach"=>"style='width:120px;'"));
367     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
368                                       "attach"=>"style='width:140px;'"));
369     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Action'>"._("Status").$sort_img_4."</a>",
370                                       "attach"=>"style='width:80px;'"));
371     $divlist->AddHeader(array("string"=>_("Action"),
372                                       "attach"=>"style='border-right:0px;width:140px;'"));
375     /* Reload the list of entries */
376     $this->reload();
378     foreach($this->entries as $key => $task){
380       $prio_actions="";
381       $action = "";
384       /* If WAITING add priority action
385        */  
386       if(in_array($task['STATUS'],array("waiting"))){
387         $prio_actions.= "<input class='center' type='image' src='images/prio_increase.png' 
388           title='"._("Move up in execution queue")."' name='prio_up_".$key."'>&nbsp;";
389         $prio_actions.= "<input class='center' type='image' src='images/prio_decrease.png' 
390           title='"._("Move down in execution queue")."' name='prio_down_".$key."'>&nbsp;";
391       }
392     
393       /* If WAITING add pause action
394        */  
395       if(in_array($task['STATUS'],array("waiting"))){
396         $prio_actions.= "<input class='center' type='image' src='images/status_pause.png' 
397           title='"._("Pause job")."' name='pause_".$key."'>&nbsp;";
398       }
400       /* If PAUSED add resume action
401        */  
402       if(in_array($task['STATUS'],array("paused"))){
403         $prio_actions.= "<input class='center' type='image' src='images/status_start.png' 
404           title='"._("Resume job")."' name='resume_".$key."'>&nbsp;";
405       }
407       /* If PAUSED or WAITING add execution action
408        */  
409       if(in_array($task['STATUS'],array("paused","waiting"))){
410         $prio_actions.= "<input class='center' type='image' src='images/rocket.png' 
411           title='"._("Force execution now!")."' name='execute_process_".$key."'>&nbsp;";
412       }
414       /* Add logview button, currently ever.
415        */  
416       if(TRUE){
417         $action .= "<input type='image' src='images/fai_hook.png' name='log_view_".$key."' 
418           class='center' alt='"._("Log view")."'>&nbsp;";
419       }
421       /* If PAUSED or WAITING add edit action
422        */  
423       if(in_array($task['STATUS'],array("waiting"))){
424         $action.= "<input type='image' src='images/lists/edit.png' name='edit_task_".$key."' 
425           class='center' alt='"._("Edit")."'>";
426       }
428       /* If PROCESSING add abort action
429        */  
430       if(in_array($task['STATUS'],array("processing")) && preg_match("/install/",$task['HEADERTAG'])){
431         $action.= "<img src='images/empty.png'>";
432         $action.= "<input class='center' type='image' src='images/small_error.png' 
433           title='"._("Abort execution")."' name='abort_process_".$key."'>";
434       }
436       /* If WAITING or ERROR add remove action
437        */  
438       if( $this->acl_is_removeable() && in_array($task['STATUS'],array("waiting","error","processed"))){
439         $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."' 
440           class='center' alt='"._("Remove")."'>";
441       }
442       if(in_array($task['STATUS'],array("processing")) && !preg_match("/install/",$task['HEADERTAG'])){
443         $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."' 
444           class='center' alt='"._("Remove")."'>";
445       }
447       /* Create entry display name and tooltip */
448       $color = "";
449       $display = $task['MACADDRESS'];
450       $tooltip = "";
451       if(isset($task['PLAINNAME']) && !preg_match("/none/i",$task['PLAINNAME'])){
452         $display = $task['PLAINNAME'];
453         $tooltip = " title='".$task['MACADDRESS']."' ";
454       }
455       $display2= $task['HEADERTAG'];
456      
457       /* Check if this event exists as Daemon class 
458        * In this case, display a more accurate entry.
459        */ 
460       if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
461         $evt_name   = $this->events['QUEUED'][$task['HEADERTAG']];
462         $event_type = $this->events['BY_CLASS'][$evt_name];
463         $display2   = $event_type['s_Menu_Name'];
465         if(strlen($display2) > 20){
466           $display2 = substr($display2,0,18)."...";
467         }
469         if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
470           $display2 = $event_type['ListImage']."&nbsp;".$display2;
471         }
472       } 
474       $status = $task['STATUS'];
475   
476       if($status == "waiting"){
477         $status = "<img class='center' src='images/clock.png' alt=''>&nbsp;"._("Waiting");
478       }
479       if($status == "error"){
480         $status = "<img class='center' src='images/false.png' alt=''>&nbsp;"._("Error");
481       }
482       if($status == "processed"){
483         $status = "<img class='center' src='images/true.png' alt=''>&nbsp;"._("Processed");
484       }
486       /* Special handling for all entries that have 
487           STATUS == "processing" && PROGRESS == NUMERIC
488        */
489       if($status == "processing" && isset($task['PROGRESS'])){
490         $percent = $task['PROGRESS'];
492         /* Show activation? */
493         if ($percent == "goto-activation"){
494           $status = "<img class='center' src='images/lists/off.png' alt=''>&nbsp;"._("Locked");
496         /* Show hardware detect? */
497         } elseif ($percent == "goto-hardware-detection") {
498           $status = "<img class='center' src='images/hardware.png' alt=''>&nbsp;"._("Detection");
500         /* Real percent */
501         } else {
502          if (preg_match('/install/', $task['HEADERTAG'])){
503             $status = "<img src='progress.php?x=80&amp;y=13&amp;p=".$percent."' alt='".$percent."&nbsp;%'>";
504           } else {
505             $status = preg_replace('/ /', '&nbsp;', _("in progress"));
506           }
507         }
508       }
510       /* Create each field */
511       $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$key."'>" ,
512                       "attach" => "style='width:20px;".$color."'");
513       $field1 = array("string" => $display,
514                       "attach" => $tooltip."style='".$color."'");
515       $field1a= array("string" => $display2,
516                       "attach" => "style='".$color.";width:120px;'");
517       $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:140px;'");
518       $field3 = array("string" => $status,"attach" => "style='".$color.";width:80px;'");
519       $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:140px;border-right:0px;'");
520       $divlist->AddElement(array($field0,$field1,$field1a,$field2,$field3,$field4));
521     }
523     $smarty = get_smarty();
524     $smarty->assign("events",$this->events);
525     $smarty->assign("start",$this->start);
526     $smarty->assign("start_real", ($this->start + 1));
527     $smarty->assign("ranges", array("10" => "10",
528                                     "20" => "20",
529                                     "25" => "25",
530                                     "50" => "50",
531                                     "100"=> "100",
532                                     "200"=> "200",
533                                     "9999" => "*"));
535     $count = $this->o_queue->number_of_queued_entries($this->event_tags);
536     if(!$count) $count = $this->range;
537     $divlist->SetListFooter(range_selector($count, $this->start, $this->range,"range"));
538     $smarty->assign("range",$this->range);
539     $smarty->assign("div",$divlist->Draw());
540     return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
541   }
544   /*! \brief  Move an entry up or down in the queue, by updating its execution timestamp  
545       @param  $id     Integer  The ID of the entry which should be updated.
546       @param  $type   String   "up" / "down"
547       @return boolean TRUE in case of success else FALSE
548   */
549   public function update_priority($id,$type = "up")
550   {
551     if($type == "up"){
552       $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp DESC");
553     }else{
554       $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp ASC");
555     }
556     $last = array();
557     foreach($tmp as $entry){
558       if($entry['ID'] == $id){
559         if(count($last)){
560           $time = strtotime($last['TIMESTAMP']);
561           if($type == "up"){
562             $time ++;
563           }else{
564             $time --;
565           }
566           $time_str = date("YmdHis",$time); 
567           return($this->o_queue->update_entries(array($id),array("timestamp" => $time_str)));
568         }else{
569           return(FALSE);
570         }
571       }
572       $last = $entry;
573     }
574     return(FALSE);
575   }
578   /*! \brief  Resumes to status 'waiting'.
579    *  @return Boolean TRUE in case of success, else FALSE. 
580    */
581   private function resume_queue_entries($ids)
582   {
583     if(!count($ids)){
584       return;
585     }
587     /* Entries are resumed by setting the status to 
588      *  'waiting'
589      */
590     $data = array("status"    => "waiting");
591   
592     /* Check if given ids are valid and check if the status
593      *  allows resuming.
594      */
595     $update_ids = array();
596     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
597       if(isset($entry['STATUS']) && preg_match("/paused/",$entry['STATUS'])){
598         $update_ids[] = $entry['ID'];
599       }
600     }
602     /* Tell the daemon that we have entries to update.
603      */
604     if(count($update_ids)){
605       if(!$this->o_queue->update_entries($update_ids,$data)){
606         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
607         return(FALSE);
608       }
609     }
610     return(TRUE);
611   }
614   /*! \brief  Force queue job to be done as far as possible.
615    *  @return Boolean TRUE in case of success, else FALSE.
616    */
617   private function execute_queue_entries($ids)
618   {
619     if(!count($ids)){
620       return;
621     }
623     /* Execution is forced by updating the status to 
624      *  waiting and setting the timestamp to current time.
625      */
626     $data = array(  "timestamp" => date("YmdHis",time()), 
627                     "status"    => "waiting");
629     /* Only allow execution of paused or waiting entries 
630      */
631     $update_ids = array();
632     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
633       if(in_array($entry['STATUS'],array("paused","waiting"))){
634         $update_ids[] = $entry['ID'];
635       }
636     }
638     /* Tell the daemon that we want to update some entries
639      */
640     if(count($update_ids)){
641       if(!$this->o_queue->update_entries($update_ids,$data)){
642         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entries.")) , ERROR_DIALOG);
643         return(FALSE);
644       }
645     }
646     return(TRUE);
647   }
650   /*! \brief  Force queue job to be done as far as possible.
651    *  @return Boolean TRUE in case of success, else FALSE.
652    */
653   private function abort_queue_entries($ids)
654   {
655     if(!count($ids)){
656       return;
657     }
659     /* Entries are paused by setting the status to
660      *  something different from 'waiting'.
661      * We simply use 'paused'.
662      */
663     $data = array("status"    => "paused");
665     /* Detect if the ids we got are valid and
666      *  check if the status allows pausing.
667      */
668     $update_ids = array();
669     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
670       if(isset($entry['STATUS']) && preg_match("/processing/",$entry['STATUS'])){
671         if(isset($entry['MACADDRESS'])){
672           $update_ids[] = $entry['MACADDRESS'];
673         }else{
674           trigger_error("No mac address found in event.");
675         }
676       }
677     }
679     if(class_available("DaemonEvent_faireboot")){
680       $tmp = new DaemonEvent_faireboot($this->config);
681       $tmp->add_targets($update_ids);
682       $tmp->set_type(TRIGGERED_EVENT);
683       if(!$this->o_queue->append($tmp)){
684         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
685         return(FALSE);
686       }
687     }else{
688       msg_dialog::display(_("Error"),
689           sprintf(_("The job could not be aborted, the required class '%s' was not found."),
690             "DaemonEvent_faireboot") , ERROR_DIALOG);
691     }
692   }
695   /*! \brief Pauses the specified queue entry from execution.
696    *  @return Boolean TRUE in case of success, else FALSE. 
697    */
698   private function pause_queue_entries($ids)
699   {
700     if(!count($ids)){
701       return;
702     }
704     /* Entries are paused by setting the status to 
705      *  something different from 'waiting'.
706      * We simply use 'paused'.
707      */   
708     $data = array("status"    => "paused");
710     /* Detect if the ids we got are valid and
711      *  check if the status allows pausing.
712      */ 
713     $update_ids = array();
714     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
715       if(isset($entry['STATUS']) && preg_match("/waiting/",$entry['STATUS'])){
716         $update_ids[] = $entry['ID'];
717       }
718     }
720     /* Tell the daemon that we want to update some entries
721      */
722     if(count($update_ids)){
723       if(!$this->o_queue->update_entries($update_ids,$data)){
724         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
725         return(FALSE);
726       }
727     }
728     return(TRUE);
729   }
732   /*! \brief  Request list of queued jobs.
733    *  @return Returns an array of all queued jobs.
734    */
735   function reload()
736   {
738     /* Sort map   html-post-name => daemon-col-name
739      */
740     $map = array(
741         "QueuePosition" => "id",
742         "Action"        => "status",
743         "TaskID"        => "headertag",
744         "TargetName"    => "macaddress",
745         "Schedule"      => "timestamp");
747     /* Create sort header 
748      */
749     if(!isset($map[$this->sort_by])){
750       $sort = "id DESC";
751     }else{
752       $sort   = $map[$this->sort_by]; 
753       if($this->sort_dir == "up"){
754         $sort.= " ASC";
755       }else{
756         $sort.= " DESC";
757       }
758     }
759      
760     /* Get entries. */ 
761     $start  = $this->start; 
762     $stop   = $this->range;
763     $entries = $this->o_queue->get_queued_entries($this->event_tags,$start,$stop,$sort);
764     if ($this->o_queue->is_error()){
765       msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
766     }
768     /* Assign entries by id.
769      */
770     $this->entries = array();
771     
772     foreach($entries as $entry){
773       $this->entries[$entry['ID']]= $entry;
774     }
775   }
778   /*! \brief  Handle post jobs, like sorting.
779    */
780   function save_object()
781   {
782     /* Check for sorting changes 
783      */
784     $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
785     if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
786       $sort = $_GET['sort'];
787       if($this->sort_by == $sort){
788         if($this->sort_dir == "up"){
789           $this->sort_dir = "down";
790         }else{
791           $this->sort_dir = "up";
792         }
793       }
794       $this->sort_by = $sort;
795     }
797     /* Range selection used? */
798     if(isset($_POST['range']) && is_numeric($_POST['range'])){
799       $this->range = $_POST['range'];
800     }
801   
802     /* Save filter settings */ 
803     $gotomasses_filter = session::get("gotomasses_filter");
804     foreach(array("range","sort_by","sort_dir") as $attr){
805       $gotomasses_filter[$attr] = $this->$attr;
806     }
807     session::set("gotomasses_filter",$gotomasses_filter);
808  
809     /* Page changed. */
810     if(isset($_GET['start'])){
811       $start = $_GET['start'];
812       if(is_numeric($start) || $start == 0){
813         $this->start = $start;
814       }
815     }
817     /* Check start stop and reset if necessary */
818     $count = $this->o_queue->number_of_queued_entries($this->event_tags);
819     if($this->start >= $count){
820       $this->start = $count -1;
821     }
822     if($this->start < 0){
823       $this->start = 0;
824     }
825   }
828   function save()
829   {
830     // We do not save anything here.
831   }
834   /*! \brief  Return a list of all selected items.
835     @return Array   Returns an array containing all selected item ids.
836    */
837   function list_get_selected_items()
838   {
839     $ids = array();
840     foreach($_POST as $name => $value){
841       if(preg_match("/^item_selected_[0-9]*$/",$name)){
842         $id   = preg_replace("/^item_selected_/","",$name);
843         $ids[$id] = $id;
844       }
845     }
846     return($ids);
847   }
850   static function plInfo()
851   {
852     return (array(
853           "plShortName"   => _("System mass deployment"),
854           "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
855           "plSelfModify"  => FALSE,
856           "plDepends"     => array(),
857           "plPriority"    => 0,
858           "plSection"     => array("addon"),
859           "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
860           "plProvidedAcls" => array("Comment"   => _("Description")) 
861           ));
862   }
864 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
865 ?>