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 "/^stop_/" => "stop",
47 "/^start_/" => "start",
49 "/^prio_up_/" => "prio_up",
50 "/^prio_down_/" => "prio_down",
52 "/^edit_task_/" => "edit",
53 "/^remove_task_/" => "remove",
54 "/^new_task_/" => "new_task");;
57 foreach($arr as $regex => $action){
58 foreach($_POST as $name => $value){
59 if(preg_match($regex,$name)){
60 $s_action = $action;
61 $s_entry = preg_replace($regex,"",$name);
62 $s_entry = preg_replace("/_(x|y)$/","",$s_entry);
63 }
64 }
65 }
67 /* Menu actions */
68 if(isset($_POST['menu_action']) && !empty($_POST['menu_action'])){
69 $s_action = $_POST['menu_action'];
70 }
72 /* Edit posted from list link */
73 if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
74 $s_action = "edit";
75 $s_entry = $_GET['id'];
76 }
79 /************
80 * Handle Priority modifications
81 ************/
83 if(preg_match("/^prio_/",$s_action)){
85 switch($s_action){
87 case 'prio_down' : $this->update_priority($s_entry,"down");break;
88 case 'prio_up' : $this->update_priority($s_entry,"up");break;
89 }
90 }
92 /************
93 * Handle start/stop modifications
94 ************/
96 if(preg_match("/^start/",$s_action) || preg_match("/^stop/",$s_action)){
98 switch($s_action){
99 case 'start_all' : $this->start_queue_entries($this->list_get_selected_items());break;
100 case 'start' : $this->start_queue_entries(array($s_entry));break;
101 case 'stop_all' : $this->stop_queue_entries ($this->list_get_selected_items());break;
102 case 'stop' : $this->stop_queue_entries (array($s_entry));break;
103 default : trigger_error("Undefined priority setting used.");
104 }
105 if($this->o_queue->is_error()){
106 msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
107 }
108 }
110 /************
111 * ADD
112 ************/
114 if(preg_match("/^add_event_/",$s_action)){
115 $type = preg_replace("/^add_event_/","",$s_action);
116 if(isset($this->events['BY_CLASS'][$type])){
117 $e_data = $this->events['BY_CLASS'][$type];
118 $this->dialog = new $e_data['CLASS_NAME']($this->config);
119 }
120 }
122 /************
123 * EDIT
124 ************/
126 if($s_action == "edit"){
127 $id = $s_entry;
128 $type = FALSE;
129 if(isset($this->entries[$id])){
130 $event = $this->entries[$s_entry];
131 if(isset($this->events['BY_QUEUED_ACTION'][$event['HEADERTAG']])){
132 $type = $this->events['BY_QUEUED_ACTION'][$event['HEADERTAG']];
133 $this->dialog = new $type['CLASS_NAME']($this->config,$event);
134 }
135 }
136 }
138 /************
139 * REMOVE
140 ************/
142 /* Remove multiple */
143 if($s_action == "remove_multiple" || $s_action == "remove"){
145 if(!$this->acl_is_removeable()){
146 msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), ERROR_DIALOG);
147 }else{
149 if($s_action == "remove"){
150 $ids = array($s_entry);
151 }else{
152 $ids = $this->list_get_selected_items();
153 }
154 if(count($ids)){
155 $this->ids_to_remove = $ids;
156 $ret = $this->o_queue->ids_exist($this->ids_to_remove);
157 $ret = $this->o_queue->get_entries_by_id($ret);
159 $tmp = "";
160 foreach($ret as $task){
161 if(isset($this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']])){
162 $evt = $this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']];
163 $tmp.= "\n".$task['ID']." - ".$evt['s_Menu_Name']." ".$task['MACADDRESS'];
164 }else{
165 $tmp.= "\n".$task['ID']." - ".$task['HEADERTAG']." ".$task['MACADDRESS'];
166 }
167 }
168 $smarty->assign("multiple", TRUE);
169 $smarty->assign("info",sprintf(_("You are about to remove the following actions from the GOsa support Daemon: %s"),"<pre>".$tmp."</pre>"));
170 $this->current = $s_entry;
171 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
172 }
173 }
174 }
176 /* Remove specified tasks */
177 if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
178 $this->o_queue->remove_entries($this->ids_to_remove);
179 $this->save();
180 }
182 /* Remove aborted */
183 if(isset($_POST['delete_cancel'])){
184 $this->ids_to_remove = array();;
185 }
188 /************
189 * EDIT
190 ************/
192 /* Close dialog */
193 if(isset($_POST['save_event_dialog'])){
194 if(is_object($this->dialog)){
195 $this->dialog->save_object();
196 if($this->dialog->is_new()){
197 $header = $this->dialog->get_schedule_action();
198 $targets = $this->dialog->get_targets();
199 $data = $this->dialog->save();
201 foreach($targets as $target){
202 $data['macaddress'] = $target;
203 $this->o_queue->send_data($header,$target,$data,TRUE);
204 if($this->o_queue->is_error()){
205 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
206 $this->o_queue->get_error()),ERROR_DIALOG);
207 }else{
208 $this->dialog = FALSE;
209 $this->current = -1;
210 }
211 }
212 }else{
213 $id = $this->dialog->get_id();
214 $data = $this->dialog->save();
215 if($this->o_queue->update_entries(array($id),$data)){
216 $this->dialog = FALSE;
217 $this->current = -1;
218 }else{
219 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
220 $this->o_queue->get_error()),ERROR_DIALOG);
221 }
222 }
223 }
224 }
227 /* Close dialog */
228 if(isset($_POST['abort_event_dialog'])){
229 $this->dialog = FALSE;
230 $this->current = -1;
231 }
233 /* Display dialogs if currently opened */
234 if(is_object($this->dialog)){
235 $this->dialog->save_object();
236 return($this->dialog->execute());
237 }
239 /************
240 * Handle Divlist
241 ************/
243 $divlist = new MultiSelectWindow($this->config,"gotoMasses",array("gotomasses"));
244 $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa deamon tasks."));
245 $divlist->SetSummary(_("List of queued deamon jobs"));
246 $divlist->EnableCloseButton(FALSE);
247 $divlist->EnableSaveButton(FALSE);
248 $divlist->SetHeadpageMode();
249 $s = ".|"._("Actions")."|\n";
250 $s.= "..|<img src='images/list_new.png' alt='' border='0' class='center'> "._("Create")."\n";
251 foreach($this->events['BY_CLASS'] as $name => $event){
252 $s.= "...|".$event['MenuImage']." ".$event['s_Menu_Name']."|add_event_".$name."\n";
253 }
254 if($this->acl_is_removeable()){
255 $s.= "..|---|\n";
256 $s.= "..|<img src='images/edittrash.png' alt='' border='0' class='center'> "._("Remove")."|remove_multiple\n";
257 }
258 if(preg_match("/w/",$this->getacl(""))){
259 $s.= "..|---|\n";
260 $s.= "..|<img src='images/status_start_all.png' alt='' border='0' class='center'> "._("Start all")."|start_all\n";
261 $s.= "..|<img src='images/status_stop_all.png' alt='' border='0' class='center'> "._("Stop all")."|stop_all\n";
262 }
264 $divlist->SetDropDownHeaderMenu($s);
266 if($this->sort_dir == "up"){
267 $sort_img = "<img src='images/sort_up.png' alt='/\' border=0>";
268 }else{
269 $sort_img = "<img src='images/sort_down.png' alt='\/' border=0>";
270 }
272 if($this->sort_by == "TargetName"){ $sort_img_1 = $sort_img; } else { $sort_img_1 = "" ;}
273 if($this->sort_by == "TaskID"){ $sort_img_2 = $sort_img; } else { $sort_img_2 = "" ;}
274 if($this->sort_by == "Schedule"){ $sort_img_3 = $sort_img; } else { $sort_img_3 = "" ;}
275 if($this->sort_by == "Action"){ $sort_img_4 = $sort_img; } else { $sort_img_4 = "" ;}
277 /* Create divlist */
278 $divlist->SetListHeader("<input type='image' src='images/list_reload.png' title='"._("Reload")."'>");
280 $plug = $_GET['plug'];
281 $chk = "<input type='checkbox' id='select_all' name='select_all'
282 onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
284 /* set Page header */
285 $divlist->AddHeader(array("string"=> $chk, "attach"=>"style='width:20px;'"));
286 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=TargetName'>"._("Target").$sort_img_1."</a>"));
287 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=TaskID'>"._("Task").$sort_img_2."</a>",
288 "attach"=>"style='width:120px;'"));
289 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
290 "attach"=>"style='width:100px;'"));
291 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=Action'>"._("Status").$sort_img_4."</a>",
292 "attach"=>"style='width:80px;'"));
293 $divlist->AddHeader(array("string"=>_("Action"),
294 "attach"=>"style='border-right:0px;width:120px;'"));
297 /* Reload the list of entries */
298 $this->reload();
300 foreach($this->entries as $key => $task){
302 $prio_actions = "<input class='center' type='image' src='images/prio_increase.png' name='prio_up_".$key."'> ";
303 $prio_actions.= "<input class='center' type='image' src='images/prio_decrease.png' name='prio_down_".$key."'> ";
304 $prio_actions.= "<input class='center' type='image' src='images/status_stop_all.png' name='stop_".$key."'> ";
305 $prio_actions.= "<input class='center' type='image' src='images/status_start_all.png' name='start_".$key."'> ";
307 $action = "<input type='image' src='images/edit.png' name='edit_task_".$key."'
308 class='center' alt='"._("Edit")."'>";
310 if($this->acl_is_removeable()){
311 $action.= "<input type='image' src='images/edittrash.png' name='remove_task_".$key."'
312 class='center' alt='"._("Reomve")."'>";
313 }
315 $color = "";
316 $display = $task['MACADDRESS'];
317 $display2= $task['HEADERTAG'];
319 /* Check if this event exists as Daemon class
320 * In this case, display a more accurate entry.
321 */
322 if(isset($this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']]['s_Menu_Name'])){
323 $event_type = $this->events['BY_QUEUED_ACTION'][$task['HEADERTAG']];
324 $display2= $event_type['s_Menu_Name'];
325 if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
326 $display2 = $event_type['ListImage']." ".$display2;
327 }
328 }
330 /* Create each field */
331 $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$key."'>" ,
332 "attach" => "style='width:20px;".$color."'");
333 $field1 = array("string" => $display,
334 "attach" => "style='".$color."'");
335 $field1a= array("string" => $display2,
336 "attach" => "style='".$color.";width:120px;'");
337 $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:100px;'");
338 $field3 = array("string" => $task['STATUS'],"attach" => "style='".$color.";width:80px;'");
339 $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:120px;border-right:0px;'");
340 $divlist->AddElement(array($field0,$field1,$field1a,$field2,$field3,$field4));
341 }
343 $smarty = get_smarty();
344 $smarty->assign("events",$this->events);
345 $smarty->assign("start",$this->start);
346 $smarty->assign("start_real", ($this->start + 1));
347 $smarty->assign("ranges", array("10" => "10",
348 "20" => "20",
349 "25" => "25",
350 "50" => "50",
351 "100"=> "100",
352 "200"=> "200",
353 "9999" => "*"));
355 $count = $this->o_queue->number_of_queued_entries();
356 $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"range"));
357 $smarty->assign("range",$this->range);
358 $smarty->assign("div",$divlist->Draw());
359 return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
360 }
363 /*! \brief Move an entry up or down in the queue, by updating its execution timestamp
364 @param $id Integer The ID of the entry which should be updated.
365 @param $type String "up" / "down"
366 @return boolean TRUE in case of success else FALSE
367 */
368 public function update_priority($id,$type = "up")
369 {
370 if($type == "up"){
371 $tmp = $this->o_queue->get_queued_entries(-1,-1,"timestamp DESC");
372 }else{
373 $tmp = $this->o_queue->get_queued_entries(-1,-1,"timestamp ASC");
374 }
375 $last = array();
376 foreach($tmp as $entry){
377 if($entry['ID'] == $id){
378 if(count($last)){
379 $time = strtotime($last['TIMESTAMP']);
380 if($type == "up"){
381 $time ++;
382 }else{
383 $time --;
384 }
385 $time_str = date("YmdHis",$time);
386 return($this->o_queue->update_entries(array($id),array("timestamp" => $time_str)));
387 }else{
388 return(FALSE);
389 }
390 }
391 $last = $entry;
392 }
393 return(FALSE);
394 }
397 /*! \brief Force queue job to be done as far as possible.
398 * @return Boolean TRUE in case of success, else FALSE.
399 */
400 private function start_queue_entries($ids)
401 {
402 if(!count($ids)){
403 return;
404 }
406 $data = array("timestamp" => date("YmdHis"),
407 "status" => "-");
408 if(!$this->o_queue->update_entries($ids,$data)){
409 msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
410 return(FALSE);
411 }
412 return(TRUE);
413 }
416 /*! \brief Stops the specified queue entry from execution.
417 * @return Boolean TRUE in case of success, else FALSE.
418 */
419 private function stop_queue_entries($ids)
420 {
421 if(!count($ids)){
422 return;
423 }
424 $data = array("timestamp" => date("YmdHis",(time() + (60*60*24*365))),
425 "status" => _("Stoppped"));
426 if(!$this->o_queue->update_entries($ids,$data)){
427 msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
428 return(FALSE);
429 }
430 return(TRUE);
431 }
434 /*! \brief Request list of queued jobs.
435 * @return Returns an array of all queued jobs.
436 */
437 function reload()
438 {
439 $map = array(
440 "QueuePosition" => "id",
441 "Action" => "status",
442 "TaskID" => "headertag",
443 "TargetName" => "macaddress",
444 "Schedule" => "timestamp");
446 if(!isset($map[$this->sort_by])){
447 $sort = "id DESC";
448 }else{
449 $sort = $map[$this->sort_by];
450 if($this->sort_dir == "up"){
451 $sort.= " ASC";
452 }else{
453 $sort.= " DESC";
454 }
455 }
457 $start = $this->start;
458 $stop = $this->range;
460 $entries = $this->o_queue->get_queued_entries($start,$stop,$sort);
461 if ($this->o_queue->is_error()){
463 msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
464 }
466 $this->entries = array();
467 foreach($entries as $entry){
468 $this->entries[$entry['ID']]= $entry;
469 }
470 }
473 /*! \brief Handle post jobs, like sorting.
474 */
475 function save_object()
476 {
477 $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
478 if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
479 $sort = $_GET['sort'];
480 if($this->sort_by == $sort){
481 if($this->sort_dir == "up"){
482 $this->sort_dir = "down";
483 }else{
484 $this->sort_dir = "up";
485 }
486 }
487 $this->sort_by = $sort;
488 }
489 if(isset($_POST['range']) && is_numeric($_POST['range'])){
490 $this->range = $_POST['range'];
491 }
492 if(isset($_GET['start'])){
493 $start = $_GET['start'];
494 if(is_numeric($start) || $start == 0){
495 $this->start = $start;
496 }
497 }
499 /* Check start stop and reset if necessary */
500 $count = $this->o_queue->number_of_queued_entries();
501 if($this->start >= $count){
502 $this->start = $count -1;
503 }
504 if($this->start < 0){
505 $this->start = 0;
506 }
507 }
510 function save()
511 {
512 // We do not save anything here.
513 }
516 /*! \brief Return a list of all selected items.
517 @return Array Returns an array containing all selected item ids.
518 */
519 function list_get_selected_items()
520 {
521 $ids = array();
522 foreach($_POST as $name => $value){
523 if(preg_match("/^item_selected_[0-9]*$/",$name)){
524 $id = preg_replace("/^item_selected_/","",$name);
525 $ids[$id] = $id;
526 }
527 }
528 return($ids);
529 }
532 static function plInfo()
533 {
534 return (array(
535 "plShortName" => _("System mass deployment"),
536 "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
537 "plSelfModify" => FALSE,
538 "plDepends" => array(),
539 "plPriority" => 0,
540 "plSection" => array("addon"),
541 "plCategory" => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
542 "plProvidedAcls" => array("Comment" => _("Description"))
543 ));
544 }
545 }
546 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
547 ?>