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 }
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 /* Ensure that the currently selected tab is valid. */
100 if(!isset($this->by_name[$this->current])) $this->current = key($this->by_name);
102 /* Rotate current to last */
103 $this->last= $this->current;
105 /* Look for pressed tab button */
106 foreach ($this->by_object as $class => &$obj){
107 if (isset($_POST[$class]) || (isset($_POST['arg']) && $_POST['arg'] == "$class")){
108 $this->current= $class;
109 break;
110 }
111 }
113 /* Save last tab object */
114 if ($this->last == $this->current){
115 $this->save_object(TRUE);
116 } else {
117 $this->save_object(FALSE);
118 }
120 /* Build tab line */
121 $display= $this->gen_tabs();
123 /* Show object */
124 $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";
125 $display.= "<tr><td>\n";
127 /* If multiple edit is enabled for this tab,
128 we have tho display different templates */
129 if(!$this->multiple_support_active){
130 $display.= $this->by_object[$this->current]->execute();
131 }else{
132 $display.= $this->by_object[$this->current]->multiple_execute();
133 }
135 /* Footer for tabbed dialog */
136 $display.= "</td></tr></table>";
138 return ($display);
139 }
141 function save_object($save_current= FALSE)
142 {
143 /* Save last tab */
144 if ($this->last != ""){
145 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
146 $this->last, "Saving");
148 if(!$this->multiple_support_active){
149 $this->by_object[$this->last]->save_object ();
150 }else{
151 $this->by_object[$this->last]->multiple_save_object();
152 }
153 }
155 /* Skip if curent and last are the same object */
156 if ($this->last == $this->current){
157 return;
158 }
160 $obj= @$this->by_object[$this->current];
161 $this->disabled= $obj->parent->disabled;
163 if ($save_current){
164 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
165 $this->current, "Saving (current)");
167 if(!$this->multiple_support_active){
168 $obj->save_object();
169 }else{
170 $obj->multiple_save_object();
171 }
172 }
173 }
175 function gen_tabs()
176 {
177 $display= "<input type=\"hidden\" name=\"arg\" value=\"\">";
178 $display.= "<table summary=\"\" cellpadding=0 cellspacing=0 border=0 style=\"width:100%;\"><tr>";
179 $index= 0;
180 $style= array("tab_left", "tab_active", "tab_near_active", "tab_right");
181 foreach ($this->by_name as $class => $name){
183 /* Activate right tabs with style "tab_right"
184 Activate near current with style "tab_near_active" */
185 if ($index == 2 || $index == 1){
186 $index++;
187 }
189 /* Activate current tab with style "tab_active " */
190 if ($class == $this->current){
191 $index++;
192 }
194 /* Paint tab */
195 $display.= "<td style=\"vertical-align:bottom;width:1px;white-space:nowrap;\">";
197 /* Shorten string if its too long for the tab headers*/
198 $title= _($name);
199 if (mb_strlen($title, 'UTF-8') > 28){
200 $title= mb_substr($title,0, 25, 'UTF-8')."...";
201 }
203 /* nobr causes w3c warnings so we use to keep the tab name in one line */
204 $title= preg_replace("/ /"," ",$title);
206 /* Take care about notifications */
207 $obj = $this->by_object[$class];
208 if ( $this->by_object[$class]->pl_notify && ($obj->is_account || $obj->ignore_account)){
209 $notify= "id=\"notify\"";
210 } else {
211 $notify= "";
212 }
214 if (session::get('js')==FALSE){
215 $display.= "<div ".$notify." class=\"$style[$index]\"><input type=\"submit\" name=\"$class\"".
216 " class=\"$style[$index]\" value=\"$title\"";
217 } else {
218 $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";
219 }
220 $display.= "></div></td>";
221 }
222 $display.= "<td style=\"vertical-align:bottom;\">\n";
223 $display.= "<div class=\"tab_border\"> </div></td></tr></table>";
225 return($display);
226 }
229 function set_acl($acl)
230 {
231 /* Look for attribute in ACL */
232 trigger_error("Don't use tabs::set_acl() its obsolete.");
233 }
235 function delete()
236 {
237 /* Check if all plugins will ACK for deletion */
238 foreach (array_reverse($this->by_object) as $key => $obj){
239 $reason= $obj->allow_remove();
240 if ($reason != ""){
241 msg_dialog::display(_("Warning"), sprintf(_("Delete process has been canceled by plugin '%s': %s"), $key, $reason), WARNING_DIALOG);
242 return;
243 }
244 }
246 /* Delete for all plugins */
247 foreach (array_reverse($this->by_object) as $obj){
248 $obj->remove_from_parent();
249 }
250 }
252 function password_change_needed()
253 {
254 /* Ask all plugins for needed password changes */
255 foreach ($this->by_object as &$obj){
256 if ($obj->password_change_needed()){
257 return TRUE;
258 }
259 }
261 return FALSE;
262 }
264 function check($ignore_account= FALSE)
265 {
266 $this->save_object(TRUE);
267 $messages= array();
269 $current_set = FALSE;
271 /* Check all plugins */
272 foreach ($this->by_object as $key => &$obj){
273 if ($obj->is_account || $ignore_account || $obj->ignore_account){
274 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$key, "Checking");
276 if(!$this->multiple_support_active){
277 $msg = $obj->check();
278 }else{
279 $msg = $obj->multiple_check();
280 }
282 if (count($msg)){
283 $obj->pl_notify= TRUE;
284 if(!$current_set){
285 $current_set = TRUE;
286 $this->current= $key;
287 $messages = $msg;
288 }
289 }else{
290 $obj->pl_notify= FALSE;
291 }
292 }else{
293 $obj->pl_notify= FALSE;
294 }
295 }
296 return ($messages);
297 }
299 function save($ignore_account= FALSE)
300 {
301 /* Save all plugins */
302 foreach ($this->by_object as $key => &$obj){
303 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
304 $key, "Saving");
306 $obj->dn= $this->dn;
308 if(!$obj instanceof plugin){
309 trigger_error("Something went wrong while saving ".$obj->dn.". Object class '".get_class($obj)."'.");
310 }else{
312 if ($obj->is_account || $ignore_account || $obj->ignore_account){
313 if ($obj->save() == 1){
314 return (1);
315 }
316 } else {
317 $obj->remove_from_parent();
318 }
319 }
320 }
321 return (0);
322 }
324 function adapt_from_template($dn, $skip= array())
325 {
326 foreach ($this->by_object as $key => &$obj){
327 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
328 $key, "Adapting");
329 $obj->parent= &$this;
330 $obj->adapt_from_template($dn, $skip);
331 }
332 }
335 /* Save attributes posted by copy & paste dialog
336 */
337 function saveCopyDialog()
338 {
339 foreach ($this->by_object as &$obj){
340 if($obj->is_account || $obj->ignore_account){
341 $obj->saveCopyDialog();
342 }
343 }
344 }
347 /* return copy & paste dialog
348 */
349 function getCopyDialog()
350 {
351 $ret = "";
352 $this->SubDialog = false;
353 foreach ($this->by_object as &$obj){
354 if($obj->is_account || $obj->ignore_account){
355 $tmp = $obj->getCopyDialog();
356 if($tmp['status'] == "SubDialog"){
357 $this->SubDialog = true;
358 return($tmp['string']);
359 }else{
360 if(!empty($tmp['string'])){
361 $ret .= $tmp['string'];
362 $ret .= "<p class='seperator'> </p>";
363 }
364 }
365 }
366 }
367 return($ret);
368 }
371 function addSpecialTabs()
372 {
373 $this->by_name['acl']= _("ACL");
374 $this->by_object['acl']= new acl($this->config, $this, $this->dn);
375 $this->by_object['acl']->parent= &$this;
376 $this->by_name['reference']= _("References");
377 $this->by_object['reference']= new reference($this->config, $this->dn);
378 $this->by_object['reference']->parent= &$this;
379 }
382 function set_acl_base($base= "")
383 {
384 /* Update reference, transfer variables */
385 $first= ($base == "");
386 foreach ($this->by_object as &$obj){
387 if ($first){
388 $first= FALSE;
389 $base= $obj->acl_base;
390 } else {
391 $obj->set_acl_base($base);
392 }
393 }
394 }
397 /*! \brief Checks if one of the used tab plugins supports multiple edit.
398 @param boolean Returns TRUE if at least one plugins supports multiple edit.
399 */
400 function multiple_support_available()
401 {
402 foreach($this->by_object as $name => $obj){
403 if($obj->multiple_support){
404 return(TRUE);
405 }
406 }
407 return(FALSE);
408 }
411 /*! \brief Enables multiple edit support for the given tab.
412 All unsupported plugins will be disabled.
413 @param boolean Returns TRUE if at least one plugin supports multiple edit
414 */
415 function enable_multiple_support()
416 {
417 if(!$this->multiple_support_available()){
418 return(FALSE);
419 }else{
420 $this->multiple_support_active = TRUE;
421 foreach($this->by_object as $name => $obj){
422 if($obj->multiple_support){
423 $this->by_object[$name]->enable_multiple_support();
424 }else{
425 unset($this->by_object[$name]);
426 unset($this->by_name[$name]);
427 }
428 }
429 }
430 return(TRUE);
431 }
432 }
433 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
434 ?>