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 ...
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.)
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 $this->dn = $dns;
88 $this->config = $config;
89 $this->s_class = $class;
91 /* Initialize collector object
92 * Used to display the ui and to collect the user input.
93 */
94 $this->o_tab = new $class($config,$tab,"new",$acl_category);
95 $this->o_tab->set_acl_base($acl_base);
97 /* Check if the specified tab object supports multiple edits
98 */
99 if($this->o_tab->multiple_support_available()){
101 /* Enable multiple actions for the collector object
102 */
103 $this->o_tab->enable_multiple_support();
105 /* Initialize the objects we want to edit at once
106 */
107 foreach($dns as $dn){
108 $obj = new $class($config,$tab,$dn,$acl_category);
109 $obj->set_acl_base($acl_base);
110 $this->a_handles[] = $obj;
111 }
112 }
114 /* Detect attributes used by all plugins and set
115 * those values as default in edit handle
116 */
117 $this->detect_multiple_used_attributes();
118 }
121 /*! \brief Detect values that are used in all edited objects.
122 * @returns array All multiple used attributes
123 */
124 private function detect_multiple_used_attributes()
125 {
126 $attrs = array();
127 restore_error_handler();
128 $first = $this->o_tab->current;
129 foreach($this->a_handles as $handle){
130 if(count($attrs) == 0){
131 $attrs = $handle->by_object[$first]->attrs;
132 }else{
134 foreach($attrs as $key => $attr){
135 if(!isset($handle->by_object[$first]->attrs[$key]) || !($attr === $handle->by_object[$first]->attrs[$key])){
136 unset($attrs[$key]);
137 }
138 }
139 }
140 }
141 foreach($this->o_tab->by_object as $name => $obj){
142 $this->o_tab->by_object[$name]->init_multiple_support($attrs);
143 }
144 }
147 /*! \brief Returns the edit ui for multiple edit.
148 @return string HTML User interface for given tab object.
149 */
150 public function execute()
151 {
152 return($this->o_tab->execute());
153 }
156 /*! \brief Checks if one of the objects we want to edit is locked.
157 @return boolean Returns TRUE if at least one entry is locked, else false.
158 */
159 public function entries_locked()
160 {
161 $ui = get_userinfo();
162 foreach($this->dn as $dn){
163 if(get_lock($dn) != ""){
164 return(TRUE);
165 }
166 }
167 return(FALSE);
168 }
171 /*! \brief Generates a lock message that can be displayed if an entry is locked.
172 @return string Returns a list of locked entries
173 */
174 public function display_lock_message()
175 {
176 $ui = get_userinfo();
177 $lock_msg = "";
178 $lock_msg.= gen_locked_message ($ui->dn, $this->dn);
179 return($lock_msg);
180 }
183 /*! \brief Locks all currently managed objects (array $this->dn)
184 @return boolean Returns TRUE
185 */
186 public function lock_entries($uid)
187 {
188 foreach($this->dn as $dn)
189 add_lock($dn,$uid);
190 return(TRUE);
191 }
194 /*! \brief Checks if the given tab object supports multiple edit.
195 @return boolean Returns TRUE if the given tab objects supports multiple edit else FALSE.
196 */
197 public function multiple_available()
198 {
199 return($this->o_tab->multiple_support_available());
200 }
203 /*! \brief Sets the currently active tab. The tab that will be displayed by $this->execute().
204 */
205 public function set_active_tab($str)
206 {
207 $this->current = $str;
208 }
211 /*! \brief Returns the object info string, that can be displayed in the tab header.
212 @return string Returns an information string, containing the list of currently edited dns.
213 */
214 public function get_object_info()
215 {
216 return(_("You are currently editing mutliple entries."));
217 }
220 /*! \brief Handles all HTML posts from the dummy tab object.
221 */
222 public function save_object()
223 {
224 $this->o_tab->save_object();
225 }
228 /*! \brief Checks if the values fetched by $this->save_object() are valid.
229 @param array Returns an array containig all error messages.
230 */
231 public function check()
232 {
233 $this->populate_values();
234 $messages = $this->o_tab->check();
235 return($messages);
236 }
239 /*! \brief Currently not implemented, later be used to trigger password changes.
240 @param boolean Returns TRUE if a password change is needed.
241 */
242 public function password_change_needed()
243 {
244 foreach($this->a_handles as $i_id => $o_handle){
245 if($o_handle->password_change_needed() && isset($o_handle->by_object['user'])){
246 new msg_dialog(_("Password reset"),_("The user password was resetted, please set a new password value!"),WARNING_DIALOG);
247 change_password ($o_handle->dn, "",0, $o_handle->by_object['user']->pw_storage);
248 }
249 }
250 return(FALSE);
251 }
254 /*! \brief Populate all collected values to the target tab objects ($this->o_handles)
255 */
256 public function populate_values()
257 {
259 if($this->multiple_available() && is_array($this->a_handles)){
260 foreach($this->o_tab->by_object as $name => $obj){
262 $values = $this->o_tab->by_object[$name]->get_multi_edit_values();
263 foreach($this->a_handles as $i_id => $o_handle){
264 foreach($values as $a_name => $a_value){
265 $this->a_handles[$i_id]->by_object[$name]->$a_name = $a_value;
266 }
267 }
268 }
269 }
270 }
273 /*! \brief Save all edited tab objects ($this->o_handles).
274 */
275 public function save()
276 {
277 if($this->multiple_available() && is_array($this->a_handles)){
278 $this->populate_values();
279 foreach($this->a_handles as $i_id => $o_handle){
280 $o_handle->save();
281 }
282 }
283 }
284 }
286 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
287 ?>