Code

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