1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class tabs
24 {
25 var $dn;
26 var $config;
27 var $acl;
28 var $is_template;
29 var $is_new= FALSE;
31 var $last= "";
32 var $current= "";
33 var $disabled= "";
34 var $by_name= array();
35 var $by_object= array();
36 var $SubDialog = false;
37 var $acl_category;
38 var $multiple_support_active = FALSE;
40 var $read_only = FALSE; // Used when the entry is opened as "readonly" due to locks.
41 var $hide_refs = FALSE;
42 var $hide_acls = FALSE;
44 function tabs(&$config, $data, $dn, $acl_category= "", $hide_refs = FALSE, $hide_acls = FALSE)
45 {
46 /* Save dn */
47 $this->dn= $dn;
48 $this->config= &$config;
49 $this->hide_refs = $hide_refs;
50 $this->hide_acls = $hide_acls;
52 if(!count($data)) {
53 $data[] = array("CLASS" => 'plugin',"NAME" => 'Error');
54 msg_dialog::display(_("Error"),
55 sprintf(_("No plugin definitions found to initialize '%s', please check your configuration file."),get_class($this)),
56 "ERROR_DIALOG");
57 }
59 $baseobject= NULL;
60 $this->acl_category = $acl_category;
61 foreach ($data as &$tab){
63 if (!plugin_available($tab['CLASS'])){
64 continue;
65 }
67 $this->by_name[$tab['CLASS']]= $tab['NAME'];
69 if ($baseobject === NULL){
70 $baseobject= new $tab['CLASS']($this->config, $this->dn);
71 $baseobject->enable_CSN_check();
72 $this->by_object[$tab['CLASS']]= $baseobject;
73 } else {
74 $this->by_object[$tab['CLASS']]= new $tab['CLASS']($this->config, $this->dn, $baseobject);
75 }
77 $this->read_only |= $this->by_object[$tab['CLASS']]->read_only;
78 $this->by_object[$tab['CLASS']]->parent= &$this;
79 $this->by_object[$tab['CLASS']]->set_acl_category($this->acl_category);
81 /* Initialize current */
82 if ($this->current == ""){
83 $this->current= $tab['CLASS'];
84 }
85 }
86 }
89 /*! \brief Reinitializes the tab classes with fresh ldap values.
90 This maybe usefull if for example the apply button was pressed.
91 */
92 function re_init()
93 {
94 $baseobject= NULL;
95 foreach($this->by_object as $name => $object){
96 $class = get_class($object);
97 if(in_array($class,array("reference","acl"))) continue;
98 if ($baseobject === NULL){
99 $baseobject= new $class($this->config, $this->dn);
100 $baseobject->enable_CSN_check();
101 $this->by_object[$name]= $baseobject;
102 } else {
103 $this->by_object[$name]= new $class($this->config, $this->dn, $baseobject);
104 }
105 $this->by_object[$name]->parent= &$this;
106 $this->by_object[$name]->set_acl_category($this->acl_category);
107 }
108 }
111 function execute()
112 {
113 /* Ensure that the currently selected tab is valid. */
114 if(!isset($this->by_name[$this->current])) $this->current = key($this->by_name);
116 /* Rotate current to last */
117 $this->last= $this->current;
119 /* Look for pressed tab button */
120 foreach ($this->by_object as $class => &$obj){
121 if (isset($_POST[$class]) || (isset($_POST['arg']) && $_POST['arg'] == "$class")){
122 $this->current= $class;
123 break;
124 }
125 }
127 /* Save last tab object */
128 if ($this->last == $this->current){
129 $this->save_object(TRUE);
130 } else {
131 $this->save_object(FALSE);
132 }
134 /* Show object */
135 $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";
136 $display.= "<tr><td>\n";
138 /* If multiple edit is enabled for this tab,
139 we have tho display different templates */
140 if(!$this->multiple_support_active){
141 $display.= $this->by_object[$this->current]->execute();
142 }else{
143 $display.= $this->by_object[$this->current]->multiple_execute();
144 }
145 $modal_dialog = $this->by_object[$this->current]->is_modal_dialog();
147 /* Build tab line */
148 $modal = TRUE;
149 $tabs= $this->gen_tabs($modal_dialog);
151 /* Footer for tabbed dialog */
152 $display = $tabs.$display."</td></tr></table>";
154 return ($display);
155 }
157 function save_object($save_current= FALSE)
158 {
159 /* Save last tab */
160 if ($this->last != ""){
161 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
162 $this->last, "Saving");
164 if(!$this->multiple_support_active){
165 $this->by_object[$this->last]->save_object ();
166 }else{
167 $this->by_object[$this->last]->multiple_save_object();
168 }
169 }
171 /* Skip if curent and last are the same object */
172 if ($this->last == $this->current){
173 return;
174 }
176 $obj= @$this->by_object[$this->current];
177 $this->disabled= $obj->parent->disabled;
179 if ($save_current){
180 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
181 $this->current, "Saving (current)");
183 if(!$this->multiple_support_active){
184 $obj->save_object();
185 }else{
186 $obj->multiple_save_object();
187 }
188 }
189 }
191 function gen_tabs($disabled = FALSE)
192 {
193 $display ="";
194 if(!$disabled){
195 $display.= "<input type=\"hidden\" name=\"arg\" value=\"\">";
196 }
197 $display.= "<table summary=\"\" cellpadding=0 cellspacing=0 border=0 style=\"width:100%;\"><tr>";
198 $index= 0;
199 $style= array("tab_left", "tab_active", "tab_near_active", "tab_right");
200 foreach ($this->by_name as $class => $name){
202 /* Activate right tabs with style "tab_right"
203 Activate near current with style "tab_near_active" */
204 if ($index == 2 || $index == 1){
205 $index++;
206 }
208 /* Activate current tab with style "tab_active " */
209 if ($class == $this->current){
210 $index++;
211 }
213 /* Paint tab */
214 $display.= "<td style=\"vertical-align:bottom;width:1px;white-space:nowrap;\">";
216 /* Shorten string if its too long for the tab headers*/
217 $title= _($name);
218 if (mb_strlen($title, 'UTF-8') > 28){
219 $title= mb_substr($title,0, 25, 'UTF-8')."...";
220 }
222 /* nobr causes w3c warnings so we use to keep the tab name in one line */
223 $title= str_replace(" "," ",$title);
225 /* Take care about notifications */
226 $obj = $this->by_object[$class];
227 if ( $this->by_object[$class]->pl_notify && ($obj->is_account || $obj->ignore_account)){
228 $notify= "id=\"notify\"";
229 } else {
230 $notify= "";
231 }
233 if($disabled){
234 $display.= "<div ".$notify." class=\"$style[$index]\"
235 style=' font-family:arial,helvetica,sans-serif;
236 font-weight:bold;
237 font-size:13px;
238 color: gray;'
239 title=\"$title\">".$title."</div>";
240 }elseif (session::global_get('js')==FALSE){
241 $display.= "<div ".$notify." class=\"$style[$index]\"><input type=\"submit\" name=\"$class\"".
242 " class=\"$style[$index]\" value=\"$title\"></div></td>";
243 } else {
244 $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></div></td>";
245 }
246 }
247 $display.= "<td style=\"vertical-align:bottom;\">\n";
248 $display.= "<div class=\"tab_border\"> </div></td></tr></table>";
250 return($display);
251 }
254 function set_acl($acl)
255 {
256 /* Look for attribute in ACL */
257 trigger_error("Don't use tabs::set_acl() its obsolete.");
258 }
260 function delete()
261 {
262 /* Check if all plugins will ACK for deletion */
263 foreach (array_reverse($this->by_object) as $key => $obj){
264 $reason= $obj->allow_remove();
265 if ($reason != ""){
266 msg_dialog::display(_("Warning"), sprintf(_("Delete process has been canceled by plugin '%s': %s"), $key, $reason), WARNING_DIALOG);
267 return;
268 }
269 }
271 /* Delete for all plugins */
272 foreach (array_reverse($this->by_object) as $obj){
273 $obj->remove_from_parent();
274 }
275 }
277 function password_change_needed()
278 {
279 /* Ask all plugins for needed password changes */
280 foreach ($this->by_object as &$obj){
281 if ($obj->password_change_needed()){
282 return TRUE;
283 }
284 }
286 return FALSE;
287 }
289 function check($ignore_account= FALSE)
290 {
291 $this->save_object(TRUE);
292 $messages= array();
294 $current_set = FALSE;
296 /* Check all plugins */
297 foreach ($this->by_object as $key => &$obj){
298 if ($obj->is_account || $ignore_account || $obj->ignore_account){
299 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$key, "Checking");
301 if(!$this->multiple_support_active){
302 $msg = $obj->check();
303 }else{
304 $msg = $obj->multiple_check();
305 }
307 if (count($msg)){
308 $obj->pl_notify= TRUE;
309 if(!$current_set){
310 $current_set = TRUE;
311 $this->current= $key;
312 $messages = $msg;
313 }
314 }else{
315 $obj->pl_notify= FALSE;
316 }
317 }else{
318 $obj->pl_notify= FALSE;
319 }
320 }
321 return ($messages);
322 }
324 function save($ignore_account= FALSE)
325 {
326 /* Save all plugins */
327 foreach ($this->by_object as $key => &$obj){
328 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
329 $key, "Saving");
331 $obj->dn= $this->dn;
333 if(!$obj instanceof plugin){
334 trigger_error("Something went wrong while saving ".$obj->dn.". Object class '".get_class($obj)."'.");
335 }else{
337 if ($obj->is_account || $ignore_account || $obj->ignore_account){
338 if ($obj->save() == 1){
339 return (1);
340 }
341 } else {
342 $obj->remove_from_parent();
343 }
344 }
345 }
346 return (0);
347 }
349 function adapt_from_template($dn, $skip= array())
350 {
351 foreach ($this->by_object as $key => &$obj){
352 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
353 $key, "Adapting");
354 $obj->parent= &$this;
355 $obj->adapt_from_template($dn, $skip);
356 }
357 }
360 /* Save attributes posted by copy & paste dialog
361 */
362 function saveCopyDialog()
363 {
364 foreach ($this->by_object as &$obj){
365 if($obj->is_account || $obj->ignore_account){
366 $obj->saveCopyDialog();
367 }
368 }
369 }
372 /* return copy & paste dialog
373 */
374 function getCopyDialog()
375 {
376 $ret = "";
377 $this->SubDialog = false;
378 foreach ($this->by_object as &$obj){
379 if($obj->is_account || $obj->ignore_account){
380 $tmp = $obj->getCopyDialog();
381 if($tmp['status'] == "SubDialog"){
382 $this->SubDialog = true;
383 return($tmp['string']);
384 }else{
385 if(!empty($tmp['string'])){
386 $ret .= $tmp['string'];
387 $ret .= "<p class='seperator'> </p>";
388 }
389 }
390 }
391 }
392 return($ret);
393 }
396 function addSpecialTabs()
397 {
398 if(!$this->hide_acls){
399 $this->by_name['acl']= _("ACL");
400 $this->by_object['acl']= new acl($this->config, $this, $this->dn);
401 $this->by_object['acl']->parent= &$this;
402 }
403 if(!$this->hide_refs){
404 $this->by_name['reference']= _("References");
405 $this->by_object['reference']= new reference($this->config, $this->dn);
406 $this->by_object['reference']->parent= &$this;
407 }
408 }
411 function set_acl_base($base= "")
412 {
413 /* Update reference, transfer variables */
414 $first= ($base == "");
415 foreach ($this->by_object as &$obj){
416 if ($first){
417 $first= FALSE;
418 $base= $obj->acl_base;
419 } else {
420 $obj->set_acl_base($base);
421 }
422 }
423 }
426 /*! \brief Checks if one of the used tab plugins supports multiple edit.
427 @param boolean Returns TRUE if at least one plugins supports multiple edit.
428 */
429 function multiple_support_available()
430 {
431 foreach($this->by_object as $name => $obj){
432 if($obj->multiple_support){
433 return(TRUE);
434 }
435 }
436 return(FALSE);
437 }
440 /*! \brief Enables multiple edit support for the given tab.
441 All unsupported plugins will be disabled.
442 @param boolean Returns TRUE if at least one plugin supports multiple edit
443 */
444 function enable_multiple_support()
445 {
446 if(!$this->multiple_support_available()){
447 return(FALSE);
448 }else{
449 $this->multiple_support_active = TRUE;
450 foreach($this->by_object as $name => $obj){
451 if($obj->multiple_support){
452 $this->by_object[$name]->enable_multiple_support();
453 }else{
454 unset($this->by_object[$name]);
455 unset($this->by_name[$name]);
456 }
457 }
458 }
459 return(TRUE);
460 }
461 }
462 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
463 ?>