Code

Added speed optimizations
[gosa.git] / include / class_tabs.inc
1 <?php
2 /*
3   This code is part of GOsa (https://gosa.gonicus.de)
4   Copyright (C) 2003  Cajus Pollmeier
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2 of the License, or
9   (at your option) any later version.
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
21 class tabs
22 {
23   var $dn;
24   var $config;
25   var $acl;
26   var $is_template;
27   var $is_new= FALSE;
29   var $last= "";
30   var $current= "";
31   var $disabled= "";
32   var $by_name= array();
33   var $by_object= array();
34   var $SubDialog = false;
36   function tabs($config, $data, $dn, $acl_category= "")
37   {
38         /* Save dn */
39         $this->dn= $dn;
40         $this->config= $config;
41         
42         $baseobject= NULL;
44         foreach ($data as $tab){
45                 $this->by_name[$tab['CLASS']]= $tab['NAME'];
47                 if ($baseobject == NULL){
48                         $baseobject= new $tab['CLASS']($this->config, $this->dn);
49                         $this->by_object[$tab['CLASS']]= $baseobject;
50                 } else {
51                         $this->by_object[$tab['CLASS']]= new $tab['CLASS']($this->config, $this->dn, $baseobject);
52                 }
54                 $this->by_object[$tab['CLASS']]->parent= &$this;
55                 $this->by_object[$tab['CLASS']]->set_acl_category($acl_category);
57                 /* Initialize current */
58                 if ($this->current == ""){
59                         $this->current= $tab['CLASS'];
60                 }
61         }
62   }
64   function execute()
65   {
66         /* Rotate current to last */
67         $this->last= $this->current;
69         /* Look for pressed tab button */
70         foreach ($this->by_object as $class => $obj){
71                 if (isset($_POST[$class]) || (isset($_POST['arg']) && $_POST['arg'] == "$class")){
72                         $this->current= $class;
73                         break;
74                 }
75         }
77         /* Save last tab object */
78         if ($this->last == $this->current){
79                 $this->save_object(TRUE);
80         } else {
81                 $this->save_object(FALSE);
82         }
84         /* Build tab line */
85         $display= $this->gen_tabs();
87         /* Show object */
88         $display.= "<table summary=\"\" cellpadding=4 cellspacing=0 border=0 style=\"width:100%; background-color:#F0F0F0; border-style:solid; border-color:black; border-top-width:0px; border-bottom-width:1px; border-left-width:1px; border-right-width:1px;\">\n";
89         $display.= "<tr><td>\n";
91         $obj= $this->by_object[$this->current];
92         $display.= $obj->execute();
93         $this->by_object[$this->current]= $obj;
95         /* Footer for tabbed dialog */
96         $display.= "</td></tr></table>";
97         return ($display);
98   }
100   function save_object($save_current= FALSE)
101   {
102         /* Save last tab */
103         if ($this->last != ""){
104                 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
105                         $this->last, "Saving");
107                 $obj= $this->by_object[$this->last];
108                 $obj->save_object ();
109                 $this->by_object[$this->last]= $obj;
110         }
112         /* Skip if curent and last are the same object */
113         if ($this->last == $this->current){
114                 return;
115         }
117         $obj= $this->by_object[$this->current];
118         $this->disabled= $obj->parent->disabled;
120         if ($save_current){
121                 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
122                         $this->current, "Saving (current)");
124                 $obj->save_object ();
125                 $this->by_object[$this->current]= $obj;
126         }
128   }
130   function gen_tabs()
131   {
132         $display= "<input type=\"hidden\" name=\"arg\" value=\"\">";
133         $display.= "<table summary=\"\" cellpadding=0 cellspacing=0 border=0 style=\"width:100%;\"><tr>";
134         $index= 0;
135         $style= array("tab_left", "tab_active", "tab_near_active", "tab_right");
136         foreach ($this->by_name as $class => $name){
138                 /* Activate right tabs with style "tab_right"
139                    Activate near current with style "tab_near_active" */
140                 if ($index == 2 || $index == 1){
141                         $index++;
142                 }
144                 /* Activate current tab with style "tab_active " */
145                 if ($class == $this->current){
146                         $index++;
147                 }
149                 /* Paint tab */
150                 $display.= "<td style=\"vertical-align:bottom;width:1px;\">";
152                 /* Shorten string if its too long for the tab headers*/
153                 $title= _($name);
154                 if (mb_strlen($title, 'UTF-8') > 28){
155                         $title= mb_substr($title,0, 25, 'UTF-8')."...";
156                 }
157                         
158                 /* nobr causes w3c warnings so we use &nbsp; to keep the tab name in one line */
159                 $title= preg_replace("/ /","&nbsp;",$title);
161                 if ($_SESSION['js']==FALSE){    
162                         $display.= "<div class=\"$style[$index]\"><input type=\"submit\" name=\"$class\"".
163                                    " class=\"$style[$index]\" value=\"$title\"";
164                 } else {                         
165                         $display.= "<div class=\"$style[$index]\"><a class=\"$style[$index]\" onclick=\"return true;\" href=\"javascript:document.mainform.arg.value='$class';document.mainform.submit();\">$title</a";
166                 }
167                 $display.= "></div></td>";
168         }
169         $display.= "<td style=\"vertical-align:bottom;\">\n";
170         $display.= "<div class=\"tab_border\">&nbsp;</div></td></tr></table>";
172         return($display);
173   }
176   function set_acl($acl)
177   {
178         /* Set local acl */
179         $this->acl= $acl;
181         /* Setup for all plugins */
182         foreach ($this->by_object as $key => $obj){
183                 $sacl= get_module_permission($acl, "$key", $this->dn);
184                 $obj->acl= $sacl;
185                 $this->by_object[$key]= $obj;
186         }
187   }
189   function delete()
190   {
191         /* Check if all plugins will ACK for deletion */
192         foreach (array_reverse($this->by_object) as $key => $obj){
193                 $reason= $obj->allow_remove();
194                 if ($reason != ""){
195                         print_red(sprintf(_("Delete process has been canceled by plugin '%s': %s"), $key, $reason));
196                         return;
197                 }
198         }
200         /* Delete for all plugins */
201         foreach (array_reverse($this->by_object) as $key => $obj){
202                 $obj->remove_from_parent();
203         }
204   }
206   function password_change_needed()
207   {
208         /* Ask all plugins for needed password changes */
209         foreach ($this->by_object as $key => $obj){
210                 if ($obj->password_change_needed()){
211                         return TRUE;
212                 }
213         }
215         return FALSE;
216   }
218   function check($ignore_account= FALSE)
219   {
220         $this->save_object(TRUE);
221         $messages= array();
223         /* Check all plugins */
224         foreach ($this->by_object as $key => $obj){
225                 if ($obj->is_account || $ignore_account || $obj->ignore_account){
226                         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
227                                 $key, "Checking");
229                         $messages= $obj->check();
230                         if (count($messages)){
231                                 $this->current= $key;
232                                 break;
233                         }
234                 }
235         }
237         return ($messages);
238   }
240   function save($ignore_account= FALSE)
241   {
242         /* Save all plugins */
243         foreach ($this->by_object as $key => $obj){
244                 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
245                         $key, "Saving");
247                 $obj->dn= $this->dn;
248                 
249                 if ($obj->is_account || $ignore_account || $obj->ignore_account){
250                         if ($obj->save() == 1){
251                                 return (1);
252                         }
253                 } else {
254                         $obj->remove_from_parent();
255                 }
256         }
257         return (0);
258   }
260   function adapt_from_template($dn)
261   {
262           foreach ($this->by_object as $key => $obj){
263                   @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
264                                   $key, "Adapting");
265                   $obj->parent= &$this;
266                   $obj->adapt_from_template($dn);
267                   $this->by_object[$key]= $obj;
268           }
269   }
271         
272   /* Save attributes posted by copy & paste dialog
273    */
274   function saveCopyDialog()
275   {
276           foreach ($this->by_object as $key => $obj){
277                   if($obj->is_account){
278                           $this->by_object[$key]->saveCopyDialog();
279                   }
280           }
281   }
284   /* return copy & paste dialog
285    */
286   function getCopyDialog()
287   {
288           $ret = "";
289           $this->SubDialog = false;
290           foreach ($this->by_object as $key => $obj){
291                   if($obj->is_account){
292                           $tmp = $this->by_object[$key]->getCopyDialog();
293                           if($tmp['status'] == "SubDialog"){
294                                   $this->SubDialog = true;
295                       return($tmp['string']);
296                   }else{
297                  $ret .= $tmp['string'];
298                           }
299                   }
300           }
301           return($ret);
302   }
305   function addSpecialTabs()
306   {
307         $this->by_name['acl']= _("ACL");
308         $this->by_object['acl']= new acl($this->config, $this, $this->dn);
309         $this->by_object['acl']->parent= &$this;
310         $this->by_name['reference']= _("References");
311         $this->by_object['reference']= new reference($this->config, $this->dn);
312         $this->by_object['reference']->parent= &$this;
313   }
316   function set_acl_base($base= "")
317   {
318         /* Update reference, transfer variables */
319         $first= ($base == "");
320         foreach ($this->by_object as $name => $obj){
321                 if ($first){
322                         $first= FALSE;
323                         $base= $obj->acl_base;
324                 } else {
325                         $obj->set_acl_base($base);
326                         $this->by_object[$name]= $obj;
327                 }
328         }
329   }
333 ?>