Code

Implement reinstall with offsets (#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             /* 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       if(is_object($this->dialog)){
326         $this->dialog->save_object();
327         if($this->dialog->multiple_events) {
328           $event_type = get_class($this->dialog);
329           $targets = $this->dialog->get_targets();
330           $timestamp = $this->dialog->timestamp;
332           $i = 1;
333           $count = count($targets);
334           while($i <= $count) {
335             $operations = $this->dialog->concurrent_operations;
336             $event = new $event_type($this->config);
337             $event->set_timestamp($timestamp);
338             $event->set_type(SCHEDULED_EVENT);
340             while($operations > 0) {
341               $i++;
342               $target = array_shift($targets);
343               $event->add_targets(array($target));
344               $operations--;
345             }
347             $event->save_object();
348             $event->get_targets();
349             if(!$this->o_queue->append($event)){
350               msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->o_queue->get_error()),ERROR_DIALOG);
351             }
353             # Calculate start time for the next run
354             $timestamp = $timestamp + ($this->dialog->time_offset*60);
355           }
357           $this->dialog = FALSE;
358           $this->current = -1;
359         }
360         else {
361           $this->dialog->save_object();
362           if(!$this->o_queue->append($this->dialog)){
363             msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->o_queue->get_error()),ERROR_DIALOG);
364           }else{
365             $this->dialog = FALSE; 
366             $this->current = -1;
367           } 
368         }
369       }
370     }
373     /* Close dialog */
374     if(isset($_POST['abort_event_dialog'])){
375       $this->dialog = FALSE;
376       $this->current = -1;
377     }
379     /* Display dialogs if currently opened */
380     if(is_object($this->dialog)){
381       $this->dialog->save_object();
382       $display = $this->dialog->execute();
384       if($this->dialog instanceOf goto_import_file && $this->dialog->import_successful){
385         $this->dialog = FALSE;
386       }else{
387         return($display);
388       }
389     }
391     /************
392      * Handle Divlist 
393      ************/
395     $divlist = new MultiSelectWindow($this->config,"gotoMasses",array("gotomasses"));
396     $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa tasks."));
397     $divlist->SetSummary(_("List of queued jobs"));
398     $divlist->EnableCloseButton(FALSE);
399     $divlist->EnableSaveButton(FALSE);
400     $divlist->SetHeadpageMode();
401     $s = ".|"._("Actions")."|\n";
402     $s.= "..|<img src='images/lists/new.png' alt='' border='0' class='center'>&nbsp;"._("Create")."\n";
404     if($this->acl_is_writeable("")){
405       foreach($this->events['SCHEDULED'] as $name =>  $event){
406         $s.= "...|".$event['MenuImage']."&nbsp;".$event['s_Menu_Name']."|add_event_".$name."\n";
407       }
408     }
409     if($this->acl_is_removeable()){
410       $s.= "..|---|\n";
411       $s.= "..|<img src='images/lists/import.png' alt='' border='0' class='center'>&nbsp;"._("Import")."|import_file\n";
412       $s.= "..|<img src='images/lists/trash.png' alt='' border='0' class='center'>&nbsp;"._("Remove")."|remove_multiple\n";
413     }
414     if(preg_match("/w/",$this->getacl(""))){
415       $s.= "..|---|\n";
416       $s.= "..|<img alt='"._("Resume")."' src='images/status_start.png' border='0' class='center'>&nbsp;"._("Resume")."|resume_all\n";
417       $s.= "..|<img alt='"._("Pause")."' src='images/status_pause.png' border='0' class='center'>&nbsp;"._("Pause")."|pause_all\n";
418       $s.= "..|<img alt='"._("Abort")."' src='images/small_error.png'  border='0' class='center'>&nbsp;"._("Abort")."|abort_process_all\n";
419       $s.= "..|<img alt='"._("Execute")."' src='images/rocket.png'       border='0' class='center'>&nbsp;"._("Execute")."|execute_process_all\n";
420     }
422     $divlist->SetDropDownHeaderMenu($s);
424     if($this->sort_dir == "up"){
425       $sort_img = "<img src='images/lists/sort-up.png' alt='/\' border=0>";
426     }else{
427       $sort_img = "<img src='images/lists/sort-down.png' alt='\/' border=0>";
428     }
430     if($this->sort_by == "TargetName"){ $sort_img_1 = $sort_img; } else { $sort_img_1 = "" ;}
431     if($this->sort_by == "TaskID"){ $sort_img_2 = $sort_img; } else { $sort_img_2 = "" ;}
432     if($this->sort_by == "Schedule"){ $sort_img_3 = $sort_img; } else { $sort_img_3 = "" ;}
433     if($this->sort_by == "Action"){ $sort_img_4 = $sort_img; } else { $sort_img_4 = "" ;}
435     /* Create divlist */
436     $divlist->SetListHeader("<input type='image' src='images/lists/reload.png' title='"._("Reload")."'>");
438     $plug  = $_GET['plug'];
439     $chk = "<input type='checkbox' id='select_all' name='select_all'
440                onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
442     /* set Page header */
443     $divlist->AddHeader(array("string"=> $chk,          "attach"=>"style='width:20px;'"));
444     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TargetName'>"._("Target").$sort_img_1."</a>"));
445     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TaskID'>"._("Task").$sort_img_2."</a>",
446                                       "attach"=>"style='width:120px;'"));
447     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
448                                       "attach"=>"style='width:140px;'"));
449     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Action'>"._("Status").$sort_img_4."</a>",
450                                       "attach"=>"style='width:80px;'"));
451     $divlist->AddHeader(array("string"=>_("Action"),
452                                       "attach"=>"style='border-right:0px;width:140px;'"));
455     /* Reload the list of entries */
456     $this->reload();
458     foreach($this->entries as $key => $task){
460       $prio_actions="";
461       $action = "";
464       /* If WAITING add priority action
465        */  
466       if(in_array($task['STATUS'],array("waiting")) && $this->acl_is_writeable("")){
467         $prio_actions.= "<input class='center' type='image' src='plugins/goto/images/prio_increase.png' 
468           title='"._("Move up")."' name='prio_up_".$key."'>&nbsp;";
469         $prio_actions.= "<input class='center' type='image' src='plugins/goto/images/prio_decrease.png' 
470           title='"._("Move down")."' name='prio_down_".$key."'>&nbsp;";
471       }
472     
473       /* If WAITING add pause action
474        */  
475       if(in_array($task['STATUS'],array("waiting")) && $this->acl_is_writeable("")){
476         $prio_actions.= "<input class='center' type='image' src='images/status_pause.png' 
477           title='"._("Pause job")."' name='pause_".$key."'>&nbsp;";
478       }
480       /* If PAUSED add resume action
481        */  
482       if(in_array($task['STATUS'],array("paused")) && $this->acl_is_writeable("")){
483         $prio_actions.= "<input class='center' type='image' src='images/status_start.png' 
484           title='"._("Resume job")."' name='resume_".$key."'>&nbsp;";
485       }
487       /* If PAUSED or WAITING add execution action
488        */  
489       if(in_array($task['STATUS'],array("paused","waiting")) && $this->acl_is_writeable("")){
490         $prio_actions.= "<input class='center' type='image' src='images/rocket.png' 
491           title='"._("Execute now")."' name='execute_process_".$key."'>&nbsp;";
492       }
494       /* Add logview button, currently ever.
495        */  
496       if($this->acl_is_readable("")){
497         $action .= "<input type='image' src='plugins/goto/images/view_logs.png' name='log_view_".$key."' 
498           class='center' title='"._("View logs")."' alt='"._("View logs")."'>&nbsp;";
499       }
501       /* If PAUSED or WAITING add edit action
502        */  
503       if(in_array($task['STATUS'],array("waiting")) && $this->acl_is_writeable("")){
504         $action.= "<input type='image' src='images/lists/edit.png' name='edit_task_".$key."' 
505           class='center' title='"._("Edit")."' alt='"._("Edit")."'>";
506       }
508       /* If PROCESSING add abort action
509        */  
510       if(in_array($task['STATUS'],array("processing")) && preg_match("/install/",$task['HEADERTAG']) && $this->acl_is_writeable("")){
511         $action.= "<img src='images/empty.png' alt=''>";
512         $action.= "<input class='center' type='image' src='images/small_error.png' 
513           title='"._("Abort job")."' name='abort_process_".$key."'>";
514       }
516       /* If WAITING or ERROR add remove action
517        */  
518       if( $this->acl_is_removeable() && in_array($task['STATUS'],array("waiting","error","processed"))){
519         $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."' 
520           class='center' title='"._("Remove")."' alt='"._("Remove")."'>";
521       }
522       if($this->acl_is_writeable("") && in_array($task['STATUS'],array("processing")) && !preg_match("/install/",$task['HEADERTAG'])){
523         $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."' 
524           class='center' title='"._("Remove")."' alt='"._("Remove")."'>";
525       }
527       /* Create entry display name and tooltip */
528       $color = "";
529       $display = $task['MACADDRESS'];
530       $tooltip = "";
531       if(isset($task['PLAINNAME']) && !preg_match("/none/i",$task['PLAINNAME'])){
532         $display = $task['PLAINNAME'];
533         $tooltip = " title='".$task['MACADDRESS']."' ";
534       }
535       $display2= $task['HEADERTAG'];
536      
537       /* Check if this event exists as Daemon class 
538        * In this case, display a more accurate entry.
539        */ 
540       if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
541         $evt_name   = $this->events['QUEUED'][$task['HEADERTAG']];
542         $event_type = $this->events['BY_CLASS'][$evt_name];
543         $display2   = $event_type['s_Menu_Name'];
545         if(strlen($display2) > 20){
546           $display2 = substr($display2,0,18)."...";
547         }
549         if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
550           $display2 = $event_type['ListImage']."&nbsp;".$display2;
551         }
552       } 
554       $status = $task['STATUS'];
555   
556       if($status == "waiting"){
557         $status = "<img class='center' src='plugins/goto/images/clock.png' alt=''>&nbsp;"._("Waiting");
558       }
559       if($status == "error"){
560         $status = "<img class='center' src='images/false.png' alt=''>&nbsp;"._("Error");
561       }
562       if($status == "processed"){
563         $status = "<img class='center' src='images/true.png' alt=''>&nbsp;"._("Processed");
564       }
566       /* Special handling for all entries that have 
567           STATUS == "processing" && PROGRESS == NUMERIC
568        */
569       if($status == "processing" && isset($task['PROGRESS'])){
570         $percent = $task['PROGRESS'];
572         /* Show activation? */
573         if ($percent == "goto-activation"){
574           $status = "<img class='center' src='images/lists/off.png' alt=''>&nbsp;"._("Locked");
576         /* Show hardware detect? */
577         } elseif ($percent == "goto-hardware-detection") {
578           $status = "<img class='center' src='plugins/goto/images/hardware.png' alt=''>&nbsp;"._("Detection");
580         /* Real percent */
581         } else {
582          if (preg_match('/install/', $task['HEADERTAG'])){
583             $status = "<img src='progress.php?x=80&y=13&p=".$task['PROGRESS']."' alt=''
584                           id='progress_".preg_replace("/:/","_",$task['MACADDRESS'])."'>";
585           } else {
586             $status = preg_replace('/ /', '&nbsp;', _("in progress"));
587           }
588         }
589       }
591       /* Create each field */
592       $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$key."'>" ,
593                       "attach" => "style='width:20px;".$color."'");
594       $field1 = array("string" => $display,
595                       "attach" => $tooltip."style='".$color."'");
596       $field1a= array("string" => $display2,
597                       "attach" => "style='".$color.";width:120px;'");
598       if ($task['TIMESTAMP'] == "19700101000000"){
599               $field2 = array("string" => _("immediately"),"attach" => "style='".$color.";width:140px;'");
600       } else {
601               $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:140px;'");
602       }
603       $field3 = array("string" => $status,"attach" => "style='".$color.";width:80px;'");
604       $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:140px;border-right:0px;'");
605       $divlist->AddElement(array($field0,$field1,$field1a,$field2,$field3,$field4));
606     }
608     $smarty = get_smarty();
609     $smarty->assign("events",$this->events);
610     $smarty->assign("start",$this->start);
611     $smarty->assign("start_real", ($this->start + 1));
612     $smarty->assign("ranges", array("10" => "10",
613                                     "20" => "20",
614                                     "25" => "25",
615                                     "50" => "50",
616                                     "100"=> "100",
617                                     "200"=> "200",
618                                     "9999" => "*"));
620     $count = $this->o_queue->number_of_queued_entries($this->event_tags);
621     if(!$count) $count = $this->range;
622     $divlist->SetListFooter(range_selector($count, $this->start, $this->range,"range"));
623     $smarty->assign("range",$this->range);
624     $smarty->assign("div",$divlist->Draw());
625     return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
626   }
629   /*! \brief  Move an entry up or down in the queue, by updating its execution timestamp  
630       @param  $id     Integer  The ID of the entry which should be updated.
631       @param  $type   String   "up" / "down"
632       @return boolean TRUE in case of success else FALSE
633   */
634   public function update_priority($id,$type = "up")
635   {
636     if($type == "up"){
637       $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp DESC");
638     }else{
639       $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp ASC");
640     }
641     $last = array();
642     foreach($tmp as $entry){
643       if($entry['ID'] == $id){
644         if(count($last)){
645           $time = strtotime($last['TIMESTAMP']);
646           if($type == "up"){
647             $time ++;
648           }else{
649             $time --;
650           }
651           $time_str = date("YmdHis",$time); 
652           return($this->o_queue->update_entries(array($id),array("timestamp" => $time_str)));
653         }else{
654           return(FALSE);
655         }
656       }
657       $last = $entry;
658     }
659     return(FALSE);
660   }
663   /*! \brief  Resumes to status 'waiting'.
664    *  @return Boolean TRUE in case of success, else FALSE. 
665    */
666   private function resume_queue_entries($ids)
667   {
668     if(!count($ids)){
669       return;
670     }
672     /* Entries are resumed by setting the status to 
673      *  'waiting'
674      */
675     $data = array("status"    => "waiting");
676   
677     /* Check if given ids are valid and check if the status
678      *  allows resuming.
679      */
680     $update_ids = array();
681     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
682       if(isset($entry['STATUS']) && preg_match("/paused/",$entry['STATUS'])){
683         $update_ids[] = $entry['ID'];
684       }
685     }
687     /* Tell the daemon that we have entries to update.
688      */
689     if(count($update_ids)){
690       if(!$this->o_queue->update_entries($update_ids,$data)){
691         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
692         return(FALSE);
693       }
694     }
695     return(TRUE);
696   }
699   /*! \brief  Force queue job to be done as far as possible.
700    *  @return Boolean TRUE in case of success, else FALSE.
701    */
702   private function execute_queue_entries($ids)
703   {
704     if(!count($ids)){
705       return;
706     }
708     /* Execution is forced by updating the status to 
709      *  waiting and setting the timestamp to current time.
710      */
711     $data = array(  "timestamp" => date("YmdHis",time()), 
712                     "status"    => "waiting");
714     /* Only allow execution of paused or waiting entries 
715      */
716     $update_ids = array();
717     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
718       if(in_array($entry['STATUS'],array("paused","waiting"))){
719         $update_ids[] = $entry['ID'];
720       }
721     }
723     /* Tell the daemon that we want to update some entries
724      */
725     if(count($update_ids)){
726       if(!$this->o_queue->update_entries($update_ids,$data)){
727         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entries.")) , ERROR_DIALOG);
728         return(FALSE);
729       }
730     }
731     return(TRUE);
732   }
735   /*! \brief  Force queue job to be done as far as possible.
736    *  @return Boolean TRUE in case of success, else FALSE.
737    */
738   private function abort_queue_entries($ids)
739   {
740     if(!count($ids)){
741       return;
742     }
744     /* Entries are paused by setting the status to
745      *  something different from 'waiting'.
746      * We simply use 'paused'.
747      */
748     $data = array("status"    => "paused");
750     /* Detect if the ids we got are valid and
751      *  check if the status allows pausing.
752      */
753     $update_ids = array();
754     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
755       if(isset($entry['STATUS']) && preg_match("/processing/",$entry['STATUS'])){
756         if(isset($entry['MACADDRESS'])){
757           $update_ids[] = $entry['MACADDRESS'];
758         }else{
759           trigger_error("No mac address found in event.");
760         }
761       }
762     }
764     if(class_available("DaemonEvent_faireboot")){
765       $tmp = new DaemonEvent_faireboot($this->config);
766       $tmp->add_targets($update_ids);
767       $tmp->set_type(TRIGGERED_EVENT);
768       $this->recently_removed = $update_ids;
769       
770       if(!$this->o_queue->append($tmp)){
771         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
772         return(FALSE);
773       }
774     }else{
775       msg_dialog::display(_("Error"),
776           sprintf(_("Required class '%s' cannot be found: job not aborted!"),
777             "DaemonEvent_faireboot") , ERROR_DIALOG);
778     }
779   }
782   /*! \brief Pauses the specified queue entry from execution.
783    *  @return Boolean TRUE in case of success, else FALSE. 
784    */
785   private function pause_queue_entries($ids)
786   {
787     if(!count($ids)){
788       return;
789     }
791     /* Entries are paused by setting the status to 
792      *  something different from 'waiting'.
793      * We simply use 'paused'.
794      */   
795     $data = array("status"    => "paused");
797     /* Detect if the ids we got are valid and
798      *  check if the status allows pausing.
799      */ 
800     $update_ids = array();
801     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
802       if(isset($entry['STATUS']) && preg_match("/waiting/",$entry['STATUS'])){
803         $update_ids[] = $entry['ID'];
804       }
805     }
807     /* Tell the daemon that we want to update some entries
808      */
809     if(count($update_ids)){
810       if(!$this->o_queue->update_entries($update_ids,$data)){
811         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
812         return(FALSE);
813       }
814     }
815     return(TRUE);
816   }
819   /*! \brief  Request list of queued jobs.
820    *  @return Returns an array of all queued jobs.
821    */
822   function reload()
823   {
825     /* Sort map   html-post-name => daemon-col-name
826      */
827     $map = array(
828         "QueuePosition" => "id",
829         "Action"        => "status",
830         "TaskID"        => "headertag",
831         "TargetName"    => "macaddress",
832         "Schedule"      => "timestamp");
834     /* Create sort header 
835      */
836     if(!isset($map[$this->sort_by])){
837       $sort = "id DESC";
838     }else{
839       $sort   = $map[$this->sort_by]; 
840       if($this->sort_dir == "up"){
841         $sort.= " ASC";
842       }else{
843         $sort.= " DESC";
844       }
845     }
847     /* Sleep a second to avoid timing issues when adding new items */
848     sleep(1);
850     /* Get entries. */ 
851     $start  = $this->start; 
852     $stop   = $this->range;
853     $entries = $this->o_queue->get_queued_entries($this->event_tags,$start,$stop,$sort);
854     if ($this->o_queue->is_error()){
855       msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
856     }
858     /* Assign entries by id.
859      */
860     $this->entries = array();
861     
862     foreach($entries as $entry){
863     
864       /* Skip entries which will be removed within the next seconds */
865       if(isset($entry['MACADDRESS']) && in_array($entry['MACADDRESS'],$this->recently_removed)){
866         continue;
867       }
868       $this->entries[$entry['ID']]= $entry;
869     }
870     $this->recently_removed = array();
871   }
874   /*! \brief  Handle post jobs, like sorting.
875    */
876   function save_object()
877   {
878     /* Check for sorting changes 
879      */
880     $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
881     if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
882       $sort = $_GET['sort'];
883       if($this->sort_by == $sort){
884         if($this->sort_dir == "up"){
885           $this->sort_dir = "down";
886         }else{
887           $this->sort_dir = "up";
888         }
889       }
890       $this->sort_by = $sort;
891     }
893     /* Range selection used? */
894     if(isset($_POST['range']) && is_numeric($_POST['range'])){
895       $this->range = $_POST['range'];
896     }
897   
898     /* Save filter settings */ 
899     $gotomasses_filter = session::get("gotomasses_filter");
900     foreach(array("range","sort_by","sort_dir") as $attr){
901       $gotomasses_filter[$attr] = $this->$attr;
902     }
903     session::set("gotomasses_filter",$gotomasses_filter);
904  
905     /* Page changed. */
906     if(isset($_GET['start'])){
907       $start = $_GET['start'];
908       if(is_numeric($start) || $start == 0){
909         $this->start = $start;
910       }
911     }
913     /* Check start stop and reset if necessary */
914     $count = $this->o_queue->number_of_queued_entries($this->event_tags);
915     if($this->start >= $count){
916       $this->start = $count -1;
917     }
918     if($this->start < 0){
919       $this->start = 0;
920     }
921   }
924   function save()
925   {
926     // We do not save anything here.
927   }
930   /*! \brief  Return a list of all selected items.
931     @return Array   Returns an array containing all selected item ids.
932    */
933   function list_get_selected_items()
934   {
935     $ids = array();
936     foreach($_POST as $name => $value){
937       if(preg_match("/^item_selected_[0-9]*$/",$name)){
938         $id   = preg_replace("/^item_selected_/","",$name);
939         $ids[$id] = $id;
940       }
941     }
942     return($ids);
943   }
946   static function plInfo()
947   {
948     return (array(
949           "plShortName"   => _("System deployment"),
950           "plDescription" => _("Provide a mechanism to automatically activate systems"),
951           "plSelfModify"  => FALSE,
952           "plDepends"     => array(),
953           "plPriority"    => 0,
954           "plSection"     => array("addon"),
955           "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System deployment"))),
956           "plProvidedAcls" => array("Comment"   => _("Description")) 
957           ));
958   }
960 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
961 ?>