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