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_active = 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_active){
98 $display.= $this->by_object[$this->current]->execute();
99 }else{
100 $display.= $this->by_object[$this->current]->multiple_execute();
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 if(!$this->multiple_support_active){
117 $this->by_object[$this->last]->save_object ();
118 }else{
119 $this->by_object[$this->last]->multiple_save_object();
120 }
121 }
123 /* Skip if curent and last are the same object */
124 if ($this->last == $this->current){
125 return;
126 }
128 $obj= @$this->by_object[$this->current];
129 $this->disabled= $obj->parent->disabled;
131 if ($save_current){
132 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
133 $this->current, "Saving (current)");
135 if(!$this->multiple_support_active){
136 $obj->save_object();
137 }else{
138 $obj->multiple_save_object();
139 }
140 }
141 }
143 function gen_tabs()
144 {
145 $display= "<input type=\"hidden\" name=\"arg\" value=\"\">";
146 $display.= "<table summary=\"\" cellpadding=0 cellspacing=0 border=0 style=\"width:100%;\"><tr>";
147 $index= 0;
148 $style= array("tab_left", "tab_active", "tab_near_active", "tab_right");
149 foreach ($this->by_name as $class => $name){
151 /* Activate right tabs with style "tab_right"
152 Activate near current with style "tab_near_active" */
153 if ($index == 2 || $index == 1){
154 $index++;
155 }
157 /* Activate current tab with style "tab_active " */
158 if ($class == $this->current){
159 $index++;
160 }
162 /* Paint tab */
163 $display.= "<td style=\"vertical-align:bottom;width:1px;white-space:nowrap;\">";
165 /* Shorten string if its too long for the tab headers*/
166 $title= _($name);
167 if (mb_strlen($title, 'UTF-8') > 28){
168 $title= mb_substr($title,0, 25, 'UTF-8')."...";
169 }
171 /* nobr causes w3c warnings so we use to keep the tab name in one line */
172 $title= preg_replace("/ /"," ",$title);
174 /* Take care about notifications */
175 $obj = $this->by_object[$class];
176 if ( $this->by_object[$class]->pl_notify && ($obj->is_account || $obj->ignore_account)){
177 $notify= "id=\"notify\"";
178 } else {
179 $notify= "";
180 }
182 if ($_SESSION['js']==FALSE){
183 $display.= "<div ".$notify." class=\"$style[$index]\"><input type=\"submit\" name=\"$class\"".
184 " class=\"$style[$index]\" value=\"$title\"";
185 } else {
186 $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";
187 }
188 $display.= "></div></td>";
189 }
190 $display.= "<td style=\"vertical-align:bottom;\">\n";
191 $display.= "<div class=\"tab_border\"> </div></td></tr></table>";
193 return($display);
194 }
197 function set_acl($acl)
198 {
199 /* Look for attribute in ACL */
200 trigger_error("Don't use tabs::set_acl() its obsolete.");
201 }
203 function delete()
204 {
205 /* Check if all plugins will ACK for deletion */
206 foreach (array_reverse($this->by_object) as $key => $obj){
207 $reason= $obj->allow_remove();
208 if ($reason != ""){
209 print_red(sprintf(_("Delete process has been canceled by plugin '%s': %s"), $key, $reason));
210 return;
211 }
212 }
214 /* Delete for all plugins */
215 foreach (array_reverse($this->by_object) as $obj){
216 $obj->remove_from_parent();
217 }
218 }
220 function password_change_needed()
221 {
222 /* Ask all plugins for needed password changes */
223 foreach ($this->by_object as &$obj){
224 if ($obj->password_change_needed()){
225 return TRUE;
226 }
227 }
229 return FALSE;
230 }
232 function check($ignore_account= FALSE)
233 {
234 $this->save_object(TRUE);
235 $messages= array();
237 $current_set = FALSE;
239 /* Check all plugins */
240 foreach ($this->by_object as $key => &$obj){
241 if ($obj->is_account || $ignore_account || $obj->ignore_account){
242 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$key, "Checking");
244 if(!$this->multiple_support_active){
245 $msg = $obj->check();
246 }else{
247 $msg = $obj->multiple_check();
248 }
250 if (count($msg)){
251 $obj->pl_notify= TRUE;
252 if(!$current_set){
253 $current_set = TRUE;
254 $this->current= $key;
255 $messages = $msg;
256 }
257 }else{
258 $obj->pl_notify= FALSE;
259 }
260 }else{
261 $obj->pl_notify= FALSE;
262 }
263 }
264 return ($messages);
265 }
267 function save($ignore_account= FALSE)
268 {
269 /* Save all plugins */
270 foreach ($this->by_object as $key => &$obj){
271 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
272 $key, "Saving");
274 $obj->dn= $this->dn;
276 if ($obj->is_account || $ignore_account || $obj->ignore_account){
277 if ($obj->save() == 1){
278 return (1);
279 }
280 } else {
281 $obj->remove_from_parent();
282 }
283 }
284 return (0);
285 }
287 function adapt_from_template($dn)
288 {
289 foreach ($this->by_object as $key => &$obj){
290 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
291 $key, "Adapting");
292 $obj->parent= &$this;
293 $obj->adapt_from_template($dn);
294 }
295 }
298 /* Save attributes posted by copy & paste dialog
299 */
300 function saveCopyDialog()
301 {
302 foreach ($this->by_object as &$obj){
303 if($obj->is_account || $obj->ignore_account){
304 $obj->saveCopyDialog();
305 }
306 }
307 }
310 /* return copy & paste dialog
311 */
312 function getCopyDialog()
313 {
314 $ret = "";
315 $this->SubDialog = false;
316 foreach ($this->by_object as &$obj){
317 if($obj->is_account || $obj->ignore_account){
318 $tmp = $obj->getCopyDialog();
319 if($tmp['status'] == "SubDialog"){
320 $this->SubDialog = true;
321 return($tmp['string']);
322 }else{
323 if(!empty($tmp['string'])){
324 $ret .= $tmp['string'];
325 $ret .= "<p class='seperator'> </p>";
326 }
327 }
328 }
329 }
330 return($ret);
331 }
334 function addSpecialTabs()
335 {
336 $this->by_name['acl']= _("ACL");
337 $this->by_object['acl']= new acl($this->config, $this, $this->dn);
338 $this->by_object['acl']->parent= &$this;
339 $this->by_name['reference']= _("References");
340 $this->by_object['reference']= new reference($this->config, $this->dn);
341 $this->by_object['reference']->parent= &$this;
342 }
345 function set_acl_base($base= "")
346 {
347 /* Update reference, transfer variables */
348 $first= ($base == "");
349 foreach ($this->by_object as &$obj){
350 if ($first){
351 $first= FALSE;
352 $base= $obj->acl_base;
353 } else {
354 $obj->set_acl_base($base);
355 }
356 }
357 }
360 /*! \brief Checks if one of the used tab plugins supports multiple edit.
361 @param boolean Returns TRUE if at least one plugins supports multiple edit.
362 */
363 function multiple_support_available()
364 {
365 foreach($this->by_object as $name => $obj){
366 if($obj->multiple_support){
367 return(TRUE);
368 }
369 }
370 return(FALSE);
371 }
374 /*! \brief Enables multiple edit support for the given tab.
375 All unsupported plugins will be disabled.
376 @param boolean Returns TRUE if at least one plugin supports multiple edit
377 */
378 function enable_multiple_support()
379 {
380 if(!$this->multiple_support_available()){
381 return(FALSE);
382 }else{
383 $this->multiple_support_active = TRUE;
384 foreach($this->by_object as $name => $obj){
385 if($obj->multiple_support){
386 $this->by_object[$name]->multiple_support_active = TRUE;
387 $this->by_object[$name]->init_multiple_support();
388 }else{
389 unset($this->by_object[$name]);
390 unset($this->by_name[$name]);
391 }
392 }
393 }
394 return(TRUE);
395 }
396 }
397 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
398 ?>