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.
42 function tabs(&$config, $data, $dn, $acl_category= "")
43 {
44 /* Save dn */
45 $this->dn= $dn;
46 $this->config= &$config;
48 if(!count($data)) {
49 $data[] = array("CLASS" => 'plugin',"NAME" => 'Error');
50 msg_dialog::display(_("Error"),
51 sprintf(_("No plugin definitions found to initialize '%s', please check your configuration file."),get_class($this)),
52 "ERROR_DIALOG");
53 }
55 $baseobject= NULL;
56 $this->acl_category = $acl_category;
57 foreach ($data as &$tab){
59 if (!plugin_available($tab['CLASS'])){
60 continue;
61 }
63 $this->by_name[$tab['CLASS']]= $tab['NAME'];
65 if ($baseobject === NULL){
66 $baseobject= new $tab['CLASS']($this->config, $this->dn);
67 $baseobject->enable_CSN_check();
68 $this->by_object[$tab['CLASS']]= $baseobject;
69 } else {
70 $this->by_object[$tab['CLASS']]= new $tab['CLASS']($this->config, $this->dn, $baseobject);
71 }
73 $this->read_only |= $this->by_object[$tab['CLASS']]->read_only;
74 $this->by_object[$tab['CLASS']]->parent= &$this;
75 $this->by_object[$tab['CLASS']]->set_acl_category($this->acl_category);
77 /* Initialize current */
78 if ($this->current == ""){
79 $this->current= $tab['CLASS'];
80 }
81 }
82 }
85 /*! \brief Reinitializes the tab classes with fresh ldap values.
86 This maybe usefull if for example the apply button was pressed.
87 */
88 function re_init()
89 {
90 $baseobject= NULL;
91 foreach($this->by_object as $name => $object){
92 $class = get_class($object);
93 if(in_array($class,array("reference","acl"))) continue;
94 if ($baseobject === NULL){
95 $baseobject= new $class($this->config, $this->dn);
96 $baseobject->enable_CSN_check();
97 $this->by_object[$name]= $baseobject;
98 } else {
99 $this->by_object[$name]= new $class($this->config, $this->dn, $baseobject);
100 }
101 $this->by_object[$name]->parent= &$this;
102 $this->by_object[$name]->set_acl_category($this->acl_category);
103 }
104 }
107 function execute()
108 {
109 /* Ensure that the currently selected tab is valid. */
110 if(!isset($this->by_name[$this->current])) $this->current = key($this->by_name);
112 /* Rotate current to last */
113 $this->last= $this->current;
115 /* Look for pressed tab button */
116 foreach ($this->by_object as $class => &$obj){
117 if (isset($_POST[$class]) || (isset($_POST['arg']) && $_POST['arg'] == "$class")){
118 $this->current= $class;
119 break;
120 }
121 }
123 /* Save last tab object */
124 if ($this->last == $this->current){
125 $this->save_object(TRUE);
126 } else {
127 $this->save_object(FALSE);
128 }
130 /* Show object */
131 $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";
132 $display.= "<tr><td>\n";
134 /* If multiple edit is enabled for this tab,
135 we have tho display different templates */
136 if(!$this->multiple_support_active){
137 $display.= $this->by_object[$this->current]->execute();
138 }else{
139 $display.= $this->by_object[$this->current]->multiple_execute();
140 }
141 $modal_dialog = $this->by_object[$this->current]->is_modal_dialog();
143 /* Build tab line */
144 $modal = TRUE;
145 $tabs= $this->gen_tabs($modal_dialog);
147 /* Footer for tabbed dialog */
148 $display = $tabs.$display."</td></tr></table>";
150 return ($display);
151 }
153 function save_object($save_current= FALSE)
154 {
155 /* Save last tab */
156 if ($this->last != ""){
157 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
158 $this->last, "Saving");
160 if(!$this->multiple_support_active){
161 $this->by_object[$this->last]->save_object ();
162 }else{
163 $this->by_object[$this->last]->multiple_save_object();
164 }
165 }
167 /* Skip if curent and last are the same object */
168 if ($this->last == $this->current){
169 return;
170 }
172 $obj= @$this->by_object[$this->current];
173 $this->disabled= $obj->parent->disabled;
175 if ($save_current){
176 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
177 $this->current, "Saving (current)");
179 if(!$this->multiple_support_active){
180 $obj->save_object();
181 }else{
182 $obj->multiple_save_object();
183 }
184 }
185 }
187 function gen_tabs($disabled = FALSE)
188 {
189 $display= "<input type=\"hidden\" name=\"arg\" value=\"\">";
190 $display.= "<table summary=\"\" cellpadding=0 cellspacing=0 border=0 style=\"width:100%;\"><tr>";
191 $index= 0;
192 $style= array("tab_left", "tab_active", "tab_near_active", "tab_right");
193 foreach ($this->by_name as $class => $name){
195 /* Activate right tabs with style "tab_right"
196 Activate near current with style "tab_near_active" */
197 if ($index == 2 || $index == 1){
198 $index++;
199 }
201 /* Activate current tab with style "tab_active " */
202 if ($class == $this->current){
203 $index++;
204 }
206 /* Paint tab */
207 $display.= "<td style=\"vertical-align:bottom;width:1px;white-space:nowrap;\">";
209 /* Shorten string if its too long for the tab headers*/
210 $title= _($name);
211 if (mb_strlen($title, 'UTF-8') > 28){
212 $title= mb_substr($title,0, 25, 'UTF-8')."...";
213 }
215 /* nobr causes w3c warnings so we use to keep the tab name in one line */
216 $title= str_replace(" "," ",$title);
218 /* Take care about notifications */
219 $obj = $this->by_object[$class];
220 if ( $this->by_object[$class]->pl_notify && ($obj->is_account || $obj->ignore_account)){
221 $notify= "id=\"notify\"";
222 } else {
223 $notify= "";
224 }
226 if($disabled){
227 $display.= "<div ".$notify." class=\"$style[$index]\"
228 style=' font-family:arial,helvetica,sans-serif;
229 font-weight:bold;
230 font-size:13px;
231 color: gray;'
232 title=\"$title\">".$title."</div>";
233 }elseif (session::global_get('js')==FALSE){
234 $display.= "<div ".$notify." class=\"$style[$index]\"><input type=\"submit\" name=\"$class\"".
235 " class=\"$style[$index]\" value=\"$title\"></div></td>";
236 } else {
237 $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>";
238 }
239 }
240 $display.= "<td style=\"vertical-align:bottom;\">\n";
241 $display.= "<div class=\"tab_border\"> </div></td></tr></table>";
243 return($display);
244 }
247 function set_acl($acl)
248 {
249 /* Look for attribute in ACL */
250 trigger_error("Don't use tabs::set_acl() its obsolete.");
251 }
253 function delete()
254 {
255 /* Check if all plugins will ACK for deletion */
256 foreach (array_reverse($this->by_object) as $key => $obj){
257 $reason= $obj->allow_remove();
258 if ($reason != ""){
259 msg_dialog::display(_("Warning"), sprintf(_("Delete process has been canceled by plugin '%s': %s"), $key, $reason), WARNING_DIALOG);
260 return;
261 }
262 }
264 /* Delete for all plugins */
265 foreach (array_reverse($this->by_object) as $obj){
266 $obj->remove_from_parent();
267 }
268 }
270 function password_change_needed()
271 {
272 /* Ask all plugins for needed password changes */
273 foreach ($this->by_object as &$obj){
274 if ($obj->password_change_needed()){
275 return TRUE;
276 }
277 }
279 return FALSE;
280 }
282 function check($ignore_account= FALSE)
283 {
284 $this->save_object(TRUE);
285 $messages= array();
287 $current_set = FALSE;
289 /* Check all plugins */
290 foreach ($this->by_object as $key => &$obj){
291 if ($obj->is_account || $ignore_account || $obj->ignore_account){
292 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$key, "Checking");
294 if(!$this->multiple_support_active){
295 $msg = $obj->check();
296 }else{
297 $msg = $obj->multiple_check();
298 }
300 if (count($msg)){
301 $obj->pl_notify= TRUE;
302 if(!$current_set){
303 $current_set = TRUE;
304 $this->current= $key;
305 $messages = $msg;
306 }
307 }else{
308 $obj->pl_notify= FALSE;
309 }
310 }else{
311 $obj->pl_notify= FALSE;
312 }
313 }
314 return ($messages);
315 }
317 function save($ignore_account= FALSE)
318 {
319 /* Save all plugins */
320 foreach ($this->by_object as $key => &$obj){
321 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
322 $key, "Saving");
324 $obj->dn= $this->dn;
326 if(!$obj instanceof plugin){
327 trigger_error("Something went wrong while saving ".$obj->dn.". Object class '".get_class($obj)."'.");
328 }else{
330 if ($obj->is_account || $ignore_account || $obj->ignore_account){
331 if ($obj->save() == 1){
332 return (1);
333 }
334 } else {
335 $obj->remove_from_parent();
336 }
337 }
338 }
339 return (0);
340 }
342 function adapt_from_template($dn, $skip= array())
343 {
344 foreach ($this->by_object as $key => &$obj){
345 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
346 $key, "Adapting");
347 $obj->parent= &$this;
348 $obj->adapt_from_template($dn, $skip);
349 }
350 }
353 /* Save attributes posted by copy & paste dialog
354 */
355 function saveCopyDialog()
356 {
357 foreach ($this->by_object as &$obj){
358 if($obj->is_account || $obj->ignore_account){
359 $obj->saveCopyDialog();
360 }
361 }
362 }
365 /* return copy & paste dialog
366 */
367 function getCopyDialog()
368 {
369 $ret = "";
370 $this->SubDialog = false;
371 foreach ($this->by_object as &$obj){
372 if($obj->is_account || $obj->ignore_account){
373 $tmp = $obj->getCopyDialog();
374 if($tmp['status'] == "SubDialog"){
375 $this->SubDialog = true;
376 return($tmp['string']);
377 }else{
378 if(!empty($tmp['string'])){
379 $ret .= $tmp['string'];
380 $ret .= "<p class='seperator'> </p>";
381 }
382 }
383 }
384 }
385 return($ret);
386 }
389 function addSpecialTabs()
390 {
391 $this->by_name['acl']= _("ACL");
392 $this->by_object['acl']= new acl($this->config, $this, $this->dn);
393 $this->by_object['acl']->parent= &$this;
394 $this->by_name['reference']= _("References");
395 $this->by_object['reference']= new reference($this->config, $this->dn);
396 $this->by_object['reference']->parent= &$this;
397 }
400 function set_acl_base($base= "")
401 {
402 /* Update reference, transfer variables */
403 $first= ($base == "");
404 foreach ($this->by_object as &$obj){
405 if ($first){
406 $first= FALSE;
407 $base= $obj->acl_base;
408 } else {
409 $obj->set_acl_base($base);
410 }
411 }
412 }
415 /*! \brief Checks if one of the used tab plugins supports multiple edit.
416 @param boolean Returns TRUE if at least one plugins supports multiple edit.
417 */
418 function multiple_support_available()
419 {
420 foreach($this->by_object as $name => $obj){
421 if($obj->multiple_support){
422 return(TRUE);
423 }
424 }
425 return(FALSE);
426 }
429 /*! \brief Enables multiple edit support for the given tab.
430 All unsupported plugins will be disabled.
431 @param boolean Returns TRUE if at least one plugin supports multiple edit
432 */
433 function enable_multiple_support()
434 {
435 if(!$this->multiple_support_available()){
436 return(FALSE);
437 }else{
438 $this->multiple_support_active = TRUE;
439 foreach($this->by_object as $name => $obj){
440 if($obj->multiple_support){
441 $this->by_object[$name]->enable_multiple_support();
442 }else{
443 unset($this->by_object[$name]);
444 unset($this->by_name[$name]);
445 }
446 }
447 }
448 return(TRUE);
449 }
450 }
451 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
452 ?>