Code

Updated gotomasses
[gosa.git] / gosa-plugins / goto / addons / goto / class_gotomasses.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class gotomasses extends management
24 {
26   var $plHeadline     = "System deployment status";
27   var $plDescription  = "System deployment status";
28   var $plIcon  = "plugins/goto/images/goto.png";
30   var $current        = FALSE;
31   var $dialog         = FALSE;
32   var $ids_to_remove  = array();
34   var $events         = array();
35   var $event_tags     = array();
36   var $recently_removed = array();
38   function __construct(&$config, $ui)
39   {
40     /* Include config object */
41     $this->config= &$config;
42     $this->o_queue = new gosaSupportDaemon(TRUE,5);
43     $this->events  = DaemonEvent::get_event_types( SYSTEM_EVENT);
45     /* Get tags that will be used in queue searches */
46     $this->event_tags = array("none");
47     foreach($this->events['SCHEDULED'] as $evt){
48       $this->event_tags[] = $evt['s_Queued_Action'];
49     }
51     // Build filter
52    # if (session::global_is_set(get_class($this)."_filter")){
53   #    $filter= session::global_get(get_class($this)."_filter");
54  #   } else {
55       $filter = new filter(get_template_path("deploy-filter.xml", true));
56       $filter->setObjectStorage($this->storagePoints);
57 #    }
58     $this->setFilter($filter);
60     // Build headpage
61     $headpage = new listing(get_template_path("deploy-list.xml", true));
62     $headpage->registerElementFilter("hostName",  "gotomasses::filterHostName");
63     $headpage->registerElementFilter("filterTask","gotomasses::filterTask");
64     $headpage->registerElementFilter("filterPeriod","gotomasses::filterPeriod");
65     $headpage->registerElementFilter("filterSchedule","gotomasses::filterSchedule");
66     $headpage->registerElementFilter("filterStatus","gotomasses::filterStatus");
67     $headpage->setFilter($filter);
69     parent::__construct($config, $ui, "Events", $headpage);
70   }
72   static function filterHostName($mac, $name ="")
73   {
74     if(isset($name[0]) && $name[0] != "none"){
75       return($name[0]);
76     }
77     return($mac[0]);
78   }
80   static function filterTask($tag)
81   {
82     $tag = $tag[0];
83     $str = $tag;
85     /* Check if this event exists as Daemon class
86      * In this case, display a more accurate entry.
87      */
88     $events  = DaemonEvent::get_event_types( SYSTEM_EVENT);
89     if(isset($events['QUEUED'][$tag])){
90       $evt_name   = $events['QUEUED'][$tag];
91       $event_type = $events['BY_CLASS'][$evt_name];
92       $str        = $event_type['s_Menu_Name'];
94       if(strlen($str) > 20){
95         $str = substr($str,0,18)."...";
96       }
98       if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
99         $str = $event_type['ListImage']."&nbsp;".$str;
100       }
101     }
102     return($str);
103   }
105   static function filterPeriod($periodic=array())
106   {
107     $period = "&nbsp;-";
108     if(isset($periodic[0]) && !preg_match("/none/i",$periodic[0])){
109       $tmp = explode("_", $periodic[0]);
110       if(count($tmp) == 2){
111         $period= $tmp[0]."&nbsp;"._($tmp[1]);
112       }
113     }
114     return($period);
115   }
117   static function filterSchedule($stamp)
118   {
119     if ($stamp['0'] == "19700101000000"){
120       return(_("immediately"));
121     } else {
122       return(date("d.m.Y H:i:s",strtotime($stamp[0])));
123     }
124   }
127   static function filterStatus($status, $mac,$headertag, $progress)
128   {
130     $mac = $mac[0];
131     $status = $status[0];
132     $progress = $progress[0];
133     $headertag = $headertag[0];
135     if($status == "waiting"){
136       $status = "<img class='center' src='plugins/goto/images/clock.png' alt=''>&nbsp;"._("Waiting");
137     }
138     if($status == "error"){
139       $status = "<img class='center' src='images/false.png' alt=''>&nbsp;"._("Error");
140     }
141     if($status == "processed"){
142       $status = "<img class='center' src='images/true.png' alt=''>&nbsp;"._("Processed");
143     }
145     /* Special handling for all entries that have
146        STATUS == "processing" && PROGRESS == NUMERIC
147      */
148     if($status == "processing" && $progress){
149       $percent = $progress;
151       /* Show activation? */
152       if ($percent == "goto-activation"){
153         $status = "<img class='center' src='images/lists/off.png' alt=''>&nbsp;"._("Locked");
155         /* Show hardware detect? */
156       } elseif ($percent == "goto-hardware-detection") {
157         $status = "<img class='center' src='plugins/goto/images/hardware.png' alt=''>&nbsp;"._("Detection");
159         /* Real percent */
160       } else {
161         if (preg_match('/install/', $headertag)){
162           $status = "<img src='progress.php?x=80&y=13&p=".$progress."' alt=''
163             id='progress_".preg_replace("/:/","_",$mac)."'>";
164         } else {
165           $status = preg_replace('/ /', '&nbsp;', _("in progress"));
166         }
167       }
168     }
169     return($status);
170   }
185   function _execute()
186   {
187     $smarty = get_smarty();
189     /************
190      * Handle posts 
191      ************/
193     $s_entry = $s_action = "";
194     $arr = array( 
196         "/^pause_/"           => "pause",
197         "/^resume_/"          => "resume",
198         "/^execute_process_/" => "execute_process",
199         "/^abort_process_/"   => "abort_process",
201         "/^prio_up_/"     => "prio_up",
202         "/^prio_down_/"   => "prio_down",
204         "/^edit_task_/"             =>  "edit",
205         "/^log_view_/"              =>  "logview",
206         "/^remove_task_/"           =>  "remove",
207         "/^new_task_/"              =>  "new_task");;
209     foreach($arr as $regex => $action){
210       foreach($_POST as $name => $value){
211         if(preg_match($regex,$name)){
212           $s_action = $action;
213           $s_entry  = preg_replace($regex,"",$name);
214           $s_entry  = preg_replace("/_(x|y)$/","",$s_entry);
215         }
216       }
217     }
219     /* Menu actions */
220     if(isset($_POST['menu_action']) && !empty($_POST['menu_action'])){
221       $s_action = $_POST['menu_action'];
222     }
224     /* Edit posted from list link */
225     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
226       $s_action = "edit";
227       $s_entry = $_GET['id'];
228     }
231     /************
232      * Import CSV file  
233      ************/
235     if($s_action == "import_file" && $this->acl_is_writeable("")){
236       $this->dialog = new goto_import_file($this->config,$this);
237     }
239     if(isset($_POST['import_abort'])){
240       $this->dialog = FALSE;
241     }
244     /************
245      * Handle Priority modifications  
246      ************/
248     if(preg_match("/^prio_/",$s_action) && $this->acl_is_writeable("")){
249       switch($s_action){
250         case 'prio_down'  : $this->update_priority($s_entry,"down");break;
251         case 'prio_up'    : $this->update_priority($s_entry,"up");break;
252       }
253     }
255     /************
256      * Handle pause/resume/execute modifications  
257      ************/
259     if(preg_match("/^resume/",$s_action) || 
260         preg_match("/^pause/",$s_action) || 
261         preg_match("/^abort_process/",$s_action) || 
262         preg_match("/^execute_process/",$s_action)){
264       if($this->acl_is_writeable("")){
265         switch($s_action){
266           case 'resume'         : $this->resume_queue_entries   (array($s_entry));break; 
267           case 'pause'          : $this->pause_queue_entries    (array($s_entry));break; 
268           case 'execute_process': $this->execute_queue_entries  (array($s_entry));break; 
269           case 'abort_process'  : $this->abort_queue_entries    (array($s_entry));break; 
270           case 'resume_all'         : $this->resume_queue_entries   ($this->list_get_selected_items());break; 
271           case 'pause_all'          : $this->pause_queue_entries    ($this->list_get_selected_items());break; 
272           case 'execute_process_all': $this->execute_queue_entries  ($this->list_get_selected_items());break; 
273           case 'abort_process_all'  : $this->abort_queue_entries    ($this->list_get_selected_items());break; 
275           default : trigger_error("Undefined action setting used (".$s_action.").");
276         }
277       }
278       if($this->o_queue->is_error()){
279         msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
280       }
281     }
283     /************
284      * ADD 
285      ************/
287     if(preg_match("/^add_event_/",$s_action) && $this->acl_is_writeable("")){
288       $type = preg_replace("/^add_event_/","",$s_action);
289       if(isset($this->events['BY_CLASS'][$type])){
290         $e_data = $this->events['BY_CLASS'][$type];
291         $this->dialog = new $e_data['CLASS_NAME']($this->config);
292       }
293     }
295     /************
296      * EDIT
297      ************/
299     if($s_action == "edit" && $this->acl_is_writeable("")){  
300       $id =  $s_entry;
301       $type = FALSE;
302       if(isset($this->entries[$id])){
303         $event = $this->entries[$s_entry];
304         if($event['STATUS'] == "waiting" && isset($this->events['QUEUED'][$event['HEADERTAG']])){
305           $evt_name = $this->events['QUEUED'][$event['HEADERTAG']];
306           $type = $this->events['BY_CLASS'][$evt_name];
307           $this->dialog = new $type['CLASS_NAME']($this->config,$event);
308         }
309       }
310     }
313     /************
314      * LOG VIEW
315      ************/
317     if($s_action == "logview"  && $this->acl_is_readable("")){  
318       $id =  $s_entry;
319       $type = FALSE;
320       if(isset($this->entries[$id])){
321         $event = $this->entries[$s_entry];
322         $this->dialog = new gotoLogView($this->config,"",$event,$this);
323       }
324     }
327     /************
328      * REMOVE 
329      ************/
331     /* Remove multiple */
332     if($s_action == "remove_multiple" || $s_action == "remove"){
334       if(!$this->acl_is_removeable()){
335         msg_dialog::display(_("Permission"), msgPool::permDelete(), ERROR_DIALOG);
336       }else{
338         if($s_action == "remove"){
339           $ids = array($s_entry);
340         }else{
341           $ids = $this->list_get_selected_items();
342         }
344         $this->ids_to_remove = array();
346         if(count($ids)){
347           $ret = $this->o_queue->ids_exist($ids);
348           $ret = $this->o_queue->get_entries_by_id($ret);
349           $tmp = "";
351           $deleteable_jobs = array();      
352           $not_deleteable_jobs = array();      
353           foreach($ret as $task){
355             /* Create a printable job name/description */
356             if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
357               $evt_name = $this->events['QUEUED'][$task['HEADERTAG']];
358               $evt = $this->events['BY_CLASS'][$evt_name];
359               $j_name = $task['ID']." - ".$evt['s_Menu_Name']."&nbsp;".$task['MACADDRESS'];
360             }else{
361               $j_name = $task['ID']." - ".$task['HEADERTAG']."&nbsp;".$task['MACADDRESS'];
362             }
364             /* Only remove WAITING or ERROR entries */
365             if(in_array($task['STATUS'],array("waiting","error","processed")) || 
366                 ($task['STATUS'] == "processing" && !preg_match("/install/",$task['HEADERTAG'])) ){
367               $this->ids_to_remove[] = $task['ID'];
368               $deleteable_jobs[] = $j_name;
369             }else{
370               $not_deleteable_jobs[] = $j_name;
371             }
372           }
373           if(count($not_deleteable_jobs)){
374             msg_dialog::display(_("Remove"),
375                 sprintf(_("The following jobs couldn't be deleted, they have to be aborted: %s"),
376                   "<br>".msgPool::buildList($not_deleteable_jobs)),INFO_DIALOG);
377           }
379           if(count($this->ids_to_remove)){
380             $smarty->assign("multiple", TRUE); 
381             $smarty->assign("info",msgPool::deleteInfo($deleteable_jobs));
382             $this->current = $s_entry;
383             return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
384           }
385         }
386       }
387     }
389     /* Remove specified tasks */
390     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
392       /* Reboot hosts with not yet startet installations and timestamps in the past 
393        */
394       if($this->acl_is_removeable("")){
395         timezone::get_default_timezone();
396         foreach($this->ids_to_remove as $id){
397           $entry = $this->o_queue->get_entries_by_id(array($id));
398           if(isset($entry['ANSWER1'])){
399             $entry = $entry['ANSWER1'];
400             if( $entry['STATUS'] == "waiting" && 
401                 $entry['HEADERTAG'] == "trigger_action_reinstall"){
402               $evt = new DaemonEvent_reinstall($this->config,$entry);
403               if($evt->get_timestamp(FALSE)  < time()){
404                 $r_evt = new DaemonEvent_localboot($this->config);
405                 $r_evt->add_targets(array($entry['MACADDRESS']));
406                 $r_evt->set_type(TRIGGERED_EVENT);
407                 $this->o_queue->append($r_evt);
408               }
409             }
410           }
411         }
413         $this->o_queue->remove_entries($this->ids_to_remove);
414         $this->save();
415       }
416     }
418     /* Remove aborted */
419     if(isset($_POST['delete_cancel'])){
420       $this->ids_to_remove = array();;
421     }
424     /************
425      * EDIT 
426      ************/
428     /* Close dialog */
429     if(isset($_POST['save_event_dialog'])){
430       if(is_object($this->dialog)){
431         $this->dialog->save_object();
432         if(!$this->o_queue->append($this->dialog)){
433           msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->o_queue->get_error()),ERROR_DIALOG);
434         }else{
435           $this->dialog = FALSE; 
436           $this->current = -1;
437         } 
438       }
439     }
442     /* Close dialog */
443     if(isset($_POST['abort_event_dialog'])){
444       $this->dialog = FALSE;
445       $this->current = -1;
446     }
448     /* Display dialogs if currently opened */
449     if(is_object($this->dialog)){
450       $this->dialog->save_object();
451       $display = $this->dialog->execute();
453       if($this->dialog instanceOf goto_import_file && $this->dialog->import_successful){
454         $this->dialog = FALSE;
455       }else{
456         return($display);
457       }
458     }
460    /************
461     * Handle Divlist 
462     ************/
464    $divlist = new MultiSelectWindow($this->config,"gotoMasses",array("gotomasses"));
465    $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa tasks."));
466    $divlist->SetSummary(_("List of queued jobs"));
467    $divlist->EnableCloseButton(FALSE);
468    $divlist->EnableSaveButton(FALSE);
469    $divlist->SetHeadpageMode();
470    $s = ".|"._("Actions")."|\n";
471    $s.= "..|<img src='images/lists/new.png' alt='' border='0' class='center'>&nbsp;"._("Create")."\n";
473    if($this->acl_is_writeable("")){
474      foreach($this->events['SCHEDULED'] as $name =>  $event){
475        $s.= "...|".$event['MenuImage']."&nbsp;".$event['s_Menu_Name']."|add_event_".$name."\n";
476      }
477    }
478    if($this->acl_is_removeable()){
479      $s.= "..|---|\n";
480      $s.= "..|<img src='images/lists/import.png' alt='' border='0' class='center'>&nbsp;"._("Import")."|import_file\n";
481      $s.= "..|<img src='images/lists/trash.png' alt='' border='0' class='center'>&nbsp;"._("Remove")."|remove_multiple\n";
482    }
483    if(preg_match("/w/",$this->getacl(""))){
484      $s.= "..|---|\n";
485      $s.= "..|<img alt='"._("Resume")."' src='images/status_start.png' border='0' class='center'>&nbsp;"._("Resume")."|resume_all\n";
486      $s.= "..|<img alt='"._("Pause")."' src='images/status_pause.png' border='0' class='center'>&nbsp;"._("Pause")."|pause_all\n";
487      $s.= "..|<img alt='"._("Abort")."' src='images/small_error.png'  border='0' class='center'>&nbsp;"._("Abort")."|abort_process_all\n";
488      $s.= "..|<img alt='"._("Execute")."' src='images/rocket.png'       border='0' class='center'>&nbsp;"._("Execute")."|execute_process_all\n";
489    }
491    $divlist->SetDropDownHeaderMenu($s);
493    if($this->sort_dir == "up"){
494      $sort_img = "<img src='images/lists/sort-up.png' alt='/\' border=0>";
495    }else{
496      $sort_img = "<img src='images/lists/sort-down.png' alt='\/' border=0>";
497    }
499    if($this->sort_by == "TargetName"){ $sort_img_1 = $sort_img; } else { $sort_img_1 = "" ;}
500    if($this->sort_by == "TaskID"){ $sort_img_2 = $sort_img; } else { $sort_img_2 = "" ;}
501    if($this->sort_by == "Schedule"){ $sort_img_3 = $sort_img; } else { $sort_img_3 = "" ;}
502    if($this->sort_by == "Action"){ $sort_img_4 = $sort_img; } else { $sort_img_4 = "" ;}
504    /* Create divlist */
505    $divlist->SetListHeader("<input type='image' src='images/lists/reload.png' title='"._("Reload")."'>");
507    $plug  = $_GET['plug'];
508    $chk = "<input type='checkbox' id='select_all' name='select_all'
509               onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
511    /* set Page header */
512    $divlist->AddHeader(array("string"=> $chk,          "attach"=>"style='width:20px;'"));
513    $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TargetName'>"._("Target").$sort_img_1."</a>"));
514    $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TaskID'>"._("Task").$sort_img_2."</a>",
515                                      "attach"=>"style='width:120px;'"));
516    $divlist->AddHeader(array("string"=>_("Period"),
517                                      "attach"=>"style='width:60px;'"));
518    $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
519                                      "attach"=>"style='width:140px;'"));
520    $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Action'>"._("Status").$sort_img_4."</a>",
521                                      "attach"=>"style='width:80px;'"));
522    $divlist->AddHeader(array("string"=>_("Action"),
523                                       "attach"=>"style='border-right:0px;width:140px;'"));
526     /* Reload the list of entries */
527     $this->reload();
529     foreach($this->entries as $key => $task){
531       $prio_actions="";
532       $action = "";
535       /* If WAITING add priority action
536        */  
537       if(in_array($task['STATUS'],array("waiting")) && $this->acl_is_writeable("")){
538         $prio_actions.= "<input class='center' type='image' src='plugins/goto/images/prio_increase.png' 
539           title='"._("Move up")."' name='prio_up_".$key."'>&nbsp;";
540         $prio_actions.= "<input class='center' type='image' src='plugins/goto/images/prio_decrease.png' 
541           title='"._("Move down")."' name='prio_down_".$key."'>&nbsp;";
542       }
544       /* If WAITING add pause action
545        */  
546       if(in_array($task['STATUS'],array("waiting")) && $this->acl_is_writeable("")){
547         $prio_actions.= "<input class='center' type='image' src='images/status_pause.png' 
548           title='"._("Pause job")."' name='pause_".$key."'>&nbsp;";
549       }
551       /* If PAUSED add resume action
552        */  
553       if(in_array($task['STATUS'],array("paused")) && $this->acl_is_writeable("")){
554         $prio_actions.= "<input class='center' type='image' src='images/status_start.png' 
555           title='"._("Resume job")."' name='resume_".$key."'>&nbsp;";
556       }
558       /* If PAUSED or WAITING add execution action
559        */  
560       if(in_array($task['STATUS'],array("paused","waiting")) && $this->acl_is_writeable("")){
561         $prio_actions.= "<input class='center' type='image' src='images/rocket.png' 
562           title='"._("Execute now")."' name='execute_process_".$key."'>&nbsp;";
563       }
565       /* Add logview button, currently ever.
566        */  
567       if($this->acl_is_readable("")){
568         $action .= "<input type='image' src='plugins/goto/images/view_logs.png' name='log_view_".$key."' 
569           class='center' title='"._("View logs")."' alt='"._("View logs")."'>&nbsp;";
570       }
572       /* If PAUSED or WAITING add edit action
573        */  
574       if(in_array($task['STATUS'],array("waiting")) && $this->acl_is_writeable("")){
575         $action.= "<input type='image' src='images/lists/edit.png' name='edit_task_".$key."' 
576           class='center' title='"._("Edit")."' alt='"._("Edit")."'>";
577       }
579       /* If PROCESSING add abort action
580        */  
581       if(in_array($task['STATUS'],array("processing")) && preg_match("/install/",$task['HEADERTAG']) && $this->acl_is_writeable("")){
582         $action.= "<img src='images/empty.png' alt=''>";
583         $action.= "<input class='center' type='image' src='images/small_error.png' 
584           title='"._("Abort job")."' name='abort_process_".$key."'>";
585       }
587       /* If WAITING or ERROR add remove action
588        */  
589       if( $this->acl_is_removeable() && in_array($task['STATUS'],array("waiting","error","processed"))){
590         $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."' 
591           class='center' title='"._("Remove")."' alt='"._("Remove")."'>";
592       }
593       if($this->acl_is_writeable("") && in_array($task['STATUS'],array("processing")) && !preg_match("/install/",$task['HEADERTAG'])){
594         $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."' 
595           class='center' title='"._("Remove")."' alt='"._("Remove")."'>";
596       }
598       /* Create entry display name and tooltip */
599       $color = "";
600       $display = $task['MACADDRESS'];
601       $tooltip = "";
602       if(isset($task['PLAINNAME']) && !preg_match("/none/i",$task['PLAINNAME'])){
603         $display = $task['PLAINNAME'];
604         $tooltip = " title='".$task['MACADDRESS']."' ";
605       }
608       $display2= $task['HEADERTAG'];
610       /* Check if this event exists as Daemon class 
611        * In this case, display a more accurate entry.
612        */ 
613       if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
614         $evt_name   = $this->events['QUEUED'][$task['HEADERTAG']];
615         $event_type = $this->events['BY_CLASS'][$evt_name];
616         $display2   = $event_type['s_Menu_Name'];
618         if(strlen($display2) > 20){
619           $display2 = substr($display2,0,18)."...";
620         }
622         if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
623           $display2 = $event_type['ListImage']."&nbsp;".$display2;
624         }
625       } 
627       $status = $task['STATUS'];
629       if($status == "waiting"){
630         $status = "<img class='center' src='plugins/goto/images/clock.png' alt=''>&nbsp;"._("Waiting");
631       }
632       if($status == "error"){
633         $status = "<img class='center' src='images/false.png' alt=''>&nbsp;"._("Error");
634       }
635       if($status == "processed"){
636         $status = "<img class='center' src='images/true.png' alt=''>&nbsp;"._("Processed");
637       }
639       /* Special handling for all entries that have 
640          STATUS == "processing" && PROGRESS == NUMERIC
641        */
642       if($status == "processing" && isset($task['PROGRESS'])){
643         $percent = $task['PROGRESS'];
645         /* Show activation? */
646         if ($percent == "goto-activation"){
647           $status = "<img class='center' src='images/lists/off.png' alt=''>&nbsp;"._("Locked");
649           /* Show hardware detect? */
650         } elseif ($percent == "goto-hardware-detection") {
651           $status = "<img class='center' src='plugins/goto/images/hardware.png' alt=''>&nbsp;"._("Detection");
653           /* Real percent */
654         } else {
655           if (preg_match('/install/', $task['HEADERTAG'])){
656             $status = "<img src='progress.php?x=80&y=13&p=".$task['PROGRESS']."' alt=''
657               id='progress_".preg_replace("/:/","_",$task['MACADDRESS'])."'>";
658           } else {
659             $status = preg_replace('/ /', '&nbsp;', _("in progress"));
660           }
661         }
662       }
664       // Check whether this is a periodical job or not.
665       $period = "";
666       if(isset($task['PERIODIC']) && !preg_match("/none/i",$task['PERIODIC'])){
667         $tmp = explode("_", $task['PERIODIC']);
668         if(count($tmp) == 2){
669           $period= $tmp[0]."&nbsp;"._($tmp[1]);
670         }
671       }
673       /* Create each field */
674       $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$key."'>" ,
675           "attach" => "style='width:20px;".$color."'");
676       $field1 = array("string" => $display,
677           "attach" => $tooltip."style='".$color."'");
678       $field1a= array("string" => $display2,
679           "attach" => "style='".$color.";width:120px;'");
680       $field1b= array("string" => $period,
681           "attach" => "style='".$color.";width:60px;'");
682       if ($task['TIMESTAMP'] == "19700101000000"){
683         $field2 = array("string" => _("immediately"),"attach" => "style='".$color.";width:140px;'");
684       } else {
685         $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:140px;'");
686       }
687       $field3 = array("string" => $status,"attach" => "style='".$color.";width:80px;'");
688       $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:140px;border-right:0px;'");
689       $divlist->AddElement(array($field0,$field1,$field1a,$field1b,$field2,$field3,$field4));
690    }
692    $smarty = get_smarty();
693    $smarty->assign("events",$this->events);
694    $smarty->assign("start",$this->start);
695    $smarty->assign("start_real", ($this->start + 1));
696    $smarty->assign("ranges", array("10" => "10",
697          "20" => "20",
698          "25" => "25",
699          "50" => "50",
700          "100"=> "100",
701          "200"=> "200",
702          "9999" => "*"));
704    $count = $this->o_queue->number_of_queued_entries($this->event_tags);
705    if(!$count) $count = $this->range;
706     $divlist->SetListFooter(range_selector($count, $this->start, $this->range,"range"));
707    $smarty->assign("range",$this->range);
708     $smarty->assign("div",$divlist->Draw());
710     return(management::execute());
711     return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
712   }
715   /*! \brief  Move an entry up or down in the queue, by updating its execution timestamp  
716     @param  $id     Integer  The ID of the entry which should be updated.
717     @param  $type   String   "up" / "down"
718     @return boolean TRUE in case of success else FALSE
719    */
720   public function update_priority($id,$type = "up")
721   {
722     if($type == "up"){
723       $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp DESC");
724     }else{
725       $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp ASC");
726     }
727     $last = array();
728     foreach($tmp as $entry){
729       if($entry['ID'] == $id){
730         if(count($last)){
731           $time = strtotime($last['TIMESTAMP']);
732           if($type == "up"){
733             $time ++;
734           }else{
735             $time --;
736           }
737           $time_str = date("YmdHis",$time); 
738           return($this->o_queue->update_entries(array($id),array("timestamp" => $time_str)));
739         }else{
740           return(FALSE);
741         }
742       }
743       $last = $entry;
744     }
745     return(FALSE);
746   }
749   /*! \brief  Resumes to status 'waiting'.
750    *  @return Boolean TRUE in case of success, else FALSE. 
751    */
752   private function resume_queue_entries($ids)
753   {
754     if(!count($ids)){
755       return;
756     }
758     /* Entries are resumed by setting the status to 
759      *  'waiting'
760      */
761     $data = array("status"    => "waiting");
763     /* Check if given ids are valid and check if the status
764      *  allows resuming.
765      */
766     $update_ids = array();
767     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
768       if(isset($entry['STATUS']) && preg_match("/paused/",$entry['STATUS'])){
769         $update_ids[] = $entry['ID'];
770       }
771     }
773     /* Tell the daemon that we have entries to update.
774      */
775     if(count($update_ids)){
776       if(!$this->o_queue->update_entries($update_ids,$data)){
777         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
778         return(FALSE);
779       }
780     }
781     return(TRUE);
782   }
785   /*! \brief  Force queue job to be done as far as possible.
786    *  @return Boolean TRUE in case of success, else FALSE.
787    */
788   private function execute_queue_entries($ids)
789   {
790     if(!count($ids)){
791       return;
792     }
794     /* Execution is forced by updating the status to 
795      *  waiting and setting the timestamp to current time.
796      */
797     $data = array(  "timestamp" => date("YmdHis",time()), 
798         "status"    => "waiting");
800     /* Only allow execution of paused or waiting entries 
801      */
802     $update_ids = array();
803     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
804       if(in_array($entry['STATUS'],array("paused","waiting"))){
805         $update_ids[] = $entry['ID'];
806       }
807     }
809     /* Tell the daemon that we want to update some entries
810      */
811     if(count($update_ids)){
812       if(!$this->o_queue->update_entries($update_ids,$data)){
813         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entries.")) , ERROR_DIALOG);
814         return(FALSE);
815       }
816     }
817     return(TRUE);
818   }
821   /*! \brief  Force queue job to be done as far as possible.
822    *  @return Boolean TRUE in case of success, else FALSE.
823    */
824   private function abort_queue_entries($ids)
825   {
826     if(!count($ids)){
827       return;
828     }
830     /* Entries are paused by setting the status to
831      *  something different from 'waiting'.
832      * We simply use 'paused'.
833      */
834     $data = array("status"    => "paused");
836     /* Detect if the ids we got are valid and
837      *  check if the status allows pausing.
838      */
839     $update_ids = array();
840     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
841       if(isset($entry['STATUS']) && preg_match("/processing/",$entry['STATUS'])){
842         if(isset($entry['MACADDRESS'])){
843           $update_ids[] = $entry['MACADDRESS'];
844         }else{
845           trigger_error("No mac address found in event.");
846         }
847       }
848     }
850     if(class_available("DaemonEvent_faireboot")){
851       $tmp = new DaemonEvent_faireboot($this->config);
852       $tmp->add_targets($update_ids);
853       $tmp->set_type(TRIGGERED_EVENT);
854       $this->recently_removed = $update_ids;
856       if(!$this->o_queue->append($tmp)){
857         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
858         return(FALSE);
859       }
860     }else{
861       msg_dialog::display(_("Error"),
862           sprintf(_("Required class '%s' cannot be found: job not aborted!"),
863             "DaemonEvent_faireboot") , ERROR_DIALOG);
864     }
865   }
868   /*! \brief Pauses the specified queue entry from execution.
869    *  @return Boolean TRUE in case of success, else FALSE. 
870    */
871   private function pause_queue_entries($ids)
872   {
873     if(!count($ids)){
874       return;
875     }
877     /* Entries are paused by setting the status to 
878      *  something different from 'waiting'.
879      * We simply use 'paused'.
880      */   
881     $data = array("status"    => "paused");
883     /* Detect if the ids we got are valid and
884      *  check if the status allows pausing.
885      */ 
886     $update_ids = array();
887     foreach($this->o_queue->get_entries_by_id($ids) as $entry){
888       if(isset($entry['STATUS']) && preg_match("/waiting/",$entry['STATUS'])){
889         $update_ids[] = $entry['ID'];
890       }
891     }
893     /* Tell the daemon that we want to update some entries
894      */
895     if(count($update_ids)){
896       if(!$this->o_queue->update_entries($update_ids,$data)){
897         msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
898         return(FALSE);
899       }
900     }
901     return(TRUE);
902   }
905   /*! \brief  Request list of queued jobs.
906    *  @return Returns an array of all queued jobs.
907    */
908   function reload()
909   {
911     /* Sort map   html-post-name => daemon-col-name
912      */
913     $map = array(
914         "QueuePosition" => "id",
915         "Action"        => "status",
916         "TaskID"        => "headertag",
917         "TargetName"    => "macaddress",
918         "Schedule"      => "timestamp");
920     /* Create sort header 
921      */
922     if(!isset($map[$this->sort_by])){
923       $sort = "id DESC";
924     }else{
925       $sort   = $map[$this->sort_by]; 
926       if($this->sort_dir == "up"){
927         $sort.= " ASC";
928       }else{
929         $sort.= " DESC";
930       }
931     }
933     /* Get entries. */ 
934     $start  = $this->start; 
935     $stop   = $this->range;
936     $entries = $this->o_queue->get_queued_entries($this->event_tags,$start,$stop,$sort);
937     if ($this->o_queue->is_error()){
938       msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
939     }
941     /* Assign entries by id.
942      */
943     $this->entries = array();
945     foreach($entries as $entry){
947       /* Skip entries which will be removed within the next seconds */
948       if(isset($entry['MACADDRESS']) && in_array($entry['MACADDRESS'],$this->recently_removed)){
949         continue;
950       }
951       $this->entries[$entry['ID']]= $entry;
952     }
953     $this->recently_removed = array();
954   }
956   function save_object(){}
958   /*! \brief  Handle post jobs, like sorting.
959    */
960   function _save_object()
961   {
962     /* Check for sorting changes 
963      */
964     $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
965     if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
966       $sort = $_GET['sort'];
967       if($this->sort_by == $sort){
968         if($this->sort_dir == "up"){
969           $this->sort_dir = "down";
970         }else{
971           $this->sort_dir = "up";
972         }
973       }
974       $this->sort_by = $sort;
975     }
977     /* Range selection used? */
978     if(isset($_POST['range']) && is_numeric($_POST['range'])){
979       $this->range = $_POST['range'];
980     }
982     /* Save filter settings */ 
983     $gotomasses_filter = session::get("gotomasses_filter");
984     foreach(array("range","sort_by","sort_dir") as $attr){
985       $gotomasses_filter[$attr] = $this->$attr;
986     }
987     session::set("gotomasses_filter",$gotomasses_filter);
989     /* Page changed. */
990     if(isset($_GET['start'])){
991       $start = $_GET['start'];
992       if(is_numeric($start) || $start == 0){
993         $this->start = $start;
994       }
995     }
997     /* Check start stop and reset if necessary */
998     $count = $this->o_queue->number_of_queued_entries($this->event_tags);
999     if($this->start >= $count){
1000       $this->start = $count -1;
1001     }
1002     if($this->start < 0){
1003       $this->start = 0;
1004     }
1005   }
1008   function save()
1009   {
1010     // We do not save anything here.
1011   }
1014   /*! \brief  Return a list of all selected items.
1015     @return Array   Returns an array containing all selected item ids.
1016    */
1017   function list_get_selected_items()
1018   {
1019     $ids = array();
1020     foreach($_POST as $name => $value){
1021       if(preg_match("/^item_selected_[0-9]*$/",$name)){
1022         $id   = preg_replace("/^item_selected_/","",$name);
1023         $ids[$id] = $id;
1024       }
1025     }
1026     return($ids);
1027   }
1030   static function plInfo()
1031   {
1032     return (array(
1033           "plShortName"   => _("System deployment"),
1034           "plDescription" => _("Provide a mechanism to automatically activate systems"),
1035           "plSelfModify"  => FALSE,
1036           "plDepends"     => array(),
1037           "plPriority"    => 0,
1038           "plSection"     => array("addon"),
1039           "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System deployment"))),
1040           "plProvidedAcls" => array("Comment"   => _("Description")) 
1041           ));
1042   }
1045   function set_acl_base($base)
1046   {
1047     $this->acl_base= $base;
1048   }
1051   function set_acl_category($category)
1052   {
1053     $this->acl_category= "$category/";
1054   }
1057   function acl_is_writeable($attribute,$skip_write = FALSE)
1058   {
1059     if($this->read_only) return(FALSE);
1060     $ui= get_userinfo();
1061     return preg_match('/w/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute, $skip_write));
1062   }
1065   function acl_is_readable($attribute)
1066   {
1067     $ui= get_userinfo();
1068     return preg_match('/r/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute));
1069   }
1072   function acl_is_createable($base ="")
1073   {
1074     if($this->read_only) return(FALSE);
1075     $ui= get_userinfo();
1076     if($base == "") $base = $this->acl_base;
1077     return preg_match('/c/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
1078   }
1081   function acl_is_removeable($base ="")
1082   {
1083     if($this->read_only) return(FALSE);
1084     $ui= get_userinfo();
1085     if($base == "") $base = $this->acl_base;
1086     return preg_match('/d/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
1087   }
1089   function acl_is_moveable($base = "")
1090   {
1091     if($this->read_only) return(FALSE);
1092     $ui= get_userinfo();
1093     if($base == "") $base = $this->acl_base;
1094     return preg_match('/m/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
1095   }
1098   function getacl($attribute,$skip_write= FALSE)
1099   {
1100     $ui= get_userinfo();
1101     $skip_write |= $this->read_only;
1102     return  $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute,$skip_write);
1103   }
1106 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1107 ?>