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