Code

c96a0eda623374891b472e6a4ca2da6bd371abfe
[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
32 Using this class: 
33 =================
34   Simple Example:
36     $dn   = array(dn1,dn2,dn3);
37     $tmp  = new multi_plug($config,"usertabs",$config->data['TABS']['USERTABS'],$dn);
38     echo $tmp->execute();
40   $tmp can now be used like the normal tab class, execute, save_object ...
41   
42   To enable multipe edit for a specific plugin, 
43   just set the plugin variable 'multiple_support' to true:
45     var $multiple_support = TRUE;
47   If plugin::multiple_support is true, the member function 
48   multiple_execute() will be called and displayed, NOT execute().
50   (I will put this in the wiki, later. This are just notes for me.)
51   
52 */
55 /*! \brief   Handles multiple edits for a given set of dns.
56     \author  Fabian Hickert
57     \version 1.01
58     \date    2007/12/07
60     This class edits multiple tab objects at once. 
61     1. There is a dummy tab object initialized that collects the user input.
62     2. All given objects specified by '$dn' will be initialized and the collected
63      data from the dummy object will be populated to them.
64  */
65 class multi_plug
66 {
67   /* Tab handler for each given dn entry */
68         public $a_handles = array();
70   /* Dummy handler which collects the data */
71   private $o_tab     = NULL;  
73   public $dn      = array();
74   public $config  = NULL;
75   private $s_class= "";
76   public $current = "";
78   /*! \brief    Creates a multi_plug object
79      @param   object  $config GOsa Configuration object
80      @param   string  $class  The class name of the tab we want to edit. e.g. usertabs
81      @param   string  $tab    The config tab name e.g. USERTABS 
82      @param   array   $dns    The object dns we want to edit.
83      @return  object  multi_plug
84    */
85   public function __construct($config,$class,$tab,$dns,$acl_base,$acl_category)
86   {
87     if(!count($dns)){
88       return;
89     }
91     $this->dn       = $dns;
92     $this->config   = $config;
93     $this->s_class  = $class;
95     /* Initialize collector object 
96      * Used to display the ui and to collect the user input.
97      */
98     $this->o_tab    = new $class($config,$tab,"new",$acl_category);
99     $this->o_tab->set_acl_base($acl_base);
101     /* Check if the specified tab object supports multiple edits 
102      */
103     if($this->o_tab->multiple_support_available()){
105       /* Enable multiple actions for the collector object 
106        */ 
107       $this->o_tab->enable_multiple_support();
109       /* Initialize the objects we want to edit at once 
110        */
111       foreach($dns as $dn){
112         $obj = new $class($config,$tab,$dn,$acl_category);
113         $obj->set_acl_base($acl_base);
114         $this->a_handles[] = $obj;
115       }
116     }
118     /* Detect attributes used by all plugins and set 
119      *  those values as default in edit handle 
120      */
121     $this->detect_multiple_used_attributes();
122   }
125   /*! \brief    Combine two ldap result arrays. 
126    * @param     array   $base   Base array 
127    * @param     array   $add    Array to add
128    * @returns   array   Combination of $base and $add
129    */  
130   private function array_combine($base,$add)
131   {
132     foreach($add as $key => $attr) {
133       if(!is_numeric($key)){
134         if(!isset($base[$key])){
135           $base[$key] = $add[$key]; 
136         }elseif(is_array($add[$key])){
137           for($i=0;$i<$add[$key]['count'];$i++){
138             if(!in_array($add[$key][$i],$base[$key])){
139               $base[$key][] = $add[$key][$i];
140               $base[$key]['count']++;
141             }
142           }
143         }
144       }
145     }
146     return($base);
147   }
150   /*! \brief    Intersect two ldap result arrays/Inner join of two ldap result arrays
151    * @param     array   $base   Base array 
152    * @param     array   $minus  Array number two
153    * @returns   array   Result intersection
154    */  
155   private function array_intersect($base,$minus)
156   {
157     foreach($base as $key => $entry){
158       if(is_numeric($key) || !isset($minus[$key])){
159         unset($base[$key]);
160       }elseif(gettype($base[$key]) != gettype($minus[$key])){
161         unset($base[$key]);
162       }elseif(is_string($base[$key]) && $base[$key]!=$minus[$key]){
163         unset($base[$key]);
164       }elseif(is_array($base[$key])){
165         for($i = 0 ; $i < $base[$key]['count'] ; $i ++){
166           if(isset($base[$key][$i]) && !in_array($base[$key][$i],$minus[$key])){
167             $base[$key]['count'] --;
168             if($base[$key]['count'] == 0){
169               unset($base[$key]);
170               break;
171             }else{
172               unset($base[$key][$i]);
173             }
174           }
175         }
176       }
177     }
178     return($base);
179   }
181   
182   /*! \brief    Detect values that are used in all edited objects.
183    * @returns   array   All multiple used attributes
184    */  
185   private function detect_multiple_used_attributes()
186   {
187     $attrs = array();
188     $first = $this->o_tab->current;
189     $all   = array();
190     foreach($this->a_handles as $handle){
191       if(count($attrs) == 0){
192         $attrs = $handle->by_object[$first]->attrs;
193       }else{
194         $attrs = $this->array_intersect($attrs,$handle->by_object[$first]->attrs);
195       }
196       $all = $this->array_combine($all,$handle->by_object[$first]->attrs);
197     }
198     foreach($this->o_tab->by_object as $name => $obj){
199       $this->o_tab->by_object[$name]->init_multiple_support($attrs,$all);
200     }
201   }
204   /*! \brief    Returns the edit ui for multiple edit.
205      @return    string  HTML User interface for given tab object.
206    */
207   public function execute()
208   {
209     return($this->o_tab->execute());
210   }
213   /*! \brief    Checks if one of the objects we want to edit is locked. 
214      @return    boolean   Returns TRUE if at least one entry is locked, else false.
215    */
216   public function entries_locked()
217   {
218     $ui = get_userinfo();
219     foreach($this->dn as $dn){
220       if(get_lock($dn) != ""){
221         return(TRUE);
222       }
223     }
224     return(FALSE);
225   }
228   /*! \brief    Generates a lock message that can be displayed if an entry is locked.
229      @return    string  Returns a list of locked entries 
230    */
231   public function display_lock_message()
232   {
233     $ui = get_userinfo();
234     $lock_msg = "";
235     $lock_msg.=  gen_locked_message ($ui->dn, $this->dn);
236     return($lock_msg);
237   }
240   /*! \brief    Locks all currently managed objects (array $this->dn) 
241      @return    boolean   Returns TRUE
242    */
243   public function lock_entries($uid)
244   {
245     foreach($this->dn as $dn)
246     add_lock($dn,$uid);
247     return(TRUE);
248   }
251   /*! \brief    Checks if the given tab object supports multiple edit.
252      @return    boolean   Returns TRUE if the given tab objects supports multiple edit else FALSE.
253    */
254   public function multiple_available()
255   { 
256     if(isset($this->o_tab) && is_object($this->o_tab)){
257       return($this->o_tab->multiple_support_available());
258     }else{
259       return(FALSE);
260     }
261   }
264   /*! \brief    Sets the currently active tab. The tab that will be displayed by $this->execute(). 
265    */
266   public function set_active_tab($str)
267   {
268     $this->current = $str;
269   }
272   /*! \brief    Returns the object info string, that can be displayed in the tab header.
273       @return   string  Returns an information string, containing the list of currently edited dns.
274    */
275   public function get_object_info()
276   {
277     return(_("You are currently editing mutliple entries."));
278   }
281   /*! \brief    Handles all HTML posts from the dummy tab object. 
282    */
283   public function save_object()
284   {
285     $this->o_tab->save_object(); 
286   }
289   /*! \brief    Checks if the values fetched by $this->save_object() are valid.
290       @param    array Returns an array containig all error messages.
291    */
292   public function check()
293   {
294     $messages = $this->o_tab->check();
295     return($messages);
296   }
299   /*! \brief    Currently not implemented, later be used to trigger password changes. 
300       @param    boolean Returns TRUE if a password change is needed.
301    */
302   public function password_change_needed()
303   {
304     foreach($this->a_handles as $i_id => $o_handle){
305       if($o_handle->password_change_needed() && isset($o_handle->by_object['user'])){
306         new msg_dialog(_("Password reset"),_("The user password was resetted, please set a new password value!"),WARNING_DIALOG);
307         change_password ($o_handle->dn, "",0, $o_handle->by_object['user']->pw_storage);
308       }
309     }
310     return(FALSE);
311   }
314   /*! \brief    Populate all collected values to the target tab objects ($this->o_handles)
315    */
316   public function populate_values()
317   {
318     if($this->multiple_available() && is_array($this->a_handles)){
319       foreach($this->o_tab->by_object as $name => $obj){
321         $values = $this->o_tab->by_object[$name]->get_multi_edit_values();
322         foreach($this->a_handles as $i_id => $o_handle){
323           $this->a_handles[$i_id]->by_object[$name]->set_multi_edit_values($values);
324         }
325       }
326     }
327   }
330   /*! \brief    Save all edited tab objects ($this->o_handles). 
331    */
332   public function save()
333   {
334     if($this->multiple_available() && is_array($this->a_handles)){
335       $this->populate_values();
336       foreach($this->a_handles as $i_id => $o_handle){
337         $o_handle->save();
338       }
339     }
340   }
343 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
344 ?>