Code

Some changes
[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","20082","secret-gosa-password",FALSE,1);
28   }
31   function execute()
32   {
33     $smarty = get_smarty();
35     /************
36      * Handle posts 
37      ************/
39     $s_entry = $s_action = "";
40     $arr = array( 
41                   "/^prio_top_/"      => "prio_top",
42                   "/^prio_increase_/" => "prio_increase",
43                   "/^prio_decrease_/" => "prio_decrease",
44                   "/^prio_bottom_/"   => "prio_bottom",
46                   "/^multiple_prio_top_/"      => "mprio_top",
47                   "/^multiple_prio_increase_/" => "mprio_increase",
48                   "/^multiple_prio_decrease_/" => "mprio_decrease",
49                   "/^multiple_prio_bottom_/"   => "mprio_bottom",
51                   "/^edit_task_/"             =>  "edit",
52                   "/^remove_task_/"           =>  "remove",
53                   "/^new_task_/"              =>  "new_task",
54                   "/^remove_multiple_task_/"  => "remove_multiple");
56     foreach($arr as $regex => $action){
57       foreach($_POST as $name => $value){
58         if(preg_match($regex,$name)){
59           $s_action = $action;
60           $s_entry  = preg_replace($regex,"",$name);
61           $s_entry  = preg_replace("/_(x|y)$/","",$s_entry);
62         }
63       }
64     }
66     /* Edit posted from list link */
67     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
68       $s_action = "edit";
69       $s_entry = $_GET['id'];
70     }
72     /************
73      * REMOVE 
74      ************/
75     
76     if(preg_match("/^mprio_/",$s_action) || preg_match("/^prio_/",$s_action)){
78       switch($s_action){
79         case 'mprio_top'       : foreach($this->list_get_selected_items() as $id ){
80                                    $this->o_queue->max_entry_priority($id);
81                                  }
82                                  break;
83         case 'mprio_increase'  : foreach($this->list_get_selected_items() as $id ){
84                                    $this->o_queue->increase_entry_priority($id);
85                                  }
86                                  break;
87         case 'mprio_decrease'  : foreach($this->list_get_selected_items() as $id ){
88                                    $this->o_queue->decrease_entry_priority($id);
89                                  }
90                                  break;
91         case 'mprio_bottom'    : foreach($this->list_get_selected_items() as $id ){
92                                    $this->o_queue->min_entry_priority($id);
93                                  }
94                                  break;
95         case 'prio_top'       : $this->o_queue->max_entry_priority($s_entry);break;
96         case 'prio_increase'  : $this->o_queue->increase_entry_priority($s_entry);break;
97         case 'prio_decrease'  : $this->o_queue->decrease_entry_priority($s_entry);break;
98         case 'prio_bottom'    : $this->o_queue->min_entry_priority($s_entry);break;
99         default : trigger_error("Undefined priority setting used.");
100       }
101       if($this->o_queue->is_error()){
102         print_red($this->o_queue->get_error());
103       }
104     }
106     /************
107      * REMOVE 
108      ************/
110     /* Remove multiple */
111     if($s_action == "remove_multiple" || $s_action == "remove"){
113       if(!$this->acl_is_removeable()){
114         print_red(_("You are not allowed to remove a task."));
115       }else{
117         if($s_action == "remove"){
118           $ids = array($s_entry);
119         }else{
120           $ids = $this->list_get_selected_items();
121         }
122   
124         $this->ids_to_remove = $ids;
125         $tmp = "";
126         foreach($this->ids_to_remove as $key => $id){
127           if($this->o_queue->id_exists($id)){
128             $task = $this->o_queue->get_entry($id);
129             $tmp.= "\n".$task['ROWID'];
130           }else{
131             unset($this->ids_to_remove[$key]);
132           }
133         }
134         $smarty->assign("multiple", TRUE); 
135         $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
136         $this->current = $s_entry;
137         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
138       }
139     }
141     /* Remove specified tasks */
142     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
143       foreach($this->ids_to_remove as $id){
144         if($this->o_queue->id_exists($id)){
145           $this->o_queue->remove_entry($id);
146         }
147       }
148       $this->save();
149     }
151     /* Remove aborted */
152     if(isset($_POST['delete_cancel'])){
153       $this->ids_to_remove = array();;
154     }
157 #   /************
158 #    * EDIT 
159 #    ************/
161 #   /* Edit selected entry */
162 #   if($s_action == "edit"){
163 #     $entry = $this->o_queue->get_entry($s_entry);
164 #     if($entry){
165 #       $this->dialog = new goto_task($this->config,$this,$entry);
166 #       $this->current = $s_entry;
167 #     }
168 #   }
170 #   /* New entry */
171 #   if($s_action== "new_task" && $this->acl_is_createable()){
172 #     $this->dialog = new goto_task($this->config,$this);
173 #     $this->current = -1;
174 #   }
175
176 #   /* Close dialog */
177 #   if(isset($_POST['close_goto_task'])){
178 #     $this->dialog = FALSE;
179 #     $this->current = -1;
180 #   }
182 #   /* Close dialog */
183 #   if((isset($_POST['save_goto_task']) || isset($_POST['apply_goto_task'])) && is_object($this->dialog) ){
184 #     $this->dialog->save_object();
185 #     $msgs = $this->dialog->check();
186 #     if(count($msgs)){
187 #       foreach($msgs as $msg){
188 #         print_red($msg);  
189 #       }
190 #     }else{  
191 #       
192 #       if($this->o_queue->id_exists($this->current)){
193 #         $this->o_queue->update_entry($this->current,$this->dialog->save());
194 #       }else{
195 #         $tmp = $this->dialog->save();
196 #         $tmp2= array();
197 #         $targets =$tmp['Target'];
198 #         foreach($targets as $target){
199 #           $tmp['Target'] = array($target);
200 #           $tmp2[] = $tmp;
201 #         }
202 #         if(!$this->o_queue->add_multiple($tmp2)){
203 #           print_red($this->o_queue->get_error());
204 #         }
205 #       }
206 #       if(!isset($_POST['apply_goto_task'])){
207 #         $this->dialog = FALSE;
208 #         $this->current = -1;
209 #       }
210 #       $this->save();
211 #     }
212 #   }
214 #   /* Display dialogs if currently opened */
215 #   if(is_object($this->dialog)){
216 #     $this->dialog->save_object();
217 #     return($this->dialog->execute());
218 #   }
220     /************
221      * Handle Divlist 
222      ************/
224     $this->divlist->execute();
225     $entries = $this->get_queue_entries();
226     $this->divlist->SetEntries($entries);
227     return($this->divlist->Draw());
228   }
230    
231   /*! \brief  Request list of queued jobs.
232    *  @return Returns an array of all queued jobs.
233    */
234   function get_queue_entries()
235   {
236     if(!$this->o_queue->load()){
237       print_red(sprintf(_("An error occurred while requesting queue entries. Error was '%s'."),$this->o_queue->get_error()));
238       return(array());
239     }
240     $tasks = array();
241     $ret = array();
242     while($entry = $this->o_queue->fetch()){
243       $task = $entry['ROWID']; 
244       $ret[]= $entry;
245     }
246     $map = array("QueuePosition" => "ROWID",
247                  "Action"        => "STATUS",
248                  "TaskID"        => "HEADERTAG",
249                  "TargetName"    => "MACADDRESS",
250                  "Schedule"      => "TIMESTAMP");
251     $sort_tmp = array();
252     foreach($ret as $entry_id => $entry){
253       $sort_tmp[$entry_id] = $entry[$map[$this->sort_by]];
254     } 
255     natcasesort($sort_tmp);
256     $return = array();
257     foreach($sort_tmp as $entry_id => $value){
258       $return[] = $ret[$entry_id];
259     } 
260     if($this->sort_dir != "up"){
261       $return = array_reverse($return);
262     } 
263     return($return);
264   }
267   /*! \brief  Handle post jobs, like sorting.
268    */
269   function save_object()
270   {
271     $this->divlist->save_object();
272     $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
273     if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
274       $sort = $_GET['sort'];
275       if($this->sort_by == $sort){
276         if($this->sort_dir == "up"){
277            $this->sort_dir = "down";
278         }else{
279            $this->sort_dir = "up";
280         }
281       }
282       $this->sort_by = $sort;
283     }
284   }
287   /* Return list of object groups */
288   function get_object_groups()
289   {
290     $ret = array();
291     $ldap = $this->config->get_ldap_link();
292     $ldap->cd($this->config->current['BASE']);
293     $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
294     while($attrs = $ldap->fetch()){
295       $ret[$attrs['cn'][0]] = $attrs['cn'][0];
296     }
297     return($ret); 
298   }
300   
301   function save()
302   {
303     // We do not save anything here.
304   }
307   /*! \brief  Return a list of all selected items.
308       @return Array   Returns an array containing all selected item ids.
309    */
310   function list_get_selected_items()
311   {
312     $ids = array();
313     foreach($_POST as $name => $value){
314       if(preg_match("/^item_selected_[0-9]*$/",$name)){
315         $id   = preg_replace("/^item_selected_/","",$name);
316         $ids[$id] = $id;
317       }
318     }
319     return($ids);
320   }
323  function get_actions()
324  {
325    /* Prepare list of available actions */
326    $actions = array(       
327        "Dummy."          => "!!!!!!!! Update",                     
328        "reboot"          => _("Reboot"),
329        "localboot"       => _("Localboot"),
330        "halt"            => _("Halt system"),
331        "initial_install" => _("Initial installation"),
332        "update"          => _("Update"),
333        "reinstall"       => _("(Re)Install"),
334        "rescan"          => _("Rescan"),
335        "wake"            => _("Wake"),
336        "memcheck"        => _("Memory check"));
337    return($actions);
338  }
341   static function plInfo()
342   {
343     return (array(
344         "plShortName"   => _("System mass deployment"),
345         "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
346         "plSelfModify"  => FALSE,
347         "plDepends"     => array(),
348         "plPriority"    => 0,
349         "plSection"     => array("addon"),
350         "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
351         "plProvidedAcls" => array("Comment"   => _("Description"), 
352                                   "Action"    => _("Action"),
353                                   "Day"       => _("Day"),
354                                   "Minute"    => _("Minute"),
355                                   "Hour"      => _("Hour"),
356                                   "Month"     => _("Month"),
357                                   "Weekday"   => _("Week day"),
358                                   "Target"    => _("Target"))
359         ));
360   }
362 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
363 ?>