Code

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