Code

Updated Tab looking
[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   {
117     echo "Do not render TABS while a sub-dialog is opened!<br>";
119     // Ensure that the currently selected tab is valid.
120     if(!isset($this->by_name[$this->current])) {
121       $this->current = key($this->by_name);
122     }
124     // Rotate current to last 
125     $this->last= $this->current;
127     // Look for pressed tab button
128     foreach ($this->by_object as $class => &$obj){
129       if (isset($_POST[$class]) || (isset($_POST['arg']) && $_POST['arg'] == "$class")){
130         $this->current= $class;
131         break;
132       }
133     }
135     // Save last tab object 
136     if ($this->last == $this->current){
137       $this->save_object(TRUE);
138     } else {
139       $this->save_object(FALSE);
140     }
142     /* If multiple edit is enabled for this tab, 
143        we have tho display different templates */
144     if(!$this->multiple_support_active){
145       $display= $this->by_object[$this->current]->execute();
146     }else{
147       $display= $this->by_object[$this->current]->multiple_execute();
148     }
149     $tabs= $this->gen_tabs();
151     $display =          "{$tabs}
152                         <input type='hidden' name='arg' value=''>
153                           <div class='tab-content'>{$display}
154                         </div>";
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()
194   {
195     $display = "\n  <div class='tabs'>";
196     $display.= "\n    <ul>";
199     foreach ($this->by_name as $class => $name){
201       // Shorten string if its too long for the tab headers
202       $title= _($name);
203       if (mb_strlen($title, 'UTF-8') > 28){
204         $title= mb_substr($title,0, 25, 'UTF-8')."...";
205       }
207       // nobr causes w3c warnings so we use &nbsp; to keep the tab name in one line 
208       $title= str_replace(" ","&nbsp;",$title);
210       // Take care about notifications 
211       $obj = $this->by_object[$class];
212       $notify ="";
213       if ( $this->by_object[$class]->pl_notify && ($obj->is_account || $obj->ignore_account)){
214         $notify= "!";
215       }
216       $tabClass = ($this->current == $class) ? "class='current'" :"";
217       $onClick = "document.mainform.arg.value='{$class}'; document.mainform.submit();";
218       $display.= "<li {$tabClass} onClick=\"{$onClick}\"><a href='#'>$title{$notify}</a></li>";
219     }
220     $display.="\n    </ul>";
221     $display.="\n  </div>";
222     return($display);
223   }
226   function set_acl($acl)
227   {
228         /* Look for attribute in ACL */
229           trigger_error("Don't use tabs::set_acl() its obsolete.");
230   }
232   function delete()
233   {
234     /* Check if all plugins will ACK for deletion */
235     foreach (array_reverse($this->by_object) as $key => $obj){
236       $reason= $obj->allow_remove();
237       if ($reason != ""){
238         msg_dialog::display(_("Warning"), sprintf(_("Delete process has been canceled by plugin '%s': %s"), $key, $reason), WARNING_DIALOG);
239         return;
240       }
241     }
243     /* Delete for all plugins */
244     foreach (array_reverse($this->by_object) as $obj){
245       $obj->remove_from_parent();
246     }
247   }
249   function password_change_needed()
250   {
251     /* Ask all plugins for needed password changes */
252     foreach ($this->by_object as &$obj){
253       if ($obj->password_change_needed()){
254         return TRUE;
255       }
256     }
258     return FALSE;
259   }
261   function check($ignore_account= FALSE)
262   {
263     $this->save_object(TRUE);
264     $messages= array();
266     $current_set = FALSE;
268     /* Check all plugins */
269     foreach ($this->by_object as $key => &$obj){
270       if ($obj->is_account || $ignore_account || $obj->ignore_account){
271         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$key, "Checking");
273         if(!$this->multiple_support_active){
274           $msg = $obj->check();
275         }else{
276           $msg = $obj->multiple_check();
277         }
279         if (count($msg)){
280           $obj->pl_notify= TRUE;
281           if(!$current_set){
282             $current_set = TRUE;
283             $this->current= $key;
284             $messages = $msg;
285           }
286         }else{
287           $obj->pl_notify= FALSE;
288         }
289       }else{
290         $obj->pl_notify= FALSE;
291       }
292     }
293     return ($messages);
294   }
296   function save($ignore_account= FALSE)
297   {
298     /* Save all plugins */
299     foreach ($this->by_object as $key => &$obj){
300       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
301           $key, "Saving");
303       $obj->dn= $this->dn;
305       if(!$obj instanceof plugin && !$obj instanceOf management){
306         trigger_error("Something went wrong while saving ".$obj->dn.". Object class '".get_class($obj)."'.");
307       }else{
309         if ($obj->is_account || $ignore_account || $obj->ignore_account){
310           if ($obj->save() == 1){
311             return (1);
312           }
313         } else {
314           $obj->remove_from_parent();
315         }
316       }
317     }
318     return (0);
319   }
321   function adapt_from_template($dn, $skip= array())
322   {
323     foreach ($this->by_object as $key => &$obj){
324       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
325           $key, "Adapting");
326       $obj->parent= &$this;
327       $obj->adapt_from_template($dn, $skip);
328     }
329   }
331         
332   /* Save attributes posted by copy & paste dialog
333    */
334   function saveCopyDialog()
335   {
336           foreach ($this->by_object as &$obj){
337                   if($obj->is_account || $obj->ignore_account){
338                           $obj->saveCopyDialog();
339                   }
340           }
341   }
344   /* return copy & paste dialog
345    */
346   function getCopyDialog()
347   {
348     $ret = "";
349     $this->SubDialog = false;
350     foreach ($this->by_object as &$obj){
351       if($obj->is_account || $obj->ignore_account){
352         $tmp = $obj->getCopyDialog();
353         if($tmp['status'] == "SubDialog"){
354           $this->SubDialog = true;
355           return($tmp['string']);
356         }else{
357           if(!empty($tmp['string'])){
358             $ret .= $tmp['string'];
359             $ret .= "<p class='seperator'>&nbsp;</p>";
360           }
361         }
362       }
363     }
364     return($ret);
365   }
368   function addSpecialTabs()
369   {
370     if(!$this->hide_acls){
371       $this->by_name['acl']= _("ACL");
372       $this->by_object['acl']= new acl($this->config, $this, $this->dn);
373       $this->by_object['acl']->parent= &$this;
374     }
375     if(!$this->hide_refs){
376       $this->by_name['reference']= _("References");
377       $this->by_object['reference']= new reference($this->config, $this->dn);
378       $this->by_object['reference']->parent= &$this;
379     }
380   }
383   function set_acl_base($base= "")
384   {
385     /* Update reference, transfer variables */
386     $first= ($base == "");
387     foreach ($this->by_object as &$obj){
388       if ($first){
389         $first= FALSE;
390         $base= $obj->acl_base;
391       } else {
392         $obj->set_acl_base($base);
393       }
394     }
395   }
397  
398   /*!   \brief    Checks if one of the used tab plugins supports multiple edit.
399         @param    boolean Returns TRUE if at least one plugins supports multiple edit. 
400   */
401   function multiple_support_available()
402   {
403     foreach($this->by_object as $name => $obj){
404       if($obj->multiple_support){
405         return(TRUE);
406       }
407     }
408     return(FALSE);
409   }  
412   /*!   \brief    Enables multiple edit support for the given tab.
413                   All unsupported plugins will be disabled.
414         @param    boolean Returns TRUE if at least one plugin supports multiple edit 
415   */
416   function enable_multiple_support()
417   {
418     if(!$this->multiple_support_available()){
419       return(FALSE);
420     }else{
421       $this->multiple_support_active = TRUE;
422       foreach($this->by_object as $name => $obj){
423         if($obj->multiple_support){
424           $this->by_object[$name]->enable_multiple_support();
425         }else{
426           unset($this->by_object[$name]);
427           unset($this->by_name[$name]);
428         }
429       }
430     }
431     return(TRUE);
432   }
434 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
435 ?>