Code

Moved fixes to trunk
[gosa.git] / 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_entries = 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     /* Check specified dn 
47      */
48     if(is_string($dn)){
50     }elseif(is_array($dn)&&count($dn) == 1){
51       $this->dn = $dn[key($dn)];
52     }elseif(is_array($dn)&&count($dn) > 1){
53       $this->multiple_entries = TRUE;
54     }
56     foreach ($data as &$tab){
58       /* Check we want to handle multiple obejcts at once 
59        *  and if this is supported by enabled plugins 
60        */
61       $tmp = get_class_vars($tab['CLASS']);
62       if($this->multiple_entries  && !$tmp['multiple_support']){
63         continue;
64       } 
66       $this->by_name[$tab['CLASS']]= $tab['NAME'];
68       if ($baseobject === NULL){
69         $baseobject= new $tab['CLASS']($this->config, $this->dn);
70         $this->by_object[$tab['CLASS']]= $baseobject;
71       } else {
72         $this->by_object[$tab['CLASS']]= new $tab['CLASS']($this->config, $this->dn, $baseobject);
73       }
75       $this->by_object[$tab['CLASS']]->parent= &$this;
76       $this->by_object[$tab['CLASS']]->set_acl_category($acl_category);
78       /* Initialize current */
79       if ($this->current == ""){
80         $this->current= $tab['CLASS'];
81       }
82     }
84     /* Return false if tabs */
85     if(!count($this->by_object)){
86       return(FALSE);
87     }
88   }
90   function execute()
91   {
92     /* Rotate current to last */
93     $this->last= $this->current;
95     /* Look for pressed tab button */
96     foreach ($this->by_object as $class => &$obj){
97       if (isset($_POST[$class]) || (isset($_POST['arg']) && $_POST['arg'] == "$class")){
98         $this->current= $class;
99         break;
100       }
101     }
103     /* Save last tab object */
104     if ($this->last == $this->current){
105       $this->save_object(TRUE);
106     } else {
107       $this->save_object(FALSE);
108     }
110     /* Build tab line */
111     $display= $this->gen_tabs();
113     /* Show object */
114     $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";
115     $display.= "<tr><td>\n";
117     $display.= $this->by_object[$this->current]->execute();
119     /* Footer for tabbed dialog */
120     $display.= "</td></tr></table>";
122     return ($display);
123   }
125   function save_object($save_current= FALSE)
126   {
127     /* Save last tab */
128     if ($this->last != ""){
129       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
130           $this->last, "Saving");
132       $this->by_object[$this->last]->save_object ();
133     }
135     /* Skip if curent and last are the same object */
136     if ($this->last == $this->current){
137       return;
138     }
140     $obj= @$this->by_object[$this->current];
141     $this->disabled= $obj->parent->disabled;
143     if ($save_current){
144       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
145           $this->current, "Saving (current)");
147       $obj->save_object ();
148     }
149   }
151   function gen_tabs()
152   {
153     $display= "<input type=\"hidden\" name=\"arg\" value=\"\">";
154     $display.= "<table summary=\"\" cellpadding=0 cellspacing=0 border=0 style=\"width:100%;\"><tr>";
155     $index= 0;
156     $style= array("tab_left", "tab_active", "tab_near_active", "tab_right");
157     foreach ($this->by_name as $class => $name){
159       /* Activate right tabs with style "tab_right"
160          Activate near current with style "tab_near_active" */
161       if ($index == 2 || $index == 1){
162         $index++;
163       }
165       /* Activate current tab with style "tab_active " */
166       if ($class == $this->current){
167         $index++;
168       }
170       /* Paint tab */
171       $display.= "<td style=\"vertical-align:bottom;width:1px;white-space:nowrap;\">";
173       /* Shorten string if its too long for the tab headers*/
174       $title= _($name);
175       if (mb_strlen($title, 'UTF-8') > 28){
176         $title= mb_substr($title,0, 25, 'UTF-8')."...";
177       }
179       /* nobr causes w3c warnings so we use &nbsp; to keep the tab name in one line */
180       $title= preg_replace("/ /","&nbsp;",$title);
182       /* Take care about notifications */
183       if ($this->by_object[$class]->pl_notify){
184         $notify= "id=\"notify\"";
185       } else {
186         $notify= "";
187       }
189       if ($_SESSION['js']==FALSE){      
190         $display.= "<div ".$notify." class=\"$style[$index]\"><input type=\"submit\" name=\"$class\"".
191           " class=\"$style[$index]\" value=\"$title\"";
192       } else {                   
193         $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";
194       }
195       $display.= "></div></td>";
196     }
197     $display.= "<td style=\"vertical-align:bottom;\">\n";
198     $display.= "<div class=\"tab_border\">&nbsp;</div></td></tr></table>";
200     return($display);
201   }
204   function set_acl($acl)
205   {
206         /* Look for attribute in ACL */
207           trigger_error("Don't use tabs::set_acl() its obsolete.");
208   }
210   function delete()
211   {
212     /* Check if all plugins will ACK for deletion */
213     foreach (array_reverse($this->by_object) as $key => $obj){
214       $reason= $obj->allow_remove();
215       if ($reason != ""){
216         print_red(sprintf(_("Delete process has been canceled by plugin '%s': %s"), $key, $reason));
217         return;
218       }
219     }
221     /* Delete for all plugins */
222     foreach (array_reverse($this->by_object) as $obj){
223       $obj->remove_from_parent();
224     }
225   }
227   function password_change_needed()
228   {
229     /* Ask all plugins for needed password changes */
230     foreach ($this->by_object as &$obj){
231       if ($obj->password_change_needed()){
232         return TRUE;
233       }
234     }
236     return FALSE;
237   }
239   function check($ignore_account= FALSE)
240   {
241     $this->save_object(TRUE);
242     $messages= array();
244     $current_set = FALSE;
246     /* Check all plugins */
247     foreach ($this->by_object as $key => &$obj){
248       if ($obj->is_account || $ignore_account || $obj->ignore_account){
249         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$key, "Checking");
251         $msg = $obj->check();
253         if (count($msg)){
254           $obj->pl_notify= TRUE;
255           if(!$current_set){
256             $current_set = TRUE;
257             $this->current= $key;
258             $messages = $msg;
259           }
260         }else{
261           $obj->pl_notify= FALSE;
262         }
263       }else{
264         $obj->pl_notify= FALSE;
265       }
266     }
267     return ($messages);
268   }
270   function save($ignore_account= FALSE)
271   {
272     if($this->multiple_entries){
273       echo "Save is not supported for multiple entries yet";
275     }else{
276       /* Save all plugins */
277       foreach ($this->by_object as $key => &$obj){
278         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
279             $key, "Saving");
281         $obj->dn= $this->dn;
283         if ($obj->is_account || $ignore_account || $obj->ignore_account){
284           if ($obj->save() == 1){
285             return (1);
286           }
287         } else {
288           $obj->remove_from_parent();
289         }
290       }
291     }
292     return (0);
293   }
295   function adapt_from_template($dn)
296   {
297     foreach ($this->by_object as $key => &$obj){
298       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
299           $key, "Adapting");
300       $obj->parent= &$this;
301       $obj->adapt_from_template($dn);
302     }
303   }
305         
306   /* Save attributes posted by copy & paste dialog
307    */
308   function saveCopyDialog()
309   {
310           foreach ($this->by_object as &$obj){
311                   if($obj->is_account){
312                           $obj->saveCopyDialog();
313                   }
314           }
315   }
318   /* return copy & paste dialog
319    */
320   function getCopyDialog()
321   {
322     $ret = "";
323     $this->SubDialog = false;
324     foreach ($this->by_object as &$obj){
325       if($obj->is_account){
326         $tmp = $obj->getCopyDialog();
327         if($tmp['status'] == "SubDialog"){
328           $this->SubDialog = true;
329           return($tmp['string']);
330         }else{
331           if(!empty($tmp['string'])){
332             $ret .= $tmp['string'];
333             $ret .= "<p class='seperator'>&nbsp;</p>";
334           }
335         }
336       }
337     }
338     return($ret);
339   }
342   function addSpecialTabs()
343   {
344     if(!$this->multiple_entries){
345       $this->by_name['acl']= _("ACL");
346       $this->by_object['acl']= new acl($this->config, $this, $this->dn);
347       $this->by_object['acl']->parent= &$this;
348       $this->by_name['reference']= _("References");
349       $this->by_object['reference']= new reference($this->config, $this->dn);
350       $this->by_object['reference']->parent= &$this;
351     }
352   }
355   function set_acl_base($base= "")
356   {
357     /* Update reference, transfer variables */
358     $first= ($base == "");
359     foreach ($this->by_object as &$obj){
360       if ($first){
361         $first= FALSE;
362         $base= $obj->acl_base;
363       } else {
364         $obj->set_acl_base($base);
365       }
366     }
367   }
370 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
371 ?>