Code

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