Code

Synced strings
[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{
205           if(!isset($add[$key]['count'])){
206             $add[$key]['count'] = count($add[$key]);
207           }
208           for($i=0;$i<$add[$key]['count'];$i++){
209             if(!in_array($add[$key][$i],$base[$key])){
210               $base[$key][] = $add[$key][$i];
211               $base[$key]['count']++;
212             }
213           }
214         }
215       }
216     }
217     return($base);
218   }
221   /*! \brief    Intersect two ldap result arrays/Inner join of two ldap result arrays
222    * @param     array   $base   Base array 
223    * @param     array   $minus  Array number two
224    * @returns   array   Result intersection
225    */  
226   private function array_intersect($base,$minus)
227   {
228     foreach($base as $key => $entry){
229       if(is_numeric($key) || !isset($minus[$key])){
230         unset($base[$key]);
231       }elseif(gettype($base[$key]) != gettype($minus[$key])){
232         unset($base[$key]);
233       }elseif(is_string($base[$key]) && $base[$key]!=$minus[$key]){
234         unset($base[$key]);
235       }elseif(is_array($base[$key])){
236         $tmp = array();
237           if(!isset($base[$key]['count'])){
238             $base[$key]['count'] = count($base[$key]);
239           }
240         for($i = 0 ; $i < $base[$key]['count'] ; $i ++){
241           if(isset($base[$key][$i]) && in_array($base[$key][$i],$minus[$key])){
242             $tmp[] = $base[$key][$i];
243           }
244         }
245         if(count($tmp)){
246           $tmp['count'] = count($tmp);
247           $base[$key] = $tmp;
248         }else{
249           unset($base[$key]);
250         }
251       }
252     }
253     return($base);
254   }
256   
257   /*! \brief    Detect values that are used in all edited objects.
258    */  
259   private function detect_multiple_used_attributes()
260   {
261     foreach($this->o_tab->by_object as $name => $plug){
263       if(empty($name))    continue;
265       $attrs = array();
266       $all   = array();
267       foreach($this->a_handles as $hid => $handle){
268         $h_attrs = $this->a_handles[$hid]->by_object[$name]->get_multi_init_values();
269         if(count($attrs) == 0){
270           $attrs = $h_attrs;
271         }else{
272           $attrs = $this->array_intersect($attrs,$h_attrs);
273         }
274         $all = $this->array_combine($all,$h_attrs);
275       }
276       $this->o_tab->by_object[$name]->init_multiple_support($attrs,$all);
277     }
278   }
281   /*! \brief    Returns the edit ui for multiple edit.
282      @return    string  HTML User interface for given tab object.
283    */
284   public function execute()
285   {
286     $str = $this->o_tab->execute();
287     return($str);
288   }
291   /*! \brief    Checks if one of the objects we want to edit is locked. 
292      @return    boolean   Returns TRUE if at least one entry is locked, else false.
293    */
294   public function entries_locked()
295   {
296     $ui = get_userinfo();
297     foreach($this->dn as $dn){
298       if(get_lock($dn) != ""){
299         return(TRUE);
300       }
301     }
302     return(FALSE);
303   }
306   /*! \brief    Generates a lock message that can be displayed if an entry is locked.
307      @return    string  Returns a list of locked entries 
308    */
309   public function display_lock_message()
310   {
311     $ui = get_userinfo();
312     $lock_msg = "";
313     $lock_msg.=  gen_locked_message ($ui->dn, $this->dn);
314     return($lock_msg);
315   }
318   /*! \brief    Locks all currently managed objects (array $this->dn) 
319      @return    boolean   Returns TRUE
320    */
321   public function lock_entries($uid)
322   {
323     foreach($this->dn as $dn)
324     add_lock($dn,$uid);
325     return(TRUE);
326   }
329   /*! \brief    Checks if the given tab object supports multiple edit.
330      @return    boolean   Returns TRUE if the given tab objects supports multiple edit else FALSE.
331    */
332   public function multiple_available()
333   { 
334     if(isset($this->o_tab) && is_object($this->o_tab)){
335       return($this->o_tab->multiple_support_available());
336     }else{
337       return(FALSE);
338     }
339   }
342   /*! \brief    Sets the currently active tab. The tab that will be displayed by $this->execute(). 
343    */
344   public function set_active_tab($str)
345   {
346     $this->current = $str;
347   }
350   /*! \brief    Returns the object info string, that can be displayed in the tab header.
351       @return   string  Returns an information string, containing the list of currently edited dns.
352    */
353   public function get_object_info()
354   {
355     return(_("You are currently editing mutliple entries."));
356   }
359   /*! \brief    Handles all HTML posts from the dummy tab object. 
360    */
361   public function save_object()
362   {
363     $this->o_tab->save_object(); 
364   }
367   /*! \brief    Checks if the values fetched by $this->save_object() are valid.
368       @param    array Returns an array containig all error messages.
369    */
370   public function check()
371   {
372     $messages = $this->o_tab->check();
373     return($messages);
374   }
377   /*! \brief    Currently not implemented, later be used to trigger password changes. 
378       @param    boolean Returns TRUE if a password change is needed.
379    */
380   public function password_change_needed()
381   {
382     foreach($this->a_handles as $i_id => $o_handle){
383       if($o_handle->password_change_needed() && isset($o_handle->by_object['user'])){
384         new msg_dialog(_("Password reset"),_("The user password was resetted, please set a new password value!"),WARNING_DIALOG);
385         change_password ($o_handle->dn, "",0, $o_handle->by_object['user']->pw_storage);
386       }
387     }
388     return(FALSE);
389   }
392   /*! \brief    Populate all collected values to the target tab objects ($this->o_handles)
393    */
394   public function populate_values()
395   {
396     if($this->multiple_available() && is_array($this->a_handles)){
397       foreach($this->o_tab->by_object as $name => $obj){
399         $values = $this->o_tab->by_object[$name]->get_multi_edit_values();
400         foreach($this->a_handles as $i_id => $o_handle){
401           $this->a_handles[$i_id]->by_object[$name]->set_multi_edit_values($values);
402         }
403       }
404     }
405   }
408   /*! \brief    Save all edited tab objects ($this->o_handles). 
409    */
410   public function save()
411   {
412     if($this->multiple_available() && is_array($this->a_handles)){
413       $this->populate_values();
414       foreach($this->a_handles as $i_id => $o_handle){
415         $o_handle->save();
416       }
417     }
418   }
421 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
422 ?>