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_EDIT_ACTION'][$event['HEADERTAG']])){
132 $type = $this->events['BY_EDIT_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 $tmp.= "\n".$task['ID']." - ".$task['HEADERTAG']." ".$task['MACADDRESS'];
162 }
163 $smarty->assign("multiple", TRUE);
164 $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
165 $this->current = $s_entry;
166 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
167 }
168 }
169 }
171 /* Remove specified tasks */
172 if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
173 $this->o_queue->remove_entries($this->ids_to_remove);
174 $this->save();
175 }
177 /* Remove aborted */
178 if(isset($_POST['delete_cancel'])){
179 $this->ids_to_remove = array();;
180 }
183 /************
184 * EDIT
185 ************/
187 /* Close dialog */
188 if(isset($_POST['save_event_dialog'])){
190 if(is_object($this->dialog)){
192 if($this->dialog->is_new()){
193 $header = $this->dialog->get_header_tag();
194 $targets = $this->dialog->get_targets();
195 $data = $this->dialog->save();
196 $data['timestamp'] = $this->dialog->get_timestamp();
199 foreach($targets as $target){
200 $data['mac'] = $target;
201 if(!$this->o_queue->send($header,$target,$data,TRUE)){
202 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
203 $this->o_queue->get_error()),ERROR_DIALOG);
204 }
205 }
206 $this->dialog = FALSE;
207 $this->current = -1;
208 }else{
209 $id = $this->dialog->get_id();
210 $data = $this->dialog->save();
211 $data['timestamp'] = $this->dialog->get_timestamp();
212 if($this->o_queue->update_entries(array($id),$data)){
213 $this->dialog = FALSE;
214 $this->current = -1;
215 }
216 }
217 }
218 }
221 /* Close dialog */
222 if(isset($_POST['abort_event_dialog'])){
223 $this->dialog = FALSE;
224 $this->current = -1;
225 }
227 /* Display dialogs if currently opened */
228 if(is_object($this->dialog)){
229 $this->dialog->save_object();
230 return($this->dialog->execute());
231 }
233 /************
234 * Handle Divlist
235 ************/
237 $divlist = new MultiSelectWindow($this->config,"gotoMasses",array("gotomasses"));
238 $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa deamon tasks."));
239 $divlist->SetSummary(_("List of queued deamon jobs."));
240 $divlist->EnableCloseButton(FALSE);
241 $divlist->EnableSaveButton(FALSE);
242 $divlist->SetHeadpageMode();
243 $s = ".|"._("Actions")."|\n";
244 $s.= "..|<img src='images/list_new.png' alt='' border='0' class='center'> "._("Create")."\n";
245 foreach($this->events['BY_CLASS'] as $name => $event){
246 $s.= "...|".$event['MenuImage']." ".$event['s_Menu_Name']."|add_event_".$name."\n";
247 }
248 if($this->acl_is_removeable()){
249 $s.= "..|---|\n";
250 $s.= "..|<img src='images/edittrash.png' alt='' border='0' class='center'> "._("Remove")."|remove_multiple\n";
251 }
252 if(preg_match("/w/",$this->getacl(""))){
253 $s.= "..|---|\n";
254 $s.= "..|<img src='images/status_start_all.png' alt='' border='0' class='center'> "._("Start all")."|start_all\n";
255 $s.= "..|<img src='images/status_stop_all.png' alt='' border='0' class='center'> "._("Stop all")."|stop_all\n";
256 }
258 $divlist->SetDropDownHeaderMenu($s);
260 if($this->sort_dir == "up"){
261 $sort_img = "<img src='images/sort_up.png' alt='/\' border=0>";
262 }else{
263 $sort_img = "<img src='images/sort_down.png' alt='\/' border=0>";
264 }
266 if($this->sort_by == "TargetName"){ $sort_img_1 = $sort_img; } else { $sort_img_1 = "" ;}
267 if($this->sort_by == "TaskID"){ $sort_img_2 = $sort_img; } else { $sort_img_2 = "" ;}
268 if($this->sort_by == "Schedule"){ $sort_img_3 = $sort_img; } else { $sort_img_3 = "" ;}
269 if($this->sort_by == "Action"){ $sort_img_4 = $sort_img; } else { $sort_img_4 = "" ;}
271 /* Create divlist */
272 $divlist->SetListHeader("<input type='image' src='images/list_reload.png' title='"._("Reload")."'>");
274 $plug = $_GET['plug'];
275 $chk = "<input type='checkbox' id='select_all' name='select_all'
276 onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
278 /* set Page header */
279 $divlist->AddHeader(array("string"=> $chk, "attach"=>"style='width:20px;'"));
280 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=TargetName'>"._("Target").$sort_img_1."</a>"));
281 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=TaskID'>"._("Task").$sort_img_2."</a>",
282 "attach"=>"style='width:120px;'"));
283 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
284 "attach"=>"style='width:100px;'"));
285 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=Action'>"._("Status").$sort_img_4."</a>",
286 "attach"=>"style='width:80px;'"));
287 $divlist->AddHeader(array("string"=>_("Action"),
288 "attach"=>"style='border-right:0px;width:120px;'"));
291 /* Reload the list of entries */
292 $this->reload();
294 foreach($this->entries as $key => $task){
296 $prio_actions = "<input class='center' type='image' src='images/prio_increase.png' name='prio_up_".$key."'> ";
297 $prio_actions.= "<input class='center' type='image' src='images/prio_decrease.png' name='prio_down_".$key."'> ";
298 $prio_actions.= "<input class='center' type='image' src='images/status_stop_all.png' name='stop_".$key."'> ";
299 $prio_actions.= "<input class='center' type='image' src='images/status_start_all.png' name='start_".$key."'> ";
301 $action = "<input type='image' src='images/edit.png' name='edit_task_".$key."'
302 class='center' alt='"._("Edit")."'>";
304 if($this->acl_is_removeable()){
305 $action.= "<input type='image' src='images/edittrash.png' name='remove_task_".$key."'
306 class='center' alt='"._("Reomve")."'>";
307 }
309 $color = "";
310 $display = $task['MACADDRESS'];
311 $display2= $task['HEADERTAG'];
313 /* Check if this event exists as Daemon class
314 * In this case, display a more accurate entry.
315 */
316 if(isset($this->events['BY_EDIT_ACTION'][$task['HEADERTAG']]['s_Menu_Name'])){
317 $event_type = $this->events['BY_EDIT_ACTION'][$task['HEADERTAG']];
318 $display2= $event_type['s_Menu_Name'];
319 if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
320 $display2 = $event_type['ListImage']." ".$display2;
321 }
322 }
324 /* Create each field */
325 $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$key."'>" ,
326 "attach" => "style='width:20px;".$color."'");
327 $field1 = array("string" => $display,
328 "attach" => "style='".$color."'");
329 $field1a= array("string" => $display2,
330 "attach" => "style='".$color.";width:120px;'");
331 $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:100px;'");
332 $field3 = array("string" => $task['STATUS'],"attach" => "style='".$color.";width:80px;'");
333 $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:120px;border-right:0px;'");
334 $divlist->AddElement(array($field0,$field1,$field1a,$field2,$field3,$field4));
335 }
337 $smarty = get_smarty();
338 $smarty->assign("events",$this->events);
339 $smarty->assign("start",$this->start);
340 $smarty->assign("start_real", ($this->start + 1));
341 $smarty->assign("ranges", array("10" => "10",
342 "20" => "20",
343 "25" => "25",
344 "50" => "50",
345 "100"=> "100",
346 "200"=> "200",
347 "9999" => "*"));
349 $count = $this->o_queue->number_of_queued_entries();
350 $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"range"));
351 $smarty->assign("range",$this->range);
352 $smarty->assign("div",$divlist->Draw());
353 return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
354 }
357 /*! \brief Move an entry up or down in the queue, by updating its execution timestamp
358 @param $id Integer The ID of the entry which should be updated.
359 @param $type String "up" / "down"
360 @return boolean TRUE in case of success else FALSE
361 */
362 public function update_priority($id,$type = "up")
363 {
364 if($type == "up"){
365 $tmp = $this->o_queue->get_queued_entries(-1,-1,"timestamp DESC");
366 }else{
367 $tmp = $this->o_queue->get_queued_entries(-1,-1,"timestamp ASC");
368 }
369 $last = array();
370 foreach($tmp as $entry){
371 if($entry['ID'] == $id){
372 if(count($last)){
373 $time = strtotime($last['TIMESTAMP']);
374 if($type == "up"){
375 $time ++;
376 }else{
377 $time --;
378 }
379 $time_str = date("YmdHis",$time);
380 return($this->o_queue->update_entries(array($id),array("timestamp" => $time_str)));
381 }else{
382 return(FALSE);
383 }
384 }
385 $last = $entry;
386 }
387 return(FALSE);
388 }
391 /*! \brief Force queue job to be done as far as possible.
392 * @return Boolean TRUE in case of success, else FALSE.
393 */
394 private function start_queue_entries($ids)
395 {
396 if(!count($ids)){
397 return;
398 }
400 $data = array("timestamp" => date("YmdHis"),
401 "status" => "-");
402 if(!$this->o_queue->update_entries($ids,$data)){
403 msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
404 return(FALSE);
405 }
406 return(TRUE);
407 }
410 /*! \brief Stops the specified queue entry from execution.
411 * @return Boolean TRUE in case of success, else FALSE.
412 */
413 private function stop_queue_entries($ids)
414 {
415 if(!count($ids)){
416 return;
417 }
418 $data = array("timestamp" => date("YmdHis",(time() + (60*60*24*365))),
419 "status" => _("Stoppped"));
420 if(!$this->o_queue->update_entries($ids,$data)){
421 msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
422 return(FALSE);
423 }
424 return(TRUE);
425 }
428 /*! \brief Request list of queued jobs.
429 * @return Returns an array of all queued jobs.
430 */
431 function reload()
432 {
433 $map = array(
434 "QueuePosition" => "id",
435 "Action" => "status",
436 "TaskID" => "headertag",
437 "TargetName" => "macaddress",
438 "Schedule" => "timestamp");
440 if(!isset($map[$this->sort_by])){
441 $sort = "id DESC";
442 }else{
443 $sort = $map[$this->sort_by];
444 if($this->sort_dir == "up"){
445 $sort.= " ASC";
446 }else{
447 $sort.= " DESC";
448 }
449 }
451 $start = $this->start;
452 $stop = $this->range;
454 $entries = $this->o_queue->get_queued_entries($start,$stop,$sort);
455 if ($this->o_queue->is_error()){
457 msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
458 }
460 $this->entries = array();
461 foreach($entries as $entry){
462 $this->entries[$entry['ID']]= $entry;
463 }
464 }
467 /*! \brief Handle post jobs, like sorting.
468 */
469 function save_object()
470 {
471 $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
472 if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
473 $sort = $_GET['sort'];
474 if($this->sort_by == $sort){
475 if($this->sort_dir == "up"){
476 $this->sort_dir = "down";
477 }else{
478 $this->sort_dir = "up";
479 }
480 }
481 $this->sort_by = $sort;
482 }
483 if(isset($_POST['range']) && is_numeric($_POST['range'])){
484 $this->range = $_POST['range'];
485 }
486 if(isset($_GET['start'])){
487 $start = $_GET['start'];
488 if(is_numeric($start) || $start == 0){
489 $this->start = $start;
490 }
491 }
493 /* Check start stop and reset if necessary */
494 $count = $this->o_queue->number_of_queued_entries();
495 if($this->start >= $count){
496 $this->start = $count -1;
497 }
498 if($this->start < 0){
499 $this->start = 0;
500 }
501 }
504 function save()
505 {
506 // We do not save anything here.
507 }
510 /*! \brief Return a list of all selected items.
511 @return Array Returns an array containing all selected item ids.
512 */
513 function list_get_selected_items()
514 {
515 $ids = array();
516 foreach($_POST as $name => $value){
517 if(preg_match("/^item_selected_[0-9]*$/",$name)){
518 $id = preg_replace("/^item_selected_/","",$name);
519 $ids[$id] = $id;
520 }
521 }
522 return($ids);
523 }
526 static function plInfo()
527 {
528 return (array(
529 "plShortName" => _("System mass deployment"),
530 "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
531 "plSelfModify" => FALSE,
532 "plDepends" => array(),
533 "plPriority" => 0,
534 "plSection" => array("addon"),
535 "plCategory" => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
536 "plProvidedAcls" => array("Comment" => _("Description"),
537 ));
538 }
539 }
540 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
541 ?>