Code

Updated Attributes detection for multi plug
[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 = "";
135   /*! \brief    Creates a multi_plug object
136      @param   object  $config GOsa Configuration object
137      @param   string  $class  The class name of the tab we want to edit. e.g. usertabs
138      @param   string  $tab    The config tab name e.g. USERTABS 
139      @param   array   $dns    The object dns we want to edit.
140      @return  object  multi_plug
141    */
142   public function __construct($config,$class,$tab,$dns,$acl_base,$acl_category)
143   {
144     if(!count($dns)){
145       return;
146     }
148     $this->dn       = $dns;
149     $this->config   = $config;
150     $this->s_class  = $class;
152     /* Initialize collector object 
153      * Used to display the ui and to collect the user input.
154      */
155     $this->o_tab    = new $class($config,$tab,"new",$acl_category);
156     $this->o_tab->set_acl_base($acl_base);
158     /* Check if the specified tab object supports multiple edits 
159      */
160     if($this->o_tab->multiple_support_available()){
162       /* Enable multiple actions for the collector object 
163        */ 
164       $this->o_tab->enable_multiple_support();
166       /* Initialize the objects we want to edit at once 
167        */
168       foreach($dns as $dn){
169         $obj = new $class($config,$tab,$dn,$acl_category);
170         $obj->set_acl_base($acl_base);
171         $this->a_handles[] = $obj;
172       }
173     }
175     /* Detect attributes used by all plugins and set 
176      *  those values as default in edit handle 
177      */
178     $this->detect_multiple_used_attributes();
179   }
182   /*! \brief    Combine two ldap result arrays. 
183    * @param     array   $base   Base array 
184    * @param     array   $add    Array to add
185    * @returns   array   Combination of $base and $add
186    */  
187   private function array_combine($base,$add)
188   {
190     foreach($add as $key => $attr) {
191       if(!is_numeric($key)){
192   
193         if(!is_array($add[$key])){
194           $add[$key] = array('count' => 1,$add[$key]);
195         }
197         if(!isset($base[$key])){
198           $base[$key] = $add[$key]; 
199         }else{
200           if(!isset($add[$key]['count'])){
201             $add[$key]['count'] = count($add[$key]);
202           }
203           for($i=0;$i<$add[$key]['count'];$i++){
204             if(!in_array($add[$key][$i],$base[$key])){
205               $base[$key][] = $add[$key][$i];
206               $base[$key]['count']++;
207             }
208           }
209         }
210       }
211     }
212     return($base);
213   }
216   /*! \brief    Intersect two ldap result arrays/Inner join of two ldap result arrays
217    * @param     array   $base   Base array 
218    * @param     array   $minus  Array number two
219    * @returns   array   Result intersection
220    */  
221   private function array_intersect($base,$minus)
222   {
223     foreach($base as $key => $entry){
224       if(is_numeric($key) || !isset($minus[$key])){
225         unset($base[$key]);
226       }elseif(gettype($base[$key]) != gettype($minus[$key])){
227         unset($base[$key]);
228       }elseif(is_string($base[$key]) && $base[$key]!=$minus[$key]){
229         unset($base[$key]);
230       }elseif(is_array($base[$key])){
231         $tmp = array();
232           if(!isset($base[$key]['count'])){
233             $base[$key]['count'] = count($base[$key]);
234           }
235         for($i = 0 ; $i < $base[$key]['count'] ; $i ++){
236           if(isset($base[$key][$i]) && in_array($base[$key][$i],$minus[$key])){
237             $tmp[] = $base[$key][$i];
238           }
239         }
240         if(count($tmp)){
241           $tmp['count'] = count($tmp);
242           $base[$key] = $tmp;
243         }else{
244           unset($base[$key]);
245         }
246       }
247     }
248     return($base);
249   }
251   
252   /*! \brief    Detect values that are used in all edited objects.
253    */  
254   private function detect_multiple_used_attributes()
255   {
256     foreach($this->o_tab->by_object as $name => $plug){
258       if(empty($name))    continue;
260       $attrs = array();
261       $all   = array();
262       foreach($this->a_handles as $hid => $handle){
263         $h_attrs = $this->a_handles[$hid]->by_object[$name]->get_multi_init_values();
264         if(count($attrs) == 0){
265           $attrs = $h_attrs;
266         }else{
267           $attrs = $this->array_intersect($attrs,$h_attrs);
268         }
269         $all = $this->array_combine($all,$h_attrs);
270       }
271       $this->o_tab->by_object[$name]->init_multiple_support($attrs,$all);
272     }
273   }
276   /*! \brief    Returns the edit ui for multiple edit.
277      @return    string  HTML User interface for given tab object.
278    */
279   public function execute()
280   {
281     return($this->o_tab->execute());
282   }
285   /*! \brief    Checks if one of the objects we want to edit is locked. 
286      @return    boolean   Returns TRUE if at least one entry is locked, else false.
287    */
288   public function entries_locked()
289   {
290     $ui = get_userinfo();
291     foreach($this->dn as $dn){
292       if(get_lock($dn) != ""){
293         return(TRUE);
294       }
295     }
296     return(FALSE);
297   }
300   /*! \brief    Generates a lock message that can be displayed if an entry is locked.
301      @return    string  Returns a list of locked entries 
302    */
303   public function display_lock_message()
304   {
305     $ui = get_userinfo();
306     $lock_msg = "";
307     $lock_msg.=  gen_locked_message ($ui->dn, $this->dn);
308     return($lock_msg);
309   }
312   /*! \brief    Locks all currently managed objects (array $this->dn) 
313      @return    boolean   Returns TRUE
314    */
315   public function lock_entries($uid)
316   {
317     foreach($this->dn as $dn)
318     add_lock($dn,$uid);
319     return(TRUE);
320   }
323   /*! \brief    Checks if the given tab object supports multiple edit.
324      @return    boolean   Returns TRUE if the given tab objects supports multiple edit else FALSE.
325    */
326   public function multiple_available()
327   { 
328     if(isset($this->o_tab) && is_object($this->o_tab)){
329       return($this->o_tab->multiple_support_available());
330     }else{
331       return(FALSE);
332     }
333   }
336   /*! \brief    Sets the currently active tab. The tab that will be displayed by $this->execute(). 
337    */
338   public function set_active_tab($str)
339   {
340     $this->current = $str;
341   }
344   /*! \brief    Returns the object info string, that can be displayed in the tab header.
345       @return   string  Returns an information string, containing the list of currently edited dns.
346    */
347   public function get_object_info()
348   {
349     return(_("You are currently editing mutliple entries."));
350   }
353   /*! \brief    Handles all HTML posts from the dummy tab object. 
354    */
355   public function save_object()
356   {
357     $this->o_tab->save_object(); 
358   }
361   /*! \brief    Checks if the values fetched by $this->save_object() are valid.
362       @param    array Returns an array containig all error messages.
363    */
364   public function check()
365   {
366     $messages = $this->o_tab->check();
367     return($messages);
368   }
371   /*! \brief    Currently not implemented, later be used to trigger password changes. 
372       @param    boolean Returns TRUE if a password change is needed.
373    */
374   public function password_change_needed()
375   {
376     foreach($this->a_handles as $i_id => $o_handle){
377       if($o_handle->password_change_needed() && isset($o_handle->by_object['user'])){
378         new msg_dialog(_("Password reset"),_("The user password was resetted, please set a new password value!"),WARNING_DIALOG);
379         change_password ($o_handle->dn, "",0, $o_handle->by_object['user']->pw_storage);
380       }
381     }
382     return(FALSE);
383   }
386   /*! \brief    Populate all collected values to the target tab objects ($this->o_handles)
387    */
388   public function populate_values()
389   {
390     if($this->multiple_available() && is_array($this->a_handles)){
391       foreach($this->o_tab->by_object as $name => $obj){
393         $values = $this->o_tab->by_object[$name]->get_multi_edit_values();
394         foreach($this->a_handles as $i_id => $o_handle){
395           $this->a_handles[$i_id]->by_object[$name]->set_multi_edit_values($values);
396         }
397       }
398     }
399   }
402   /*! \brief    Save all edited tab objects ($this->o_handles). 
403    */
404   public function save()
405   {
406     if($this->multiple_available() && is_array($this->a_handles)){
407       $this->populate_values();
408       foreach($this->a_handles as $i_id => $o_handle){
409         $o_handle->save();
410       }
411     }
412   }
415 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
416 ?>