Code

Updated gotomasses.
[gosa.git] / gosa-plugins / goto / addons / gotomasses / class_gotomasses.inc
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();
39  
40     /************
41      * Handle posts 
42      ************/
43     
44     $s_entry = $s_action = "";
45     $arr = array( 
46         "/^stop_/"        => "stop",
47         "/^stop_all/"     => "stop_all",
48         "/^start_/"       => "start",
49         "/^start_all/"    => "start_all",
51         "/^prio_up_/"     => "prio_up",
52         "/^prio_down_/"   => "prio_down",
54         "/^edit_task_/"             =>  "edit",
55         "/^remove_task_/"           =>  "remove",
56         "/^new_task_/"              =>  "new_task",
57         "/^remove_multiple_task_/"  => "remove_multiple");
59     foreach($arr as $regex => $action){
60       foreach($_POST as $name => $value){
61         if(preg_match($regex,$name)){
62           $s_action = $action;
63           $s_entry  = preg_replace($regex,"",$name);
64           $s_entry  = preg_replace("/_(x|y)$/","",$s_entry);
65         }
66       }
67     }
69     /* Menu actions */
70     if(isset($_POST['menu_action']) && !empty($_POST['menu_action'])){
71       $s_action = $_POST['menu_action'];
72     }
73     
74     /* Edit posted from list link */
75     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
76       $s_action = "edit";
77       $s_entry = $_GET['id'];
78     }
81     /************
82      * Handle Priority modifications  
83      ************/
85     if(preg_match("/^prio_/",$s_action)){
87       switch($s_action){
89         case 'prio_down'  : $this->update_priority($s_entry,"down");break;
90         case 'prio_up'    : $this->update_priority($s_entry,"up");break;
91       }
92     }
94     /************
95      * Handle start/stop modifications  
96      ************/
98     if(preg_match("/^start/",$s_action) || preg_match("/^stop/",$s_action)){
100       switch($s_action){
101         case 'start_all'   : $this->start_queue_entries($this->list_get_selected_items());break;
102         case 'start'       : $this->start_queue_entries(array($s_entry));break; 
103         case 'stop_all'    : $this->stop_queue_entries ($this->list_get_selected_items());break;
104         case 'stop'        : $this->stop_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      ************/
115   
116     if(preg_match("/^add_event_/",$s_action)){
117       $type = preg_replace("/^add_event_/","",$s_action);
118       if(isset($this->events[$type])){
119         $e_data = $this->events[$type];
120         $this->dialog = new $e_data['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_EDIT_ACTION'][$event['HEADERTAG']])){
134           $type = $this->events['BY_EDIT_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             $tmp.= "\n".$task['ID']." - ".$task['HEADERTAG']."&nbsp;".$task['MACADDRESS'];
164           }
165           $smarty->assign("multiple", TRUE); 
166           $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
167           $this->current = $s_entry;
168           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
169         }
170       }
171     }
173     /* Remove specified tasks */
174     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
175       $this->o_queue->remove_entries($this->ids_to_remove);
176       $this->save();
177     }
179     /* Remove aborted */
180     if(isset($_POST['delete_cancel'])){
181       $this->ids_to_remove = array();;
182     }
185     /************
186      * EDIT 
187      ************/
189     /* Close dialog */
190     if(isset($_POST['save_event_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(); 
197         
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       }
209     }
212     /* Close dialog */
213     if(isset($_POST['abort_event_dialog'])){
214       $this->dialog = FALSE;
215       $this->current = -1;
216     }
218     /* Display dialogs if currently opened */
219     if(is_object($this->dialog)){
220       $this->dialog->save_object();
221       return($this->dialog->execute());
222     }
224     /************
225      * Handle Divlist 
226      ************/
228     $divlist = new MultiSelectWindow($this->config,"gotoMasses",array("gotomasses"));
229     $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa deamon tasks."));
230     $divlist->SetSummary(_("List of queued deamon jobs."));
231     $divlist->EnableCloseButton(FALSE);
232     $divlist->EnableSaveButton(FALSE);
233     $divlist->SetHeadpageMode();
234     $s = ".|"._("Actions")."|\n";
235     $s.= "..|<img src='images/list_new.png' alt='' border='0' class='center'>&nbsp;"._("Create")."\n";
236     foreach($this->events['BY_CLASS'] as $name =>  $event){
237       $s.= "...|".$event['MenuImage']."&nbsp;".$event['s_Menu_Name']."|add_event_".$name."\n";
238     }
240     $divlist->SetDropDownHeaderMenu($s);
242     if($this->sort_dir == "up"){
243       $sort_img = "<img src='images/sort_up.png' alt='/\' border=0>";
244     }else{
245       $sort_img = "<img src='images/sort_down.png' alt='\/' border=0>";
246     }
248     if($this->sort_by == "TargetName"){ $sort_img_1 = $sort_img; } else { $sort_img_1 = "" ;}
249     if($this->sort_by == "TaskID"){ $sort_img_2 = $sort_img; } else { $sort_img_2 = "" ;}
250     if($this->sort_by == "Schedule"){ $sort_img_3 = $sort_img; } else { $sort_img_3 = "" ;}
251     if($this->sort_by == "Action"){ $sort_img_4 = $sort_img; } else { $sort_img_4 = "" ;}
253     /* Display add button if allowed */
254     $header = "<div style='padding:5px'>";
255     if($this->acl_is_removeable()){
256       $header .= "<input type='image' src='images/edittrash.png' name='remove_multiple_task' class='center'>&nbsp;";
257     }
258     if(preg_match("/w/",$this->getacl(""))){
259       $header .= "<input type='image' src='images/status_stop_all.png' name='stop_all' class='center'>&nbsp;";
260       $header .= "<input type='image' src='images/status_start_all.png' name='start_all' class='center'>&nbsp;";
261     }
262     $header .= "</div>";
264     /* Create divlist */
265     $divlist->SetListHeader($header);
267     $plug  = $_GET['plug'];
268     $chk = "<input type='checkbox' id='select_all' name='select_all'
269                onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
271     /* set Page header */
272     $divlist->AddHeader(array("string"=> $chk,          "attach"=>"style='width:20px;'"));
273     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TargetName'>"._("Target").$sort_img_1."</a>"));
274     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=TaskID'>"._("Task").$sort_img_2."</a>",
275                                       "attach"=>"style='width:80px;'"));
276     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
277                                       "attach"=>"style='width:100px;'"));
278     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=Action'>"._("Status").$sort_img_4."</a>",
279                                       "attach"=>"style='width:80px;'"));
280     $divlist->AddHeader(array("string"=>_("Action"),
281                                       "attach"=>"style='border-right:0px;width:120px;'"));
284     /* Reload the list of entries */
285     $this->reload();
287     foreach($this->entries as $key => $task){
289       $prio_actions = "<input class='center' type='image' src='images/prio_increase.png' name='prio_up_".$task['ID']."'>&nbsp;";
290       $prio_actions.= "<input class='center' type='image' src='images/prio_decrease.png' name='prio_down_".$task['ID']."'>&nbsp;";
291       $prio_actions.= "<input class='center' type='image' src='images/status_stop_all.png' name='stop_".$task['ID']."'>&nbsp;";
292       $prio_actions.= "<input class='center' type='image' src='images/status_start_all.png' name='start_".$task['ID']."'>&nbsp;";
294       $action = "<input type='image' src='images/edit.png' name='edit_task_".$task['ID']."' 
295         class='center' alt='"._("Edit")."'>";
297       if($this->acl_is_removeable()){
298         $action.= "<input type='image' src='images/edittrash.png' name='remove_task_".$task['ID']."' 
299           class='center' alt='"._("Reomve")."'>";
300       }
302       $color = "";
303       $display = $task['MACADDRESS'];
304       $display2= $task['HEADERTAG'];
306       if( isset($this->events['BY_EDIT_ACTION'][$display2]['ListImage']) && 
307          !empty($this->events['BY_EDIT_ACTION'][$display2]['ListImage'])){
308         $display2 = $this->events['BY_EDIT_ACTION'][$display2]['ListImage'];
309       }
311       /* Create each field */
312       $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$task['ID']."'>" ,
313                       "attach" => "style='width:20px;".$color."'");
314       $field1 = array("string" => $display,
315                       "attach" => "style='".$color."'");
316       $field1a= array("string" => $display2,
317                       "attach" => "style='".$color.";width:80px; text-align:right;'");
318       $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:100px;'");
319       $field3 = array("string" => $task['STATUS'],"attach" => "style='".$color.";width:80px;'");
320       $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:120px;border-right:0px;'");
321       $divlist->AddElement(array($field0,$field1,$field1a,$field2,$field3,$field4));
322     }
324     $smarty = get_smarty();
325     $smarty->assign("events",$this->events);
326     $smarty->assign("start",$this->start);
327     $smarty->assign("start_real", ($this->start + 1));
328     $smarty->assign("ranges", array("10" => "10",
329                                     "20" => "20",
330                                     "25" => "25",
331                                     "50" => "50",
332                                     "100"=> "100",
333                                     "200"=> "200",
334                                     "9999" => "*"));
336     $count = $this->o_queue->number_of_queued_entries();
337     $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"range"));
338     $smarty->assign("range",$this->range);
339     $smarty->assign("div",$divlist->Draw());
340     return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
341   }
344   /*! \brief  Move an entry up or down in the queue, by updating its execution timestamp  
345       @param  $id     Integer  The ID of the entry which should be updated.
346       @param  $type   String   "up" / "down"
347       @return boolean TRUE in case of success else FALSE
348   */
349   public function update_priority($id,$type = "up")
350   {
351     if($type == "up"){
352       $tmp = $this->o_queue->get_queued_entries(-1,-1,"timestamp DESC");
353     }else{
354       $tmp = $this->o_queue->get_queued_entries(-1,-1,"timestamp ASC");
355     }
356     $last = array();
357     foreach($tmp as $entry){
358       if($entry['ID'] == $id){
359         if(count($last)){
360           $time = strtotime($last['TIMESTAMP']);
361           if($type == "up"){
362             $time ++;
363           }else{
364             $time --;
365           }
366           $time_str = date("YmdHis",$time); 
367           return($this->o_queue->update_entries(array($id),array("timestamp" => $time_str)));
368         }else{
369           return(FALSE);
370         }
371       }
372       $last = $entry;
373     }
374     return(FALSE);
375   }
378   /*! \brief  Force queue job to be done as far as possible.
379    *  @return Boolean TRUE in case of success, else FALSE. 
380    */
381   private function start_queue_entries($ids)
382   {
383     if(!count($ids)){
384       return;
385     }
386   
387     $data = array("timestamp" => date("YmdHis"),
388                   "status"    => "-");
389     if(!$this->o_queue->update_entries($ids,$data)){
390       msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
391       return(FALSE);
392     }
393     return(TRUE);
394   }
397   /*! \brief Stops the specified queue entry from execution.
398    *  @return Boolean TRUE in case of success, else FALSE. 
399    */
400   private function stop_queue_entries($ids)
401   {
402     if(!count($ids)){
403       return;
404     }
405     $data = array("timestamp" => date("YmdHis",(time() + (60*60*24*365))),
406                   "status"    => _("Stoppped"));
407     if(!$this->o_queue->update_entries($ids,$data)){
408       msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
409       return(FALSE);
410     }
411     return(TRUE);
412   }
415   /*! \brief  Request list of queued jobs.
416    *  @return Returns an array of all queued jobs.
417    */
418   function reload()
419   {
420     $map = array(
421         "QueuePosition" => "id",
422         "Action"        => "status",
423         "TaskID"        => "headertag",
424         "TargetName"    => "macaddress",
425         "Schedule"      => "timestamp");
427     if(!isset($map[$this->sort_by])){
428       $sort = "id DESC";
429     }else{
430       $sort   = $map[$this->sort_by]; 
431       if($this->sort_dir == "up"){
432         $sort.= " ASC";
433       }else{
434         $sort.= " DESC";
435       }
436     }
437       
438     $start  = $this->start; 
439     $stop   = $this->range;
441     $entries = $this->o_queue->get_queued_entries($start,$stop,$sort);
442     if ($this->o_queue->is_error()){
444       msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
445     }
447     $this->entries = array();
448     foreach($entries as $entry){
449       $this->entries[]= $entry;
450     }
451   }
454   /*! \brief  Handle post jobs, like sorting.
455    */
456   function save_object()
457   {
458     $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
459     if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
460       $sort = $_GET['sort'];
461       if($this->sort_by == $sort){
462         if($this->sort_dir == "up"){
463           $this->sort_dir = "down";
464         }else{
465           $this->sort_dir = "up";
466         }
467       }
468       $this->sort_by = $sort;
469     }
470     if(isset($_POST['range']) && is_numeric($_POST['range'])){
471       $this->range = $_POST['range'];
472     }
473     if(isset($_GET['start'])){
474       $start = $_GET['start'];
475       if(is_numeric($start) || $start == 0){
476         $this->start = $start;
477       }
478     }
480     /* Check start stop and reset if necessary */
481     $count = $this->o_queue->number_of_queued_entries();
482     if($this->start >= $count){
483       $this->start = $count -1;
484     }
485     if($this->start < 0){
486       $this->start = 0;
487     }
488   }
491   /* Return list of object groups */
492   function get_object_groups()
493   {
494     $ret = array();
495     $ldap = $this->config->get_ldap_link();
496     $ldap->cd($this->config->current['BASE']);
497     $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
498     while($attrs = $ldap->fetch()){
499       $ret[$attrs['cn'][0]] = $attrs['cn'][0];
500     }
501     return($ret); 
502   }
505   function save()
506   {
507     // We do not save anything here.
508   }
511   /*! \brief  Return a list of all selected items.
512     @return Array   Returns an array containing all selected item ids.
513    */
514   function list_get_selected_items()
515   {
516     $ids = array();
517     foreach($_POST as $name => $value){
518       if(preg_match("/^item_selected_[0-9]*$/",$name)){
519         $id   = preg_replace("/^item_selected_/","",$name);
520         $ids[$id] = $id;
521       }
522     }
523     return($ids);
524   }
527   static function plInfo()
528   {
529     return (array(
530           "plShortName"   => _("System mass deployment"),
531           "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
532           "plSelfModify"  => FALSE,
533           "plDepends"     => array(),
534           "plPriority"    => 0,
535           "plSection"     => array("addon"),
536           "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
537           "plProvidedAcls" => array("Comment"   => _("Description"), 
538             "Action"    => _("Action"),
539             "Day"       => _("Day"),
540             "Minute"    => _("Minute"),
541             "Hour"      => _("Hour"),
542             "Month"     => _("Month"),
543             "Weekday"   => _("Week day"),
544             "Target"    => _("Target"))
545           ));
546   }
548 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
549 ?>