Code

Updated gotomasses.
[gosa.git] / gosa-core / plugins / 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 $sort_by  = "QueuePosition";
20   var $sort_dir = "up";
22   var $range    = 25;
23   var $start    = 0;
25   function gotomasses(&$config, $dn= NULL)
26   {
27     /* Include config object */
28     $this->config= &$config;
29     $this->divlist = new divListMasses($this->config,$this);
30     $this->o_queue = new gosaSupportDaemon("10.3.67.111","20081","secret-gosa-password",TRUE,10);
31   }
34   function execute()
35   {
36     $smarty = get_smarty();
38     /************
39      * Handle posts 
40      ************/
42     $s_entry = $s_action = "";
43     $arr = array( 
44         "/^stop_/"      => "stop",
45         "/^stop_all/"  => "stop_all",
46         "/^start_/"      => "start",
47         "/^start_all/"  => "start_all",
49         "/^edit_task_/"             =>  "edit",
50         "/^remove_task_/"           =>  "remove",
51         "/^new_task_/"              =>  "new_task",
52         "/^remove_multiple_task_/"  => "remove_multiple");
54     foreach($arr as $regex => $action){
55       foreach($_POST as $name => $value){
56         if(preg_match($regex,$name)){
57           $s_action = $action;
58           $s_entry  = preg_replace($regex,"",$name);
59           $s_entry  = preg_replace("/_(x|y)$/","",$s_entry);
60         }
61       }
62     }
64     /* Edit posted from list link */
65     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
66       $s_action = "edit";
67       $s_entry = $_GET['id'];
68     }
71     /************
72      * Handle Priority modifications  
73      ************/
75     if(preg_match("/^start/",$s_action) || preg_match("/^stop/",$s_action)){
77       switch($s_action){
78         case 'start_all'   : $this->start_queue_entries($this->list_get_selected_items());break;
79         case 'start'       : $this->start_queue_entries(array($s_entry));break; 
80         case 'stop_all'    : $this->stop_queue_entries ($this->list_get_selected_items());break;
81         case 'stop'        : $this->stop_queue_entries (array($s_entry));break; 
82         default : trigger_error("Undefined priority setting used.");
83       }
84       if($this->o_queue->is_error()){
85         msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
86       }
87     }
89     /************
90      * REMOVE 
91      ************/
93     /* Remove multiple */
94     if($s_action == "remove_multiple" || $s_action == "remove"){
96       if(!$this->acl_is_removeable()){
97         msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), ERROR_DIALOG);
98       }else{
100         if($s_action == "remove"){
101           $ids = array($s_entry);
102         }else{
103           $ids = $this->list_get_selected_items();
104         }
106         $this->ids_to_remove = $ids;
107         $tmp = "";
109         $ret = $this->o_queue->ids_exist($this->ids_to_remove);
110         $ret = $this->o_queue->get_entries_by_id($ret);
112         foreach($ret as $task){
113            $tmp.= "\n".$task['HEADERTAG']."&nbsp;".$task['MACADDRESS'];
114        }
115        $smarty->assign("multiple", TRUE); 
116        $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
117        $this->current = $s_entry;
118        return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
119       }
120     }
122     /* Remove specified tasks */
123     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
124       $this->o_queue->remove_entries($this->ids_to_remove);
125       $this->save();
126     }
128     /* Remove aborted */
129     if(isset($_POST['delete_cancel'])){
130       $this->ids_to_remove = array();;
131     }
134     /************
135      * EDIT 
136      ************/
138     /* Edit selected entry */
139     if($s_action == "edit"){
140       $entry = $this->o_queue->get_entry_by_id($s_entry);
141       if($entry){
142         $this->dialog = new goto_task($this->config,$this,$entry);
143         $this->current = $s_entry;
144       }
145     }
147     /* New entry */
148     if($s_action== "new_task" && $this->acl_is_createable()){
149       $this->dialog = new goto_task($this->config,$this);
150       $this->current = -1;
151     }
153     /* Close dialog */
154     if(isset($_POST['close_goto_task'])){
155       $this->dialog = FALSE;
156       $this->current = -1;
157     }
159     /* Close dialog */
160     if((isset($_POST['save_goto_task']) || isset($_POST['apply_goto_task'])) && is_object($this->dialog) ){
161       $this->dialog->save_object();
162       $msgs = $this->dialog->check();
163       if(count($msgs)){
164         foreach($msgs as $msg){
165           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
166         }
167       }else{  
169         if($this->o_queue->id_exists($this->current)){
170           $this->o_queue->update_entry($this->current,$this->dialog->save());
171         }else{
172           $tmp = $this->dialog->save();
173           $tmp2= array();
174           $targets =$tmp['Target'];
175           foreach($targets as $target){
176             $tmp['Target'] = array($target);
177             $tmp2[] = $tmp;
178           }
179           if(!$this->o_queue->add_multiple($tmp2)){
180             msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
181           }
182         }
183         if(!isset($_POST['apply_goto_task'])){
184           $this->dialog = FALSE;
185           $this->current = -1;
186         }
187         $this->save();
188       }
189     }
191     /* Display dialogs if currently opened */
192     if(is_object($this->dialog)){
193       $this->dialog->save_object();
194       return($this->dialog->execute());
195     }
197     /************
198      * Handle Divlist 
199      ************/
201     $this->divlist->execute();
202     $entries = $this->get_queued_entries();
203     $this->divlist->SetEntries($entries);
206     $smarty = get_smarty();
207     $smarty->assign("start",$this->start);
208     $smarty->assign("start_real", ($this->start + 1));
209     $smarty->assign("ranges", array("10" => "10",
210                                     "20" => "20",
211                                     "25" => "25",
212                                     "50" => "50",
213                                     "100"=> "100",
214                                     "200"=> "200",
215                                     "9999" => "*"));
216     $smarty->assign("range",$this->range);
217     $smarty->assign("div",$this->divlist->Draw());
218     return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
219   }
222   /*! \brief  Force queue job to be done as far as possible.
223    *  @return Boolean TRUE in case of success, else FALSE. 
224    */
225   private function start_queue_entries($ids)
226   {
227     $data = array("timestamp" => date("YmdHis"),
228                   "status"    => "-");
229     if(!$this->o_queue->update_entries($ids,$data)){
230       msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
231       return(FALSE);
232     }
233     return(TRUE);
234   }
237   /*! \brief Stops the specified queue entry from execution.
238    *  @return Boolean TRUE in case of success, else FALSE. 
239    */
240   private function stop_queue_entries($ids)
241   {
242     $data = array("timestamp" => "20490101010101",
243                   "status"    => "Stoppped");
244     if(!$this->o_queue->update_entries($ids,$data)){
245       msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
246       return(FALSE);
247     }
248     return(TRUE);
249   }
252   /*! \brief  Request list of queued jobs.
253    *  @return Returns an array of all queued jobs.
254    */
255   function get_queued_entries()
256   {
257     $start  = $this->range * $this->start; 
258     $stop   = $start + $this->range; 
259     $entries = $this->o_queue->get_queued_entries($start,$stop);
260     if(!is_array($entries)){
261       msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
262       return(array());
263     }
265     $tasks = array();
266     $ret = array();
267     foreach($entries as $entry){
268       $task = $entry['ID']; 
269       $ret[]= $entry;
270     }
271     $map = array("QueuePosition" => "ID",
272         "Action"        => "STATUS",
273         "TaskID"        => "HEADERTAG",
274         "TargetName"    => "MACADDRESS",
275         "Schedule"      => "TIMESTAMP");
276     $sort_tmp = array();
277     foreach($ret as $entry_id => $entry){
278       $sort_tmp[$entry_id] = $entry[$map[$this->sort_by]];
279     } 
280     natcasesort($sort_tmp);
281     $return = array();
282     foreach($sort_tmp as $entry_id => $value){
283       $return[] = $ret[$entry_id];
284     } 
285     if($this->sort_dir != "up"){
286       $return = array_reverse($return);
287     } 
288     return($return);
289   }
292   /*! \brief  Handle post jobs, like sorting.
293    */
294   function save_object()
295   {
296     $this->divlist->save_object();
297     $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
298     if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
299       $sort = $_GET['sort'];
300       if($this->sort_by == $sort){
301         if($this->sort_dir == "up"){
302           $this->sort_dir = "down";
303         }else{
304           $this->sort_dir = "up";
305         }
306       }
307       $this->sort_by = $sort;
308     }
309     if(isset($_POST['range']) && is_numeric($_POST['range'])){
310       $this->range = $_POST['range'];
311     }
312     if(isset($_POST['next_page'])){
313       $this->start ++;
314     }
315     if(isset($_POST['last_page'])){
316       $this->start --;
317       if($this->start < 0 ){
318         $this->start = 0;
319       }
320     }
321   }
324   /* Return list of object groups */
325   function get_object_groups()
326   {
327     $ret = array();
328     $ldap = $this->config->get_ldap_link();
329     $ldap->cd($this->config->current['BASE']);
330     $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
331     while($attrs = $ldap->fetch()){
332       $ret[$attrs['cn'][0]] = $attrs['cn'][0];
333     }
334     return($ret); 
335   }
338   function save()
339   {
340     // We do not save anything here.
341   }
344   /*! \brief  Return a list of all selected items.
345     @return Array   Returns an array containing all selected item ids.
346    */
347   function list_get_selected_items()
348   {
349     $ids = array();
350     foreach($_POST as $name => $value){
351       if(preg_match("/^item_selected_[0-9]*$/",$name)){
352         $id   = preg_replace("/^item_selected_/","",$name);
353         $ids[$id] = $id;
354       }
355     }
356     return($ids);
357   }
360   function get_actions()
361   {
362     /* Prepare list of available actions */
363     $actions = array(      
364         "gosa_ping"       => _("GOsa ping"),
365         "ping"            => _("Ping"),
366         "sayHello"        => _("Say hello"));
367     return($actions);
368   }
371   static function plInfo()
372   {
373     return (array(
374           "plShortName"   => _("System mass deployment"),
375           "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
376           "plSelfModify"  => FALSE,
377           "plDepends"     => array(),
378           "plPriority"    => 0,
379           "plSection"     => array("addon"),
380           "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
381           "plProvidedAcls" => array("Comment"   => _("Description"), 
382             "Action"    => _("Action"),
383             "Day"       => _("Day"),
384             "Minute"    => _("Minute"),
385             "Hour"      => _("Hour"),
386             "Month"     => _("Month"),
387             "Weekday"   => _("Week day"),
388             "Target"    => _("Target"))
389           ));
390   }
392 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
393 ?>