Code

3ca4a496168c463dcee0f2a87a1caae21202a757
[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         }
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     }
121     /* Remove specified tasks */
122     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
123       $this->o_queue->remove_entries($this->ids_to_remove);
124       $this->save();
125     }
127     /* Remove aborted */
128     if(isset($_POST['delete_cancel'])){
129       $this->ids_to_remove = array();;
130     }
133     /************
134      * EDIT 
135      ************/
137     /* Edit selected entry */
138     if($s_action == "edit"){
139       $entry = $this->o_queue->get_entry_by_id($s_entry);
140       if($entry){
141         $this->dialog = new goto_task($this->config,$this,$entry);
142         $this->current = $s_entry;
143       }
144     }
146     /* New entry */
147     if($s_action== "new_task" && $this->acl_is_createable()){
148       $this->dialog = new goto_task($this->config,$this);
149       $this->current = -1;
150     }
152     /* Close dialog */
153     if(isset($_POST['close_goto_task'])){
154       $this->dialog = FALSE;
155       $this->current = -1;
156     }
158     /* Close dialog */
159     if((isset($_POST['save_goto_task']) || isset($_POST['apply_goto_task'])) && is_object($this->dialog) ){
160       $this->dialog->save_object();
161       $msgs = $this->dialog->check();
162       if(count($msgs)){
163         foreach($msgs as $msg){
164           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
165         }
166       }else{  
168         if($this->o_queue->id_exists($this->current)){
169           $this->o_queue->update_entry($this->current,$this->dialog->save());
170         }else{
171           $tmp = $this->dialog->save();
172           $tmp2= array();
173           $targets =$tmp['Target'];
174           foreach($targets as $target){
175             $tmp['Target'] = array($target);
176             $tmp2[] = $tmp;
177           }
178           if(!$this->o_queue->add_multiple($tmp2)){
179             msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
180           }
181         }
182         if(!isset($_POST['apply_goto_task'])){
183           $this->dialog = FALSE;
184           $this->current = -1;
185         }
186         $this->save();
187       }
188     }
190     /* Display dialogs if currently opened */
191     if(is_object($this->dialog)){
192       $this->dialog->save_object();
193       return($this->dialog->execute());
194     }
196     /************
197      * Handle Divlist 
198      ************/
200     $this->divlist->execute();
201     $entries = $this->get_queued_entries();
202     $this->divlist->SetEntries($entries);
205     $smarty = get_smarty();
206     $smarty->assign("start",$this->start);
207     $smarty->assign("start_real", ($this->start + 1));
208     $smarty->assign("ranges", array("10" => "10",
209                                     "20" => "20",
210                                     "25" => "25",
211                                     "50" => "50",
212                                     "100"=> "100",
213                                     "200"=> "200",
214                                     "9999" => "*"));
216     $count = $this->o_queue->number_of_queued_entries();
217     $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"range"));
218     $smarty->assign("range",$this->range);
219     $smarty->assign("div",$this->divlist->Draw());
220     return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
221   }
224   /*! \brief  Force queue job to be done as far as possible.
225    *  @return Boolean TRUE in case of success, else FALSE. 
226    */
227   private function start_queue_entries($ids)
228   {
229     $data = array("timestamp" => date("YmdHis"),
230                   "status"    => "-");
231     if(!$this->o_queue->update_entries($ids,$data)){
232       msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
233       return(FALSE);
234     }
235     return(TRUE);
236   }
239   /*! \brief Stops the specified queue entry from execution.
240    *  @return Boolean TRUE in case of success, else FALSE. 
241    */
242   private function stop_queue_entries($ids)
243   {
244     $data = array("timestamp" => "20490101010101",
245                   "status"    => "Stoppped");
246     if(!$this->o_queue->update_entries($ids,$data)){
247       msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
248       return(FALSE);
249     }
250     return(TRUE);
251   }
254   /*! \brief  Request list of queued jobs.
255    *  @return Returns an array of all queued jobs.
256    */
257   function get_queued_entries()
258   {
259     $start  = $this->range * $this->start; 
260     $stop   = $start + $this->range; 
261     $entries = $this->o_queue->get_queued_entries($start,$stop);
262     if(!is_array($entries)){
263       msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
264       return(array());
265     }
267     $tasks = array();
268     $ret = array();
269     foreach($entries as $entry){
270       $task = $entry['ID']; 
271       $ret[]= $entry;
272     }
273     $map = array("QueuePosition" => "ID",
274         "Action"        => "STATUS",
275         "TaskID"        => "HEADERTAG",
276         "TargetName"    => "MACADDRESS",
277         "Schedule"      => "TIMESTAMP");
278     $sort_tmp = array();
279     foreach($ret as $entry_id => $entry){
280       $sort_tmp[$entry_id] = $entry[$map[$this->sort_by]];
281     } 
282     natcasesort($sort_tmp);
283     $return = array();
284     foreach($sort_tmp as $entry_id => $value){
285       $return[] = $ret[$entry_id];
286     } 
287     if($this->sort_dir != "up"){
288       $return = array_reverse($return);
289     } 
290     return($return);
291   }
294   /*! \brief  Handle post jobs, like sorting.
295    */
296   function save_object()
297   {
298     $this->divlist->save_object();
299     $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
300     if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
301       $sort = $_GET['sort'];
302       if($this->sort_by == $sort){
303         if($this->sort_dir == "up"){
304           $this->sort_dir = "down";
305         }else{
306           $this->sort_dir = "up";
307         }
308       }
309       $this->sort_by = $sort;
310     }
311     if(isset($_POST['range']) && is_numeric($_POST['range'])){
312       $this->range = $_POST['range'];
313     }
314     if(isset($_GET['start'])){
315       $start = $_GET['start'];
316       if(is_numeric($start) && $start > 0){
317         $this->start = $start;
318       }
319     }
320   }
323   /* Return list of object groups */
324   function get_object_groups()
325   {
326     $ret = array();
327     $ldap = $this->config->get_ldap_link();
328     $ldap->cd($this->config->current['BASE']);
329     $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
330     while($attrs = $ldap->fetch()){
331       $ret[$attrs['cn'][0]] = $attrs['cn'][0];
332     }
333     return($ret); 
334   }
337   function save()
338   {
339     // We do not save anything here.
340   }
343   /*! \brief  Return a list of all selected items.
344     @return Array   Returns an array containing all selected item ids.
345    */
346   function list_get_selected_items()
347   {
348     $ids = array();
349     foreach($_POST as $name => $value){
350       if(preg_match("/^item_selected_[0-9]*$/",$name)){
351         $id   = preg_replace("/^item_selected_/","",$name);
352         $ids[$id] = $id;
353       }
354     }
355     return($ids);
356   }
359   function get_actions()
360   {
361     /* Prepare list of available actions */
362     $actions = array(      
363         "gosa_ping"       => _("GOsa ping"),
364         "ping"            => _("Ping"),
365         "sayHello"        => _("Say hello"));
366     return($actions);
367   }
370   static function plInfo()
371   {
372     return (array(
373           "plShortName"   => _("System mass deployment"),
374           "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
375           "plSelfModify"  => FALSE,
376           "plDepends"     => array(),
377           "plPriority"    => 0,
378           "plSection"     => array("addon"),
379           "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
380           "plProvidedAcls" => array("Comment"   => _("Description"), 
381             "Action"    => _("Action"),
382             "Day"       => _("Day"),
383             "Minute"    => _("Minute"),
384             "Hour"      => _("Hour"),
385             "Month"     => _("Month"),
386             "Weekday"   => _("Week day"),
387             "Target"    => _("Target"))
388           ));
389   }
391 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
392 ?>