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 var $read_only = FALSE;
39 var $acl_base;
40 var $acl_category;
42 function __construct(&$config, $ui)
43 {
44 /* Include config object */
45 $this->config= &$config;
46 $this->o_queue = new gosaSupportDaemon(TRUE,5);
47 $this->events = DaemonEvent::get_event_types( SYSTEM_EVENT);
48 $this->acl_base = $config->current['BASE'];
49 $this->acl_category = "gotomasses/";
51 /* Get tags that will be used in queue searches */
52 $this->event_tags = array("none");
53 foreach($this->events['SCHEDULED'] as $evt){
54 $this->event_tags[] = $evt['s_Queued_Action'];
55 }
57 // Build filter
58 # if (session::global_is_set(get_class($this)."_filter")){
59 # $filter= session::global_get(get_class($this)."_filter");
60 # } else {
61 $filter = new filter(get_template_path("deploy-filter.xml", true));
62 $filter->setObjectStorage($this->storagePoints);
63 # }
64 $this->setFilter($filter);
66 // Build headpage
67 $headpage = new listing(get_template_path("deploy-list.xml", true));
68 $headpage->registerElementFilter("hostName", "gotomasses::filterHostName");
69 $headpage->registerElementFilter("filterTask","gotomasses::filterTask");
70 $headpage->registerElementFilter("filterPeriod","gotomasses::filterPeriod");
71 $headpage->registerElementFilter("filterSchedule","gotomasses::filterSchedule");
72 $headpage->registerElementFilter("filterStatus","gotomasses::filterStatus");
73 $headpage->setFilter($filter);
75 parent::__construct($config, $ui, "Events", $headpage);
77 $this->registerAction('prioDown', "prioDown");
78 $this->registerAction('prioUp', "prioUp");
79 $this->registerAction('prioPause', "prioPause");
80 $this->registerAction('prioResume', "prioResume");
81 $this->registerAction('processNow', "processNow");
82 $this->registerAction('viewLogs', "viewLogs");
83 $this->registerAction('abort', "abortEvent");
84 $this->registerAction('saveEventDialog', "saveEventDialog");
85 $this->registerAction('halt', 'newEntry');
86 $this->registerAction('reboot', 'newEntry');
87 $this->registerAction('wakeup', 'newEntry');
88 $this->registerAction('update', 'newEntry');
89 $this->registerAction('lock', 'newEntry');
90 $this->registerAction('activate', 'newEntry');
91 $this->registerAction('reinstall', 'newEntry');
92 $this->registerAction('import', 'importEvents');
93 }
95 function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
96 {
97 if($this->acl_is_writeable("")){
98 $type = "DaemonEvent_".$action;
99 if(isset($this->events['BY_CLASS'][$type])){
100 $e_data = $this->events['BY_CLASS'][$type];
101 $this->dialogObject = new $e_data['CLASS_NAME']($this->config);
102 }
103 }
104 }
106 function importEvents()
107 {
108 $this->dialogObject = new goto_import_file($this->config,$this);
109 }
111 static function filterHostName($mac, $name ="")
112 {
113 if(isset($name[0]) && $name[0] != "none"){
114 return($name[0]);
115 }
116 return($mac[0]);
117 }
119 static function filterTask($tag)
120 {
121 $tag = $tag[0];
122 $str = $tag;
124 /* Check if this event exists as Daemon class
125 * In this case, display a more accurate entry.
126 */
127 $events = DaemonEvent::get_event_types( SYSTEM_EVENT);
128 if(isset($events['QUEUED'][$tag])){
129 $evt_name = $events['QUEUED'][$tag];
130 $event_type = $events['BY_CLASS'][$evt_name];
131 $str = $event_type['s_Menu_Name'];
133 if(strlen($str) > 20){
134 $str = substr($str,0,18)."...";
135 }
137 if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
138 $str = $event_type['ListImage']." ".$str;
139 }
140 }
141 return($str);
142 }
144 static function filterPeriod($periodic=array())
145 {
146 $period = " -";
147 if(isset($periodic[0]) && !preg_match("/none/i",$periodic[0])){
148 $tmp = explode("_", $periodic[0]);
149 if(count($tmp) == 2){
150 $period= $tmp[0]." "._($tmp[1]);
151 }
152 }
153 return($period);
154 }
156 static function filterSchedule($stamp)
157 {
158 if ($stamp['0'] == "19700101000000"){
159 return(_("immediately"));
160 } else {
161 return(date("d.m.Y H:i:s",strtotime($stamp[0])));
162 }
163 }
166 static function filterStatus($status, $mac,$headertag, $progress)
167 {
169 $mac = $mac[0];
170 $status = $status[0];
171 $progress = $progress[0];
172 $headertag = $headertag[0];
174 if($status == "waiting"){
175 $status = "<img class='center' src='plugins/goto/images/clock.png' alt=''> "._("Waiting");
176 }
177 if($status == "error"){
178 $status = "<img class='center' src='images/false.png' alt=''> "._("Error");
179 }
180 if($status == "processed"){
181 $status = "<img class='center' src='images/true.png' alt=''> "._("Processed");
182 }
184 /* Special handling for all entries that have
185 STATUS == "processing" && PROGRESS == NUMERIC
186 */
187 if($status == "processing" && $progress){
188 $percent = $progress;
190 /* Show activation? */
191 if ($percent == "goto-activation"){
192 $status = "<img class='center' src='images/lists/off.png' alt=''> "._("Locked");
194 /* Show hardware detect? */
195 } elseif ($percent == "goto-hardware-detection") {
196 $status = "<img class='center' src='plugins/goto/images/hardware.png' alt=''> "._("Detection");
198 /* Real percent */
199 } else {
200 if (preg_match('/install/', $headertag)){
201 $status = "<img src='progress.php?x=80&y=13&p=".$progress."' alt=''
202 id='progress_".preg_replace("/:/","_",$mac)."'>";
203 } else {
204 $status = preg_replace('/ /', ' ', _("in progress"));
205 }
206 }
207 }
208 return($status);
209 }
211 function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
212 {
213 if(count($target) == 1){
214 $headpage = $this->getHeadpage();
215 $entry = $headpage->getEntry($target[0]);
216 $event = $entry['EVENT'];
217 if($event['STATUS'] == "waiting" && isset($this->events['QUEUED'][$event['HEADERTAG']])){
218 $evt_name = $this->events['QUEUED'][$event['HEADERTAG']];
219 $type = $this->events['BY_CLASS'][$evt_name];
220 $this->dialogObject = new $type['CLASS_NAME']($this->config,$event);
221 }
222 }
223 }
225 function removeEntryRequested($action="",$target=array(),$all=array())
226 {
227 if(!$this->acl_is_removeable()){
228 msg_dialog::display(_("Permission"), msgPool::permDelete(), ERROR_DIALOG);
229 }else{
231 $deleteable_jobs = array();
232 $not_deleteable_jobs = array();
233 $headpage = $this->getHeadpage();
235 foreach($target as $dn){
237 $tmp = $headpage->getEntry($dn);
238 $task = $tmp['EVENT'];
240 /* Create a printable job name/description */
241 if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
242 $evt_name = $this->events['QUEUED'][$task['HEADERTAG']];
243 $evt = $this->events['BY_CLASS'][$evt_name];
244 $j_name = $task['ID']." - ".$evt['s_Menu_Name']." ".$task['MACADDRESS'];
245 }else{
246 $j_name = $task['ID']." - ".$task['HEADERTAG']." ".$task['MACADDRESS'];
247 }
249 /* Only remove WAITING or ERROR entries */
250 if(in_array($task['STATUS'],array("waiting","error","processed")) ||
251 ($task['STATUS'] == "processing" && !preg_match("/install/",$task['HEADERTAG'])) ){
252 $this->ids_to_remove[] = $task['ID'];
253 $deleteable_jobs[] = $j_name;
254 }else{
255 $not_deleteable_jobs[] = $j_name;
256 }
257 }
258 if(count($not_deleteable_jobs)){
259 msg_dialog::display(_("Remove"),
260 sprintf(_("The following jobs couldn't be deleted, they have to be aborted: %s"),
261 "<br>".msgPool::buildList($not_deleteable_jobs)),INFO_DIALOG);
262 }
264 if(count($this->ids_to_remove)){
265 $smarty = get_smarty();
266 $smarty->assign("multiple", TRUE);
267 $smarty->assign("info",msgPool::deleteInfo($deleteable_jobs));
268 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
269 }
270 }
271 }
273 function removeEntryConfirmed($action="",$target=array(),$all=array(),$altTabClass="",$altTabType="",$altAclCategory="")
274 {
275 if($this->acl_is_removeable("")){
276 timezone::get_default_timezone();
277 foreach($this->ids_to_remove as $id){
278 $entry = $this->o_queue->get_entries_by_id(array($id));
279 if(isset($entry['ANSWER1'])){
280 $entry = $entry['ANSWER1'];
281 if( $entry['STATUS'] == "waiting" &&
282 $entry['HEADERTAG'] == "trigger_action_reinstall"){
283 $evt = new DaemonEvent_reinstall($this->config,$entry);
284 if($evt->get_timestamp(FALSE) < time()){
285 $r_evt = new DaemonEvent_localboot($this->config);
286 $r_evt->add_targets(array($entry['MACADDRESS']));
287 $r_evt->set_type(TRIGGERED_EVENT);
288 $this->o_queue->append($r_evt);
289 }
290 }
291 }
292 }
293 $this->o_queue->remove_entries($this->ids_to_remove);
294 $this->save();
295 }
296 }
299 /*! \brief Force queue job to be aborted.
300 */
301 function abortEvent($action="",$target=array(),$all=array())
302 {
303 /* Entries are paused by setting the status to
304 * something different from 'waiting'.
305 * We simply use 'paused'.
306 */
307 $data = array("status" => "paused");
309 /* Detect if the ids we got are valid and
310 * check if the status allows pausing.
311 */
312 $update_ids = array();
313 $headpage = $this->getHeadpage();
314 foreach($target as $id){
315 $tmp = $headpage->getEntry($id);
316 $update_ids[] = $tmp['MACADDRESS'][0];
317 }
319 if(class_available("DaemonEvent_faireboot")){
320 $tmp = new DaemonEvent_faireboot($this->config);
321 $tmp->add_targets($update_ids);
322 $tmp->set_type(TRIGGERED_EVENT);
323 $this->recently_removed = $update_ids;
324 if(!$this->o_queue->append($tmp)){
325 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
326 return(FALSE);
327 }
328 }else{
329 msg_dialog::display(_("Error"),
330 sprintf(_("Required class '%s' cannot be found: job not aborted!"),
331 "DaemonEvent_faireboot") , ERROR_DIALOG);
332 }
333 }
336 function prioDown($action="",$target=array(),$all=array())
337 {
338 if(count($target) == 1){
339 $this->update_priority($target[0], 'down');
340 }
341 }
343 function prioUp($action="",$target=array(),$all=array())
344 {
345 if(count($target) == 1){
346 $this->update_priority($target[0], 'up');
347 }
348 }
350 function prioPause($action="",$target=array(),$all=array())
351 {
352 $this->pause_queue_entries($target);
353 }
355 function prioResume($action="",$target=array(),$all=array())
356 {
357 $this->resume_queue_entries($target);
358 }
360 function processNow($action="",$target=array(),$all=array())
361 {
362 $this->execute_queue_entries($target);
363 }
365 function viewLogs($action="",$target=array(),$all=array())
366 {
367 if(count($target) == 1){
368 $id = $target[0];
369 $type = FALSE;
370 $headpage = $this->getHeadpage();
371 $tmp = $headpage->getEntry($id);
372 $entry = $tmp['EVENT'];
373 $this->dialogObject = new gotoLogView($this->config,"",$entry,$this);
374 }
375 }
377 function saveEventDialog()
378 {
379 if(is_object($this->dialogObject)){
380 $this->dialogObject->save_object();
381 if(!$this->o_queue->append($this->dialogObject)){
382 msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->o_queue->get_error()),ERROR_DIALOG);
383 }else{
384 $this->current = -1;
385 }
386 }
387 $this->closeDialogs();
388 }
393 /*! \brief Move an entry up or down in the queue, by updating its execution timestamp
394 @param $id Integer The ID of the entry which should be updated.
395 @param $type String "up" / "down"
396 @return boolean TRUE in case of success else FALSE
397 */
398 public function update_priority($id,$type = "up")
399 {
400 $headpage = $this->getHeadpage();
401 $entries = $headpage->getEntries();
402 $entry = $headpage->getEntry($id);
404 $map = array();
405 $last = 0;
406 $next = 0;
407 foreach($entries as $pa){
408 $map[$pa['TIMESTAMP'][0]] = $pa['TIMESTAMP'][0];
409 }
410 krsort($map);
411 $found = 0;
412 $cur = 0;
413 foreach($map as $ts){
414 if($found){
415 $next = $ts;
416 break;
417 }
418 if($ts == $entry['TIMESTAMP'][0]){
419 $found = TRUE;
420 $cur = $ts;
421 }else{
422 $last = $ts;
423 }
424 }
426 if($type == "up" && $next != 0){
427 return($this->o_queue->update_entries(array($id),array("timestamp" => $next)));
428 }elseif($type == "down" && $last != 0){
429 return($this->o_queue->update_entries(array($id),array("timestamp" => $last)));
430 }
431 }
434 function detectPostActions()
435 {
436 $action = management::detectPostActions();
437 if(isset($_POST['save_event_dialog'])) $action['action'] = "saveEventDialog";
438 if(isset($_POST['abort_event_dialog'])) $action['action'] = "cancel";
439 if(isset($_POST['delete_multiple_confirm'])) $action['action'] = "removeConfirmed";
440 if(isset($_POST['delete_cancel'])) $action['action'] = "cancel";
441 if(isset($_POST['import_abort'])) $action['action'] = "cancel";
442 return($action);
443 }
446 function closeDialogs()
447 {
448 $this->current = FALSE;
449 management::closeDialogs();
450 }
452 function _execute()
453 {
455 /************
456 * ADD
458 /* Display dialogs if currently opened */
459 if(is_object($this->dialog)){
460 $this->dialog->save_object();
461 $display = $this->dialog->execute();
463 if($this->dialog instanceOf goto_import_file && $this->dialog->import_successful){
464 $this->dialog = FALSE;
465 }else{
466 return($display);
467 }
468 }
471 $divlist->SetDropDownHeaderMenu($s);
473 if($this->sort_dir == "up"){
474 $sort_img = "<img src='images/lists/sort-up.png' alt='/\' border=0>";
475 }else{
476 $sort_img = "<img src='images/lists/sort-down.png' alt='\/' border=0>";
477 }
479 if($this->sort_by == "TargetName"){ $sort_img_1 = $sort_img; } else { $sort_img_1 = "" ;}
480 if($this->sort_by == "TaskID"){ $sort_img_2 = $sort_img; } else { $sort_img_2 = "" ;}
481 if($this->sort_by == "Schedule"){ $sort_img_3 = $sort_img; } else { $sort_img_3 = "" ;}
482 if($this->sort_by == "Action"){ $sort_img_4 = $sort_img; } else { $sort_img_4 = "" ;}
484 /* Create divlist */
485 $divlist->SetListHeader("<input type='image' src='images/lists/reload.png' title='"._("Reload")."'>");
487 $plug = $_GET['plug'];
488 $chk = "<input type='checkbox' id='select_all' name='select_all'
489 onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
491 /* set Page header */
492 $divlist->AddHeader(array("string"=> $chk, "attach"=>"style='width:20px;'"));
493 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=TargetName'>"._("Target").$sort_img_1."</a>"));
494 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=TaskID'>"._("Task").$sort_img_2."</a>",
495 "attach"=>"style='width:120px;'"));
496 $divlist->AddHeader(array("string"=>_("Period"),
497 "attach"=>"style='width:60px;'"));
498 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
499 "attach"=>"style='width:140px;'"));
500 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=Action'>"._("Status").$sort_img_4."</a>",
501 "attach"=>"style='width:80px;'"));
502 $divlist->AddHeader(array("string"=>_("Action"),
503 "attach"=>"style='border-right:0px;width:140px;'"));
506 /* Reload the list of entries */
507 $this->reload();
509 foreach($this->entries as $key => $task){
511 $prio_actions="";
512 $action = "";
515 /* If WAITING add priority action
516 */
517 if(in_array($task['STATUS'],array("waiting")) && $this->acl_is_writeable("")){
518 $prio_actions.= "<input class='center' type='image' src='plugins/goto/images/prio_increase.png'
519 title='"._("Move up")."' name='prio_up_".$key."'> ";
520 $prio_actions.= "<input class='center' type='image' src='plugins/goto/images/prio_decrease.png'
521 title='"._("Move down")."' name='prio_down_".$key."'> ";
522 }
524 /* If WAITING add pause action
525 */
526 if(in_array($task['STATUS'],array("waiting")) && $this->acl_is_writeable("")){
527 $prio_actions.= "<input class='center' type='image' src='images/status_pause.png'
528 title='"._("Pause job")."' name='pause_".$key."'> ";
529 }
531 /* If PAUSED add resume action
532 */
533 if(in_array($task['STATUS'],array("paused")) && $this->acl_is_writeable("")){
534 $prio_actions.= "<input class='center' type='image' src='images/status_start.png'
535 title='"._("Resume job")."' name='resume_".$key."'> ";
536 }
538 /* If PAUSED or WAITING add execution action
539 */
540 if(in_array($task['STATUS'],array("paused","waiting")) && $this->acl_is_writeable("")){
541 $prio_actions.= "<input class='center' type='image' src='images/rocket.png'
542 title='"._("Execute now")."' name='execute_process_".$key."'> ";
543 }
545 /* Add logview button, currently ever.
546 */
547 if($this->acl_is_readable("")){
548 $action .= "<input type='image' src='plugins/goto/images/view_logs.png' name='log_view_".$key."'
549 class='center' title='"._("View logs")."' alt='"._("View logs")."'> ";
550 }
552 /* If PAUSED or WAITING add edit action
553 */
554 if(in_array($task['STATUS'],array("waiting")) && $this->acl_is_writeable("")){
555 $action.= "<input type='image' src='images/lists/edit.png' name='edit_task_".$key."'
556 class='center' title='"._("Edit")."' alt='"._("Edit")."'>";
557 }
559 /* If PROCESSING add abort action
560 */
561 if(in_array($task['STATUS'],array("processing")) && preg_match("/install/",$task['HEADERTAG']) && $this->acl_is_writeable("")){
562 $action.= "<img src='images/empty.png' alt=''>";
563 $action.= "<input class='center' type='image' src='images/small_error.png'
564 title='"._("Abort job")."' name='abort_process_".$key."'>";
565 }
567 /* If WAITING or ERROR add remove action
568 */
569 if( $this->acl_is_removeable() && in_array($task['STATUS'],array("waiting","error","processed"))){
570 $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."'
571 class='center' title='"._("Remove")."' alt='"._("Remove")."'>";
572 }
573 if($this->acl_is_writeable("") && in_array($task['STATUS'],array("processing")) && !preg_match("/install/",$task['HEADERTAG'])){
574 $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."'
575 class='center' title='"._("Remove")."' alt='"._("Remove")."'>";
576 }
578 /* Create entry display name and tooltip */
579 $color = "";
580 $display = $task['MACADDRESS'];
581 $tooltip = "";
582 if(isset($task['PLAINNAME']) && !preg_match("/none/i",$task['PLAINNAME'])){
583 $display = $task['PLAINNAME'];
584 $tooltip = " title='".$task['MACADDRESS']."' ";
585 }
588 $display2= $task['HEADERTAG'];
590 /* Check if this event exists as Daemon class
591 * In this case, display a more accurate entry.
592 */
593 if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
594 $evt_name = $this->events['QUEUED'][$task['HEADERTAG']];
595 $event_type = $this->events['BY_CLASS'][$evt_name];
596 $display2 = $event_type['s_Menu_Name'];
598 if(strlen($display2) > 20){
599 $display2 = substr($display2,0,18)."...";
600 }
602 if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
603 $display2 = $event_type['ListImage']." ".$display2;
604 }
605 }
607 $status = $task['STATUS'];
609 if($status == "waiting"){
610 $status = "<img class='center' src='plugins/goto/images/clock.png' alt=''> "._("Waiting");
611 }
612 if($status == "error"){
613 $status = "<img class='center' src='images/false.png' alt=''> "._("Error");
614 }
615 if($status == "processed"){
616 $status = "<img class='center' src='images/true.png' alt=''> "._("Processed");
617 }
619 /* Special handling for all entries that have
620 STATUS == "processing" && PROGRESS == NUMERIC
621 */
622 if($status == "processing" && isset($task['PROGRESS'])){
623 $percent = $task['PROGRESS'];
625 /* Show activation? */
626 if ($percent == "goto-activation"){
627 $status = "<img class='center' src='images/lists/off.png' alt=''> "._("Locked");
629 /* Show hardware detect? */
630 } elseif ($percent == "goto-hardware-detection") {
631 $status = "<img class='center' src='plugins/goto/images/hardware.png' alt=''> "._("Detection");
633 /* Real percent */
634 } else {
635 if (preg_match('/install/', $task['HEADERTAG'])){
636 $status = "<img src='progress.php?x=80&y=13&p=".$task['PROGRESS']."' alt=''
637 id='progress_".preg_replace("/:/","_",$task['MACADDRESS'])."'>";
638 } else {
639 $status = preg_replace('/ /', ' ', _("in progress"));
640 }
641 }
642 }
644 // Check whether this is a periodical job or not.
645 $period = "";
646 if(isset($task['PERIODIC']) && !preg_match("/none/i",$task['PERIODIC'])){
647 $tmp = explode("_", $task['PERIODIC']);
648 if(count($tmp) == 2){
649 $period= $tmp[0]." "._($tmp[1]);
650 }
651 }
653 /* Create each field */
654 $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$key."'>" ,
655 "attach" => "style='width:20px;".$color."'");
656 $field1 = array("string" => $display,
657 "attach" => $tooltip."style='".$color."'");
658 $field1a= array("string" => $display2,
659 "attach" => "style='".$color.";width:120px;'");
660 $field1b= array("string" => $period,
661 "attach" => "style='".$color.";width:60px;'");
662 if ($task['TIMESTAMP'] == "19700101000000"){
663 $field2 = array("string" => _("immediately"),"attach" => "style='".$color.";width:140px;'");
664 } else {
665 $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:140px;'");
666 }
667 $field3 = array("string" => $status,"attach" => "style='".$color.";width:80px;'");
668 $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:140px;border-right:0px;'");
669 $divlist->AddElement(array($field0,$field1,$field1a,$field1b,$field2,$field3,$field4));
670 }
672 $smarty = get_smarty();
673 $smarty->assign("events",$this->events);
674 $smarty->assign("start",$this->start);
675 $smarty->assign("start_real", ($this->start + 1));
676 $smarty->assign("ranges", array("10" => "10",
677 "20" => "20",
678 "25" => "25",
679 "50" => "50",
680 "100"=> "100",
681 "200"=> "200",
682 "9999" => "*"));
684 $count = $this->o_queue->number_of_queued_entries($this->event_tags);
685 if(!$count) $count = $this->range;
686 $divlist->SetListFooter(range_selector($count, $this->start, $this->range,"range"));
687 $smarty->assign("range",$this->range);
688 $smarty->assign("div",$divlist->Draw());
690 return(management::execute());
691 return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
692 }
695 /*! \brief Resumes to status 'waiting'.
696 * @return Boolean TRUE in case of success, else FALSE.
697 */
698 private function resume_queue_entries($ids)
699 {
700 /* Entries are resumed by setting the status to
701 * 'waiting'
702 */
703 $data = array("status" => "waiting");
705 /* Check if given ids are valid and check if the status
706 * allows resuming.
707 */
708 $update_ids = array();
709 $headpage = $this->getHeadpage();
710 foreach($ids as $id){
711 $tmp = $headpage->getEntry($id);
712 $entry = $tmp['EVENT'];
713 if(isset($entry['STATUS']) && preg_match("/paused/",$entry['STATUS'])){
714 $update_ids[] = $entry['ID'];
715 }
716 }
718 /* Tell the daemon that we have entries to update.
719 */
720 if(count($update_ids)){
721 if(!$this->o_queue->update_entries($update_ids,$data)){
722 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
723 return(FALSE);
724 }
725 }
726 return(TRUE);
727 }
730 /*! \brief Force queue job to be done as far as possible.
731 * @return Boolean TRUE in case of success, else FALSE.
732 */
733 private function execute_queue_entries($ids)
734 {
735 /* Execution is forced by updating the status to
736 * waiting and setting the timestamp to current time.
737 */
738 $data = array( "timestamp" => date("YmdHis",time()),
739 "status" => "waiting");
741 /* Only allow execution of paused or waiting entries
742 */
743 $update_ids = array();
744 $headpage = $this->getHeadpage();
745 foreach($ids as $id){
746 $tmp = $headpage->getEntry($id);
747 $entry = $tmp['EVENT'];
748 if(in_array($entry['STATUS'],array("paused","waiting"))){
749 $update_ids[] = $entry['ID'];
750 }
751 }
753 /* Tell the daemon that we want to update some entries
754 */
755 if(count($update_ids)){
756 if(!$this->o_queue->update_entries($update_ids,$data)){
757 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entries.")) , ERROR_DIALOG);
758 return(FALSE);
759 }
760 }
761 return(TRUE);
762 }
765 /*! \brief Pauses the specified queue entry from execution.
766 * @return Boolean TRUE in case of success, else FALSE.
767 */
768 private function pause_queue_entries($ids)
769 {
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 $headpage = $this->getHeadpage();
781 foreach($ids as $id){
782 $tmp = $headpage->getEntry($id);
783 $entry = $tmp['EVENT'];
784 if(isset($entry['STATUS']) && preg_match("/waiting/",$entry['STATUS'])){
785 $update_ids[] = $entry['ID'];
786 }
787 }
789 /* Tell the daemon that we want to update some entries
790 */
791 if(count($update_ids)){
792 if(!$this->o_queue->update_entries($update_ids,$data)){
793 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
794 return(FALSE);
795 }
796 }
797 return(TRUE);
798 }
801 /*! \brief Request list of queued jobs.
802 * @return Returns an array of all queued jobs.
803 */
804 function reload()
805 {
807 /* Sort map html-post-name => daemon-col-name
808 */
809 $map = array(
810 "QueuePosition" => "id",
811 "Action" => "status",
812 "TaskID" => "headertag",
813 "TargetName" => "macaddress",
814 "Schedule" => "timestamp");
816 /* Create sort header
817 */
818 if(!isset($map[$this->sort_by])){
819 $sort = "id DESC";
820 }else{
821 $sort = $map[$this->sort_by];
822 if($this->sort_dir == "up"){
823 $sort.= " ASC";
824 }else{
825 $sort.= " DESC";
826 }
827 }
829 /* Get entries. */
830 $start = $this->start;
831 $stop = $this->range;
832 $entries = $this->o_queue->get_queued_entries($this->event_tags,$start,$stop,$sort);
833 if ($this->o_queue->is_error()){
834 msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
835 }
837 /* Assign entries by id.
838 */
839 $this->entries = array();
841 foreach($entries as $entry){
843 /* Skip entries which will be removed within the next seconds */
844 if(isset($entry['MACADDRESS']) && in_array($entry['MACADDRESS'],$this->recently_removed)){
845 continue;
846 }
847 $this->entries[$entry['ID']]= $entry;
848 }
849 $this->recently_removed = array();
850 }
852 function save_object(){}
854 /*! \brief Handle post jobs, like sorting.
855 */
856 function _save_object()
857 {
858 /* Check for sorting changes
859 */
860 $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
861 if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
862 $sort = $_GET['sort'];
863 if($this->sort_by == $sort){
864 if($this->sort_dir == "up"){
865 $this->sort_dir = "down";
866 }else{
867 $this->sort_dir = "up";
868 }
869 }
870 $this->sort_by = $sort;
871 }
873 /* Range selection used? */
874 if(isset($_POST['range']) && is_numeric($_POST['range'])){
875 $this->range = $_POST['range'];
876 }
878 /* Save filter settings */
879 $gotomasses_filter = session::get("gotomasses_filter");
880 foreach(array("range","sort_by","sort_dir") as $attr){
881 $gotomasses_filter[$attr] = $this->$attr;
882 }
883 session::set("gotomasses_filter",$gotomasses_filter);
885 /* Page changed. */
886 if(isset($_GET['start'])){
887 $start = $_GET['start'];
888 if(is_numeric($start) || $start == 0){
889 $this->start = $start;
890 }
891 }
893 /* Check start stop and reset if necessary */
894 $count = $this->o_queue->number_of_queued_entries($this->event_tags);
895 if($this->start >= $count){
896 $this->start = $count -1;
897 }
898 if($this->start < 0){
899 $this->start = 0;
900 }
901 }
904 function save()
905 {
906 // We do not save anything here.
907 }
910 /*! \brief Return a list of all selected items.
911 @return Array Returns an array containing all selected item ids.
912 */
913 function list_get_selected_items()
914 {
915 $ids = array();
916 foreach($_POST as $name => $value){
917 if(preg_match("/^item_selected_[0-9]*$/",$name)){
918 $id = preg_replace("/^item_selected_/","",$name);
919 $ids[$id] = $id;
920 }
921 }
922 return($ids);
923 }
926 static function plInfo()
927 {
928 return (array(
929 "plShortName" => _("System deployment"),
930 "plDescription" => _("Provide a mechanism to automatically activate systems"),
931 "plSelfModify" => FALSE,
932 "plDepends" => array(),
933 "plPriority" => 0,
934 "plSection" => array("addon"),
935 "plCategory" => array("gotomasses" => array("objectClass" => "none", "description" => _("System deployment"))),
936 "plProvidedAcls" => array("Comment" => _("Description"))
937 ));
938 }
941 function set_acl_base($base)
942 {
943 $this->acl_base= $base;
944 }
947 function set_acl_category($category)
948 {
949 $this->acl_category= "$category/";
950 }
953 function acl_is_writeable($attribute,$skip_write = FALSE)
954 {
955 if($this->read_only) return(FALSE);
956 $ui= get_userinfo();
957 return preg_match('/w/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute, $skip_write));
958 }
961 function acl_is_readable($attribute)
962 {
963 $ui= get_userinfo();
964 return preg_match('/r/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute));
965 }
968 function acl_is_createable($base ="")
969 {
970 if($this->read_only) return(FALSE);
971 $ui= get_userinfo();
972 if($base == "") $base = $this->acl_base;
973 return preg_match('/c/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
974 }
977 function acl_is_removeable($base ="")
978 {
979 if($this->read_only) return(FALSE);
980 $ui= get_userinfo();
981 if($base == "") $base = $this->acl_base;
982 return preg_match('/d/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
983 }
985 function acl_is_moveable($base = "")
986 {
987 if($this->read_only) return(FALSE);
988 $ui= get_userinfo();
989 if($base == "") $base = $this->acl_base;
990 return preg_match('/m/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
991 }
994 function getacl($attribute,$skip_write= FALSE)
995 {
996 $ui= get_userinfo();
997 $skip_write |= $this->read_only;
998 return $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute,$skip_write);
999 }
1001 }
1002 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1003 ?>