Code

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