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");;
105 foreach($arr as $regex => $action){
106 foreach($_POST as $name => $value){
107 if(preg_match($regex,$name)){
108 $s_action = $action;
109 $s_entry = preg_replace($regex,"",$name);
110 $s_entry = preg_replace("/_(x|y)$/","",$s_entry);
111 }
112 }
113 }
115 /* Menu actions */
116 if(isset($_POST['menu_action']) && !empty($_POST['menu_action'])){
117 $s_action = $_POST['menu_action'];
118 }
120 /* Edit posted from list link */
121 if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
122 $s_action = "edit";
123 $s_entry = $_GET['id'];
124 }
127 /************
128 * Import CSV file
129 ************/
131 if($s_action == "import_file"){
132 $this->dialog = new goto_import_file($this->config,$this);
133 }
135 if(isset($_POST['import_abort'])){
136 $this->dialog = FALSE;
137 }
140 /************
141 * Handle Priority modifications
142 ************/
144 if(preg_match("/^prio_/",$s_action)){
145 switch($s_action){
146 case 'prio_down' : $this->update_priority($s_entry,"down");break;
147 case 'prio_up' : $this->update_priority($s_entry,"up");break;
148 }
149 }
151 /************
152 * Handle pause/resume/execute modifications
153 ************/
155 if(preg_match("/^resume/",$s_action) ||
156 preg_match("/^pause/",$s_action) ||
157 preg_match("/^abort_process/",$s_action) ||
158 preg_match("/^execute_process/",$s_action)){
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 if($this->o_queue->is_error()){
173 msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
174 }
175 }
177 /************
178 * ADD
179 ************/
181 if(preg_match("/^add_event_/",$s_action)){
182 $type = preg_replace("/^add_event_/","",$s_action);
183 if(isset($this->events['BY_CLASS'][$type])){
184 $e_data = $this->events['BY_CLASS'][$type];
185 $this->dialog = new $e_data['CLASS_NAME']($this->config);
186 }
187 }
189 /************
190 * EDIT
191 ************/
193 if($s_action == "edit"){
194 $id = $s_entry;
195 $type = FALSE;
196 if(isset($this->entries[$id])){
197 $event = $this->entries[$s_entry];
198 if($event['STATUS'] == "waiting" && isset($this->events['QUEUED'][$event['HEADERTAG']])){
199 $evt_name = $this->events['QUEUED'][$event['HEADERTAG']];
200 $type = $this->events['BY_CLASS'][$evt_name];
201 $this->dialog = new $type['CLASS_NAME']($this->config,$event);
202 }
203 }
204 }
207 /************
208 * LOG VIEW
209 ************/
211 if($s_action == "logview"){
212 $id = $s_entry;
213 $type = FALSE;
214 if(isset($this->entries[$id])){
215 $event = $this->entries[$s_entry];
216 $this->dialog = new goto_log_view($this->config,"",$event,$this);
217 }
218 }
221 /************
222 * REMOVE
223 ************/
225 /* Remove multiple */
226 if($s_action == "remove_multiple" || $s_action == "remove"){
228 if(!$this->acl_is_removeable()){
229 msg_dialog::display(_("Permission"), msgPool::permDelete(), ERROR_DIALOG);
230 }else{
232 if($s_action == "remove"){
233 $ids = array($s_entry);
234 }else{
235 $ids = $this->list_get_selected_items();
236 }
238 $this->ids_to_remove = array();
240 if(count($ids)){
241 $ret = $this->o_queue->ids_exist($ids);
242 $ret = $this->o_queue->get_entries_by_id($ret);
243 $tmp = "";
245 $deleteable_jobs = array();
246 $not_deleteable_jobs = array();
247 foreach($ret as $task){
249 /* Create a printable job name/description */
250 if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
251 $evt_name = $this->events['QUEUED'][$task['HEADERTAG']];
252 $evt = $this->events['BY_CLASS'][$evt_name];
253 $j_name = $task['ID']." - ".$evt['s_Menu_Name']." ".$task['MACADDRESS'];
254 }else{
255 $j_name = $task['ID']." - ".$task['HEADERTAG']." ".$task['MACADDRESS'];
256 }
258 /* Only remove WAITING or ERROR entries */
259 if(in_array($task['STATUS'],array("waiting","error","processed")) ||
260 ($task['STATUS'] == "processing" && !preg_match("/install/",$task['HEADERTAG'])) ){
261 $this->ids_to_remove[] = $task['ID'];
262 $deleteable_jobs[] = $j_name;
263 }else{
264 $not_deleteable_jobs[] = $j_name;
265 }
266 }
267 if(count($not_deleteable_jobs)){
268 msg_dialog::display(_("Remove"),
269 sprintf(_("The following jobs couldn't be deleted, they have to be aborted: %s"),
270 "<br>".msgPool::buildList($not_deleteable_jobs)),INFO_DIALOG);
271 }
273 if(count($this->ids_to_remove)){
274 $smarty->assign("multiple", TRUE);
275 $smarty->assign("info",msgPool::deleteInfo($deleteable_jobs));
276 $this->current = $s_entry;
277 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
278 }
279 }
280 }
281 }
283 /* Remove specified tasks */
284 if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
286 /* Reboot hosts with not yet startet installations and timestamps in the past
287 */
288 timezone::get_default_timezone();
289 foreach($this->ids_to_remove as $id){
290 $entry = $this->o_queue->get_entries_by_id(array($id));
291 if(isset($entry['ANSWER1'])){
292 $entry = $entry['ANSWER1'];
293 if( $entry['STATUS'] == "waiting" &&
294 $entry['HEADERTAG'] == "trigger_action_reinstall"){
295 $evt = new DaemonEvent_reinstall($this->config,$entry);
296 if($evt->get_timestamp(FALSE) < time()){
297 $r_evt = new DaemonEvent_localboot($this->config);
298 $r_evt->add_targets(array($entry['MACADDRESS']));
299 $r_evt->set_type(TRIGGERED_EVENT);
300 $this->o_queue->append($r_evt);
301 }
302 }
303 }
304 }
306 $this->o_queue->remove_entries($this->ids_to_remove);
307 $this->save();
308 }
310 /* Remove aborted */
311 if(isset($_POST['delete_cancel'])){
312 $this->ids_to_remove = array();;
313 }
316 /************
317 * EDIT
318 ************/
320 /* Close dialog */
321 if(isset($_POST['save_event_dialog'])){
322 if(is_object($this->dialog)){
323 $this->dialog->save_object();
324 if(!$this->o_queue->append($this->dialog)){
325 msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->o_queue->get_error()),ERROR_DIALOG);
326 }else{
327 $this->dialog = FALSE;
328 $this->current = -1;
329 }
330 }
331 }
334 /* Close dialog */
335 if(isset($_POST['abort_event_dialog'])){
336 $this->dialog = FALSE;
337 $this->current = -1;
338 }
340 /* Display dialogs if currently opened */
341 if(is_object($this->dialog)){
342 $this->dialog->save_object();
343 return($this->dialog->execute());
344 }
346 /************
347 * Handle Divlist
348 ************/
350 $divlist = new MultiSelectWindow($this->config,"gotoMasses",array("gotomasses"));
351 $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa tasks."));
352 $divlist->SetSummary(_("List of queued jobs"));
353 $divlist->EnableCloseButton(FALSE);
354 $divlist->EnableSaveButton(FALSE);
355 $divlist->SetHeadpageMode();
356 $s = ".|"._("Actions")."|\n";
357 $s.= "..|<img src='images/lists/new.png' alt='' border='0' class='center'> "._("Create")."\n";
359 foreach($this->events['SCHEDULED'] as $name => $event){
360 $s.= "...|".$event['MenuImage']." ".$event['s_Menu_Name']."|add_event_".$name."\n";
361 }
362 if($this->acl_is_removeable()){
363 $s.= "..|---|\n";
364 $s.= "..|<img src='images/lists/import.png' alt='' border='0' class='center'> "._("Import")."|import_file\n";
365 $s.= "..|<img src='images/lists/trash.png' alt='' border='0' class='center'> "._("Remove")."|remove_multiple\n";
366 }
367 if(preg_match("/w/",$this->getacl(""))){
368 $s.= "..|---|\n";
369 $s.= "..|<img alt='"._("Resume")."' src='images/status_start.png' border='0' class='center'> "._("Resume")."|resume_all\n";
370 $s.= "..|<img alt='"._("Pause")."' src='images/status_pause.png' border='0' class='center'> "._("Pause")."|pause_all\n";
371 $s.= "..|<img alt='"._("Abort")."' src='images/small_error.png' border='0' class='center'> "._("Abort")."|abort_process_all\n";
372 $s.= "..|<img alt='"._("Execute")."' src='images/rocket.png' border='0' class='center'> "._("Execute")."|execute_process_all\n";
373 }
375 $divlist->SetDropDownHeaderMenu($s);
377 if($this->sort_dir == "up"){
378 $sort_img = "<img src='images/lists/sort-up.png' alt='/\' border=0>";
379 }else{
380 $sort_img = "<img src='images/lists/sort-down.png' alt='\/' border=0>";
381 }
383 if($this->sort_by == "TargetName"){ $sort_img_1 = $sort_img; } else { $sort_img_1 = "" ;}
384 if($this->sort_by == "TaskID"){ $sort_img_2 = $sort_img; } else { $sort_img_2 = "" ;}
385 if($this->sort_by == "Schedule"){ $sort_img_3 = $sort_img; } else { $sort_img_3 = "" ;}
386 if($this->sort_by == "Action"){ $sort_img_4 = $sort_img; } else { $sort_img_4 = "" ;}
388 /* Create divlist */
389 $divlist->SetListHeader("<input type='image' src='images/lists/reload.png' title='"._("Reload")."'>");
391 $plug = $_GET['plug'];
392 $chk = "<input type='checkbox' id='select_all' name='select_all'
393 onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
395 /* set Page header */
396 $divlist->AddHeader(array("string"=> $chk, "attach"=>"style='width:20px;'"));
397 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=TargetName'>"._("Target").$sort_img_1."</a>"));
398 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=TaskID'>"._("Task").$sort_img_2."</a>",
399 "attach"=>"style='width:120px;'"));
400 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
401 "attach"=>"style='width:140px;'"));
402 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=Action'>"._("Status").$sort_img_4."</a>",
403 "attach"=>"style='width:80px;'"));
404 $divlist->AddHeader(array("string"=>_("Action"),
405 "attach"=>"style='border-right:0px;width:140px;'"));
408 /* Reload the list of entries */
409 $this->reload();
411 foreach($this->entries as $key => $task){
413 $prio_actions="";
414 $action = "";
417 /* If WAITING add priority action
418 */
419 if(in_array($task['STATUS'],array("waiting"))){
420 $prio_actions.= "<input class='center' type='image' src='plugins/goto/images/prio_increase.png'
421 title='"._("Move up")."' name='prio_up_".$key."'> ";
422 $prio_actions.= "<input class='center' type='image' src='plugins/goto/images/prio_decrease.png'
423 title='"._("Move down")."' name='prio_down_".$key."'> ";
424 }
426 /* If WAITING add pause action
427 */
428 if(in_array($task['STATUS'],array("waiting"))){
429 $prio_actions.= "<input class='center' type='image' src='images/status_pause.png'
430 title='"._("Pause job")."' name='pause_".$key."'> ";
431 }
433 /* If PAUSED add resume action
434 */
435 if(in_array($task['STATUS'],array("paused"))){
436 $prio_actions.= "<input class='center' type='image' src='images/status_start.png'
437 title='"._("Resume job")."' name='resume_".$key."'> ";
438 }
440 /* If PAUSED or WAITING add execution action
441 */
442 if(in_array($task['STATUS'],array("paused","waiting"))){
443 $prio_actions.= "<input class='center' type='image' src='images/rocket.png'
444 title='"._("Execute now")."' name='execute_process_".$key."'> ";
445 }
447 /* Add logview button, currently ever.
448 */
449 if(TRUE){
450 $action .= "<input type='image' src='plugins/goto/images/view_logs.png' name='log_view_".$key."'
451 class='center' title='"._("View logs")."' alt='"._("View logs")."'> ";
452 }
454 /* If PAUSED or WAITING add edit action
455 */
456 if(in_array($task['STATUS'],array("waiting"))){
457 $action.= "<input type='image' src='images/lists/edit.png' name='edit_task_".$key."'
458 class='center' title='"._("Edit")."' alt='"._("Edit")."'>";
459 }
461 /* If PROCESSING add abort action
462 */
463 if(in_array($task['STATUS'],array("processing")) && preg_match("/install/",$task['HEADERTAG'])){
464 $action.= "<img src='images/empty.png' alt=''>";
465 $action.= "<input class='center' type='image' src='images/small_error.png'
466 title='"._("Abort job")."' name='abort_process_".$key."'>";
467 }
469 /* If WAITING or ERROR add remove action
470 */
471 if( $this->acl_is_removeable() && in_array($task['STATUS'],array("waiting","error","processed"))){
472 $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."'
473 class='center' title='"._("Remove")."' alt='"._("Remove")."'>";
474 }
475 if(in_array($task['STATUS'],array("processing")) && !preg_match("/install/",$task['HEADERTAG'])){
476 $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."'
477 class='center' title='"._("Remove")."' alt='"._("Remove")."'>";
478 }
480 /* Create entry display name and tooltip */
481 $color = "";
482 $display = $task['MACADDRESS'];
483 $tooltip = "";
484 if(isset($task['PLAINNAME']) && !preg_match("/none/i",$task['PLAINNAME'])){
485 $display = $task['PLAINNAME'];
486 $tooltip = " title='".$task['MACADDRESS']."' ";
487 }
488 $display2= $task['HEADERTAG'];
490 /* Check if this event exists as Daemon class
491 * In this case, display a more accurate entry.
492 */
493 if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
494 $evt_name = $this->events['QUEUED'][$task['HEADERTAG']];
495 $event_type = $this->events['BY_CLASS'][$evt_name];
496 $display2 = $event_type['s_Menu_Name'];
498 if(strlen($display2) > 20){
499 $display2 = substr($display2,0,18)."...";
500 }
502 if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
503 $display2 = $event_type['ListImage']." ".$display2;
504 }
505 }
507 $status = $task['STATUS'];
509 if($status == "waiting"){
510 $status = "<img class='center' src='plugins/goto/images/clock.png' alt=''> "._("Waiting");
511 }
512 if($status == "error"){
513 $status = "<img class='center' src='images/false.png' alt=''> "._("Error");
514 }
515 if($status == "processed"){
516 $status = "<img class='center' src='images/true.png' alt=''> "._("Processed");
517 }
519 /* Special handling for all entries that have
520 STATUS == "processing" && PROGRESS == NUMERIC
521 */
522 if($status == "processing" && isset($task['PROGRESS'])){
523 $percent = $task['PROGRESS'];
525 /* Show activation? */
526 if ($percent == "goto-activation"){
527 $status = "<img class='center' src='images/lists/off.png' alt=''> "._("Locked");
529 /* Show hardware detect? */
530 } elseif ($percent == "goto-hardware-detection") {
531 $status = "<img class='center' src='plugins/goto/images/hardware.png' alt=''> "._("Detection");
533 /* Real percent */
534 } else {
535 if (preg_match('/install/', $task['HEADERTAG'])){
536 $status = "<img src='progress.php?x=80&y=13&p=".$task['PROGRESS']."' alt=''
537 id='progress_".preg_replace("/:/","_",$task['MACADDRESS'])."'>";
538 } else {
539 $status = preg_replace('/ /', ' ', _("in progress"));
540 }
541 }
542 }
544 /* Create each field */
545 $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$key."'>" ,
546 "attach" => "style='width:20px;".$color."'");
547 $field1 = array("string" => $display,
548 "attach" => $tooltip."style='".$color."'");
549 $field1a= array("string" => $display2,
550 "attach" => "style='".$color.";width:120px;'");
551 $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:140px;'");
552 $field3 = array("string" => $status,"attach" => "style='".$color.";width:80px;'");
553 $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:140px;border-right:0px;'");
554 $divlist->AddElement(array($field0,$field1,$field1a,$field2,$field3,$field4));
555 }
557 $smarty = get_smarty();
558 $smarty->assign("events",$this->events);
559 $smarty->assign("start",$this->start);
560 $smarty->assign("start_real", ($this->start + 1));
561 $smarty->assign("ranges", array("10" => "10",
562 "20" => "20",
563 "25" => "25",
564 "50" => "50",
565 "100"=> "100",
566 "200"=> "200",
567 "9999" => "*"));
569 $count = $this->o_queue->number_of_queued_entries($this->event_tags);
570 if(!$count) $count = $this->range;
571 $divlist->SetListFooter(range_selector($count, $this->start, $this->range,"range"));
572 $smarty->assign("range",$this->range);
573 $smarty->assign("div",$divlist->Draw());
574 return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
575 }
578 /*! \brief Move an entry up or down in the queue, by updating its execution timestamp
579 @param $id Integer The ID of the entry which should be updated.
580 @param $type String "up" / "down"
581 @return boolean TRUE in case of success else FALSE
582 */
583 public function update_priority($id,$type = "up")
584 {
585 if($type == "up"){
586 $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp DESC");
587 }else{
588 $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp ASC");
589 }
590 $last = array();
591 foreach($tmp as $entry){
592 if($entry['ID'] == $id){
593 if(count($last)){
594 $time = strtotime($last['TIMESTAMP']);
595 if($type == "up"){
596 $time ++;
597 }else{
598 $time --;
599 }
600 $time_str = date("YmdHis",$time);
601 return($this->o_queue->update_entries(array($id),array("timestamp" => $time_str)));
602 }else{
603 return(FALSE);
604 }
605 }
606 $last = $entry;
607 }
608 return(FALSE);
609 }
612 /*! \brief Resumes to status 'waiting'.
613 * @return Boolean TRUE in case of success, else FALSE.
614 */
615 private function resume_queue_entries($ids)
616 {
617 if(!count($ids)){
618 return;
619 }
621 /* Entries are resumed by setting the status to
622 * 'waiting'
623 */
624 $data = array("status" => "waiting");
626 /* Check if given ids are valid and check if the status
627 * allows resuming.
628 */
629 $update_ids = array();
630 foreach($this->o_queue->get_entries_by_id($ids) as $entry){
631 if(isset($entry['STATUS']) && preg_match("/paused/",$entry['STATUS'])){
632 $update_ids[] = $entry['ID'];
633 }
634 }
636 /* Tell the daemon that we have entries to update.
637 */
638 if(count($update_ids)){
639 if(!$this->o_queue->update_entries($update_ids,$data)){
640 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
641 return(FALSE);
642 }
643 }
644 return(TRUE);
645 }
648 /*! \brief Force queue job to be done as far as possible.
649 * @return Boolean TRUE in case of success, else FALSE.
650 */
651 private function execute_queue_entries($ids)
652 {
653 if(!count($ids)){
654 return;
655 }
657 /* Execution is forced by updating the status to
658 * waiting and setting the timestamp to current time.
659 */
660 $data = array( "timestamp" => date("YmdHis",time()),
661 "status" => "waiting");
663 /* Only allow execution of paused or waiting entries
664 */
665 $update_ids = array();
666 foreach($this->o_queue->get_entries_by_id($ids) as $entry){
667 if(in_array($entry['STATUS'],array("paused","waiting"))){
668 $update_ids[] = $entry['ID'];
669 }
670 }
672 /* Tell the daemon that we want to update some entries
673 */
674 if(count($update_ids)){
675 if(!$this->o_queue->update_entries($update_ids,$data)){
676 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entries.")) , ERROR_DIALOG);
677 return(FALSE);
678 }
679 }
680 return(TRUE);
681 }
684 /*! \brief Force queue job to be done as far as possible.
685 * @return Boolean TRUE in case of success, else FALSE.
686 */
687 private function abort_queue_entries($ids)
688 {
689 if(!count($ids)){
690 return;
691 }
693 /* Entries are paused by setting the status to
694 * something different from 'waiting'.
695 * We simply use 'paused'.
696 */
697 $data = array("status" => "paused");
699 /* Detect if the ids we got are valid and
700 * check if the status allows pausing.
701 */
702 $update_ids = array();
703 foreach($this->o_queue->get_entries_by_id($ids) as $entry){
704 if(isset($entry['STATUS']) && preg_match("/processing/",$entry['STATUS'])){
705 if(isset($entry['MACADDRESS'])){
706 $update_ids[] = $entry['MACADDRESS'];
707 }else{
708 trigger_error("No mac address found in event.");
709 }
710 }
711 }
713 if(class_available("DaemonEvent_faireboot")){
714 $tmp = new DaemonEvent_faireboot($this->config);
715 $tmp->add_targets($update_ids);
716 $tmp->set_type(TRIGGERED_EVENT);
717 $this->recently_removed = $update_ids;
719 if(!$this->o_queue->append($tmp)){
720 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
721 return(FALSE);
722 }
723 }else{
724 msg_dialog::display(_("Error"),
725 sprintf(_("The job could not be aborted, the required class '%s' was not found."),
726 "DaemonEvent_faireboot") , ERROR_DIALOG);
727 }
728 }
731 /*! \brief Pauses the specified queue entry from execution.
732 * @return Boolean TRUE in case of success, else FALSE.
733 */
734 private function pause_queue_entries($ids)
735 {
736 if(!count($ids)){
737 return;
738 }
740 /* Entries are paused by setting the status to
741 * something different from 'waiting'.
742 * We simply use 'paused'.
743 */
744 $data = array("status" => "paused");
746 /* Detect if the ids we got are valid and
747 * check if the status allows pausing.
748 */
749 $update_ids = array();
750 foreach($this->o_queue->get_entries_by_id($ids) as $entry){
751 if(isset($entry['STATUS']) && preg_match("/waiting/",$entry['STATUS'])){
752 $update_ids[] = $entry['ID'];
753 }
754 }
756 /* Tell the daemon that we want to update some entries
757 */
758 if(count($update_ids)){
759 if(!$this->o_queue->update_entries($update_ids,$data)){
760 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
761 return(FALSE);
762 }
763 }
764 return(TRUE);
765 }
768 /*! \brief Request list of queued jobs.
769 * @return Returns an array of all queued jobs.
770 */
771 function reload()
772 {
774 /* Sort map html-post-name => daemon-col-name
775 */
776 $map = array(
777 "QueuePosition" => "id",
778 "Action" => "status",
779 "TaskID" => "headertag",
780 "TargetName" => "macaddress",
781 "Schedule" => "timestamp");
783 /* Create sort header
784 */
785 if(!isset($map[$this->sort_by])){
786 $sort = "id DESC";
787 }else{
788 $sort = $map[$this->sort_by];
789 if($this->sort_dir == "up"){
790 $sort.= " ASC";
791 }else{
792 $sort.= " DESC";
793 }
794 }
796 /* Get entries. */
797 $start = $this->start;
798 $stop = $this->range;
799 $entries = $this->o_queue->get_queued_entries($this->event_tags,$start,$stop,$sort);
800 if ($this->o_queue->is_error()){
801 msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
802 }
804 /* Assign entries by id.
805 */
806 $this->entries = array();
808 foreach($entries as $entry){
810 /* Skip entries which will be removed within the next seconds */
811 if(isset($entry['MACADDRESS']) && in_array($entry['MACADDRESS'],$this->recently_removed)){
812 continue;
813 }
814 $this->entries[$entry['ID']]= $entry;
815 }
816 $this->recently_removed = array();
817 }
820 /*! \brief Handle post jobs, like sorting.
821 */
822 function save_object()
823 {
824 /* Check for sorting changes
825 */
826 $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
827 if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
828 $sort = $_GET['sort'];
829 if($this->sort_by == $sort){
830 if($this->sort_dir == "up"){
831 $this->sort_dir = "down";
832 }else{
833 $this->sort_dir = "up";
834 }
835 }
836 $this->sort_by = $sort;
837 }
839 /* Range selection used? */
840 if(isset($_POST['range']) && is_numeric($_POST['range'])){
841 $this->range = $_POST['range'];
842 }
844 /* Save filter settings */
845 $gotomasses_filter = session::get("gotomasses_filter");
846 foreach(array("range","sort_by","sort_dir") as $attr){
847 $gotomasses_filter[$attr] = $this->$attr;
848 }
849 session::set("gotomasses_filter",$gotomasses_filter);
851 /* Page changed. */
852 if(isset($_GET['start'])){
853 $start = $_GET['start'];
854 if(is_numeric($start) || $start == 0){
855 $this->start = $start;
856 }
857 }
859 /* Check start stop and reset if necessary */
860 $count = $this->o_queue->number_of_queued_entries($this->event_tags);
861 if($this->start >= $count){
862 $this->start = $count -1;
863 }
864 if($this->start < 0){
865 $this->start = 0;
866 }
867 }
870 function save()
871 {
872 // We do not save anything here.
873 }
876 /*! \brief Return a list of all selected items.
877 @return Array Returns an array containing all selected item ids.
878 */
879 function list_get_selected_items()
880 {
881 $ids = array();
882 foreach($_POST as $name => $value){
883 if(preg_match("/^item_selected_[0-9]*$/",$name)){
884 $id = preg_replace("/^item_selected_/","",$name);
885 $ids[$id] = $id;
886 }
887 }
888 return($ids);
889 }
892 static function plInfo()
893 {
894 return (array(
895 "plShortName" => _("System mass deployment"),
896 "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
897 "plSelfModify" => FALSE,
898 "plDepends" => array(),
899 "plPriority" => 0,
900 "plSection" => array("addon"),
901 "plCategory" => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
902 "plProvidedAcls" => array("Comment" => _("Description"))
903 ));
904 }
905 }
906 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
907 ?>