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();
84 /************
85 * Handle posts
86 ************/
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 }
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 ************/
130 if($s_action == "import_file" && $this->acl_is_writeable("")){
131 $this->dialog = new goto_import_file($this->config,$this);
132 }
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 ************/
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 }
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']." ".$task['MACADDRESS'];
255 }else{
256 $j_name = $task['ID']." - ".$task['HEADERTAG']." ".$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'> "._("Create")."\n";
368 if($this->acl_is_writeable("")){
369 foreach($this->events['SCHEDULED'] as $name => $event){
370 $s.= "...|".$event['MenuImage']." ".$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'> "._("Import")."|import_file\n";
376 $s.= "..|<img src='images/lists/trash.png' alt='' border='0' class='center'> "._("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'> "._("Resume")."|resume_all\n";
381 $s.= "..|<img alt='"._("Pause")."' src='images/status_pause.png' border='0' class='center'> "._("Pause")."|pause_all\n";
382 $s.= "..|<img alt='"._("Abort")."' src='images/small_error.png' border='0' class='center'> "._("Abort")."|abort_process_all\n";
383 $s.= "..|<img alt='"._("Execute")."' src='images/rocket.png' border='0' class='center'> "._("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."&sort=TargetName'>"._("Target").$sort_img_1."</a>"));
409 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=TaskID'>"._("Task").$sort_img_2."</a>",
410 "attach"=>"style='width:120px;'"));
411 $divlist->AddHeader(array("string"=>_("Period"),
412 "attach"=>"style='width:60px;'"));
413 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
414 "attach"=>"style='width:140px;'"));
415 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=Action'>"._("Status").$sort_img_4."</a>",
416 "attach"=>"style='width:80px;'"));
417 $divlist->AddHeader(array("string"=>_("Action"),
418 "attach"=>"style='border-right:0px;width:140px;'"));
421 /* Reload the list of entries */
422 $this->reload();
424 foreach($this->entries as $key => $task){
426 $prio_actions="";
427 $action = "";
430 /* If WAITING add priority action
431 */
432 if(in_array($task['STATUS'],array("waiting")) && $this->acl_is_writeable("")){
433 $prio_actions.= "<input class='center' type='image' src='plugins/goto/images/prio_increase.png'
434 title='"._("Move up")."' name='prio_up_".$key."'> ";
435 $prio_actions.= "<input class='center' type='image' src='plugins/goto/images/prio_decrease.png'
436 title='"._("Move down")."' name='prio_down_".$key."'> ";
437 }
439 /* If WAITING add pause action
440 */
441 if(in_array($task['STATUS'],array("waiting")) && $this->acl_is_writeable("")){
442 $prio_actions.= "<input class='center' type='image' src='images/status_pause.png'
443 title='"._("Pause job")."' name='pause_".$key."'> ";
444 }
446 /* If PAUSED add resume action
447 */
448 if(in_array($task['STATUS'],array("paused")) && $this->acl_is_writeable("")){
449 $prio_actions.= "<input class='center' type='image' src='images/status_start.png'
450 title='"._("Resume job")."' name='resume_".$key."'> ";
451 }
453 /* If PAUSED or WAITING add execution action
454 */
455 if(in_array($task['STATUS'],array("paused","waiting")) && $this->acl_is_writeable("")){
456 $prio_actions.= "<input class='center' type='image' src='images/rocket.png'
457 title='"._("Execute now")."' name='execute_process_".$key."'> ";
458 }
460 /* Add logview button, currently ever.
461 */
462 if($this->acl_is_readable("")){
463 $action .= "<input type='image' src='plugins/goto/images/view_logs.png' name='log_view_".$key."'
464 class='center' title='"._("View logs")."' alt='"._("View logs")."'> ";
465 }
467 /* If PAUSED or WAITING add edit action
468 */
469 if(in_array($task['STATUS'],array("waiting")) && $this->acl_is_writeable("")){
470 $action.= "<input type='image' src='images/lists/edit.png' name='edit_task_".$key."'
471 class='center' title='"._("Edit")."' alt='"._("Edit")."'>";
472 }
474 /* If PROCESSING add abort action
475 */
476 if(in_array($task['STATUS'],array("processing")) && preg_match("/install/",$task['HEADERTAG']) && $this->acl_is_writeable("")){
477 $action.= "<img src='images/empty.png' alt=''>";
478 $action.= "<input class='center' type='image' src='images/small_error.png'
479 title='"._("Abort job")."' name='abort_process_".$key."'>";
480 }
482 /* If WAITING or ERROR add remove action
483 */
484 if( $this->acl_is_removeable() && in_array($task['STATUS'],array("waiting","error","processed"))){
485 $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."'
486 class='center' title='"._("Remove")."' alt='"._("Remove")."'>";
487 }
488 if($this->acl_is_writeable("") && in_array($task['STATUS'],array("processing")) && !preg_match("/install/",$task['HEADERTAG'])){
489 $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."'
490 class='center' title='"._("Remove")."' alt='"._("Remove")."'>";
491 }
493 /* Create entry display name and tooltip */
494 $color = "";
495 $display = $task['MACADDRESS'];
496 $tooltip = "";
497 if(isset($task['PLAINNAME']) && !preg_match("/none/i",$task['PLAINNAME'])){
498 $display = $task['PLAINNAME'];
499 $tooltip = " title='".$task['MACADDRESS']."' ";
500 }
503 $display2= $task['HEADERTAG'];
505 /* Check if this event exists as Daemon class
506 * In this case, display a more accurate entry.
507 */
508 if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
509 $evt_name = $this->events['QUEUED'][$task['HEADERTAG']];
510 $event_type = $this->events['BY_CLASS'][$evt_name];
511 $display2 = $event_type['s_Menu_Name'];
513 if(strlen($display2) > 20){
514 $display2 = substr($display2,0,18)."...";
515 }
517 if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
518 $display2 = $event_type['ListImage']." ".$display2;
519 }
520 }
522 $status = $task['STATUS'];
524 if($status == "waiting"){
525 $status = "<img class='center' src='plugins/goto/images/clock.png' alt=''> "._("Waiting");
526 }
527 if($status == "error"){
528 $status = "<img class='center' src='images/false.png' alt=''> "._("Error");
529 }
530 if($status == "processed"){
531 $status = "<img class='center' src='images/true.png' alt=''> "._("Processed");
532 }
534 /* Special handling for all entries that have
535 STATUS == "processing" && PROGRESS == NUMERIC
536 */
537 if($status == "processing" && isset($task['PROGRESS'])){
538 $percent = $task['PROGRESS'];
540 /* Show activation? */
541 if ($percent == "goto-activation"){
542 $status = "<img class='center' src='images/lists/off.png' alt=''> "._("Locked");
544 /* Show hardware detect? */
545 } elseif ($percent == "goto-hardware-detection") {
546 $status = "<img class='center' src='plugins/goto/images/hardware.png' alt=''> "._("Detection");
548 /* Real percent */
549 } else {
550 if (preg_match('/install/', $task['HEADERTAG'])){
551 $status = "<img src='progress.php?x=80&y=13&p=".$task['PROGRESS']."' alt=''
552 id='progress_".preg_replace("/:/","_",$task['MACADDRESS'])."'>";
553 } else {
554 $status = preg_replace('/ /', ' ', _("in progress"));
555 }
556 }
557 }
559 // Check whether this is a periodical job or not.
560 $period = "";
561 if(isset($task['PERIODIC']) && !preg_match("/none/i",$task['PERIODIC'])){
562 $tmp = split("_", $task['PERIODIC']);
563 if(count($tmp) == 2){
564 $period= $tmp[0]." "._($tmp[1]);
565 }
566 }
568 /* Create each field */
569 $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$key."'>" ,
570 "attach" => "style='width:20px;".$color."'");
571 $field1 = array("string" => $display,
572 "attach" => $tooltip."style='".$color."'");
573 $field1a= array("string" => $display2,
574 "attach" => "style='".$color.";width:120px;'");
575 $field1b= array("string" => $period,
576 "attach" => "style='".$color.";width:60px;'");
577 if ($task['TIMESTAMP'] == "19700101000000"){
578 $field2 = array("string" => _("immediately"),"attach" => "style='".$color.";width:140px;'");
579 } else {
580 $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:140px;'");
581 }
582 $field3 = array("string" => $status,"attach" => "style='".$color.";width:80px;'");
583 $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:140px;border-right:0px;'");
584 $divlist->AddElement(array($field0,$field1,$field1a,$field1b,$field2,$field3,$field4));
585 }
587 $smarty = get_smarty();
588 $smarty->assign("events",$this->events);
589 $smarty->assign("start",$this->start);
590 $smarty->assign("start_real", ($this->start + 1));
591 $smarty->assign("ranges", array("10" => "10",
592 "20" => "20",
593 "25" => "25",
594 "50" => "50",
595 "100"=> "100",
596 "200"=> "200",
597 "9999" => "*"));
599 $count = $this->o_queue->number_of_queued_entries($this->event_tags);
600 if(!$count) $count = $this->range;
601 $divlist->SetListFooter(range_selector($count, $this->start, $this->range,"range"));
602 $smarty->assign("range",$this->range);
603 $smarty->assign("div",$divlist->Draw());
604 return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
605 }
608 /*! \brief Move an entry up or down in the queue, by updating its execution timestamp
609 @param $id Integer The ID of the entry which should be updated.
610 @param $type String "up" / "down"
611 @return boolean TRUE in case of success else FALSE
612 */
613 public function update_priority($id,$type = "up")
614 {
615 if($type == "up"){
616 $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp DESC");
617 }else{
618 $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp ASC");
619 }
620 $last = array();
621 foreach($tmp as $entry){
622 if($entry['ID'] == $id){
623 if(count($last)){
624 $time = strtotime($last['TIMESTAMP']);
625 if($type == "up"){
626 $time ++;
627 }else{
628 $time --;
629 }
630 $time_str = date("YmdHis",$time);
631 return($this->o_queue->update_entries(array($id),array("timestamp" => $time_str)));
632 }else{
633 return(FALSE);
634 }
635 }
636 $last = $entry;
637 }
638 return(FALSE);
639 }
642 /*! \brief Resumes to status 'waiting'.
643 * @return Boolean TRUE in case of success, else FALSE.
644 */
645 private function resume_queue_entries($ids)
646 {
647 if(!count($ids)){
648 return;
649 }
651 /* Entries are resumed by setting the status to
652 * 'waiting'
653 */
654 $data = array("status" => "waiting");
656 /* Check if given ids are valid and check if the status
657 * allows resuming.
658 */
659 $update_ids = array();
660 foreach($this->o_queue->get_entries_by_id($ids) as $entry){
661 if(isset($entry['STATUS']) && preg_match("/paused/",$entry['STATUS'])){
662 $update_ids[] = $entry['ID'];
663 }
664 }
666 /* Tell the daemon that we have entries to update.
667 */
668 if(count($update_ids)){
669 if(!$this->o_queue->update_entries($update_ids,$data)){
670 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
671 return(FALSE);
672 }
673 }
674 return(TRUE);
675 }
678 /*! \brief Force queue job to be done as far as possible.
679 * @return Boolean TRUE in case of success, else FALSE.
680 */
681 private function execute_queue_entries($ids)
682 {
683 if(!count($ids)){
684 return;
685 }
687 /* Execution is forced by updating the status to
688 * waiting and setting the timestamp to current time.
689 */
690 $data = array( "timestamp" => date("YmdHis",time()),
691 "status" => "waiting");
693 /* Only allow execution of paused or waiting entries
694 */
695 $update_ids = array();
696 foreach($this->o_queue->get_entries_by_id($ids) as $entry){
697 if(in_array($entry['STATUS'],array("paused","waiting"))){
698 $update_ids[] = $entry['ID'];
699 }
700 }
702 /* Tell the daemon that we want to update some entries
703 */
704 if(count($update_ids)){
705 if(!$this->o_queue->update_entries($update_ids,$data)){
706 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entries.")) , ERROR_DIALOG);
707 return(FALSE);
708 }
709 }
710 return(TRUE);
711 }
714 /*! \brief Force queue job to be done as far as possible.
715 * @return Boolean TRUE in case of success, else FALSE.
716 */
717 private function abort_queue_entries($ids)
718 {
719 if(!count($ids)){
720 return;
721 }
723 /* Entries are paused by setting the status to
724 * something different from 'waiting'.
725 * We simply use 'paused'.
726 */
727 $data = array("status" => "paused");
729 /* Detect if the ids we got are valid and
730 * check if the status allows pausing.
731 */
732 $update_ids = array();
733 foreach($this->o_queue->get_entries_by_id($ids) as $entry){
734 if(isset($entry['STATUS']) && preg_match("/processing/",$entry['STATUS'])){
735 if(isset($entry['MACADDRESS'])){
736 $update_ids[] = $entry['MACADDRESS'];
737 }else{
738 trigger_error("No mac address found in event.");
739 }
740 }
741 }
743 if(class_available("DaemonEvent_faireboot")){
744 $tmp = new DaemonEvent_faireboot($this->config);
745 $tmp->add_targets($update_ids);
746 $tmp->set_type(TRIGGERED_EVENT);
747 $this->recently_removed = $update_ids;
749 if(!$this->o_queue->append($tmp)){
750 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
751 return(FALSE);
752 }
753 }else{
754 msg_dialog::display(_("Error"),
755 sprintf(_("Required class '%s' cannot be found: job not aborted!"),
756 "DaemonEvent_faireboot") , ERROR_DIALOG);
757 }
758 }
761 /*! \brief Pauses the specified queue entry from execution.
762 * @return Boolean TRUE in case of success, else FALSE.
763 */
764 private function pause_queue_entries($ids)
765 {
766 if(!count($ids)){
767 return;
768 }
770 /* Entries are paused by setting the status to
771 * something different from 'waiting'.
772 * We simply use 'paused'.
773 */
774 $data = array("status" => "paused");
776 /* Detect if the ids we got are valid and
777 * check if the status allows pausing.
778 */
779 $update_ids = array();
780 foreach($this->o_queue->get_entries_by_id($ids) as $entry){
781 if(isset($entry['STATUS']) && preg_match("/waiting/",$entry['STATUS'])){
782 $update_ids[] = $entry['ID'];
783 }
784 }
786 /* Tell the daemon that we want to update some entries
787 */
788 if(count($update_ids)){
789 if(!$this->o_queue->update_entries($update_ids,$data)){
790 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
791 return(FALSE);
792 }
793 }
794 return(TRUE);
795 }
798 /*! \brief Request list of queued jobs.
799 * @return Returns an array of all queued jobs.
800 */
801 function reload()
802 {
804 /* Sort map html-post-name => daemon-col-name
805 */
806 $map = array(
807 "QueuePosition" => "id",
808 "Action" => "status",
809 "TaskID" => "headertag",
810 "TargetName" => "macaddress",
811 "Schedule" => "timestamp");
813 /* Create sort header
814 */
815 if(!isset($map[$this->sort_by])){
816 $sort = "id DESC";
817 }else{
818 $sort = $map[$this->sort_by];
819 if($this->sort_dir == "up"){
820 $sort.= " ASC";
821 }else{
822 $sort.= " DESC";
823 }
824 }
826 /* Get entries. */
827 $start = $this->start;
828 $stop = $this->range;
829 $entries = $this->o_queue->get_queued_entries($this->event_tags,$start,$stop,$sort);
830 if ($this->o_queue->is_error()){
831 msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
832 }
834 /* Assign entries by id.
835 */
836 $this->entries = array();
838 foreach($entries as $entry){
840 /* Skip entries which will be removed within the next seconds */
841 if(isset($entry['MACADDRESS']) && in_array($entry['MACADDRESS'],$this->recently_removed)){
842 continue;
843 }
844 $this->entries[$entry['ID']]= $entry;
845 }
846 $this->recently_removed = array();
847 }
850 /*! \brief Handle post jobs, like sorting.
851 */
852 function save_object()
853 {
854 /* Check for sorting changes
855 */
856 $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
857 if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
858 $sort = $_GET['sort'];
859 if($this->sort_by == $sort){
860 if($this->sort_dir == "up"){
861 $this->sort_dir = "down";
862 }else{
863 $this->sort_dir = "up";
864 }
865 }
866 $this->sort_by = $sort;
867 }
869 /* Range selection used? */
870 if(isset($_POST['range']) && is_numeric($_POST['range'])){
871 $this->range = $_POST['range'];
872 }
874 /* Save filter settings */
875 $gotomasses_filter = session::get("gotomasses_filter");
876 foreach(array("range","sort_by","sort_dir") as $attr){
877 $gotomasses_filter[$attr] = $this->$attr;
878 }
879 session::set("gotomasses_filter",$gotomasses_filter);
881 /* Page changed. */
882 if(isset($_GET['start'])){
883 $start = $_GET['start'];
884 if(is_numeric($start) || $start == 0){
885 $this->start = $start;
886 }
887 }
889 /* Check start stop and reset if necessary */
890 $count = $this->o_queue->number_of_queued_entries($this->event_tags);
891 if($this->start >= $count){
892 $this->start = $count -1;
893 }
894 if($this->start < 0){
895 $this->start = 0;
896 }
897 }
900 function save()
901 {
902 // We do not save anything here.
903 }
906 /*! \brief Return a list of all selected items.
907 @return Array Returns an array containing all selected item ids.
908 */
909 function list_get_selected_items()
910 {
911 $ids = array();
912 foreach($_POST as $name => $value){
913 if(preg_match("/^item_selected_[0-9]*$/",$name)){
914 $id = preg_replace("/^item_selected_/","",$name);
915 $ids[$id] = $id;
916 }
917 }
918 return($ids);
919 }
922 static function plInfo()
923 {
924 return (array(
925 "plShortName" => _("System deployment"),
926 "plDescription" => _("Provide a mechanism to automatically activate systems"),
927 "plSelfModify" => FALSE,
928 "plDepends" => array(),
929 "plPriority" => 0,
930 "plSection" => array("addon"),
931 "plCategory" => array("gotomasses" => array("objectClass" => "none", "description" => _("System deployment"))),
932 "plProvidedAcls" => array("Comment" => _("Description"))
933 ));
934 }
935 }
936 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
937 ?>