Code

f277f815bd7c0d5455838d15bfdee31ced4c82de
[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 $read_only = FALSE; // Used when the entry is opened as "readonly" due to locks.
41   var $hide_refs = FALSE;
42   var $hide_acls = FALSE;
43   
44   function tabs(&$config, $data, $dn, $acl_category= "", $hide_refs = FALSE, $hide_acls = FALSE)
45   {
46     /* Save dn */
47     $this->dn= $dn;
48     $this->config= &$config;
49     $this->hide_refs = $hide_refs;
50     $this->hide_acls = $hide_acls;
52     if(!count($data)) {
53       $data[] = array("CLASS" => 'plugin',"NAME" => 'Error');
54       msg_dialog::display(_("Error"),
55         sprintf(_("No plugin definitions found to initialize '%s', please check your configuration file."),get_class($this)),
56         "ERROR_DIALOG");
57     }
59     $baseobject= NULL;
60     $this->acl_category = $acl_category;
61     foreach ($data as &$tab){
63       if (!plugin_available($tab['CLASS'])){
64         continue;
65       }
67       $this->by_name[$tab['CLASS']]= $tab['NAME'];
69       if ($baseobject === NULL){
70         $baseobject= new $tab['CLASS']($this->config, $this->dn);
71         $baseobject->enable_CSN_check();
72         $this->by_object[$tab['CLASS']]= $baseobject;
73       } else {
74         $this->by_object[$tab['CLASS']]= new $tab['CLASS']($this->config, $this->dn, $baseobject);
75       }
77       $this->read_only |= $this->by_object[$tab['CLASS']]->read_only;
78       $this->by_object[$tab['CLASS']]->parent= &$this;
79       $this->by_object[$tab['CLASS']]->set_acl_category($this->acl_category);
81       /* Initialize current */
82       if ($this->current == ""){
83         $this->current= $tab['CLASS'];
84       }
85     }
86   }
88  
89   /*! \brief Reinitializes the tab classes with fresh ldap values.
90              This maybe usefull if for example the apply button was pressed.
91    */ 
92   function re_init()
93   {
94     $baseobject= NULL;
95     foreach($this->by_object as $name => $object){
96       $class = get_class($object);
97       if(in_array($class,array("reference","acl"))) continue;
98       if ($baseobject === NULL){
99         $baseobject= new $class($this->config, $this->dn);
100         $baseobject->enable_CSN_check();
101         $this->by_object[$name]= $baseobject;
102       } else {
103         $this->by_object[$name]= new $class($this->config, $this->dn, $baseobject);
104       }
105       $this->by_object[$name]->parent= &$this;
106       $this->by_object[$name]->set_acl_category($this->acl_category);
107     }
108   }
111   function execute()
112   {
113     /* Ensure that the currently selected tab is valid. */
114     if(!isset($this->by_name[$this->current])) $this->current = key($this->by_name);
116     /* Rotate current to last */
117     $this->last= $this->current;
119     /* Look for pressed tab button */
120     foreach ($this->by_object as $class => &$obj){
121       if (isset($_POST[$class]) || (isset($_POST['arg']) && $_POST['arg'] == "$class")){
122         $this->current= $class;
123         break;
124       }
125     }
127     /* Save last tab object */
128     if ($this->last == $this->current){
129       $this->save_object(TRUE);
130     } else {
131       $this->save_object(FALSE);
132     }
134     /* Show object */
135     $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";
136     $display.= "<tr><td>\n";
138     /* If multiple edit is enabled for this tab, 
139        we have tho display different templates */
140     if(!$this->multiple_support_active){
141       $display.= $this->by_object[$this->current]->execute();
142     }else{
143       $display.= $this->by_object[$this->current]->multiple_execute();
144     }
145     $modal_dialog = $this->by_object[$this->current]->is_modal_dialog();
146     
147     /* Build tab line */
148     $modal = TRUE;
149     $tabs= $this->gen_tabs($modal_dialog);
151     /* Footer for tabbed dialog */
152     $display = $tabs.$display."</td></tr></table>";
154     return ($display);
155   }
157   function save_object($save_current= FALSE)
158   {
159     /* Save last tab */
160     if ($this->last != ""){
161       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
162           $this->last, "Saving");
164       if(!$this->multiple_support_active){
165         $this->by_object[$this->last]->save_object ();
166       }else{
167         $this->by_object[$this->last]->multiple_save_object();
168       }
169     }
171     /* Skip if curent and last are the same object */
172     if ($this->last == $this->current){
173       return;
174     }
176     $obj= @$this->by_object[$this->current];
177     $this->disabled= $obj->parent->disabled;
179     if ($save_current){
180       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
181           $this->current, "Saving (current)");
183       if(!$this->multiple_support_active){
184         $obj->save_object();
185       }else{
186         $obj->multiple_save_object();
187       }
188     }
189   }
191   function gen_tabs($disabled = FALSE)
192   {
193     $display= "<input type=\"hidden\" name=\"arg\" value=\"\">";
194     $display.= "<table summary=\"\" cellpadding=0 cellspacing=0 border=0 style=\"width:100%;\"><tr>";
195     $index= 0;
196     $style= array("tab_left", "tab_active", "tab_near_active", "tab_right");
197     foreach ($this->by_name as $class => $name){
199       /* Activate right tabs with style "tab_right"
200          Activate near current with style "tab_near_active" */
201       if ($index == 2 || $index == 1){
202         $index++;
203       }
205       /* Activate current tab with style "tab_active " */
206       if ($class == $this->current){
207         $index++;
208       }
210       /* Paint tab */
211       $display.= "<td style=\"vertical-align:bottom;width:1px;white-space:nowrap;\">";
213       /* Shorten string if its too long for the tab headers*/
214       $title= _($name);
215       if (mb_strlen($title, 'UTF-8') > 28){
216         $title= mb_substr($title,0, 25, 'UTF-8')."...";
217       }
219       /* nobr causes w3c warnings so we use &nbsp; to keep the tab name in one line */
220       $title= str_replace(" ","&nbsp;",$title);
222       /* Take care about notifications */
223       $obj = $this->by_object[$class];
224       if ( $this->by_object[$class]->pl_notify && ($obj->is_account || $obj->ignore_account)){
225         $notify= "id=\"notify\"";
226       } else {
227         $notify= "";
228       }
230       if($disabled){
231         $display.= "<div ".$notify." class=\"$style[$index]\" 
232           style=' font-family:arial,helvetica,sans-serif;
233                   font-weight:bold;
234                   font-size:13px; 
235                   color: gray;'
236           title=\"$title\">".$title."</div>";
237       }elseif (session::global_get('js')==FALSE){       
238         $display.= "<div ".$notify." class=\"$style[$index]\"><input type=\"submit\" name=\"$class\"".
239           " class=\"$style[$index]\" value=\"$title\"></div></td>";
240       } else {                   
241         $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></div></td>";
242       }
243     }
244     $display.= "<td style=\"vertical-align:bottom;\">\n";
245     $display.= "<div class=\"tab_border\">&nbsp;</div></td></tr></table>";
247     return($display);
248   }
251   function set_acl($acl)
252   {
253         /* Look for attribute in ACL */
254           trigger_error("Don't use tabs::set_acl() its obsolete.");
255   }
257   function delete()
258   {
259     /* Check if all plugins will ACK for deletion */
260     foreach (array_reverse($this->by_object) as $key => $obj){
261       $reason= $obj->allow_remove();
262       if ($reason != ""){
263         msg_dialog::display(_("Warning"), sprintf(_("Delete process has been canceled by plugin '%s': %s"), $key, $reason), WARNING_DIALOG);
264         return;
265       }
266     }
268     /* Delete for all plugins */
269     foreach (array_reverse($this->by_object) as $obj){
270       $obj->remove_from_parent();
271     }
272   }
274   function password_change_needed()
275   {
276     /* Ask all plugins for needed password changes */
277     foreach ($this->by_object as &$obj){
278       if ($obj->password_change_needed()){
279         return TRUE;
280       }
281     }
283     return FALSE;
284   }
286   function check($ignore_account= FALSE)
287   {
288     $this->save_object(TRUE);
289     $messages= array();
291     $current_set = FALSE;
293     /* Check all plugins */
294     foreach ($this->by_object as $key => &$obj){
295       if ($obj->is_account || $ignore_account || $obj->ignore_account){
296         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$key, "Checking");
298         if(!$this->multiple_support_active){
299           $msg = $obj->check();
300         }else{
301           $msg = $obj->multiple_check();
302         }
304         if (count($msg)){
305           $obj->pl_notify= TRUE;
306           if(!$current_set){
307             $current_set = TRUE;
308             $this->current= $key;
309             $messages = $msg;
310           }
311         }else{
312           $obj->pl_notify= FALSE;
313         }
314       }else{
315         $obj->pl_notify= FALSE;
316       }
317     }
318     return ($messages);
319   }
321   function save($ignore_account= FALSE)
322   {
323     /* Save all plugins */
324     foreach ($this->by_object as $key => &$obj){
325       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
326           $key, "Saving");
328       $obj->dn= $this->dn;
330       if(!$obj instanceof plugin){
331         trigger_error("Something went wrong while saving ".$obj->dn.". Object class '".get_class($obj)."'.");
332       }else{
334         if ($obj->is_account || $ignore_account || $obj->ignore_account){
335           if ($obj->save() == 1){
336             return (1);
337           }
338         } else {
339           $obj->remove_from_parent();
340         }
341       }
342     }
343     return (0);
344   }
346   function adapt_from_template($dn, $skip= array())
347   {
348     foreach ($this->by_object as $key => &$obj){
349       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
350           $key, "Adapting");
351       $obj->parent= &$this;
352       $obj->adapt_from_template($dn, $skip);
353     }
354   }
356         
357   /* Save attributes posted by copy & paste dialog
358    */
359   function saveCopyDialog()
360   {
361           foreach ($this->by_object as &$obj){
362                   if($obj->is_account || $obj->ignore_account){
363                           $obj->saveCopyDialog();
364                   }
365           }
366   }
369   /* return copy & paste dialog
370    */
371   function getCopyDialog()
372   {
373     $ret = "";
374     $this->SubDialog = false;
375     foreach ($this->by_object as &$obj){
376       if($obj->is_account || $obj->ignore_account){
377         $tmp = $obj->getCopyDialog();
378         if($tmp['status'] == "SubDialog"){
379           $this->SubDialog = true;
380           return($tmp['string']);
381         }else{
382           if(!empty($tmp['string'])){
383             $ret .= $tmp['string'];
384             $ret .= "<p class='seperator'>&nbsp;</p>";
385           }
386         }
387       }
388     }
389     return($ret);
390   }
393   function addSpecialTabs()
394   {
395     if(!$this->hide_acls){
396       $this->by_name['acl']= _("ACL");
397       $this->by_object['acl']= new acl($this->config, $this, $this->dn);
398       $this->by_object['acl']->parent= &$this;
399     }
400     if(!$this->hide_refs){
401       $this->by_name['reference']= _("References");
402       $this->by_object['reference']= new reference($this->config, $this->dn);
403       $this->by_object['reference']->parent= &$this;
404     }
405   }
408   function set_acl_base($base= "")
409   {
410     /* Update reference, transfer variables */
411     $first= ($base == "");
412     foreach ($this->by_object as &$obj){
413       if ($first){
414         $first= FALSE;
415         $base= $obj->acl_base;
416       } else {
417         $obj->set_acl_base($base);
418       }
419     }
420   }
422  
423   /*!   \brief    Checks if one of the used tab plugins supports multiple edit.
424         @param    boolean Returns TRUE if at least one plugins supports multiple edit. 
425   */
426   function multiple_support_available()
427   {
428     foreach($this->by_object as $name => $obj){
429       if($obj->multiple_support){
430         return(TRUE);
431       }
432     }
433     return(FALSE);
434   }  
437   /*!   \brief    Enables multiple edit support for the given tab.
438                   All unsupported plugins will be disabled.
439         @param    boolean Returns TRUE if at least one plugin supports multiple edit 
440   */
441   function enable_multiple_support()
442   {
443     if(!$this->multiple_support_available()){
444       return(FALSE);
445     }else{
446       $this->multiple_support_active = TRUE;
447       foreach($this->by_object as $name => $obj){
448         if($obj->multiple_support){
449           $this->by_object[$name]->enable_multiple_support();
450         }else{
451           unset($this->by_object[$name]);
452           unset($this->by_name[$name]);
453         }
454       }
455     }
456     return(TRUE);
457   }
459 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
460 ?>