1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2003 Cajus Pollmeier
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 class tabs
22 {
23 var $dn;
24 var $config;
25 var $acl;
26 var $is_template;
27 var $is_new= FALSE;
29 var $last= "";
30 var $current= "";
31 var $disabled= "";
32 var $by_name= array();
33 var $by_object= array();
34 var $SubDialog = false;
36 var $multiple_support = FALSE;
38 function tabs(&$config, $data, $dn, $acl_category= "")
39 {
40 /* Save dn */
41 $this->dn= $dn;
42 $this->config= &$config;
44 $baseobject= NULL;
46 foreach ($data as &$tab){
47 $this->by_name[$tab['CLASS']]= $tab['NAME'];
49 if ($baseobject === NULL){
50 $baseobject= new $tab['CLASS']($this->config, $this->dn);
51 $baseobject->enable_CSN_check();
52 $this->by_object[$tab['CLASS']]= $baseobject;
53 } else {
54 $this->by_object[$tab['CLASS']]= new $tab['CLASS']($this->config, $this->dn, $baseobject);
55 }
57 $this->by_object[$tab['CLASS']]->parent= &$this;
58 $this->by_object[$tab['CLASS']]->set_acl_category($acl_category);
60 /* Initialize current */
61 if ($this->current == ""){
62 $this->current= $tab['CLASS'];
63 }
64 }
65 }
68 function execute()
69 {
70 /* Rotate current to last */
71 $this->last= $this->current;
73 /* Look for pressed tab button */
74 foreach ($this->by_object as $class => &$obj){
75 if (isset($_POST[$class]) || (isset($_POST['arg']) && $_POST['arg'] == "$class")){
76 $this->current= $class;
77 break;
78 }
79 }
81 /* Save last tab object */
82 if ($this->last == $this->current){
83 $this->save_object(TRUE);
84 } else {
85 $this->save_object(FALSE);
86 }
88 /* Build tab line */
89 $display= $this->gen_tabs();
91 /* Show object */
92 $display.= "<table summary=\"\" cellpadding=4 cellspacing=0 border=0 style=\"width:100%; background-color:#F8F8F8; border-style:solid; border-color:#AAA; border-top-width:0px; border-bottom-width:1px; border-left-width:1px; border-right-width:1px;\">\n";
93 $display.= "<tr><td>\n";
95 /* If multiple edit is enabled for this tab,
96 we have tho display different templates */
97 if(!$this->multiple_support){
98 $display.= $this->by_object[$this->current]->execute();
99 }else{
100 $display.= $this->by_object[$this->current]->execute_multiple();
101 }
103 /* Footer for tabbed dialog */
104 $display.= "</td></tr></table>";
106 return ($display);
107 }
109 function save_object($save_current= FALSE)
110 {
111 /* Save last tab */
112 if ($this->last != ""){
113 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
114 $this->last, "Saving");
116 $this->by_object[$this->last]->save_object ();
117 }
119 /* Skip if curent and last are the same object */
120 if ($this->last == $this->current){
121 return;
122 }
124 $obj= @$this->by_object[$this->current];
125 $this->disabled= $obj->parent->disabled;
127 if ($save_current){
128 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
129 $this->current, "Saving (current)");
131 $obj->save_object ();
132 }
133 }
135 function gen_tabs()
136 {
137 $display= "<input type=\"hidden\" name=\"arg\" value=\"\">";
138 $display.= "<table summary=\"\" cellpadding=0 cellspacing=0 border=0 style=\"width:100%;\"><tr>";
139 $index= 0;
140 $style= array("tab_left", "tab_active", "tab_near_active", "tab_right");
141 foreach ($this->by_name as $class => $name){
143 /* Activate right tabs with style "tab_right"
144 Activate near current with style "tab_near_active" */
145 if ($index == 2 || $index == 1){
146 $index++;
147 }
149 /* Activate current tab with style "tab_active " */
150 if ($class == $this->current){
151 $index++;
152 }
154 /* Paint tab */
155 $display.= "<td style=\"vertical-align:bottom;width:1px;white-space:nowrap;\">";
157 /* Shorten string if its too long for the tab headers*/
158 $title= _($name);
159 if (mb_strlen($title, 'UTF-8') > 28){
160 $title= mb_substr($title,0, 25, 'UTF-8')."...";
161 }
163 /* nobr causes w3c warnings so we use to keep the tab name in one line */
164 $title= preg_replace("/ /"," ",$title);
166 /* Take care about notifications */
167 $obj = $this->by_object[$class];
168 if ( $this->by_object[$class]->pl_notify && ($obj->is_account || $obj->ignore_account)){
169 $notify= "id=\"notify\"";
170 } else {
171 $notify= "";
172 }
174 if ($_SESSION['js']==FALSE){
175 $display.= "<div ".$notify." class=\"$style[$index]\"><input type=\"submit\" name=\"$class\"".
176 " class=\"$style[$index]\" value=\"$title\"";
177 } else {
178 $display.= "<div ".$notify." class=\"$style[$index]\"><a class=\"$style[$index]\" onclick=\"return true;\" href=\"javascript:document.mainform.arg.value='$class';document.mainform.submit();\">$title</a";
179 }
180 $display.= "></div></td>";
181 }
182 $display.= "<td style=\"vertical-align:bottom;\">\n";
183 $display.= "<div class=\"tab_border\"> </div></td></tr></table>";
185 return($display);
186 }
189 function set_acl($acl)
190 {
191 /* Look for attribute in ACL */
192 trigger_error("Don't use tabs::set_acl() its obsolete.");
193 }
195 function delete()
196 {
197 /* Check if all plugins will ACK for deletion */
198 foreach (array_reverse($this->by_object) as $key => $obj){
199 $reason= $obj->allow_remove();
200 if ($reason != ""){
201 print_red(sprintf(_("Delete process has been canceled by plugin '%s': %s"), $key, $reason));
202 return;
203 }
204 }
206 /* Delete for all plugins */
207 foreach (array_reverse($this->by_object) as $obj){
208 $obj->remove_from_parent();
209 }
210 }
212 function password_change_needed()
213 {
214 /* Ask all plugins for needed password changes */
215 foreach ($this->by_object as &$obj){
216 if ($obj->password_change_needed()){
217 return TRUE;
218 }
219 }
221 return FALSE;
222 }
224 function check($ignore_account= FALSE)
225 {
226 $this->save_object(TRUE);
227 $messages= array();
229 $current_set = FALSE;
231 /* Check all plugins */
232 foreach ($this->by_object as $key => &$obj){
233 if ($obj->is_account || $ignore_account || $obj->ignore_account){
234 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$key, "Checking");
236 $msg = $obj->check();
238 if (count($msg)){
239 $obj->pl_notify= TRUE;
240 if(!$current_set){
241 $current_set = TRUE;
242 $this->current= $key;
243 $messages = $msg;
244 }
245 }else{
246 $obj->pl_notify= FALSE;
247 }
248 }else{
249 $obj->pl_notify= FALSE;
250 }
251 }
252 return ($messages);
253 }
255 function save($ignore_account= FALSE)
256 {
257 /* Save all plugins */
258 foreach ($this->by_object as $key => &$obj){
259 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
260 $key, "Saving");
262 $obj->dn= $this->dn;
264 if ($obj->is_account || $ignore_account || $obj->ignore_account){
265 if ($obj->save() == 1){
266 return (1);
267 }
268 } else {
269 $obj->remove_from_parent();
270 }
271 }
272 return (0);
273 }
275 function adapt_from_template($dn)
276 {
277 foreach ($this->by_object as $key => &$obj){
278 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
279 $key, "Adapting");
280 $obj->parent= &$this;
281 $obj->adapt_from_template($dn);
282 }
283 }
286 /* Save attributes posted by copy & paste dialog
287 */
288 function saveCopyDialog()
289 {
290 foreach ($this->by_object as &$obj){
291 if($obj->is_account || $obj->ignore_account){
292 $obj->saveCopyDialog();
293 }
294 }
295 }
298 /* return copy & paste dialog
299 */
300 function getCopyDialog()
301 {
302 $ret = "";
303 $this->SubDialog = false;
304 foreach ($this->by_object as &$obj){
305 if($obj->is_account || $obj->ignore_account){
306 $tmp = $obj->getCopyDialog();
307 if($tmp['status'] == "SubDialog"){
308 $this->SubDialog = true;
309 return($tmp['string']);
310 }else{
311 if(!empty($tmp['string'])){
312 $ret .= $tmp['string'];
313 $ret .= "<p class='seperator'> </p>";
314 }
315 }
316 }
317 }
318 return($ret);
319 }
322 function addSpecialTabs()
323 {
324 $this->by_name['acl']= _("ACL");
325 $this->by_object['acl']= new acl($this->config, $this, $this->dn);
326 $this->by_object['acl']->parent= &$this;
327 $this->by_name['reference']= _("References");
328 $this->by_object['reference']= new reference($this->config, $this->dn);
329 $this->by_object['reference']->parent= &$this;
330 }
333 function set_acl_base($base= "")
334 {
335 /* Update reference, transfer variables */
336 $first= ($base == "");
337 foreach ($this->by_object as &$obj){
338 if ($first){
339 $first= FALSE;
340 $base= $obj->acl_base;
341 } else {
342 $obj->set_acl_base($base);
343 }
344 }
345 }
348 /*! \brief Checks if one of the used tab plugins supports multiple edit.
349 @param boolean Returns TRUE if at least one plugins supports multiple edit.
350 */
351 function multiple_support_available()
352 {
353 foreach($this->by_object as $name => $obj){
354 if($obj->multiple_support){
355 return(TRUE);
356 }
357 }
358 return(FALSE);
359 }
362 /*! \brief Enables multiple edit support for the given tab.
363 All unsupported plugins will be disabled.
364 @param boolean Returns TRUE if at least one plugin supports multiple edit
365 */
366 function enable_multiple_support()
367 {
368 if(!$this->multiple_support_available()){
369 return(FALSE);
370 }else{
371 $this->multiple_support = TRUE;
372 foreach($this->by_object as $name => $obj){
373 if($obj->multiple_support){
374 $this->by_object[$name]->multiple_support_active = TRUE;
375 }else{
376 unset($this->by_object[$name]);
377 unset($this->by_name[$name]);
378 }
379 }
380 }
381 return(TRUE);
382 }
383 }
384 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
385 ?>