Code

Skip action if nothing is selected.
[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      ************/
41     
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         }
105         if(count($ids)){
106           $this->ids_to_remove = $ids;
107           $ret = $this->o_queue->ids_exist($this->ids_to_remove);
108           $ret = $this->o_queue->get_entries_by_id($ret);
110           $tmp = "";
111           foreach($ret as $task){
112             $tmp.= "\n".$task['ID']." - ".$task['HEADERTAG']."&nbsp;".$task['MACADDRESS'];
113           }
114           $smarty->assign("multiple", TRUE); 
115           $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
116           $this->current = $s_entry;
117           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
118         }
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_entries(array($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" => "*"));
217     $count = $this->o_queue->number_of_queued_entries();
218     $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"range"));
219     $smarty->assign("range",$this->range);
220     $smarty->assign("div",$this->divlist->Draw());
221     return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
222   }
225   /*! \brief  Force queue job to be done as far as possible.
226    *  @return Boolean TRUE in case of success, else FALSE. 
227    */
228   private function start_queue_entries($ids)
229   {
230     if(!count($ids)){
231       return;
232     }
233   
234     $data = array("timestamp" => date("YmdHis"),
235                   "status"    => "-");
236     if(!$this->o_queue->update_entries($ids,$data)){
237       msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
238       return(FALSE);
239     }
240     return(TRUE);
241   }
244   /*! \brief Stops the specified queue entry from execution.
245    *  @return Boolean TRUE in case of success, else FALSE. 
246    */
247   private function stop_queue_entries($ids)
248   {
249     if(!count($ids)){
250       return;
251     }
252     $data = array("timestamp" => "20490101010101",
253                   "status"    => "Stoppped");
254     if(!$this->o_queue->update_entries($ids,$data)){
255       msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
256       return(FALSE);
257     }
258     return(TRUE);
259   }
262   /*! \brief  Request list of queued jobs.
263    *  @return Returns an array of all queued jobs.
264    */
265   function get_queued_entries()
266   {
267     $map = array(
268         "QueuePosition" => "id",
269         "Action"        => "status",
270         "TaskID"        => "headertag",
271         "TargetName"    => "macaddress",
272         "Schedule"      => "timestamp");
274     if(!isset($map[$this->sort_by])){
275       $sort = "id DESC";
276     }else{
277       $sort   = $map[$this->sort_by]; 
278       if($this->sort_dir == "up"){
279         $sort.= " ASC";
280       }else{
281         $sort.= " DESC";
282       }
283     }
284       
285     $start  = $this->start; 
286     $stop   = $this->range;
287     $entries = $this->o_queue->get_queued_entries($start,$stop,$sort);
288     if(!is_array($entries) || !isset($entries['XML']) || !is_array($entries['XML'])){
289       msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
290       return(array());
291     }
293     $ret = array();
294     foreach($entries['XML'] as $entry){
295       $task = $entry['ID']; 
296       $ret[]= $entry;
297     }
298     return($ret);
299   }
302   /*! \brief  Handle post jobs, like sorting.
303    */
304   function save_object()
305   {
306     $this->divlist->save_object();
307     $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
308     if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
309       $sort = $_GET['sort'];
310       if($this->sort_by == $sort){
311         if($this->sort_dir == "up"){
312           $this->sort_dir = "down";
313         }else{
314           $this->sort_dir = "up";
315         }
316       }
317       $this->sort_by = $sort;
318     }
319     if(isset($_POST['range']) && is_numeric($_POST['range'])){
320       $this->range = $_POST['range'];
321     }
322     if(isset($_GET['start'])){
323       $start = $_GET['start'];
324       if(is_numeric($start) || $start == 0){
325         $this->start = $start;
326       }
327     }
329     /* Check start stop and reset if necessary */
330     $count = $this->o_queue->number_of_queued_entries();
331     if($this->start >= $count){
332       $this->start = $count -1;
333     }
334     if($this->start < 0){
335       $this->start = 0;
336     }
337   }
340   /* Return list of object groups */
341   function get_object_groups()
342   {
343     $ret = array();
344     $ldap = $this->config->get_ldap_link();
345     $ldap->cd($this->config->current['BASE']);
346     $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
347     while($attrs = $ldap->fetch()){
348       $ret[$attrs['cn'][0]] = $attrs['cn'][0];
349     }
350     return($ret); 
351   }
354   function save()
355   {
356     // We do not save anything here.
357   }
360   /*! \brief  Return a list of all selected items.
361     @return Array   Returns an array containing all selected item ids.
362    */
363   function list_get_selected_items()
364   {
365     $ids = array();
366     foreach($_POST as $name => $value){
367       if(preg_match("/^item_selected_[0-9]*$/",$name)){
368         $id   = preg_replace("/^item_selected_/","",$name);
369         $ids[$id] = $id;
370       }
371     }
372     return($ids);
373   }
376   function get_actions()
377   {
378     /* Prepare list of available actions */
379     $actions = array(      
380         "gosa_ping"       => _("GOsa ping"),
381         "ping"            => _("Ping"),
382         "sayHello"        => _("Say hello"));
383     return($actions);
384   }
387   static function plInfo()
388   {
389     return (array(
390           "plShortName"   => _("System mass deployment"),
391           "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
392           "plSelfModify"  => FALSE,
393           "plDepends"     => array(),
394           "plPriority"    => 0,
395           "plSection"     => array("addon"),
396           "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
397           "plProvidedAcls" => array("Comment"   => _("Description"), 
398             "Action"    => _("Action"),
399             "Day"       => _("Day"),
400             "Minute"    => _("Minute"),
401             "Hour"      => _("Hour"),
402             "Month"     => _("Month"),
403             "Weekday"   => _("Week day"),
404             "Target"    => _("Target"))
405           ));
406   }
408 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
409 ?>