Code

Updates done during the linuxtag
[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 = "";
243           foreach($ret as $task){
245             /* Only remove WAITING or ERROR entries */
246             if(in_array($task['STATUS'],array("waiting","error","processed")) || 
247                ($task['STATUS'] == "processing" && !preg_match("/install/",$task['HEADERTAG'])) ){
248               $this->ids_to_remove[] = $task['ID'];
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                 $tmp.= "\n".$task['ID']." - ".$evt['s_Menu_Name']."&nbsp;".$task['MACADDRESS'];
253               }else{
254                 $tmp.= "\n".$task['ID']." - ".$task['HEADERTAG']."&nbsp;".$task['MACADDRESS'];
255               }
256             }
257           }
258           $smarty->assign("multiple", TRUE); 
259           $smarty->assign("info",msgPool::deleteInfo("<pre>".$tmp."</pre>"));
260           $this->current = $s_entry;
261           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
262         }
263       }
264     }
266     /* Remove specified tasks */
267     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
269       /* Reboot hosts with not yet startet installations and timestamps in the past 
270        */
271       timezone::get_default_timezone();
272       foreach($this->ids_to_remove as $id){
273         $entry = $this->o_queue->get_entries_by_id(array($id));
274         if(isset($entry['ANSWER1'])){
275           $entry = $entry['ANSWER1'];
276           if( $entry['STATUS'] == "waiting" && 
277               $entry['HEADERTAG'] == "trigger_action_reinstall"){
278             $evt = new DaemonEvent_reinstall($this->config,$entry);
279             if($evt->get_timestamp(FALSE)  < time()){
280               $r_evt = new DaemonEvent_localboot($this->config);
281               $r_evt->add_targets(array($entry['MACADDRESS']));
282               $r_evt->set_type(TRIGGERED_EVENT);
283               $this->o_queue->append($r_evt);
284             }
285           }
286         }
287       }
289       $this->o_queue->remove_entries($this->ids_to_remove);
290       $this->save();
291     }
293     /* Remove aborted */
294     if(isset($_POST['delete_cancel'])){
295       $this->ids_to_remove = array();;
296     }
299     /************
300      * EDIT 
301      ************/
303     /* Close dialog */
304     if(isset($_POST['save_event_dialog'])){
305       if(is_object($this->dialog)){
306         $this->dialog->save_object();
307         if(!$this->o_queue->append($this->dialog)){
308           msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->o_queue->get_error()),ERROR_DIALOG);
309         }else{
310           $this->dialog = FALSE; 
311           $this->current = -1;
312         } 
313       }
314     }
317     /* Close dialog */
318     if(isset($_POST['abort_event_dialog'])){
319       $this->dialog = FALSE;
320       $this->current = -1;
321     }
323     /* Display dialogs if currently opened */
324     if(is_object($this->dialog)){
325       $this->dialog->save_object();
326       return($this->dialog->execute());
327     }
329     /************
330      * Handle Divlist 
331      ************/
333     $divlist = new MultiSelectWindow($this->config,"gotoMasses",array("gotomasses"));
334     $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa tasks."));
335     $divlist->SetSummary(_("List of queued jobs"));
336     $divlist->EnableCloseButton(FALSE);
337     $divlist->EnableSaveButton(FALSE);
338     $divlist->SetHeadpageMode();
339     $s = ".|"._("Actions")."|\n";
340     $s.= "..|<img src='images/lists/new.png' alt='' border='0' class='center'>&nbsp;"._("Create")."\n";
342     foreach($this->events['SCHEDULED'] as $name =>  $event){
343       $s.= "...|".$event['MenuImage']."&nbsp;".$event['s_Menu_Name']."|add_event_".$name."\n";
344     }
345     if($this->acl_is_removeable()){
346       $s.= "..|---|\n";
347       $s.= "..|<img src='images/lists/import.png' alt='' border='0' class='center'>&nbsp;"._("Import")."|import_file\n";
348       $s.= "..|<img src='images/lists/trash.png' alt='' border='0' class='center'>&nbsp;"._("Remove")."|remove_multiple\n";
349     }
350     if(preg_match("/w/",$this->getacl(""))){
351       $s.= "..|---|\n";
352       $s.= "..|<img alt='"._("Resume")."' src='images/status_start.png' border='0' class='center'>&nbsp;"._("Resume")."|resume_all\n";
353       $s.= "..|<img alt='"._("Pause")."' src='images/status_pause.png' border='0' class='center'>&nbsp;"._("Pause")."|pause_all\n";
354       $s.= "..|<img alt='"._("Abort")."' src='images/small_error.png'  border='0' class='center'>&nbsp;"._("Abort")."|abort_process_all\n";
355       $s.= "..|<img alt='"._("Execute")."' src='images/rocket.png'       border='0' class='center'>&nbsp;"._("Execute")."|execute_process_all\n";
356     }
358     $divlist->SetDropDownHeaderMenu($s);
360     if($this->sort_dir == "up"){
361       $sort_img = "<img src='images/lists/sort-up.png' alt='/\' border=0>";
362     }else{
363       $sort_img = "<img src='images/lists/sort-down.png' alt='\/' border=0>";
364     }
366     if($this->sort_by == "TargetName"){ $sort_img_1 = $sort_img; } else { $sort_img_1 = "" ;}
367     if($this->sort_by == "TaskID"){ $sort_img_2 = $sort_img; } else { $sort_img_2 = "" ;}
368     if($this->sort_by == "Schedule"){ $sort_img_3 = $sort_img; } else { $sort_img_3 = "" ;}
369     if($this->sort_by == "Action"){ $sort_img_4 = $sort_img; } else { $sort_img_4 = "" ;}
371     /* Create divlist */
372     $divlist->SetListHeader("<input type='image' src='images/lists/reload.png' title='"._("Reload")."'>");
374     $plug  = $_GET['plug'];
375     $chk = "<input type='checkbox' id='select_all' name='select_all'
376                onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
378     /* set Page header */
379     $divlist->AddHeader(array("string"=> $chk,          "attach"=>"style='width:20px;'"));
380     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TargetName'>"._("Target").$sort_img_1."</a>"));
381     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TaskID'>"._("Task").$sort_img_2."</a>",
382                                       "attach"=>"style='width:120px;'"));
383     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
384                                       "attach"=>"style='width:140px;'"));
385     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Action'>"._("Status").$sort_img_4."</a>",
386                                       "attach"=>"style='width:80px;'"));
387     $divlist->AddHeader(array("string"=>_("Action"),
388                                       "attach"=>"style='border-right:0px;width:140px;'"));
391     /* Reload the list of entries */
392     $this->reload();
394     foreach($this->entries as $key => $task){
396       $prio_actions="";
397       $action = "";
400       /* If WAITING add priority action
401        */  
402       if(in_array($task['STATUS'],array("waiting"))){
403         $prio_actions.= "<input class='center' type='image' src='images/prio_increase.png' 
404           title='"._("Move up")."' name='prio_up_".$key."'>&nbsp;";
405         $prio_actions.= "<input class='center' type='image' src='images/prio_decrease.png' 
406           title='"._("Move down")."' name='prio_down_".$key."'>&nbsp;";
407       }
408     
409       /* If WAITING add pause action
410        */  
411       if(in_array($task['STATUS'],array("waiting"))){
412         $prio_actions.= "<input class='center' type='image' src='images/status_pause.png' 
413           title='"._("Pause job")."' name='pause_".$key."'>&nbsp;";
414       }
416       /* If PAUSED add resume action
417        */  
418       if(in_array($task['STATUS'],array("paused"))){
419         $prio_actions.= "<input class='center' type='image' src='images/status_start.png' 
420           title='"._("Resume job")."' name='resume_".$key."'>&nbsp;";
421       }
423       /* If PAUSED or WAITING add execution action
424        */  
425       if(in_array($task['STATUS'],array("paused","waiting"))){
426         $prio_actions.= "<input class='center' type='image' src='images/rocket.png' 
427           title='"._("Execute now")."' name='execute_process_".$key."'>&nbsp;";
428       }
430       /* Add logview button, currently ever.
431        */  
432       if(TRUE){
433         $action .= "<input type='image' src='images/fai_hook.png' name='log_view_".$key."' 
434           class='center' title='"._("View logs")."' alt='"._("View logs")."'>&nbsp;";
435       }
437       /* If PAUSED or WAITING add edit action
438        */  
439       if(in_array($task['STATUS'],array("waiting"))){
440         $action.= "<input type='image' src='images/lists/edit.png' name='edit_task_".$key."' 
441           class='center' title='"._("Edit")."' alt='"._("Edit")."'>";
442       }
444       /* If PROCESSING add abort action
445        */  
446       if(in_array($task['STATUS'],array("processing")) && preg_match("/install/",$task['HEADERTAG'])){
447         $action.= "<img src='images/empty.png' alt=''>";
448         $action.= "<input class='center' type='image' src='images/small_error.png' 
449           title='"._("Abort job")."' name='abort_process_".$key."'>";
450       }
452       /* If WAITING or ERROR add remove action
453        */  
454       if( $this->acl_is_removeable() && in_array($task['STATUS'],array("waiting","error","processed"))){
455         $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."' 
456           class='center' title='"._("Remove")."' alt='"._("Remove")."'>";
457       }
458       if(in_array($task['STATUS'],array("processing")) && !preg_match("/install/",$task['HEADERTAG'])){
459         $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."' 
460           class='center' title='"._("Remove")."' alt='"._("Remove")."'>";
461       }
463       /* Create entry display name and tooltip */
464       $color = "";
465       $display = $task['MACADDRESS'];
466       $tooltip = "";
467       if(isset($task['PLAINNAME']) && !preg_match("/none/i",$task['PLAINNAME'])){
468         $display = $task['PLAINNAME'];
469         $tooltip = " title='".$task['MACADDRESS']."' ";
470       }
471       $display2= $task['HEADERTAG'];
472      
473       /* Check if this event exists as Daemon class 
474        * In this case, display a more accurate entry.
475        */ 
476       if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
477         $evt_name   = $this->events['QUEUED'][$task['HEADERTAG']];
478         $event_type = $this->events['BY_CLASS'][$evt_name];
479         $display2   = $event_type['s_Menu_Name'];
481         if(strlen($display2) > 20){
482           $display2 = substr($display2,0,18)."...";
483         }
485         if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
486           $display2 = $event_type['ListImage']."&nbsp;".$display2;
487         }
488       } 
490       $status = $task['STATUS'];
491   
492       if($status == "waiting"){
493         $status = "<img class='center' src='images/clock.png' alt=''>&nbsp;"._("Waiting");
494       }
495       if($status == "error"){
496         $status = "<img class='center' src='images/false.png' alt=''>&nbsp;"._("Error");
497       }
498       if($status == "processed"){
499         $status = "<img class='center' src='images/true.png' alt=''>&nbsp;"._("Processed");
500       }
502       /* Special handling for all entries that have 
503           STATUS == "processing" && PROGRESS == NUMERIC
504        */
505       if($status == "processing" && isset($task['PROGRESS'])){
506         $percent = $task['PROGRESS'];
508         /* Show activation? */
509         if ($percent == "goto-activation"){
510           $status = "<img class='center' src='images/lists/off.png' alt=''>&nbsp;"._("Locked");
512         /* Show hardware detect? */
513         } elseif ($percent == "goto-hardware-detection") {
514           $status = "<img class='center' src='images/hardware.png' alt=''>&nbsp;"._("Detection");
516         /* Real percent */
517         } else {
518          if (preg_match('/install/', $task['HEADERTAG'])){
519             $status = "<img src='progress.php?x=80&y=13&p=".$task['PROGRESS']."' alt=''
520                           id='progress_".preg_replace("/:/","_",$task['MACADDRESS'])."'>";
521           } else {
522             $status = preg_replace('/ /', '&nbsp;', _("in progress"));
523           }
524         }
525       }
527       /* Create each field */
528       $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$key."'>" ,
529                       "attach" => "style='width:20px;".$color."'");
530       $field1 = array("string" => $display,
531                       "attach" => $tooltip."style='".$color."'");
532       $field1a= array("string" => $display2,
533                       "attach" => "style='".$color.";width:120px;'");
534       $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:140px;'");
535       $field3 = array("string" => $status,"attach" => "style='".$color.";width:80px;'");
536       $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:140px;border-right:0px;'");
537       $divlist->AddElement(array($field0,$field1,$field1a,$field2,$field3,$field4));
538     }
540     $smarty = get_smarty();
541     $smarty->assign("events",$this->events);
542     $smarty->assign("start",$this->start);
543     $smarty->assign("start_real", ($this->start + 1));
544     $smarty->assign("ranges", array("10" => "10",
545                                     "20" => "20",
546                                     "25" => "25",
547                                     "50" => "50",
548                                     "100"=> "100",
549                                     "200"=> "200",
550                                     "9999" => "*"));
552     $count = $this->o_queue->number_of_queued_entries($this->event_tags);
553     if(!$count) $count = $this->range;
554     $divlist->SetListFooter(range_selector($count, $this->start, $this->range,"range"));
555     $smarty->assign("range",$this->range);
556     $smarty->assign("div",$divlist->Draw());
557     return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
558   }
561   /*! \brief  Move an entry up or down in the queue, by updating its execution timestamp  
562       @param  $id     Integer  The ID of the entry which should be updated.
563       @param  $type   String   "up" / "down"
564       @return boolean TRUE in case of success else FALSE
565   */
566   public function update_priority($id,$type = "up")
567   {
568     if($type == "up"){
569       $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp DESC");
570     }else{
571       $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp ASC");
572     }
573     $last = array();
574     foreach($tmp as $entry){
575       if($entry['ID'] == $id){
576         if(count($last)){
577           $time = strtotime($last['TIMESTAMP']);
578           if($type == "up"){
579             $time ++;
580           }else{
581             $time --;
582           }
583           $time_str = date("YmdHis",$time); 
584           return($this->o_queue->update_entries(array($id),array("timestamp" => $time_str)));
585         }else{
586           return(FALSE);
587         }
588       }
589       $last = $entry;
590     }
591     return(FALSE);
592   }
595   /*! \brief  Resumes to status 'waiting'.
596    *  @return Boolean TRUE in case of success, else FALSE. 
597    */
598   private function resume_queue_entries($ids)
599   {
600     if(!count($ids)){
601       return;
602     }
604     /* Entries are resumed by setting the status to 
605      *  'waiting'
606      */
607     $data = array("status"    => "waiting");
608   
609     /* Check if given ids are valid and check if the status
610      *  allows resuming.
611      */
612     $update_ids = array();
613     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
614       if(isset($entry['STATUS']) && preg_match("/paused/",$entry['STATUS'])){
615         $update_ids[] = $entry['ID'];
616       }
617     }
619     /* Tell the daemon that we have entries to update.
620      */
621     if(count($update_ids)){
622       if(!$this->o_queue->update_entries($update_ids,$data)){
623         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
624         return(FALSE);
625       }
626     }
627     return(TRUE);
628   }
631   /*! \brief  Force queue job to be done as far as possible.
632    *  @return Boolean TRUE in case of success, else FALSE.
633    */
634   private function execute_queue_entries($ids)
635   {
636     if(!count($ids)){
637       return;
638     }
640     /* Execution is forced by updating the status to 
641      *  waiting and setting the timestamp to current time.
642      */
643     $data = array(  "timestamp" => date("YmdHis",time()), 
644                     "status"    => "waiting");
646     /* Only allow execution of paused or waiting entries 
647      */
648     $update_ids = array();
649     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
650       if(in_array($entry['STATUS'],array("paused","waiting"))){
651         $update_ids[] = $entry['ID'];
652       }
653     }
655     /* Tell the daemon that we want to update some entries
656      */
657     if(count($update_ids)){
658       if(!$this->o_queue->update_entries($update_ids,$data)){
659         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entries.")) , ERROR_DIALOG);
660         return(FALSE);
661       }
662     }
663     return(TRUE);
664   }
667   /*! \brief  Force queue job to be done as far as possible.
668    *  @return Boolean TRUE in case of success, else FALSE.
669    */
670   private function abort_queue_entries($ids)
671   {
672     if(!count($ids)){
673       return;
674     }
676     /* Entries are paused by setting the status to
677      *  something different from 'waiting'.
678      * We simply use 'paused'.
679      */
680     $data = array("status"    => "paused");
682     /* Detect if the ids we got are valid and
683      *  check if the status allows pausing.
684      */
685     $update_ids = array();
686     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
687       if(isset($entry['STATUS']) && preg_match("/processing/",$entry['STATUS'])){
688         if(isset($entry['MACADDRESS'])){
689           $update_ids[] = $entry['MACADDRESS'];
690         }else{
691           trigger_error("No mac address found in event.");
692         }
693       }
694     }
696     if(class_available("DaemonEvent_faireboot")){
697       $tmp = new DaemonEvent_faireboot($this->config);
698       $tmp->add_targets($update_ids);
699       $tmp->set_type(TRIGGERED_EVENT);
700       $this->recently_removed = $update_ids;
701       
702       if(!$this->o_queue->append($tmp)){
703         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
704         return(FALSE);
705       }
706     }else{
707       msg_dialog::display(_("Error"),
708           sprintf(_("The job could not be aborted, the required class '%s' was not found."),
709             "DaemonEvent_faireboot") , ERROR_DIALOG);
710     }
711   }
714   /*! \brief Pauses the specified queue entry from execution.
715    *  @return Boolean TRUE in case of success, else FALSE. 
716    */
717   private function pause_queue_entries($ids)
718   {
719     if(!count($ids)){
720       return;
721     }
723     /* Entries are paused by setting the status to 
724      *  something different from 'waiting'.
725      * We simply use 'paused'.
726      */   
727     $data = array("status"    => "paused");
729     /* Detect if the ids we got are valid and
730      *  check if the status allows pausing.
731      */ 
732     $update_ids = array();
733     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
734       if(isset($entry['STATUS']) && preg_match("/waiting/",$entry['STATUS'])){
735         $update_ids[] = $entry['ID'];
736       }
737     }
739     /* Tell the daemon that we want to update some entries
740      */
741     if(count($update_ids)){
742       if(!$this->o_queue->update_entries($update_ids,$data)){
743         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
744         return(FALSE);
745       }
746     }
747     return(TRUE);
748   }
751   /*! \brief  Request list of queued jobs.
752    *  @return Returns an array of all queued jobs.
753    */
754   function reload()
755   {
757     /* Sort map   html-post-name => daemon-col-name
758      */
759     $map = array(
760         "QueuePosition" => "id",
761         "Action"        => "status",
762         "TaskID"        => "headertag",
763         "TargetName"    => "macaddress",
764         "Schedule"      => "timestamp");
766     /* Create sort header 
767      */
768     if(!isset($map[$this->sort_by])){
769       $sort = "id DESC";
770     }else{
771       $sort   = $map[$this->sort_by]; 
772       if($this->sort_dir == "up"){
773         $sort.= " ASC";
774       }else{
775         $sort.= " DESC";
776       }
777     }
778      
779     /* Get entries. */ 
780     $start  = $this->start; 
781     $stop   = $this->range;
782     $entries = $this->o_queue->get_queued_entries($this->event_tags,$start,$stop,$sort);
783     if ($this->o_queue->is_error()){
784       msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
785     }
787     /* Assign entries by id.
788      */
789     $this->entries = array();
790     
791     foreach($entries as $entry){
792     
793       /* Skip entries which will be removed within the next seconds */
794       if(isset($entry['MACADDRESS']) && in_array($entry['MACADDRESS'],$this->recently_removed)){
795         continue;
796       }
797       $this->entries[$entry['ID']]= $entry;
798     }
799     $this->recently_removed = array();
800   }
803   /*! \brief  Handle post jobs, like sorting.
804    */
805   function save_object()
806   {
807     /* Check for sorting changes 
808      */
809     $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
810     if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
811       $sort = $_GET['sort'];
812       if($this->sort_by == $sort){
813         if($this->sort_dir == "up"){
814           $this->sort_dir = "down";
815         }else{
816           $this->sort_dir = "up";
817         }
818       }
819       $this->sort_by = $sort;
820     }
822     /* Range selection used? */
823     if(isset($_POST['range']) && is_numeric($_POST['range'])){
824       $this->range = $_POST['range'];
825     }
826   
827     /* Save filter settings */ 
828     $gotomasses_filter = session::get("gotomasses_filter");
829     foreach(array("range","sort_by","sort_dir") as $attr){
830       $gotomasses_filter[$attr] = $this->$attr;
831     }
832     session::set("gotomasses_filter",$gotomasses_filter);
833  
834     /* Page changed. */
835     if(isset($_GET['start'])){
836       $start = $_GET['start'];
837       if(is_numeric($start) || $start == 0){
838         $this->start = $start;
839       }
840     }
842     /* Check start stop and reset if necessary */
843     $count = $this->o_queue->number_of_queued_entries($this->event_tags);
844     if($this->start >= $count){
845       $this->start = $count -1;
846     }
847     if($this->start < 0){
848       $this->start = 0;
849     }
850   }
853   function save()
854   {
855     // We do not save anything here.
856   }
859   /*! \brief  Return a list of all selected items.
860     @return Array   Returns an array containing all selected item ids.
861    */
862   function list_get_selected_items()
863   {
864     $ids = array();
865     foreach($_POST as $name => $value){
866       if(preg_match("/^item_selected_[0-9]*$/",$name)){
867         $id   = preg_replace("/^item_selected_/","",$name);
868         $ids[$id] = $id;
869       }
870     }
871     return($ids);
872   }
875   static function plInfo()
876   {
877     return (array(
878           "plShortName"   => _("System mass deployment"),
879           "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
880           "plSelfModify"  => FALSE,
881           "plDepends"     => array(),
882           "plPriority"    => 0,
883           "plSection"     => array("addon"),
884           "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
885           "plProvidedAcls" => array("Comment"   => _("Description")) 
886           ));
887   }
889 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
890 ?>