Code

Updated setup
[gosa.git] / gosa-core / include / class_multi_plug.inc
1 <?php
2 /*
3    This code is part of GOsa (https://oss.gonicus.de/labs/gosa/)
4    Copyright (C) 2007 Fabian Hickert
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 /*
23 Data structure : 
24 ================ 
25   |->o_tab                      <-- dummy object, collects HTML posts, displays ui 
26   |->a_handles                  <-- tab object for each given dn
27      |->tab object for dn 1 
28      |->tab object for dn 2
29       ...
30      |->tab object for dn n
33 Other functions implemented:
34 ============================
36 CLASS tab
37   - multiple_support_available()  Check if there is at least one plugin with 
38                                   enabled multiple edit support
39   - enable_multiple_support()     Enable multiple edit, for this tab.
40   
41 CLASS plugin
42   - enable_multiple_support()     Enable multiple support for this plugin.
43   - init_multiple_support()       Init summy object, to preset some values.
44   - multiple_execute()            Display dummy object ui.
45   - multiple_save_object()        Get posted values in multiple edit mode.
46   - multiple_check()              Check values specified in dummy object.
47   - get_multi_edit_values()       Get values changed from dummy object.
48   - set_multi_edit_values()       Set values collected with get_multi_edit_values.
49                                   to all objects currently edited.
52 Process:
53 ========
55 multi_plug::multi_plug()
56   |->o_tab = new tab()                      #Initialize ui handle
57   |
58   |->handles
59   | |->handles[] = new tab()                #Initialize objects we want to edit at once
60   |
61   |->o_tab->enable_multiple_support()       #Enable multiple support for ui handle
62   |->detect_multiple_used_attributes()      #Update ui handle with some default values
63     |->handles[]
64       |->by_object->get_multi_init_values() #Get attributes from all handles
65     |->o_tab
66       |->by_object->init_multiple_support() #Assign values to ui handle
67   |
68   |->execute()                              #Display ui
69   | |->o_tab->execute()
70       |->by_object->multiple_execute()
71   |
72   |->check()                                #Check given values
73     |->o_tab->check()
74       |->by_object->multiple_check()
75   |
76   |->save_object()                          #Save posts 
77     |->o_tab->save_object()
78       |->by_object->multiple_save_object()
79   |
80   |->save()                                 #Save collected values 
81     |->populate_values()                    #Populate values to all handles
82       |->o_tab->get_multi_edit_values()     #Get values to populate
83       |->handles->set_multi_edit_values()   #Set values 
84     |->handles->save()                      #Save handles
85   
89 Using this class: 
90 =================
91   Simple Example:
93     $dn   = array(dn1,dn2,dn3);
94     $tmp  = new multi_plug($config,"usertabs",$config->data['TABS']['USERTABS'],$dn);
95     echo $tmp->execute();
97   $tmp can now be used like the normal tab class, execute, save_object ...
98   
99   To enable multipe edit for a specific plugin, 
100   just set the plugin variable 'multiple_support' to true:
102     var $multiple_support = TRUE;
104   If plugin::multiple_support is true, the member function 
105   multiple_execute() will be called and displayed, NOT execute().
107   (I will put this in the wiki, later. This are just notes for me.)
108   
109 */
112 /*! \brief   Handles multiple edits for a given set of dns.
113     \author  Fabian Hickert
114     \version 1.01
115     \date    2007/12/07
117     This class edits multiple tab objects at once. 
118     1. There is a dummy tab object initialized that collects the user input.
119     2. All given objects specified by '$dn' will be initialized and the collected
120      data from the dummy object will be populated to them.
121  */
122 class multi_plug
124   /* Tab handler for each given dn entry */
125         public $a_handles = array();
127   /* Dummy handler which collects the data */
128   private $o_tab     = NULL;  
130   public $dn      = array();
131   public $config  = NULL;
132   private $s_class= "";
133   public $current = "";
134   public $by_object = array();
135   public $by_name = array();
137   /*! \brief    Creates a multi_plug object
138      @param   object  $config GOsa Configuration object
139      @param   string  $class  The class name of the tab we want to edit. e.g. usertabs
140      @param   string  $tab    The config tab name e.g. USERTABS 
141      @param   array   $dns    The object dns we want to edit.
142      @return  object  multi_plug
143    */
144   public function __construct($config,$class,$tab,$dns,$acl_base,$acl_category)
145   {
146     if(!count($dns)){
147       return;
148     }
150     $this->dn       = $dns;
151     $this->config   = $config;
152     $this->s_class  = $class;
154     /* Initialize collector object 
155      * Used to display the ui and to collect the user input.
156      */
157     $this->o_tab    = new $class($config,$tab,"new",$acl_category);
158     $this->o_tab->set_acl_base($acl_base);
159     $this->by_object = &$this->o_tab->by_object;
160     $this->by_name   = &$this->o_tab->by_name;
161     $this->current   = &$this->o_tab->current;
163     /* Check if the specified tab object supports multiple edits 
164      */
165     if($this->o_tab->multiple_support_available()){
167       /* Enable multiple actions for the collector object 
168        */ 
169       $this->o_tab->enable_multiple_support();
171       /* Initialize the objects we want to edit at once 
172        */
173       foreach($dns as $dn){
174         $obj = new $class($config,$tab,$dn,$acl_category);
175         $obj->set_acl_base($acl_base);
176         $this->a_handles[] = $obj;
177       }
178     }
180     /* Detect attributes used by all plugins and set 
181      *  those values as default in edit handle 
182      */
183     $this->detect_multiple_used_attributes();
184   }
187   /*! \brief    Combine two ldap result arrays. 
188    * @param     array   $base   Base array 
189    * @param     array   $add    Array to add
190    * @returns   array   Combination of $base and $add
191    */  
192   private function array_combine($base,$add)
193   {
195     foreach($add as $key => $attr) {
196       if(!is_numeric($key)){
197   
198         if(!is_array($add[$key])){
199           $add[$key] = array('count' => 1,$add[$key]);
200         }
202         if(!isset($base[$key])){
203           $base[$key] = $add[$key]; 
204         }else{
206           if(!isset($base[$key]['count'])){
207             $base[$key]['count'] = count($base[$key]);
208           }
210           if(!isset($add[$key]['count'])){
211             $add[$key]['count'] = count($add[$key]);
212           }
213           for($i=0;$i<$add[$key]['count'];$i++){
214             if(!in_array($add[$key][$i],$base[$key])){
215               $base[$key][] = $add[$key][$i];
216               $base[$key]['count']++;
217             }
218           }
219         }
220       }
221     }
222     return($base);
223   }
226   /*! \brief    Intersect two ldap result arrays/Inner join of two ldap result arrays
227    * @param     array   $base   Base array 
228    * @param     array   $minus  Array number two
229    * @returns   array   Result intersection
230    */  
231   private function array_intersect($base,$minus)
232   {
233     foreach($base as $key => $entry){
234       if(is_numeric($key) || !isset($minus[$key])){
235         unset($base[$key]);
236       }elseif(gettype($base[$key]) != gettype($minus[$key])){
237         unset($base[$key]);
238       }elseif(is_string($base[$key]) && $base[$key]!=$minus[$key]){
239         unset($base[$key]);
240       }elseif(is_array($base[$key])){
241         $tmp = array();
242           if(!isset($base[$key]['count'])){
243             $base[$key]['count'] = count($base[$key]);
244           }
245         for($i = 0 ; $i < $base[$key]['count'] ; $i ++){
246           if(isset($base[$key][$i]) && in_array($base[$key][$i],$minus[$key])){
247             $tmp[] = $base[$key][$i];
248           }
249         }
250         if(count($tmp)){
251           $tmp['count'] = count($tmp);
252           $base[$key] = $tmp;
253         }else{
254           unset($base[$key]);
255         }
256       }
257     }
258     return($base);
259   }
261   
262   /*! \brief    Detect values that are used in all edited objects.
263    */  
264   private function detect_multiple_used_attributes()
265   {
266     foreach($this->o_tab->by_object as $name => $plug){
268       if(empty($name))    continue;
270       $attrs = array();
271       $all   = array();
272       foreach($this->a_handles as $hid => $handle){
273         $h_attrs = $this->a_handles[$hid]->by_object[$name]->get_multi_init_values();
274         if(count($attrs) == 0){
275           $attrs = $h_attrs;
276         }else{
277           $attrs = $this->array_intersect($attrs,$h_attrs);
278         }
279         $all = $this->array_combine($all,$h_attrs);
280       }
281       $this->o_tab->by_object[$name]->init_multiple_support($attrs,$all);
282     }
283   }
286   /*! \brief    Returns the edit ui for multiple edit.
287      @return    string  HTML User interface for given tab object.
288    */
289   public function execute()
290   {
291     $str = $this->o_tab->execute();
292     return($str);
293   }
296   /*! \brief    Checks if one of the objects we want to edit is locked. 
297      @return    boolean   Returns TRUE if at least one entry is locked, else false.
298    */
299   public function entries_locked()
300   {
301     $ui = get_userinfo();
302     foreach($this->dn as $dn){
303       if(get_lock($dn) != ""){
304         return(TRUE);
305       }
306     }
307     return(FALSE);
308   }
311   /*! \brief    Generates a lock message that can be displayed if an entry is locked.
312      @return    string  Returns a list of locked entries 
313    */
314   public function display_lock_message()
315   {
316     $ui = get_userinfo();
317     $lock_msg = "";
318     $lock_msg.=  gen_locked_message ($ui->dn, $this->dn);
319     return($lock_msg);
320   }
323   /*! \brief    Locks all currently managed objects (array $this->dn) 
324      @return    boolean   Returns TRUE
325    */
326   public function lock_entries($uid)
327   {
328     foreach($this->dn as $dn)
329     add_lock($dn,$uid);
330     return(TRUE);
331   }
334   /*! \brief    Checks if the given tab object supports multiple edit.
335      @return    boolean   Returns TRUE if the given tab objects supports multiple edit else FALSE.
336    */
337   public function multiple_available()
338   { 
339     if(isset($this->o_tab) && is_object($this->o_tab)){
340       return($this->o_tab->multiple_support_available());
341     }else{
342       return(FALSE);
343     }
344   }
347   /*! \brief    Sets the currently active tab. The tab that will be displayed by $this->execute(). 
348    */
349   public function set_active_tab($str)
350   {
351     $this->current = $str;
352   }
355   /*! \brief    Returns the object info string, that can be displayed in the tab header.
356       @return   string  Returns an information string, containing the list of currently edited dns.
357    */
358   public function get_object_info()
359   {
360     return(_("You are currently editing mutliple entries."));
361   }
364   /*! \brief    Handles all HTML posts from the dummy tab object. 
365    */
366   public function save_object()
367   {
368     $this->o_tab->save_object(); 
369   }
372   /*! \brief    Checks if the values fetched by $this->save_object() are valid.
373       @param    array Returns an array containig all error messages.
374    */
375   public function check()
376   {
377     $messages = $this->o_tab->check();
378     return($messages);
379   }
382   /*! \brief    Currently not implemented, later be used to trigger password changes. 
383       @param    boolean Returns TRUE if a password change is needed.
384    */
385   public function password_change_needed()
386   {
387     foreach($this->a_handles as $i_id => $o_handle){
388       if($o_handle->password_change_needed() && isset($o_handle->by_object['user'])){
389         new msg_dialog(_("Password reset"),_("The user password was resetted, please set a new password value!"),WARNING_DIALOG);
390         change_password ($o_handle->dn, "",0, $o_handle->by_object['user']->pw_storage);
391       }
392     }
393     return(FALSE);
394   }
397   /*! \brief    Populate all collected values to the target tab objects ($this->o_handles)
398    */
399   public function populate_values()
400   {
401     if($this->multiple_available() && is_array($this->a_handles)){
402       foreach($this->o_tab->by_object as $name => $obj){
404         $values = $this->o_tab->by_object[$name]->get_multi_edit_values();
405         foreach($this->a_handles as $i_id => $o_handle){
406           $this->a_handles[$i_id]->by_object[$name]->set_multi_edit_values($values);
407         }
408       }
409     }
410   }
413   /*! \brief    Save all edited tab objects ($this->o_handles). 
414    */
415   public function save()
416   {
417     if($this->multiple_available() && is_array($this->a_handles)){
418       $this->populate_values();
419       foreach($this->a_handles as $i_id => $o_handle){
420         $o_handle->save();
421       }
422     }
423   }
426 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
427 ?>