Code

Updated setup
[gosa.git] / gosa-core / 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   var $multiple_support_active = FALSE;
38   function tabs(&$config, $data, $dn, $acl_category= "")
39   {
40     /* Save dn */
41     $this->dn= $dn;
42     $this->config= &$config;
44     $baseobject= NULL;
46     foreach ($data as &$tab){
48       if (!plugin_available($tab['CLASS'])){
49         continue;
50       }
52       $this->by_name[$tab['CLASS']]= $tab['NAME'];
54       if ($baseobject === NULL){
55         $baseobject= new $tab['CLASS']($this->config, $this->dn);
56         $baseobject->enable_CSN_check();
57         $this->by_object[$tab['CLASS']]= $baseobject;
58       } else {
59         $this->by_object[$tab['CLASS']]= new $tab['CLASS']($this->config, $this->dn, $baseobject);
60       }
62       $this->by_object[$tab['CLASS']]->parent= &$this;
63       $this->by_object[$tab['CLASS']]->set_acl_category($acl_category);
65       /* Initialize current */
66       if ($this->current == ""){
67         $this->current= $tab['CLASS'];
68       }
69     }
70   }
73   function execute()
74   {
75     /* Rotate current to last */
76     $this->last= $this->current;
78     /* Look for pressed tab button */
79     foreach ($this->by_object as $class => &$obj){
80       if (isset($_POST[$class]) || (isset($_POST['arg']) && $_POST['arg'] == "$class")){
81         $this->current= $class;
82         break;
83       }
84     }
86     /* Save last tab object */
87     if ($this->last == $this->current){
88       $this->save_object(TRUE);
89     } else {
90       $this->save_object(FALSE);
91     }
93     /* Build tab line */
94     $display= $this->gen_tabs();
96     /* Show object */
97     $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";
98     $display.= "<tr><td>\n";
100     /* If multiple edit is enabled for this tab, 
101        we have tho display different templates */
102     if(!$this->multiple_support_active){
103       $display.= $this->by_object[$this->current]->execute();
104     }else{
105       $display.= $this->by_object[$this->current]->multiple_execute();
106     }
108     /* Footer for tabbed dialog */
109     $display.= "</td></tr></table>";
111     return ($display);
112   }
114   function save_object($save_current= FALSE)
115   {
116     /* Save last tab */
117     if ($this->last != ""){
118       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
119           $this->last, "Saving");
121       if(!$this->multiple_support_active){
122         $this->by_object[$this->last]->save_object ();
123       }else{
124         $this->by_object[$this->last]->multiple_save_object();
125       }
126     }
128     /* Skip if curent and last are the same object */
129     if ($this->last == $this->current){
130       return;
131     }
133     $obj= @$this->by_object[$this->current];
134     $this->disabled= $obj->parent->disabled;
136     if ($save_current){
137       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
138           $this->current, "Saving (current)");
140       if(!$this->multiple_support_active){
141         $obj->save_object();
142       }else{
143         $obj->multiple_save_object();
144       }
145     }
146   }
148   function gen_tabs()
149   {
150     $display= "<input type=\"hidden\" name=\"arg\" value=\"\">";
151     $display.= "<table summary=\"\" cellpadding=0 cellspacing=0 border=0 style=\"width:100%;\"><tr>";
152     $index= 0;
153     $style= array("tab_left", "tab_active", "tab_near_active", "tab_right");
154     foreach ($this->by_name as $class => $name){
156       /* Activate right tabs with style "tab_right"
157          Activate near current with style "tab_near_active" */
158       if ($index == 2 || $index == 1){
159         $index++;
160       }
162       /* Activate current tab with style "tab_active " */
163       if ($class == $this->current){
164         $index++;
165       }
167       /* Paint tab */
168       $display.= "<td style=\"vertical-align:bottom;width:1px;white-space:nowrap;\">";
170       /* Shorten string if its too long for the tab headers*/
171       $title= _($name);
172       if (mb_strlen($title, 'UTF-8') > 28){
173         $title= mb_substr($title,0, 25, 'UTF-8')."...";
174       }
176       /* nobr causes w3c warnings so we use &nbsp; to keep the tab name in one line */
177       $title= preg_replace("/ /","&nbsp;",$title);
179       /* Take care about notifications */
180       $obj = $this->by_object[$class];
181       if ( $this->by_object[$class]->pl_notify && ($obj->is_account || $obj->ignore_account)){
182         $notify= "id=\"notify\"";
183       } else {
184         $notify= "";
185       }
187       if (session::get('js')==FALSE){   
188         $display.= "<div ".$notify." class=\"$style[$index]\"><input type=\"submit\" name=\"$class\"".
189           " class=\"$style[$index]\" value=\"$title\"";
190       } else {                   
191         $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";
192       }
193       $display.= "></div></td>";
194     }
195     $display.= "<td style=\"vertical-align:bottom;\">\n";
196     $display.= "<div class=\"tab_border\">&nbsp;</div></td></tr></table>";
198     return($display);
199   }
202   function set_acl($acl)
203   {
204         /* Look for attribute in ACL */
205           trigger_error("Don't use tabs::set_acl() its obsolete.");
206   }
208   function delete()
209   {
210     /* Check if all plugins will ACK for deletion */
211     foreach (array_reverse($this->by_object) as $key => $obj){
212       $reason= $obj->allow_remove();
213       if ($reason != ""){
214         msg_dialog::display(_("Warning"), sprintf(_("Delete process has been canceled by plugin '%s': %s"), $key, $reason), WARNING_DIALOG);
215         return;
216       }
217     }
219     /* Delete for all plugins */
220     foreach (array_reverse($this->by_object) as $obj){
221       $obj->remove_from_parent();
222     }
223   }
225   function password_change_needed()
226   {
227     /* Ask all plugins for needed password changes */
228     foreach ($this->by_object as &$obj){
229       if ($obj->password_change_needed()){
230         return TRUE;
231       }
232     }
234     return FALSE;
235   }
237   function check($ignore_account= FALSE)
238   {
239     $this->save_object(TRUE);
240     $messages= array();
242     $current_set = FALSE;
244     /* Check all plugins */
245     foreach ($this->by_object as $key => &$obj){
246       if ($obj->is_account || $ignore_account || $obj->ignore_account){
247         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$key, "Checking");
249         if(!$this->multiple_support_active){
250           $msg = $obj->check();
251         }else{
252           $msg = $obj->multiple_check();
253         }
255         if (count($msg)){
256           $obj->pl_notify= TRUE;
257           if(!$current_set){
258             $current_set = TRUE;
259             $this->current= $key;
260             $messages = $msg;
261           }
262         }else{
263           $obj->pl_notify= FALSE;
264         }
265       }else{
266         $obj->pl_notify= FALSE;
267       }
268     }
269     return ($messages);
270   }
272   function save($ignore_account= FALSE)
273   {
274     /* Save all plugins */
275     foreach ($this->by_object as $key => &$obj){
276       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
277           $key, "Saving");
279       $obj->dn= $this->dn;
281       if ($obj->is_account || $ignore_account || $obj->ignore_account){
282         if ($obj->save() == 1){
283           return (1);
284         }
285       } else {
286         $obj->remove_from_parent();
287       }
288     }
289     return (0);
290   }
292   function adapt_from_template($dn)
293   {
294     foreach ($this->by_object as $key => &$obj){
295       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
296           $key, "Adapting");
297       $obj->parent= &$this;
298       $obj->adapt_from_template($dn);
299     }
300   }
302         
303   /* Save attributes posted by copy & paste dialog
304    */
305   function saveCopyDialog()
306   {
307           foreach ($this->by_object as &$obj){
308                   if($obj->is_account || $obj->ignore_account){
309                           $obj->saveCopyDialog();
310                   }
311           }
312   }
315   /* return copy & paste dialog
316    */
317   function getCopyDialog()
318   {
319     $ret = "";
320     $this->SubDialog = false;
321     foreach ($this->by_object as &$obj){
322       if($obj->is_account || $obj->ignore_account){
323         $tmp = $obj->getCopyDialog();
324         if($tmp['status'] == "SubDialog"){
325           $this->SubDialog = true;
326           return($tmp['string']);
327         }else{
328           if(!empty($tmp['string'])){
329             $ret .= $tmp['string'];
330             $ret .= "<p class='seperator'>&nbsp;</p>";
331           }
332         }
333       }
334     }
335     return($ret);
336   }
339   function addSpecialTabs()
340   {
341     $this->by_name['acl']= _("ACL");
342     $this->by_object['acl']= new acl($this->config, $this, $this->dn);
343     $this->by_object['acl']->parent= &$this;
344     $this->by_name['reference']= _("References");
345     $this->by_object['reference']= new reference($this->config, $this->dn);
346     $this->by_object['reference']->parent= &$this;
347   }
350   function set_acl_base($base= "")
351   {
352     /* Update reference, transfer variables */
353     $first= ($base == "");
354     foreach ($this->by_object as &$obj){
355       if ($first){
356         $first= FALSE;
357         $base= $obj->acl_base;
358       } else {
359         $obj->set_acl_base($base);
360       }
361     }
362   }
364  
365   /*!   \brief    Checks if one of the used tab plugins supports multiple edit.
366         @param    boolean Returns TRUE if at least one plugins supports multiple edit. 
367   */
368   function multiple_support_available()
369   {
370     foreach($this->by_object as $name => $obj){
371       if($obj->multiple_support){
372         return(TRUE);
373       }
374     }
375     return(FALSE);
376   }  
379   /*!   \brief    Enables multiple edit support for the given tab.
380                   All unsupported plugins will be disabled.
381         @param    boolean Returns TRUE if at least one plugin supports multiple edit 
382   */
383   function enable_multiple_support()
384   {
385     if(!$this->multiple_support_available()){
386       return(FALSE);
387     }else{
388       $this->multiple_support_active = TRUE;
389       foreach($this->by_object as $name => $obj){
390         if($obj->multiple_support){
391           $this->by_object[$name]->enable_multiple_support();
392         }else{
393           unset($this->by_object[$name]);
394           unset($this->by_name[$name]);
395         }
396       }
397     }
398     return(TRUE);
399   }
401 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
402 ?>