1 <?php
3 class gotomasses extends plugin
4 {
5 /* Definitions */
6 var $plHeadline = "System deployment";
7 var $plDescription = "This does something";
9 /* attribute list for save action */
10 var $attributes= array();
11 var $objectclasses= array();
13 /* Queue tasks */
14 var $current = FALSE;
15 var $dialog = FALSE;
16 var $ids_to_remove = array();
17 var $divlist = NULL;
19 var $events = array();
21 var $sort_by = "Schedule";
22 var $sort_dir = "down";
23 var $entries = array();
24 var $range = 25;
25 var $start = 0;
27 function gotomasses(&$config, $dn= NULL)
28 {
29 /* Include config object */
30 $this->config= &$config;
31 $this->o_queue = new gosaSupportDaemon(TRUE,10);
32 $this->events = DaemonEvent::get_event_types();
33 }
36 function execute()
37 {
38 $smarty = get_smarty();
40 /************
41 * Handle posts
42 ************/
44 $s_entry = $s_action = "";
45 $arr = array(
46 "/^pause_/" => "pause",
47 "/^resume_/" => "resume",
48 "/^execute_process_/" => "execute_process",
50 "/^prio_up_/" => "prio_up",
51 "/^prio_down_/" => "prio_down",
53 "/^edit_task_/" => "edit",
54 "/^remove_task_/" => "remove",
55 "/^new_task_/" => "new_task");;
58 foreach($arr as $regex => $action){
59 foreach($_POST as $name => $value){
60 if(preg_match($regex,$name)){
61 $s_action = $action;
62 $s_entry = preg_replace($regex,"",$name);
63 $s_entry = preg_replace("/_(x|y)$/","",$s_entry);
64 }
65 }
66 }
68 /* Menu actions */
69 if(isset($_POST['menu_action']) && !empty($_POST['menu_action'])){
70 $s_action = $_POST['menu_action'];
71 }
73 /* Edit posted from list link */
74 if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
75 $s_action = "edit";
76 $s_entry = $_GET['id'];
77 }
80 /************
81 * Handle Priority modifications
82 ************/
84 if(preg_match("/^prio_/",$s_action)){
86 switch($s_action){
88 case 'prio_down' : $this->update_priority($s_entry,"down");break;
89 case 'prio_up' : $this->update_priority($s_entry,"up");break;
90 }
91 }
93 /************
94 * Handle pause/resume/execute modifications
95 ************/
97 if(preg_match("/^resume/",$s_action) ||
98 preg_match("/^pause/",$s_action) ||
99 preg_match("/^execute_process/",$s_action)){
101 switch($s_action){
102 case 'resume' : $this->resume_queue_entries(array($s_entry));break;
103 case 'pause' : $this->pause_queue_entries (array($s_entry));break;
104 case 'execute_process': $this->execute_queue_entries (array($s_entry));break;
105 default : trigger_error("Undefined priority setting used.");
106 }
107 if($this->o_queue->is_error()){
108 msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
109 }
110 }
112 /************
113 * ADD
114 ************/
116 if(preg_match("/^add_event_/",$s_action)){
117 $type = preg_replace("/^add_event_/","",$s_action);
118 if(isset($this->events['BY_CLASS'][$type])){
119 $e_data = $this->events['BY_CLASS'][$type];
120 $this->dialog = new $e_data['CLASS_NAME']($this->config);
121 }
122 }
124 /************
125 * EDIT
126 ************/
128 if($s_action == "edit"){
129 $id = $s_entry;
130 $type = FALSE;
131 if(isset($this->entries[$id])){
132 $event = $this->entries[$s_entry];
133 if(isset($this->events['BY_QUEUED_ACTION'][$event['HEADERTAG']])){
134 $type = $this->events['BY_QUEUED_ACTION'][$event['HEADERTAG']];
135 $this->dialog = new $type['CLASS_NAME']($this->config,$event);
136 }
137 }
138 }
140 /************
141 * REMOVE
142 ************/
144 /* Remove multiple */
145 if($s_action == "remove_multiple" || $s_action == "remove"){
147 if(!$this->acl_is_removeable()){
148 msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), ERROR_DIALOG);
149 }else{
151 if($s_action == "remove"){
152 $ids = array($s_entry);
153 }else{
154 $ids = $this->list_get_selected_items();
155 }
156 if(count($ids)){
157 $this->ids_to_remove = $ids;
158 $ret = $this->o_queue->ids_exist($this->ids_to_remove);
159 $ret = $this->o_queue->get_entries_by_id($ret);
161 $tmp = "";
162 foreach($ret as $task){
163 if(isset($this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']])){
164 $evt = $this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']];
165 $tmp.= "\n".$task['ID']." - ".$evt['s_Menu_Name']." ".$task['MACADDRESS'];
166 }else{
167 $tmp.= "\n".$task['ID']." - ".$task['HEADERTAG']." ".$task['MACADDRESS'];
168 }
169 }
170 $smarty->assign("multiple", TRUE);
171 $smarty->assign("info",sprintf(_("You are about to remove the following actions from the GOsa support Daemon: %s"),"<pre>".$tmp."</pre>"));
172 $this->current = $s_entry;
173 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
174 }
175 }
176 }
178 /* Remove specified tasks */
179 if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
180 $this->o_queue->remove_entries($this->ids_to_remove);
181 $this->save();
182 }
184 /* Remove aborted */
185 if(isset($_POST['delete_cancel'])){
186 $this->ids_to_remove = array();;
187 }
190 /************
191 * EDIT
192 ************/
194 /* Close dialog */
195 if(isset($_POST['save_event_dialog'])){
196 if(is_object($this->dialog)){
197 $this->dialog->save_object();
198 if($this->dialog->is_new()){
199 $header = $this->dialog->get_schedule_action();
200 $targets = $this->dialog->get_targets();
201 $data = $this->dialog->save();
203 foreach($targets as $target){
204 $data['macaddress'] = $target;
205 $this->o_queue->send_data($header,$target,$data,TRUE);
206 if($this->o_queue->is_error()){
207 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
208 $this->o_queue->get_error()),ERROR_DIALOG);
209 }else{
210 $this->dialog = FALSE;
211 $this->current = -1;
212 }
213 }
214 }else{
215 $id = $this->dialog->get_id();
216 $data = $this->dialog->save();
217 if($this->o_queue->update_entries(array($id),$data)){
218 $this->dialog = FALSE;
219 $this->current = -1;
220 }else{
221 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
222 $this->o_queue->get_error()),ERROR_DIALOG);
223 }
224 }
225 }
226 }
229 /* Close dialog */
230 if(isset($_POST['abort_event_dialog'])){
231 $this->dialog = FALSE;
232 $this->current = -1;
233 }
235 /* Display dialogs if currently opened */
236 if(is_object($this->dialog)){
237 $this->dialog->save_object();
238 return($this->dialog->execute());
239 }
241 /************
242 * Handle Divlist
243 ************/
245 $divlist = new MultiSelectWindow($this->config,"gotoMasses",array("gotomasses"));
246 $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa deamon tasks."));
247 $divlist->SetSummary(_("List of queued deamon jobs"));
248 $divlist->EnableCloseButton(FALSE);
249 $divlist->EnableSaveButton(FALSE);
250 $divlist->SetHeadpageMode();
251 $s = ".|"._("Actions")."|\n";
252 $s.= "..|<img src='images/list_new.png' alt='' border='0' class='center'> "._("Create")."\n";
253 foreach($this->events['BY_CLASS'] as $name => $event){
254 $s.= "...|".$event['MenuImage']." ".$event['s_Menu_Name']."|add_event_".$name."\n";
255 }
256 if($this->acl_is_removeable()){
257 $s.= "..|---|\n";
258 $s.= "..|<img src='images/edittrash.png' alt='' border='0' class='center'> "._("Remove")."|remove_multiple\n";
259 }
260 if(preg_match("/w/",$this->getacl(""))){
261 $s.= "..|---|\n";
262 $s.= "..|<img src='images/status_start.png' alt='' border='0' class='center'> "._("Resume all")."|resume_all_\n";
263 $s.= "..|<img src='images/status_pause.png' alt='' border='0' class='center'> "._("Pause all")."|pause_all_\n";
264 }
266 $divlist->SetDropDownHeaderMenu($s);
268 if($this->sort_dir == "up"){
269 $sort_img = "<img src='images/sort_up.png' alt='/\' border=0>";
270 }else{
271 $sort_img = "<img src='images/sort_down.png' alt='\/' border=0>";
272 }
274 if($this->sort_by == "TargetName"){ $sort_img_1 = $sort_img; } else { $sort_img_1 = "" ;}
275 if($this->sort_by == "TaskID"){ $sort_img_2 = $sort_img; } else { $sort_img_2 = "" ;}
276 if($this->sort_by == "Schedule"){ $sort_img_3 = $sort_img; } else { $sort_img_3 = "" ;}
277 if($this->sort_by == "Action"){ $sort_img_4 = $sort_img; } else { $sort_img_4 = "" ;}
279 /* Create divlist */
280 $divlist->SetListHeader("<input type='image' src='images/list_reload.png' title='"._("Reload")."'>");
282 $plug = $_GET['plug'];
283 $chk = "<input type='checkbox' id='select_all' name='select_all'
284 onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
286 /* set Page header */
287 $divlist->AddHeader(array("string"=> $chk, "attach"=>"style='width:20px;'"));
288 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=TargetName'>"._("Target").$sort_img_1."</a>"));
289 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=TaskID'>"._("Task").$sort_img_2."</a>",
290 "attach"=>"style='width:120px;'"));
291 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
292 "attach"=>"style='width:100px;'"));
293 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=Action'>"._("Status").$sort_img_4."</a>",
294 "attach"=>"style='width:80px;'"));
295 $divlist->AddHeader(array("string"=>_("Action"),
296 "attach"=>"style='border-right:0px;width:120px;'"));
299 /* Reload the list of entries */
300 $this->reload();
302 foreach($this->entries as $key => $task){
304 $prio_actions="";
305 if(isset($task['STATUS']) && preg_match("/waiting/",$task['STATUS'])){
306 $prio_actions.= "<input class='center' type='image' src='images/prio_increase.png'
307 title='"._("Move up in execution queue")."' name='prio_up_".$key."'> ";
308 $prio_actions.= "<input class='center' type='image' src='images/prio_decrease.png'
309 title='"._("Move down in execution queue")."' name='prio_down_".$key."'> ";
310 }
312 if(isset($task['STATUS']) && preg_match("/waiting/",$task['STATUS'])){
313 $prio_actions.= "<input class='center' type='image' src='images/status_pause.png'
314 title='"._("Pause job")."' name='pause_".$key."'> ";
315 }
316 if(isset($task['STATUS']) && preg_match("/paused/",$task['STATUS'])){
317 $prio_actions.= "<input class='center' type='image' src='images/status_start.png'
318 title='"._("Resume job")."' name='resume_".$key."'> ";
319 }
321 if(isset($task['STATUS']) && preg_match("/processing/",$task['STATUS'])){
322 $prio_actions.= "<input class='center' type='image' src='images/small_error.png'
323 title='"._("Abort execution")."' name='abort_process_".$key."'> ";
324 }
326 if(isset($task['STATUS']) &&
327 (preg_match("/paused/",$task['STATUS']) || preg_match("/waiting/",$task['STATUS']))){
328 $prio_actions.= "<input class='center' type='image' src='images/rocket.png'
329 title='"._("Force execution now!")."' name='execute_process_".$key."'> ";
330 }
332 $action = "<input type='image' src='images/edit.png' name='edit_task_".$key."'
333 class='center' alt='"._("Edit")."'>";
335 if($this->acl_is_removeable()){
336 $action.= "<input type='image' src='images/edittrash.png' name='remove_task_".$key."'
337 class='center' alt='"._("Reomve")."'>";
338 }
340 $color = "";
341 $display = $task['MACADDRESS'];
342 $display2= $task['HEADERTAG'];
344 /* Check if this event exists as Daemon class
345 * In this case, display a more accurate entry.
346 */
347 if(isset($this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']]['s_Menu_Name'])){
348 $event_type = $this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']];
349 $display2= $event_type['s_Menu_Name'];
350 if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
351 $display2 = $event_type['ListImage']." ".$display2;
352 }
353 }
355 $status = $task['STATUS'];
357 /* Special handling for all entries that have
358 STATUS == "processing" && PROGRESS == NUMERIC
359 */
360 if($status == "processing" && isset($task['PROGRESS'])){
361 $percent = $task['PROGRESS'];
362 $status = "<img src='progress.php?x=80&y=13&p=".$percent."' alt='".$percent." %'>";
363 }
366 /* Create each field */
367 $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$key."'>" ,
368 "attach" => "style='width:20px;".$color."'");
369 $field1 = array("string" => $display,
370 "attach" => "style='".$color."'");
371 $field1a= array("string" => $display2,
372 "attach" => "style='".$color.";width:120px;'");
373 $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:100px;'");
374 $field3 = array("string" => $status,"attach" => "style='".$color.";width:80px;'");
375 $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:120px;border-right:0px;'");
376 $divlist->AddElement(array($field0,$field1,$field1a,$field2,$field3,$field4));
377 }
379 $smarty = get_smarty();
380 $smarty->assign("events",$this->events);
381 $smarty->assign("start",$this->start);
382 $smarty->assign("start_real", ($this->start + 1));
383 $smarty->assign("ranges", array("10" => "10",
384 "20" => "20",
385 "25" => "25",
386 "50" => "50",
387 "100"=> "100",
388 "200"=> "200",
389 "9999" => "*"));
391 $count = $this->o_queue->number_of_queued_entries();
392 $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"range"));
393 $smarty->assign("range",$this->range);
394 $smarty->assign("div",$divlist->Draw());
395 return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
396 }
399 /*! \brief Move an entry up or down in the queue, by updating its execution timestamp
400 @param $id Integer The ID of the entry which should be updated.
401 @param $type String "up" / "down"
402 @return boolean TRUE in case of success else FALSE
403 */
404 public function update_priority($id,$type = "up")
405 {
406 if($type == "up"){
407 $tmp = $this->o_queue->get_queued_entries(-1,-1,"timestamp DESC");
408 }else{
409 $tmp = $this->o_queue->get_queued_entries(-1,-1,"timestamp ASC");
410 }
411 $last = array();
412 foreach($tmp as $entry){
413 if($entry['ID'] == $id){
414 if(count($last)){
415 $time = strtotime($last['TIMESTAMP']);
416 if($type == "up"){
417 $time ++;
418 }else{
419 $time --;
420 }
421 $time_str = date("YmdHis",$time);
422 return($this->o_queue->update_entries(array($id),array("timestamp" => $time_str)));
423 }else{
424 return(FALSE);
425 }
426 }
427 $last = $entry;
428 }
429 return(FALSE);
430 }
433 /*! \brief Resumes to status 'waiting'.
434 * @return Boolean TRUE in case of success, else FALSE.
435 */
436 private function resume_queue_entries($ids)
437 {
438 if(!count($ids)){
439 return;
440 }
442 $data = array("status" => "waiting");
443 if(!$this->o_queue->update_entries($ids,$data)){
444 msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
445 return(FALSE);
446 }
447 return(TRUE);
448 }
451 /*! \brief Force queue job to be done as far as possible.
452 * @return Boolean TRUE in case of success, else FALSE.
453 */
454 private function execute_queue_entries($ids)
455 {
456 if(!count($ids)){
457 return;
458 }
460 $data = array( "timestamp" => date("YmdHis",time()),
461 "status" => "waiting");
462 if(!$this->o_queue->update_entries($ids,$data)){
463 msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
464 return(FALSE);
465 }
466 return(TRUE);
467 }
470 /*! \brief Pauses the specified queue entry from execution.
471 * @return Boolean TRUE in case of success, else FALSE.
472 */
473 private function pause_queue_entries($ids)
474 {
475 if(!count($ids)){
476 return;
477 }
478 $data = array("status" => "paused");
479 if(!$this->o_queue->update_entries($ids,$data)){
480 msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
481 return(FALSE);
482 }
483 return(TRUE);
484 }
487 /*! \brief Request list of queued jobs.
488 * @return Returns an array of all queued jobs.
489 */
490 function reload()
491 {
492 $map = array(
493 "QueuePosition" => "id",
494 "Action" => "status",
495 "TaskID" => "headertag",
496 "TargetName" => "macaddress",
497 "Schedule" => "timestamp");
499 if(!isset($map[$this->sort_by])){
500 $sort = "id DESC";
501 }else{
502 $sort = $map[$this->sort_by];
503 if($this->sort_dir == "up"){
504 $sort.= " ASC";
505 }else{
506 $sort.= " DESC";
507 }
508 }
510 $start = $this->start;
511 $stop = $this->range;
513 $entries = $this->o_queue->get_queued_entries($start,$stop,$sort);
514 if ($this->o_queue->is_error()){
516 msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
517 }
519 $this->entries = array();
520 foreach($entries as $entry){
521 $this->entries[$entry['ID']]= $entry;
522 }
523 }
526 /*! \brief Handle post jobs, like sorting.
527 */
528 function save_object()
529 {
530 $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
531 if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
532 $sort = $_GET['sort'];
533 if($this->sort_by == $sort){
534 if($this->sort_dir == "up"){
535 $this->sort_dir = "down";
536 }else{
537 $this->sort_dir = "up";
538 }
539 }
540 $this->sort_by = $sort;
541 }
542 if(isset($_POST['range']) && is_numeric($_POST['range'])){
543 $this->range = $_POST['range'];
544 }
545 if(isset($_GET['start'])){
546 $start = $_GET['start'];
547 if(is_numeric($start) || $start == 0){
548 $this->start = $start;
549 }
550 }
552 /* Check start stop and reset if necessary */
553 $count = $this->o_queue->number_of_queued_entries();
554 if($this->start >= $count){
555 $this->start = $count -1;
556 }
557 if($this->start < 0){
558 $this->start = 0;
559 }
560 }
563 function save()
564 {
565 // We do not save anything here.
566 }
569 /*! \brief Return a list of all selected items.
570 @return Array Returns an array containing all selected item ids.
571 */
572 function list_get_selected_items()
573 {
574 $ids = array();
575 foreach($_POST as $name => $value){
576 if(preg_match("/^item_selected_[0-9]*$/",$name)){
577 $id = preg_replace("/^item_selected_/","",$name);
578 $ids[$id] = $id;
579 }
580 }
581 return($ids);
582 }
585 static function plInfo()
586 {
587 return (array(
588 "plShortName" => _("System mass deployment"),
589 "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
590 "plSelfModify" => FALSE,
591 "plDepends" => array(),
592 "plPriority" => 0,
593 "plSection" => array("addon"),
594 "plCategory" => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
595 "plProvidedAcls" => array("Comment" => _("Description"))
596 ));
597 }
598 }
599 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
600 ?>