Code

Fixed alignment of the icon menu
[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         trigger_error(sprintf("Unknown class '%s'!", $tab['CLASS']));
67         continue;
68       }
69       if ($this->current == "")  $this->current= $tab['CLASS'];
71       $this->by_name[$tab['CLASS']]= $tab['NAME'];
73       if ($baseobject === NULL){
74         $baseobject= new $tab['CLASS']($this->config, $this->dn);
75         $baseobject->enable_CSN_check();
76         $this->by_object[$tab['CLASS']]= $baseobject;
77       } else {
78         $this->by_object[$tab['CLASS']]= new $tab['CLASS']($this->config, $this->dn, $baseobject);
79       }
81       $this->read_only |= $this->by_object[$tab['CLASS']]->read_only;
82       $this->by_object[$tab['CLASS']]->parent= &$this;
83       $this->by_object[$tab['CLASS']]->set_acl_category($this->acl_category);
84     }
85   }
87  
88   /*! \brief Reinitializes the tab classes with fresh ldap values.
89              This maybe usefull if for example the apply button was pressed.
90    */ 
91   function re_init()
92   {
93     $baseobject= NULL;
94     foreach($this->by_object as $name => $object){
95       $class = get_class($object);
96       if(in_array($class,array("reference","acl"))) continue;
97       if ($baseobject === NULL){
98         $baseobject= new $class($this->config, $this->dn);
99         $baseobject->enable_CSN_check();
100         $this->by_object[$name]= $baseobject;
101       } else {
102         $this->by_object[$name]= new $class($this->config, $this->dn, $baseobject);
103       }
104       $this->by_object[$name]->parent= &$this;
105       $this->by_object[$name]->set_acl_category($this->acl_category);
106     }
107   }
110   function execute()
111   {
112     // Ensure that the currently selected tab is valid.
113     if(!isset($this->by_name[$this->current])) {
114       $this->current = key($this->by_name);
115     }
117     // Rotate current to last 
118     $this->last= $this->current;
120     // Look for pressed tab button
121     foreach ($this->by_object as $class => &$obj){
122       if (isset($_POST[$class]) || (isset($_POST['arg']) && $_POST['arg'] == "$class")){
123         $this->current= $class;
124         break;
125       }
126     }
128     // Save last tab object 
129     if ($this->last == $this->current){
130       $this->save_object(TRUE);
131     } else {
132       $this->save_object(FALSE);
133     }
135     /* If multiple edit is enabled for this tab, 
136        we have tho display different templates */
137     if(!$this->multiple_support_active){
138       $display= $this->by_object[$this->current]->execute();
139     }else{
140       $display= $this->by_object[$this->current]->multiple_execute();
141     }
142     $tabs= $this->gen_tabs();
143     $display =   
144       "\n        {$tabs}".
145       "\n        <input type='hidden' name='arg' value=''>".
146       "\n        <div class='tab-content'>".
147       "\n          {$display}".
148       "\n        </div>";
150     return ($display);
151   }
153   function save_object($save_current= FALSE)
154   {
156   
158     /* Save last tab */
159     if ($this->last != ""){
160       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
161           $this->last, "Saving");
163       if(!$this->multiple_support_active){
164         $this->by_object[$this->last]->save_object ();
165       }else{
166         $this->by_object[$this->last]->multiple_save_object();
167       }
168     }
170     /* Skip if curent and last are the same object */
171     if ($this->last == $this->current){
172       return;
173     }
175     $obj= @$this->by_object[$this->current];
176     $this->disabled= $obj->parent->disabled;
178     if ($save_current){
179       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
180           $this->current, "Saving (current)");
182       if(!$this->multiple_support_active){
183         $obj->save_object();
184       }else{
185         $obj->multiple_save_object();
186       }
187     }
188   }
190   function gen_tabs()
191   {
192     if($this->by_object[$this->current]->is_modal_dialog()) return("");
194     $display = "\n  <div class='tabs'>";
195     $display.= "\n    <ul>";
198     foreach ($this->by_name as $class => $name){
200       // Shorten string if its too long for the tab headers
201       $title= _($name);
202       if (mb_strlen($title, 'UTF-8') > 28){
203         $title= mb_substr($title,0, 25, 'UTF-8')."...";
204       }
206       // nobr causes w3c warnings so we use &nbsp; to keep the tab name in one line 
207       $title= str_replace(" ","&nbsp;",$title);
209       // Take care about notifications 
210       $obj = $this->by_object[$class];
211       $notify ="";
212       if ( $this->by_object[$class]->pl_notify && ($obj->is_account || $obj->ignore_account)){
213         $notify= "!";
214       }
215       $tabClass = ($this->current == $class) ? "class='current'" :"";
216       $onClick = "document.mainform.arg.value='{$class}'; document.mainform.submit();";
217       $display.= "\n      <li {$tabClass} onClick=\"{$onClick}\"><a href='#'>$title{$notify}</a></li>";
218     }
219     $display.="\n    </ul>";
220     $display.="\n  </div>";
221     return($display);
222   }
225   function set_acl($acl)
226   {
227         /* Look for attribute in ACL */
228           trigger_error("Don't use tabs::set_acl() its obsolete.");
229   }
231   function delete()
232   {
233     /* Check if all plugins will ACK for deletion */
234     foreach (array_reverse($this->by_object) as $key => $obj){
235       $reason= $obj->allow_remove();
236       if ($reason != ""){
237         msg_dialog::display(_("Warning"), sprintf(_("Delete process has been canceled by plugin '%s': %s"), $key, $reason), WARNING_DIALOG);
238         return;
239       }
240     }
242     /* Delete for all plugins */
243     foreach (array_reverse($this->by_object) as $obj){
244       $obj->remove_from_parent();
245     }
246   }
248   function password_change_needed()
249   {
250     /* Ask all plugins for needed password changes */
251     foreach ($this->by_object as &$obj){
252       if ($obj->password_change_needed()){
253         return TRUE;
254       }
255     }
257     return FALSE;
258   }
260   function check($ignore_account= FALSE)
261   {
262     $this->save_object(TRUE);
263     $messages= array();
265     $current_set = FALSE;
267     /* Check all plugins */
268     foreach ($this->by_object as $key => &$obj){
269       if ($obj->is_account || $ignore_account || $obj->ignore_account){
270         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$key, "Checking");
272         if(!$this->multiple_support_active){
273           $msg = $obj->check();
274         }else{
275           $msg = $obj->multiple_check();
276         }
278         if (count($msg)){
279           $obj->pl_notify= TRUE;
280           if(!$current_set){
281             $current_set = TRUE;
282             $this->current= $key;
283             $messages = $msg;
284           }
285         }else{
286           $obj->pl_notify= FALSE;
287         }
288       }else{
289         $obj->pl_notify= FALSE;
290       }
291     }
292     return ($messages);
293   }
295   function save($ignore_account= FALSE)
296   {
297     /* Save all plugins */
298     foreach ($this->by_object as $key => &$obj){
299       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
300           $key, "Saving");
302       $obj->dn= $this->dn;
304       if(!$obj instanceof plugin && !$obj instanceOf management){
305         trigger_error("Something went wrong while saving ".$obj->dn.". Object class '".get_class($obj)."'.");
306       }else{
308         if ($obj->is_account || $ignore_account || $obj->ignore_account){
309           if ($obj->save() == 1){
310             return (1);
311           }
312         } else {
313           $obj->remove_from_parent();
314         }
315       }
316     }
317     return (0);
318   }
320   function adapt_from_template($dn, $skip= array())
321   {
322     foreach ($this->by_object as $key => &$obj){
323       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
324           $key, "Adapting");
325       $obj->parent= &$this;
326       $obj->adapt_from_template($dn, $skip);
327     }
328   }
330         
331   /* Save attributes posted by copy & paste dialog
332    */
333   function saveCopyDialog()
334   {
335           foreach ($this->by_object as &$obj){
336                   if($obj->is_account || $obj->ignore_account){
337                           $obj->saveCopyDialog();
338                   }
339           }
340   }
343   /* return copy & paste dialog
344    */
345   function getCopyDialog()
346   {
347     $ret = "";
348     $this->SubDialog = false;
349     foreach ($this->by_object as &$obj){
350       if($obj->is_account || $obj->ignore_account){
351         $tmp = $obj->getCopyDialog();
352         if($tmp['status'] == "SubDialog"){
353           $this->SubDialog = true;
354           return($tmp['string']);
355         }else{
356           if(!empty($tmp['string'])){
357             $ret .= $tmp['string'];
358             $ret .= "<p class='seperator'>&nbsp;</p>";
359           }
360         }
361       }
362     }
363     return($ret);
364   }
367   function addSpecialTabs()
368   {
369     if(!$this->hide_acls){
370       $this->by_name['acl']= _("ACL");
371       $this->by_object['acl']= new acl($this->config, $this, $this->dn);
372       $this->by_object['acl']->parent= &$this;
373     }
374     if(!$this->hide_refs){
375       $this->by_name['reference']= _("References");
376       $this->by_object['reference']= new reference($this->config, $this->dn);
377       $this->by_object['reference']->parent= &$this;
378     }
379   }
382   function set_acl_base($base= "")
383   {
384     /* Update reference, transfer variables */
385     $first= ($base == "");
386     foreach ($this->by_object as &$obj){
387       if ($first){
388         $first= FALSE;
389         $base= $obj->acl_base;
390       } else {
391         $obj->set_acl_base($base);
392       }
393     }
394   }
396  
397   /*!   \brief    Checks if one of the used tab plugins supports multiple edit.
398         @param    boolean Returns TRUE if at least one plugins supports multiple edit. 
399   */
400   function multiple_support_available()
401   {
402     foreach($this->by_object as $name => $obj){
403       if($obj->multiple_support){
404         return(TRUE);
405       }
406     }
407     return(FALSE);
408   }  
411   /*!   \brief    Enables multiple edit support for the given tab.
412                   All unsupported plugins will be disabled.
413         @param    boolean Returns TRUE if at least one plugin supports multiple edit 
414   */
415   function enable_multiple_support()
416   {
417     if(!$this->multiple_support_available()){
418       return(FALSE);
419     }else{
420       $this->multiple_support_active = TRUE;
421       foreach($this->by_object as $name => $obj){
422         if($obj->multiple_support){
423           $this->by_object[$name]->enable_multiple_support();
424         }else{
425           unset($this->by_object[$name]);
426           unset($this->by_name[$name]);
427         }
428       }
429     }
430     return(TRUE);
431   }
433 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
434 ?>