Code

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