Code

Updated comment
[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   function tabs(&$config, $data, $dn, $acl_category= "")
41   {
42     /* Save dn */
43     $this->dn= $dn;
44     $this->config= &$config;
46     $baseobject= NULL;
47     $this->acl_category = $acl_category;
48     foreach ($data as &$tab){
50       if (!plugin_available($tab['CLASS'])){
51         continue;
52       }
54       $this->by_name[$tab['CLASS']]= $tab['NAME'];
56       if ($baseobject === NULL){
57         $baseobject= new $tab['CLASS']($this->config, $this->dn);
58         $baseobject->enable_CSN_check();
59         $this->by_object[$tab['CLASS']]= $baseobject;
60       } else {
61         $this->by_object[$tab['CLASS']]= new $tab['CLASS']($this->config, $this->dn, $baseobject);
62       }
64       $this->by_object[$tab['CLASS']]->parent= &$this;
65       $this->by_object[$tab['CLASS']]->set_acl_category($this->acl_category);
67       /* Initialize current */
68       if ($this->current == ""){
69         $this->current= $tab['CLASS'];
70       }
71     }
72   }
74  
75   /*! \brief Reinitializes the tab classes with fresh ldap values.
76              This maybe usefull if for example the apply button was pressed.
77    */ 
78   function re_init()
79   {
80     $baseobject= NULL;
81     foreach($this->by_object as $name => $object){
82       $class = get_class($object);
83       if(in_array($class,array("reference","acl"))) continue;
84       if ($baseobject === NULL){
85         $baseobject= new $class($this->config, $this->dn);
86         $baseobject->enable_CSN_check();
87         $this->by_object[$name]= $baseobject;
88       } else {
89         $this->by_object[$name]= new $class($this->config, $this->dn, $baseobject);
90       }
91       $this->by_object[$name]->parent= &$this;
92       $this->by_object[$name]->set_acl_category($this->acl_category);
93     }
94   }
97   function execute()
98   {
99     /* Rotate current to last */
100     $this->last= $this->current;
102     /* Look for pressed tab button */
103     foreach ($this->by_object as $class => &$obj){
104       if (isset($_POST[$class]) || (isset($_POST['arg']) && $_POST['arg'] == "$class")){
105         $this->current= $class;
106         break;
107       }
108     }
110     /* Save last tab object */
111     if ($this->last == $this->current){
112       $this->save_object(TRUE);
113     } else {
114       $this->save_object(FALSE);
115     }
117     /* Build tab line */
118     $display= $this->gen_tabs();
120     /* Show object */
121     $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";
122     $display.= "<tr><td>\n";
124     /* If multiple edit is enabled for this tab, 
125        we have tho display different templates */
126     if(!$this->multiple_support_active){
127       $display.= $this->by_object[$this->current]->execute();
128     }else{
129       $display.= $this->by_object[$this->current]->multiple_execute();
130     }
132     /* Footer for tabbed dialog */
133     $display.= "</td></tr></table>";
135     return ($display);
136   }
138   function save_object($save_current= FALSE)
139   {
140     /* Save last tab */
141     if ($this->last != ""){
142       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
143           $this->last, "Saving");
145       if(!$this->multiple_support_active){
146         $this->by_object[$this->last]->save_object ();
147       }else{
148         $this->by_object[$this->last]->multiple_save_object();
149       }
150     }
152     /* Skip if curent and last are the same object */
153     if ($this->last == $this->current){
154       return;
155     }
157     $obj= @$this->by_object[$this->current];
158     $this->disabled= $obj->parent->disabled;
160     if ($save_current){
161       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
162           $this->current, "Saving (current)");
164       if(!$this->multiple_support_active){
165         $obj->save_object();
166       }else{
167         $obj->multiple_save_object();
168       }
169     }
170   }
172   function gen_tabs()
173   {
174     $display= "<input type=\"hidden\" name=\"arg\" value=\"\">";
175     $display.= "<table summary=\"\" cellpadding=0 cellspacing=0 border=0 style=\"width:100%;\"><tr>";
176     $index= 0;
177     $style= array("tab_left", "tab_active", "tab_near_active", "tab_right");
178     foreach ($this->by_name as $class => $name){
180       /* Activate right tabs with style "tab_right"
181          Activate near current with style "tab_near_active" */
182       if ($index == 2 || $index == 1){
183         $index++;
184       }
186       /* Activate current tab with style "tab_active " */
187       if ($class == $this->current){
188         $index++;
189       }
191       /* Paint tab */
192       $display.= "<td style=\"vertical-align:bottom;width:1px;white-space:nowrap;\">";
194       /* Shorten string if its too long for the tab headers*/
195       $title= _($name);
196       if (mb_strlen($title, 'UTF-8') > 28){
197         $title= mb_substr($title,0, 25, 'UTF-8')."...";
198       }
200       /* nobr causes w3c warnings so we use &nbsp; to keep the tab name in one line */
201       $title= preg_replace("/ /","&nbsp;",$title);
203       /* Take care about notifications */
204       $obj = $this->by_object[$class];
205       if ( $this->by_object[$class]->pl_notify && ($obj->is_account || $obj->ignore_account)){
206         $notify= "id=\"notify\"";
207       } else {
208         $notify= "";
209       }
211       if (session::get('js')==FALSE){   
212         $display.= "<div ".$notify." class=\"$style[$index]\"><input type=\"submit\" name=\"$class\"".
213           " class=\"$style[$index]\" value=\"$title\"";
214       } else {                   
215         $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";
216       }
217       $display.= "></div></td>";
218     }
219     $display.= "<td style=\"vertical-align:bottom;\">\n";
220     $display.= "<div class=\"tab_border\">&nbsp;</div></td></tr></table>";
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->is_account || $ignore_account || $obj->ignore_account){
306         if ($obj->save() == 1){
307           return (1);
308         }
309       } else {
310         $obj->remove_from_parent();
311       }
312     }
313     return (0);
314   }
316   function adapt_from_template($dn, $skip= array())
317   {
318     foreach ($this->by_object as $key => &$obj){
319       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
320           $key, "Adapting");
321       $obj->parent= &$this;
322       $obj->adapt_from_template($dn, $skip);
323     }
324   }
326         
327   /* Save attributes posted by copy & paste dialog
328    */
329   function saveCopyDialog()
330   {
331           foreach ($this->by_object as &$obj){
332                   if($obj->is_account || $obj->ignore_account){
333                           $obj->saveCopyDialog();
334                   }
335           }
336   }
339   /* return copy & paste dialog
340    */
341   function getCopyDialog()
342   {
343     $ret = "";
344     $this->SubDialog = false;
345     foreach ($this->by_object as &$obj){
346       if($obj->is_account || $obj->ignore_account){
347         $tmp = $obj->getCopyDialog();
348         if($tmp['status'] == "SubDialog"){
349           $this->SubDialog = true;
350           return($tmp['string']);
351         }else{
352           if(!empty($tmp['string'])){
353             $ret .= $tmp['string'];
354             $ret .= "<p class='seperator'>&nbsp;</p>";
355           }
356         }
357       }
358     }
359     return($ret);
360   }
363   function addSpecialTabs()
364   {
365     $this->by_name['acl']= _("ACL");
366     $this->by_object['acl']= new acl($this->config, $this, $this->dn);
367     $this->by_object['acl']->parent= &$this;
368     $this->by_name['reference']= _("References");
369     $this->by_object['reference']= new reference($this->config, $this->dn);
370     $this->by_object['reference']->parent= &$this;
371   }
374   function set_acl_base($base= "")
375   {
376     /* Update reference, transfer variables */
377     $first= ($base == "");
378     foreach ($this->by_object as &$obj){
379       if ($first){
380         $first= FALSE;
381         $base= $obj->acl_base;
382       } else {
383         $obj->set_acl_base($base);
384       }
385     }
386   }
388  
389   /*!   \brief    Checks if one of the used tab plugins supports multiple edit.
390         @param    boolean Returns TRUE if at least one plugins supports multiple edit. 
391   */
392   function multiple_support_available()
393   {
394     foreach($this->by_object as $name => $obj){
395       if($obj->multiple_support){
396         return(TRUE);
397       }
398     }
399     return(FALSE);
400   }  
403   /*!   \brief    Enables multiple edit support for the given tab.
404                   All unsupported plugins will be disabled.
405         @param    boolean Returns TRUE if at least one plugin supports multiple edit 
406   */
407   function enable_multiple_support()
408   {
409     if(!$this->multiple_support_available()){
410       return(FALSE);
411     }else{
412       $this->multiple_support_active = TRUE;
413       foreach($this->by_object as $name => $obj){
414         if($obj->multiple_support){
415           $this->by_object[$name]->enable_multiple_support();
416         }else{
417           unset($this->by_object[$name]);
418           unset($this->by_name[$name]);
419         }
420       }
421     }
422     return(TRUE);
423   }
425 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
426 ?>