Code

3245241a2ae241e11a07ff86d3e5ebb186b64dec
[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 instanceof plugin){
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     $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     $this->by_name['reference']= _("References");
374     $this->by_object['reference']= new reference($this->config, $this->dn);
375     $this->by_object['reference']->parent= &$this;
376   }
379   function set_acl_base($base= "")
380   {
381     /* Update reference, transfer variables */
382     $first= ($base == "");
383     foreach ($this->by_object as &$obj){
384       if ($first){
385         $first= FALSE;
386         $base= $obj->acl_base;
387       } else {
388         $obj->set_acl_base($base);
389       }
390     }
391   }
393  
394   /*!   \brief    Checks if one of the used tab plugins supports multiple edit.
395         @param    boolean Returns TRUE if at least one plugins supports multiple edit. 
396   */
397   function multiple_support_available()
398   {
399     foreach($this->by_object as $name => $obj){
400       if($obj->multiple_support){
401         return(TRUE);
402       }
403     }
404     return(FALSE);
405   }  
408   /*!   \brief    Enables multiple edit support for the given tab.
409                   All unsupported plugins will be disabled.
410         @param    boolean Returns TRUE if at least one plugin supports multiple edit 
411   */
412   function enable_multiple_support()
413   {
414     if(!$this->multiple_support_available()){
415       return(FALSE);
416     }else{
417       $this->multiple_support_active = TRUE;
418       foreach($this->by_object as $name => $obj){
419         if($obj->multiple_support){
420           $this->by_object[$name]->enable_multiple_support();
421         }else{
422           unset($this->by_object[$name]);
423           unset($this->by_name[$name]);
424         }
425       }
426     }
427     return(TRUE);
428   }
430 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
431 ?>