Code

Bugfix for #4271
[gosa.git] / trunk / 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";
28   var $plIcon         = "plugins/goto/images/goto.png";
30   /* attribute list for save action */
31   var $attributes= array();
32   var $objectclasses= array();
34   /* Queue tasks */
35   var $current        = FALSE;
36   var $dialog         = FALSE;
37   var $ids_to_remove  = array();
38   var $divlist        = NULL;
40   var $events         = array();
41   var $event_tags     = array();
43   var $sort_by  = "Schedule";
44   var $sort_dir = "up";
45   var $entries  = array();
46   var $range    = 25;
47   var $start    = 0;
49   var $recently_removed = array();
51   function gotomasses(&$config, $dn= NULL)
52   {
53     /* Include config object */
54     $this->config= &$config;
55     $this->o_queue = new gosaSupportDaemon(TRUE,5);
56     $this->events  = DaemonEvent::get_event_types( SYSTEM_EVENT);
58     /* Get tags that will be used in queue searches */
59     $this->event_tags = array("none");
60     foreach($this->events['SCHEDULED'] as $evt){
61       $this->event_tags[] = $evt['s_Queued_Action'];
62     }
64     /* Load filter settings */
65     if(!session::is_set("gotomasses_filter")){
66       $gotomasses_filter = 
67         array(
68             "range" => $this->range,
69             "sort_by" => $this->sort_by,
70             "sort_dir" => $this->sort_dir);
71       session::set("gotomasses_filter",$gotomasses_filter);
72     }
73     $gotomasses_filter = session::get("gotomasses_filter");
74     foreach(array("range","sort_by","sort_dir") as $attr) {
75       $this->$attr = $gotomasses_filter[$attr];
76     }
77   }
80   function execute()
81   {
82     $smarty = get_smarty();
83  
84     /************
85      * Handle posts 
86      ************/
87     
88     $s_entry = $s_action = "";
89     $arr = array( 
91         "/^pause_/"           => "pause",
92         "/^resume_/"          => "resume",
93         "/^execute_process_/" => "execute_process",
94         "/^abort_process_/"   => "abort_process",
96         "/^prio_up_/"     => "prio_up",
97         "/^prio_down_/"   => "prio_down",
99         "/^edit_task_/"             =>  "edit",
100         "/^log_view_/"              =>  "logview",
101         "/^remove_task_/"           =>  "remove",
102         "/^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" && $this->acl_is_writeable("")){
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) && $this->acl_is_writeable("")){
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       if($this->acl_is_writeable("")){
160         switch($s_action){
161           case 'resume'         : $this->resume_queue_entries   (array($s_entry));break; 
162           case 'pause'          : $this->pause_queue_entries    (array($s_entry));break; 
163           case 'execute_process': $this->execute_queue_entries  (array($s_entry));break; 
164           case 'abort_process'  : $this->abort_queue_entries    (array($s_entry));break; 
165           case 'resume_all'         : $this->resume_queue_entries   ($this->list_get_selected_items());break; 
166           case 'pause_all'          : $this->pause_queue_entries    ($this->list_get_selected_items());break; 
167           case 'execute_process_all': $this->execute_queue_entries  ($this->list_get_selected_items());break; 
168           case 'abort_process_all'  : $this->abort_queue_entries    ($this->list_get_selected_items());break; 
170           default : trigger_error("Undefined action setting used (".$s_action.").");
171         }
172       }
173       if($this->o_queue->is_error()){
174         msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
175       }
176     }
178     /************
179      * ADD 
180      ************/
181   
182     if(preg_match("/^add_event_/",$s_action) && $this->acl_is_writeable("")){
183       $type = preg_replace("/^add_event_/","",$s_action);
184       if(isset($this->events['BY_CLASS'][$type])){
185         $e_data = $this->events['BY_CLASS'][$type];
186         $this->dialog = new $e_data['CLASS_NAME']($this->config);
187       }
188     }
190     /************
191      * EDIT
192      ************/
194     if($s_action == "edit" && $this->acl_is_writeable("")){  
195       $id =  $s_entry;
196       $type = FALSE;
197       if(isset($this->entries[$id])){
198         $event = $this->entries[$s_entry];
199         if($event['STATUS'] == "waiting" && isset($this->events['QUEUED'][$event['HEADERTAG']])){
200           $evt_name = $this->events['QUEUED'][$event['HEADERTAG']];
201           $type = $this->events['BY_CLASS'][$evt_name];
202           $this->dialog = new $type['CLASS_NAME']($this->config,$event);
203         }
204       }
205     }
207     
208     /************
209      * LOG VIEW
210      ************/
212     if($s_action == "logview"  && $this->acl_is_readable("")){  
213       $id =  $s_entry;
214       $type = FALSE;
215       if(isset($this->entries[$id])){
216         $event = $this->entries[$s_entry];
217         $this->dialog = new gotoLogView($this->config,"",$event,$this);
218       }
219     }
222     /************
223      * REMOVE 
224      ************/
226     /* Remove multiple */
227     if($s_action == "remove_multiple" || $s_action == "remove"){
229       if(!$this->acl_is_removeable()){
230         msg_dialog::display(_("Permission"), msgPool::permDelete(), ERROR_DIALOG);
231       }else{
233         if($s_action == "remove"){
234           $ids = array($s_entry);
235         }else{
236           $ids = $this->list_get_selected_items();
237         }
239         $this->ids_to_remove = array();
241         if(count($ids)){
242           $ret = $this->o_queue->ids_exist($ids);
243           $ret = $this->o_queue->get_entries_by_id($ret);
244           $tmp = "";
246           $deleteable_jobs = array();      
247           $not_deleteable_jobs = array();      
248           foreach($ret as $task){
250             /* Create a printable job name/description */
251             if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
252               $evt_name = $this->events['QUEUED'][$task['HEADERTAG']];
253               $evt = $this->events['BY_CLASS'][$evt_name];
254               $j_name = $task['ID']." - ".$evt['s_Menu_Name']."&nbsp;".$task['MACADDRESS'];
255             }else{
256               $j_name = $task['ID']." - ".$task['HEADERTAG']."&nbsp;".$task['MACADDRESS'];
257             }
259             /* If system name is available show it in parantheses next to each entry */
260             if(isset($task['PLAINNAME']) && $task['PLAINNAME'] != 'none') {
261               $j_name .= sprintf(" (%s) ", $task['PLAINNAME']);
262             }
264             /* Only remove WAITING or ERROR entries */
265             if(in_array($task['STATUS'],array("waiting","error","processed")) || 
266                 ($task['STATUS'] == "processing" && !preg_match("/install/",$task['HEADERTAG'])) ){
267               $this->ids_to_remove[] = $task['ID'];
268               $deleteable_jobs[] = $j_name;
269             }else{
270               $not_deleteable_jobs[] = $j_name;
271             }
272           }
273           if(count($not_deleteable_jobs)){
274             msg_dialog::display(_("Remove"),
275                 sprintf(_("The following jobs couldn't be deleted, they have to be aborted: %s"),
276                   "<br>".msgPool::buildList($not_deleteable_jobs)),INFO_DIALOG);
277           }
279           if(count($this->ids_to_remove)){
280             $smarty->assign("multiple", TRUE); 
281             $smarty->assign("info",msgPool::deleteInfo($deleteable_jobs));
282             $this->current = $s_entry;
283             return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
284           }
285         }
286       }
287     }
289     /* Remove specified tasks */
290     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
292       /* Reboot hosts with not yet startet installations and timestamps in the past 
293        */
294       if($this->acl_is_removeable("")){
295         timezone::get_default_timezone();
296         foreach($this->ids_to_remove as $id){
297           $entry = $this->o_queue->get_entries_by_id(array($id));
298           if(isset($entry['ANSWER1'])){
299             $entry = $entry['ANSWER1'];
300             if( $entry['STATUS'] == "waiting" && 
301                 $entry['HEADERTAG'] == "trigger_action_reinstall"){
302               $evt = new DaemonEvent_reinstall($this->config,$entry);
303               if($evt->get_timestamp(FALSE)  < time()){
304                 $r_evt = new DaemonEvent_localboot($this->config);
305                 $r_evt->add_targets(array($entry['MACADDRESS']));
306                 $r_evt->set_type(TRIGGERED_EVENT);
307                 $this->o_queue->append($r_evt);
308               }
309             }
310           }
311         }
313         $this->o_queue->remove_entries($this->ids_to_remove);
314         $this->save();
315       }
316     }
318     /* Remove aborted */
319     if(isset($_POST['delete_cancel'])){
320       $this->ids_to_remove = array();;
321     }
324     /************
325      * EDIT 
326      ************/
328     /* Close dialog */
329     if(isset($_POST['save_event_dialog'])){
330       list($this->dialog, $this->current) = DaemonEvent::save_event_dialog($this->dialog, $this->current, $this->o_queue, $this->config);
331     }
334     /* Close dialog */
335     if(isset($_POST['abort_event_dialog'])){
336       $this->dialog = FALSE;
337       $this->current = -1;
338     }
340     /* Display dialogs if currently opened */
341     if(is_object($this->dialog)){
342       $this->dialog->save_object();
343       $display = $this->dialog->execute();
345       if($this->dialog instanceOf goto_import_file && $this->dialog->import_successful){
346         $this->dialog = FALSE;
347       }else{
348         return($display);
349       }
350     }
352     /************
353      * Handle Divlist 
354      ************/
356     $divlist = new MultiSelectWindow($this->config,"gotoMasses",array("gotomasses"));
357     $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa tasks."));
358     $divlist->SetSummary(_("List of queued jobs"));
359     $divlist->EnableCloseButton(FALSE);
360     $divlist->EnableSaveButton(FALSE);
361     $divlist->SetHeadpageMode();
362     $s = ".|"._("Actions")."|\n";
363     $s.= "..|<img src='images/lists/new.png' alt='' border='0' class='center'>&nbsp;"._("Create")."\n";
365     if($this->acl_is_writeable("")){
366       foreach($this->events['SCHEDULED'] as $name =>  $event){
367         $s.= "...|".$event['MenuImage']."&nbsp;".$event['s_Menu_Name']."|add_event_".$name."\n";
368       }
369     }
370     if($this->acl_is_removeable()){
371       $s.= "..|---|\n";
372       $s.= "..|<img src='images/lists/import.png' alt='' border='0' class='center'>&nbsp;"._("Import")."|import_file\n";
373       $s.= "..|<img src='images/lists/trash.png' alt='' border='0' class='center'>&nbsp;"._("Remove")."|remove_multiple\n";
374     }
375     if(preg_match("/w/",$this->getacl(""))){
376       $s.= "..|---|\n";
377       $s.= "..|<img alt='"._("Resume")."' src='images/status_start.png' border='0' class='center'>&nbsp;"._("Resume")."|resume_all\n";
378       $s.= "..|<img alt='"._("Pause")."' src='images/status_pause.png' border='0' class='center'>&nbsp;"._("Pause")."|pause_all\n";
379       $s.= "..|<img alt='"._("Abort")."' src='images/small_error.png'  border='0' class='center'>&nbsp;"._("Abort")."|abort_process_all\n";
380       $s.= "..|<img alt='"._("Execute")."' src='images/rocket.png'       border='0' class='center'>&nbsp;"._("Execute")."|execute_process_all\n";
381     }
383     $divlist->SetDropDownHeaderMenu($s);
385     if($this->sort_dir == "up"){
386       $sort_img = "<img src='images/lists/sort-up.png' alt='/\' border=0>";
387     }else{
388       $sort_img = "<img src='images/lists/sort-down.png' alt='\/' border=0>";
389     }
391     if($this->sort_by == "TargetName"){ $sort_img_1 = $sort_img; } else { $sort_img_1 = "" ;}
392     if($this->sort_by == "TaskID"){ $sort_img_2 = $sort_img; } else { $sort_img_2 = "" ;}
393     if($this->sort_by == "Schedule"){ $sort_img_3 = $sort_img; } else { $sort_img_3 = "" ;}
394     if($this->sort_by == "Action"){ $sort_img_4 = $sort_img; } else { $sort_img_4 = "" ;}
396     /* Create divlist */
397     $divlist->SetListHeader("<input type='image' src='images/lists/reload.png' title='"._("Reload")."'>");
399     $plug  = $_GET['plug'];
400     $chk = "<input type='checkbox' id='select_all' name='select_all'
401                onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
403     /* set Page header */
404     $divlist->AddHeader(array("string"=> $chk,          "attach"=>"style='width:20px;'"));
405     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TargetName'>"._("Target").$sort_img_1."</a>"));
406     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TaskID'>"._("Task").$sort_img_2."</a>",
407                                       "attach"=>"style='width:120px;'"));
408     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
409                                       "attach"=>"style='width:140px;'"));
410     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Action'>"._("Status").$sort_img_4."</a>",
411                                       "attach"=>"style='width:80px;'"));
412     $divlist->AddHeader(array("string"=>_("Action"),
413                                       "attach"=>"style='border-right:0px;width:140px;'"));
416     /* Reload the list of entries */
417     $this->reload();
419     foreach($this->entries as $key => $task){
421       $prio_actions="";
422       $action = "";
425       /* If WAITING add priority action
426        */  
427       if(in_array($task['STATUS'],array("waiting")) && $this->acl_is_writeable("")){
428         $prio_actions.= "<input class='center' type='image' src='plugins/goto/images/prio_increase.png' 
429           title='"._("Move up")."' name='prio_up_".$key."'>&nbsp;";
430         $prio_actions.= "<input class='center' type='image' src='plugins/goto/images/prio_decrease.png' 
431           title='"._("Move down")."' name='prio_down_".$key."'>&nbsp;";
432       }
433     
434       /* If WAITING add pause action
435        */  
436       if(in_array($task['STATUS'],array("waiting")) && $this->acl_is_writeable("")){
437         $prio_actions.= "<input class='center' type='image' src='images/status_pause.png' 
438           title='"._("Pause job")."' name='pause_".$key."'>&nbsp;";
439       }
441       /* If PAUSED add resume action
442        */  
443       if(in_array($task['STATUS'],array("paused")) && $this->acl_is_writeable("")){
444         $prio_actions.= "<input class='center' type='image' src='images/status_start.png' 
445           title='"._("Resume job")."' name='resume_".$key."'>&nbsp;";
446       }
448       /* If PAUSED or WAITING add execution action
449        */  
450       if(in_array($task['STATUS'],array("paused","waiting")) && $this->acl_is_writeable("")){
451         $prio_actions.= "<input class='center' type='image' src='images/rocket.png' 
452           title='"._("Execute now")."' name='execute_process_".$key."'>&nbsp;";
453       }
455       /* Add logview button, currently ever.
456        */  
457       if($this->acl_is_readable("")){
458         $action .= "<input type='image' src='plugins/goto/images/view_logs.png' name='log_view_".$key."' 
459           class='center' title='"._("View logs")."' alt='"._("View logs")."'>&nbsp;";
460       }
462       /* If PAUSED or WAITING add edit action
463        */  
464       if(in_array($task['STATUS'],array("waiting")) && $this->acl_is_writeable("")){
465         $action.= "<input type='image' src='images/lists/edit.png' name='edit_task_".$key."' 
466           class='center' title='"._("Edit")."' alt='"._("Edit")."'>";
467       }
469       /* If PROCESSING add abort action
470        */  
471       if(in_array($task['STATUS'],array("processing")) && preg_match("/install/",$task['HEADERTAG']) && $this->acl_is_writeable("")){
472         $action.= "<img src='images/empty.png' alt=''>";
473         $action.= "<input class='center' type='image' src='images/small_error.png' 
474           title='"._("Abort job")."' name='abort_process_".$key."'>";
475       }
477       /* If WAITING or ERROR add remove action
478        */  
479       if( $this->acl_is_removeable() && in_array($task['STATUS'],array("waiting","error","processed"))){
480         $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."' 
481           class='center' title='"._("Remove")."' alt='"._("Remove")."'>";
482       }
483       if($this->acl_is_writeable("") && in_array($task['STATUS'],array("processing")) && !preg_match("/install/",$task['HEADERTAG'])){
484         $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."' 
485           class='center' title='"._("Remove")."' alt='"._("Remove")."'>";
486       }
488       /* Create entry display name and tooltip */
489       $color = "";
490       $display = $task['MACADDRESS'];
491       $tooltip = "";
492       if(isset($task['PLAINNAME']) && !preg_match("/none/i",$task['PLAINNAME'])){
493         $display = $task['PLAINNAME'];
494         $tooltip = " title='".$task['MACADDRESS']."' ";
495       }
496       $display2= $task['HEADERTAG'];
497      
498       /* Check if this event exists as Daemon class 
499        * In this case, display a more accurate entry.
500        */ 
501       if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
502         $evt_name   = $this->events['QUEUED'][$task['HEADERTAG']];
503         $event_type = $this->events['BY_CLASS'][$evt_name];
504         $display2   = $event_type['s_Menu_Name'];
506         if(strlen($display2) > 20){
507           $display2 = substr($display2,0,18)."...";
508         }
510         if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
511           $display2 = $event_type['ListImage']."&nbsp;".$display2;
512         }
513       } 
515       $status = $task['STATUS'];
516   
517       if($status == "waiting"){
518         $status = "<img class='center' src='plugins/goto/images/clock.png' alt=''>&nbsp;"._("Waiting");
519       }
520       if($status == "error"){
521         $status = "<img class='center' src='images/false.png' alt=''>&nbsp;"._("Error");
522       }
523       if($status == "processed"){
524         $status = "<img class='center' src='images/true.png' alt=''>&nbsp;"._("Processed");
525       }
527       /* Special handling for all entries that have 
528           STATUS == "processing" && PROGRESS == NUMERIC
529        */
530       if($status == "processing" && isset($task['PROGRESS'])){
531         $percent = $task['PROGRESS'];
533         /* Show activation? */
534         if ($percent == "goto-activation"){
535           $status = "<img class='center' src='images/lists/off.png' alt=''>&nbsp;"._("Locked");
537         /* Show hardware detect? */
538         } elseif ($percent == "goto-hardware-detection") {
539           $status = "<img class='center' src='plugins/goto/images/hardware.png' alt=''>&nbsp;"._("Detection");
541         /* Real percent */
542         } else {
543          if (preg_match('/install/', $task['HEADERTAG'])){
544             $status = "<img src='progress.php?x=80&y=13&p=".$task['PROGRESS']."' alt=''
545                           id='progress_".preg_replace("/:/","_",$task['MACADDRESS'])."'>";
546           } else {
547             $status = preg_replace('/ /', '&nbsp;', _("in progress"));
548           }
549         }
550       }
552       /* Create each field */
553       $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$key."'>" ,
554                       "attach" => "style='width:20px;".$color."'");
555       $field1 = array("string" => $display,
556                       "attach" => $tooltip."style='".$color."'");
557       $field1a= array("string" => $display2,
558                       "attach" => "style='".$color.";width:120px;'");
559       if ($task['TIMESTAMP'] == "19700101000000"){
560               $field2 = array("string" => _("immediately"),"attach" => "style='".$color.";width:140px;'");
561       } else {
562               $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:140px;'");
563       }
564       $field3 = array("string" => $status,"attach" => "style='".$color.";width:80px;'");
565       $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:140px;border-right:0px;'");
566       $divlist->AddElement(array($field0,$field1,$field1a,$field2,$field3,$field4));
567     }
569     $smarty = get_smarty();
570     $smarty->assign("events",$this->events);
571     $smarty->assign("start",$this->start);
572     $smarty->assign("start_real", ($this->start + 1));
573     $smarty->assign("ranges", array("10" => "10",
574                                     "20" => "20",
575                                     "25" => "25",
576                                     "50" => "50",
577                                     "100"=> "100",
578                                     "200"=> "200",
579                                     "9999" => "*"));
581     $count = $this->o_queue->number_of_queued_entries($this->event_tags);
582     if(!$count) $count = $this->range;
583     $divlist->SetListFooter(range_selector($count, $this->start, $this->range,"range"));
584     $smarty->assign("range",$this->range);
585     $smarty->assign("div",$divlist->Draw());
586     return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
587   }
590   /*! \brief  Move an entry up or down in the queue, by updating its execution timestamp  
591       @param  $id     Integer  The ID of the entry which should be updated.
592       @param  $type   String   "up" / "down"
593       @return boolean TRUE in case of success else FALSE
594   */
595   public function update_priority($id,$type = "up")
596   {
597     if($type == "up"){
598       $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp DESC");
599     }else{
600       $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp ASC");
601     }
602     $last = array();
603     foreach($tmp as $entry){
604       if($entry['ID'] == $id){
605         if(count($last)){
606           $time = strtotime($last['TIMESTAMP']);
607           if($type == "up"){
608             $time ++;
609           }else{
610             $time --;
611           }
612           $time_str = date("YmdHis",$time); 
613           return($this->o_queue->update_entries(array($id),array("timestamp" => $time_str)));
614         }else{
615           return(FALSE);
616         }
617       }
618       $last = $entry;
619     }
620     return(FALSE);
621   }
624   /*! \brief  Resumes to status 'waiting'.
625    *  @return Boolean TRUE in case of success, else FALSE. 
626    */
627   private function resume_queue_entries($ids)
628   {
629     if(!count($ids)){
630       return;
631     }
633     /* Entries are resumed by setting the status to 
634      *  'waiting'
635      */
636     $data = array("status"    => "waiting");
637   
638     /* Check if given ids are valid and check if the status
639      *  allows resuming.
640      */
641     $update_ids = array();
642     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
643       if(isset($entry['STATUS']) && preg_match("/paused/",$entry['STATUS'])){
644         $update_ids[] = $entry['ID'];
645       }
646     }
648     /* Tell the daemon that we have entries to update.
649      */
650     if(count($update_ids)){
651       if(!$this->o_queue->update_entries($update_ids,$data)){
652         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
653         return(FALSE);
654       }
655     }
656     return(TRUE);
657   }
660   /*! \brief  Force queue job to be done as far as possible.
661    *  @return Boolean TRUE in case of success, else FALSE.
662    */
663   private function execute_queue_entries($ids)
664   {
665     if(!count($ids)){
666       return;
667     }
669     /* Execution is forced by updating the status to 
670      *  waiting and setting the timestamp to current time.
671      */
672     $data = array(  "timestamp" => date("YmdHis",time()), 
673                     "status"    => "waiting");
675     /* Only allow execution of paused or waiting entries 
676      */
677     $update_ids = array();
678     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
679       if(in_array($entry['STATUS'],array("paused","waiting"))){
680         $update_ids[] = $entry['ID'];
681       }
682     }
684     /* Tell the daemon that we want to update some entries
685      */
686     if(count($update_ids)){
687       if(!$this->o_queue->update_entries($update_ids,$data)){
688         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entries.")) , ERROR_DIALOG);
689         return(FALSE);
690       }
691     }
692     return(TRUE);
693   }
696   /*! \brief  Force queue job to be done as far as possible.
697    *  @return Boolean TRUE in case of success, else FALSE.
698    */
699   private function abort_queue_entries($ids)
700   {
701     if(!count($ids)){
702       return;
703     }
705     /* Entries are paused by setting the status to
706      *  something different from 'waiting'.
707      * We simply use 'paused'.
708      */
709     $data = array("status"    => "paused");
711     /* Detect if the ids we got are valid and
712      *  check if the status allows pausing.
713      */
714     $update_ids = array();
715     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
716       if(isset($entry['STATUS']) && preg_match("/processing/",$entry['STATUS'])){
717         if(isset($entry['MACADDRESS'])){
718           $update_ids[] = $entry['MACADDRESS'];
719         }else{
720           trigger_error("No mac address found in event.");
721         }
722       }
723     }
725     if(class_available("DaemonEvent_faireboot")){
726       $tmp = new DaemonEvent_faireboot($this->config);
727       $tmp->add_targets($update_ids);
728       $tmp->set_type(TRIGGERED_EVENT);
729       $this->recently_removed = $update_ids;
730       
731       if(!$this->o_queue->append($tmp)){
732         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
733         return(FALSE);
734       }
735     }else{
736       msg_dialog::display(_("Error"),
737           sprintf(_("Required class '%s' cannot be found: job not aborted!"),
738             "DaemonEvent_faireboot") , ERROR_DIALOG);
739     }
740   }
743   /*! \brief Pauses the specified queue entry from execution.
744    *  @return Boolean TRUE in case of success, else FALSE. 
745    */
746   private function pause_queue_entries($ids)
747   {
748     if(!count($ids)){
749       return;
750     }
752     /* Entries are paused by setting the status to 
753      *  something different from 'waiting'.
754      * We simply use 'paused'.
755      */   
756     $data = array("status"    => "paused");
758     /* Detect if the ids we got are valid and
759      *  check if the status allows pausing.
760      */ 
761     $update_ids = array();
762     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
763       if(isset($entry['STATUS']) && preg_match("/waiting/",$entry['STATUS'])){
764         $update_ids[] = $entry['ID'];
765       }
766     }
768     /* Tell the daemon that we want to update some entries
769      */
770     if(count($update_ids)){
771       if(!$this->o_queue->update_entries($update_ids,$data)){
772         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
773         return(FALSE);
774       }
775     }
776     return(TRUE);
777   }
780   /*! \brief  Request list of queued jobs.
781    *  @return Returns an array of all queued jobs.
782    */
783   function reload()
784   {
786     /* Sort map   html-post-name => daemon-col-name
787      */
788     $map = array(
789         "QueuePosition" => "id",
790         "Action"        => "status",
791         "TaskID"        => "headertag",
792         "TargetName"    => "macaddress",
793         "Schedule"      => "timestamp");
795     /* Create sort header 
796      */
797     if(!isset($map[$this->sort_by])){
798       $sort = "id DESC";
799     }else{
800       $sort   = $map[$this->sort_by]; 
801       if($this->sort_dir == "up"){
802         $sort.= " ASC";
803       }else{
804         $sort.= " DESC";
805       }
806     }
808     /* Sleep a second to avoid timing issues when adding new items */
809     sleep(1);
811     /* Get entries. */ 
812     $start  = $this->start; 
813     $stop   = $this->range;
814     $entries = $this->o_queue->get_queued_entries($this->event_tags,$start,$stop,$sort);
815     if ($this->o_queue->is_error()){
816       msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
817     }
819     /* Assign entries by id.
820      */
821     $this->entries = array();
822     
823     foreach($entries as $entry){
824     
825       /* Skip entries which will be removed within the next seconds */
826       if(isset($entry['MACADDRESS']) && in_array($entry['MACADDRESS'],$this->recently_removed)){
827         continue;
828       }
829       $this->entries[$entry['ID']]= $entry;
830     }
831     $this->recently_removed = array();
832   }
835   /*! \brief  Handle post jobs, like sorting.
836    */
837   function save_object()
838   {
839     /* Check for sorting changes 
840      */
841     $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
842     if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
843       $sort = $_GET['sort'];
844       if($this->sort_by == $sort){
845         if($this->sort_dir == "up"){
846           $this->sort_dir = "down";
847         }else{
848           $this->sort_dir = "up";
849         }
850       }
851       $this->sort_by = $sort;
852     }
854     /* Range selection used? */
855     if(isset($_POST['range']) && is_numeric($_POST['range'])){
856       $this->range = $_POST['range'];
857     }
858   
859     /* Save filter settings */ 
860     $gotomasses_filter = session::get("gotomasses_filter");
861     foreach(array("range","sort_by","sort_dir") as $attr){
862       $gotomasses_filter[$attr] = $this->$attr;
863     }
864     session::set("gotomasses_filter",$gotomasses_filter);
865  
866     /* Page changed. */
867     if(isset($_GET['start'])){
868       $start = $_GET['start'];
869       if(is_numeric($start) || $start == 0){
870         $this->start = $start;
871       }
872     }
874     /* Check start stop and reset if necessary */
875     $count = $this->o_queue->number_of_queued_entries($this->event_tags);
876     if($this->start >= $count){
877       $this->start = $count -1;
878     }
879     if($this->start < 0){
880       $this->start = 0;
881     }
882   }
885   function save()
886   {
887     // We do not save anything here.
888   }
891   /*! \brief  Return a list of all selected items.
892     @return Array   Returns an array containing all selected item ids.
893    */
894   function list_get_selected_items()
895   {
896     $ids = array();
897     foreach($_POST as $name => $value){
898       if(preg_match("/^item_selected_[0-9]*$/",$name)){
899         $id   = preg_replace("/^item_selected_/","",$name);
900         $ids[$id] = $id;
901       }
902     }
903     return($ids);
904   }
907   static function plInfo()
908   {
909     return (array(
910           "plShortName"   => _("System deployment"),
911           "plDescription" => _("Provide a mechanism to automatically activate systems"),
912           "plSelfModify"  => FALSE,
913           "plDepends"     => array(),
914           "plPriority"    => 0,
915           "plSection"     => array("addon"),
916           "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System deployment"))),
917           "plProvidedAcls" => array("Comment"   => _("Description")) 
918           ));
919   }
921 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
922 ?>