Code

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