Code

fixed property
[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 $parent = null; // A parent object if available, e.g. a management class.
42   var $read_only = FALSE; // Used when the entry is opened as "readonly" due to locks.
43   var $hide_refs = FALSE;
44   var $hide_acls = FALSE;
45   
46   function tabs(&$config, $data, $dn, $acl_category= "", $hide_refs = FALSE, $hide_acls = FALSE)
47   {
48     /* Save dn */
49     $this->dn= $dn;
50     $this->config= &$config;
51     $this->hide_refs = $hide_refs;
52     $this->hide_acls = $hide_acls;
54     if(!count($data)) {
55       $data[] = array("CLASS" => 'plugin',"NAME" => 'Error');
56       msg_dialog::display(_("Error"),
57         sprintf(_("No plugin definition for %s found: please check the configuration file!"), bold(get_class($this))),
58         "ERROR_DIALOG");
59     }
61     $baseobject= NULL;
62     $this->acl_category = $acl_category;
63     foreach ($data as &$tab){
65       if (!plugin_available($tab['CLASS'])){
66         trigger_error(sprintf("Unknown class %s!", bold($tab['CLASS'])));
67         continue;
68       }
69       if ($this->current == "")  $this->current= $tab['CLASS'];
71       $this->by_name[$tab['CLASS']]= $tab['NAME'];
73       if ($baseobject === NULL){
74         $baseobject= new $tab['CLASS']($this->config, $this->dn);
75         $baseobject->enable_CSN_check();
76         $this->by_object[$tab['CLASS']]= $baseobject;
77       } else {
78         $this->by_object[$tab['CLASS']]= new $tab['CLASS']($this->config, $this->dn, $baseobject);
79       }
81       $this->read_only |= $this->by_object[$tab['CLASS']]->read_only;
82       $this->by_object[$tab['CLASS']]->parent= &$this;
83       $this->by_object[$tab['CLASS']]->set_acl_category($this->acl_category);
84     }
85   }
87  
88   /*! \brief Reinitializes the tab classes with fresh ldap values.
89              This maybe usefull if for example the apply button was pressed.
90    */ 
91   function re_init()
92   {
93     $baseobject= NULL;
94     foreach($this->by_object as $name => $object){
95       $class = get_class($object);
96       if(in_array($class,array("reference","acl"))) continue;
97       if ($baseobject === NULL){
98         $baseobject= new $class($this->config, $this->dn);
99         $baseobject->enable_CSN_check();
100         $this->by_object[$name]= $baseobject;
101       } else {
102         $this->by_object[$name]= new $class($this->config, $this->dn, $baseobject);
103       }
104       $this->by_object[$name]->parent= &$this;
105       $this->by_object[$name]->set_acl_category($this->acl_category);
106     }
107   }
110   function execute()
111   {
112     // Ensure that the currently selected tab is valid.
113     if(!isset($this->by_name[$this->current])) {
114       $this->current = key($this->by_name);
115     }
117     pathNavigator::registerPlugin($this);
119     // Rotate current to last 
120     $this->last= $this->current;
122     // Look for pressed tab button
123     foreach ($this->by_object as $class => &$obj){
124       if (isset($_POST[$class]) || (isset($_POST['arg']) && $_POST['arg'] == "$class")){
125         $this->current= $class;
126         break;
127       }
128     }
130     // Save last tab object 
131     if ($this->last == $this->current){
132       $this->save_object(TRUE);
133     } else {
134       $this->save_object(FALSE);
135     }
137     /* If multiple edit is enabled for this tab, 
138        we have tho display different templates */
139     if(!$this->multiple_support_active){
140       $display= $this->by_object[$this->current]->execute();
141     }else{
142       $display= $this->by_object[$this->current]->multiple_execute();
143     }
144     $tabs= $this->gen_tabs();
146     if($this->is_modal_dialog()){
147         $display =   
148             "\n        <div class='plugin'>".
149             "\n          {$display}".
150             "\n        </div>";
151     }else{
153         $display =   
154             "\n        {$tabs}".
155             "\n        <input type='hidden' name='arg' value=''>".
156             "\n        <div class='tab-content'>".
157             "\n          {$display}".
158             "\n        </div>";
159     }
160     return ($display);
161   }
163   function save_object($save_current= FALSE)
164   {
165     /* Save last tab */
166     if ($this->last != ""){
167       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
168           $this->last, "Saving");
170       if(!$this->multiple_support_active){
171         $this->by_object[$this->last]->save_object ();
172       }else{
173         $this->by_object[$this->last]->multiple_save_object();
174       }
175     }
177     /* Skip if curent and last are the same object */
178     if ($this->last == $this->current){
179       return;
180     }
182     $obj= @$this->by_object[$this->current];
183     $this->disabled= $obj->parent->disabled;
185     if ($save_current){
186       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
187           $this->current, "Saving (current)");
189       if(!$this->multiple_support_active){
190         $obj->save_object();
191       }else{
192         $obj->multiple_save_object();
193       }
194     }
195   }
198   function is_modal_dialog()
199   {
200     return($this->by_object[$this->current]->is_modal_dialog());
201   }
203   function gen_tabs()
204   {
205     if($this->is_modal_dialog()) return("");
207     $display = "\n  <div class='tabs'>";
208     $display.= "\n    <ul>";
210     foreach ($this->by_name as $class => $name){
212       // Shorten string if its too long for the tab headers
213       $title= _($name);
214       if (mb_strlen($title, 'UTF-8') > 28){
215         $title= mb_substr($title,0, 25, 'UTF-8')."...";
216       }
218       // nobr causes w3c warnings so we use &nbsp; to keep the tab name in one line 
219       $title= str_replace(" ","&nbsp;",$title);
221       // Take care about notifications 
222       $obj = $this->by_object[$class];
223       $tabClass = ($this->current == $class) ? "current" :"";
224       if ( $this->by_object[$class]->pl_notify && ($obj->is_account || $obj->ignore_account)){
225         $tabClass .= " info";
226       }
227       if(!empty($tabClass)) $tabClass="class='{$tabClass}'";
228       $onClick = "document.mainform.arg.value='{$class}'; document.mainform.submit();";
229       $display.= "\n      <li {$tabClass} onClick=\"{$onClick}\">{$title}</li>";
230     }
231     $display.="\n    </ul>";
232     $display.="\n  </div>";
233     return($display);
234   }
237   function set_acl($acl)
238   {
239         /* Look for attribute in ACL */
240           trigger_error("Don't use tabs::set_acl() its obsolete.");
241   }
243   function delete()
244   {
245     /* Check if all plugins will ACK for deletion */
246     foreach (array_reverse($this->by_object) as $key => $obj){
247       $reason= $obj->allow_remove();
248       if ($reason != ""){
249         msg_dialog::display(_("Warning"), sprintf(_("Delete process has been canceled by plugin %s: %s"), bold($key), $reason), WARNING_DIALOG);
250         return;
251       }
252     }
254     /* Delete for all plugins */
255     foreach (array_reverse($this->by_object) as $obj){
256       $obj->remove_from_parent();
257     }
258   }
260   function password_change_needed()
261   {
262     /* Ask all plugins for needed password changes */
263     foreach ($this->by_object as &$obj){
264       if ($obj->password_change_needed()){
265         return TRUE;
266       }
267     }
269     return FALSE;
270   }
272   function check($ignore_account= FALSE)
273   {
274     $this->save_object(TRUE);
275     $messages= array();
277     $current_set = FALSE;
279     /* Check all plugins */
280     foreach ($this->by_object as $key => &$obj){
281       if ($obj->is_account || $ignore_account || $obj->ignore_account){
282         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$key, "Checking");
284         if(!$this->multiple_support_active){
285           $msg = $obj->check();
286         }else{
287           $msg = $obj->multiple_check();
288         }
290         if (count($msg)){
291           $obj->pl_notify= TRUE;
292           if(!$current_set){
293             $current_set = TRUE;
294             $this->current= $key;
295             $messages = $msg;
296           }
297         }else{
298           $obj->pl_notify= FALSE;
299         }
300       }else{
301         $obj->pl_notify= FALSE;
302       }
303     }
304     return ($messages);
305   }
307   function save($ignore_account= FALSE)
308   {
309     /* Save all plugins */
310     foreach ($this->by_object as $key => &$obj){
311       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
312           $key, "Saving");
314       $obj->dn= $this->dn;
316       if(!$obj instanceof plugin && !$obj instanceOf management){
317         trigger_error("Something went wrong while saving ".$obj->dn.". Object class '".get_class($obj)."'.");
318       }else{
320         if ($obj->is_account || $ignore_account || $obj->ignore_account){
321           if ($obj->save() == 1){
322             return (1);
323           }
324         } else {
325           $obj->remove_from_parent();
326         }
327       }
328     }
329     return (0);
330   }
332   function adapt_from_template($dn, $skip= array())
333   {
334     foreach ($this->by_object as $key => &$obj){
335       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
336           $key, "Adapting");
337       $obj->parent= &$this;
338       $obj->adapt_from_template($dn, $skip);
339     }
340   }
342         
343   /* Save attributes posted by copy & paste dialog
344    */
345   function saveCopyDialog()
346   {
347           foreach ($this->by_object as &$obj){
348                   if($obj->is_account || $obj->ignore_account){
349                           $obj->saveCopyDialog();
350                   }
351           }
352   }
355   /* return copy & paste dialog
356    */
357   function getCopyDialog()
358   {
359     $ret = "";
360     $this->SubDialog = false;
361     foreach ($this->by_object as &$obj){
362       if($obj->is_account || $obj->ignore_account){
363         $tmp = $obj->getCopyDialog();
364         if($tmp['status'] == "SubDialog"){
365           $this->SubDialog = true;
366           return($tmp['string']);
367         }else{
368           if(!empty($tmp['string'])){
369             $ret .= $tmp['string'];
370             $ret .= "<hr>";
371           }
372         }
373       }
374     }
375     return($ret);
376   }
379   function addSpecialTabs()
380   {
381     if(!$this->hide_acls){
382       $this->by_name['acl']= _("ACL");
383       $this->by_object['acl']= new acl($this->config, $this, $this->dn);
384       $this->by_object['acl']->parent= &$this;
385     }
386     if(!$this->hide_refs){
387       $this->by_name['reference']= _("References");
388       $this->by_object['reference']= new reference($this->config, $this->dn);
389       $this->by_object['reference']->parent= &$this;
390       $this->by_object['reference']->set_acl_category($this->acl_category);
391     }
392   }
395   function set_acl_base($base= "")
396   {
397     /* Update reference, transfer variables */
398     $first= ($base == "");
399     foreach ($this->by_object as &$obj){
400       if ($first){
401         $first= FALSE;
402         $base= $obj->acl_base;
403       } else {
404         $obj->set_acl_base($base);
405       }
406     }
407   }
409  
410   /*!   \brief    Checks if one of the used tab plugins supports multiple edit.
411         @param    boolean Returns TRUE if at least one plugins supports multiple edit. 
412   */
413   function multiple_support_available()
414   {
415     foreach($this->by_object as $name => $obj){
416       if($obj->multiple_support){
417         return(TRUE);
418       }
419     }
420     return(FALSE);
421   }  
424   /*!   \brief    Enables multiple edit support for the given tab.
425                   All unsupported plugins will be disabled.
426         @param    boolean Returns TRUE if at least one plugin supports multiple edit 
427   */
428   function enable_multiple_support()
429   {
430     if(!$this->multiple_support_available()){
431       return(FALSE);
432     }else{
433       $this->multiple_support_active = TRUE;
434       foreach($this->by_object as $name => $obj){
435         if($obj->multiple_support){
436           $this->by_object[$name]->enable_multiple_support();
437         }else{
438           unset($this->by_object[$name]);
439           unset($this->by_name[$name]);
440         }
441       }
442     }
443     return(TRUE);
444   }
446   function setReadOnly($s = TRUE)
447   {
448     foreach($this->by_object as $name => $obj){
449       $this->by_object[$name]->read_only = $s;
450     }
451     $this->read_only = $s;
452   }
454 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
455 ?>