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;
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 }
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 }
161 // Detect if we've modifications right now.
162 // - A plugin state has to be changed, this is not a goo solution, but
163 // currently it does what we want.
164 // (It would be better to ask the plugins if something has changed)
165 $this->isPluginModified |= isset($_POST['modify_state']);
167 // - Capture the plugins modification status.
168 foreach ($this->by_name as $class => $name){
169 $this->isPluginModified |= (isset($this->by_object[$class]->is_modified)) && $this->by_object[$class]->is_modified;
170 }
171 $display="<input type='hidden' id='pluginModified' name='pluginModified' value='{$this->isPluginModified}'>".$display;
173 return ($display);
174 }
176 function save_object($save_current= FALSE)
177 {
178 /* Save last tab */
179 if ($this->last != ""){
180 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
181 $this->last, "Saving");
183 if(!$this->multiple_support_active){
184 $this->by_object[$this->last]->save_object ();
185 }else{
186 $this->by_object[$this->last]->multiple_save_object();
187 }
188 }
190 /* Skip if curent and last are the same object */
191 if ($this->last == $this->current){
192 return;
193 }
195 $obj= @$this->by_object[$this->current];
196 $this->disabled= $obj->parent->disabled;
198 if ($save_current){
199 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
200 $this->current, "Saving (current)");
202 if(!$this->multiple_support_active){
203 $obj->save_object();
204 }else{
205 $obj->multiple_save_object();
206 }
207 }
208 }
211 function is_modal_dialog()
212 {
213 return($this->by_object[$this->current]->is_modal_dialog());
214 }
216 function gen_tabs()
217 {
218 if($this->is_modal_dialog()) return("");
220 $display = "\n <div class='tabs'>";
221 $display.= "\n <ul>";
223 foreach ($this->by_name as $class => $name){
225 // Shorten string if its too long for the tab headers
226 $title= _($name);
227 if (mb_strlen($title, 'UTF-8') > 28){
228 $title= mb_substr($title,0, 25, 'UTF-8')."...";
229 }
231 // nobr causes w3c warnings so we use to keep the tab name in one line
232 $title= str_replace(" "," ",$title);
234 // Take care about notifications
235 $obj = $this->by_object[$class];
236 $tabClass = ($this->current == $class) ? "current" :"";
237 if ( $this->by_object[$class]->pl_notify && ($obj->is_account || $obj->ignore_account)){
238 $tabClass .= " info";
239 }
240 if(!empty($tabClass)) $tabClass="class='{$tabClass}'";
241 $onClick = "document.mainform.arg.value='{$class}'; document.mainform.submit();";
242 $display.= "\n <li {$tabClass} onClick=\"{$onClick}\">{$title}</li>";
243 }
244 $display.="\n </ul>";
245 $display.="\n </div>";
246 return($display);
247 }
250 function set_acl($acl)
251 {
252 /* Look for attribute in ACL */
253 trigger_error("Don't use tabs::set_acl() its obsolete.");
254 }
256 function delete()
257 {
258 /* Check if all plugins will ACK for deletion */
259 foreach (array_reverse($this->by_object) as $key => $obj){
260 $reason= $obj->allow_remove();
261 if ($reason != ""){
262 msg_dialog::display(_("Warning"), sprintf(_("Delete process has been canceled by plugin %s: %s"), bold($key), $reason), WARNING_DIALOG);
263 return;
264 }
265 }
267 /* Delete for all plugins */
268 foreach (array_reverse($this->by_object) as $obj){
269 $obj->remove_from_parent();
270 }
271 }
273 function password_change_needed()
274 {
275 /* Ask all plugins for needed password changes */
276 foreach ($this->by_object as &$obj){
277 if ($obj->password_change_needed()){
278 return TRUE;
279 }
280 }
282 return FALSE;
283 }
285 function check($ignore_account= FALSE)
286 {
287 $this->save_object(TRUE);
288 $messages= array();
290 $current_set = FALSE;
292 /* Check all plugins */
293 foreach ($this->by_object as $key => &$obj){
294 if ($obj->is_account || $ignore_account || $obj->ignore_account){
295 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$key, "Checking");
297 if(!$this->multiple_support_active){
298 $msg = $obj->check();
299 }else{
300 $msg = $obj->multiple_check();
301 }
303 if (count($msg)){
304 $obj->pl_notify= TRUE;
305 if(!$current_set){
306 $current_set = TRUE;
307 $this->current= $key;
308 $messages = $msg;
309 }
310 }else{
311 $obj->pl_notify= FALSE;
312 }
313 }else{
314 $obj->pl_notify= FALSE;
315 }
316 }
317 return ($messages);
318 }
320 function save($ignore_account= FALSE)
321 {
322 /* Save all plugins */
323 foreach ($this->by_object as $key => &$obj){
324 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
325 $key, "Saving");
327 $obj->dn= $this->dn;
329 if(!$obj instanceof plugin && !$obj instanceOf management){
330 trigger_error("Something went wrong while saving ".$obj->dn.". Object class '".get_class($obj)."'.");
331 }else{
333 if ($obj->is_account || $ignore_account || $obj->ignore_account){
334 if ($obj->save() == 1){
335 return (1);
336 }
337 } else {
338 $obj->remove_from_parent();
339 }
340 }
341 }
342 return (0);
343 }
345 function adapt_from_template($dn, $skip= array())
346 {
347 foreach ($this->by_object as $key => &$obj){
348 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
349 $key, "Adapting");
350 $obj->parent= &$this;
351 $obj->adapt_from_template($dn, $skip);
352 }
353 }
356 /* Save attributes posted by copy & paste dialog
357 */
358 function saveCopyDialog()
359 {
360 foreach ($this->by_object as &$obj){
361 if($obj->is_account || $obj->ignore_account){
362 $obj->saveCopyDialog();
363 }
364 }
365 }
368 /* return copy & paste dialog
369 */
370 function getCopyDialog()
371 {
372 $ret = "";
373 $this->SubDialog = false;
374 foreach ($this->by_object as &$obj){
375 if($obj->is_account || $obj->ignore_account){
376 $tmp = $obj->getCopyDialog();
377 if($tmp['status'] == "SubDialog"){
378 $this->SubDialog = true;
379 return($tmp['string']);
380 }else{
381 if(!empty($tmp['string'])){
382 $ret .= $tmp['string'];
383 $ret .= "<hr>";
384 }
385 }
386 }
387 }
388 return($ret);
389 }
392 function addSpecialTabs()
393 {
394 if(!$this->hide_acls){
395 $this->by_name['acl']= _("ACL");
396 $this->by_object['acl']= new acl($this->config, $this, $this->dn);
397 $this->by_object['acl']->parent= &$this;
398 }
399 if(!$this->hide_refs){
400 $this->by_name['reference']= _("References");
401 $this->by_object['reference']= new reference($this->config, $this->dn);
402 $this->by_object['reference']->parent= &$this;
403 $this->by_object['reference']->set_acl_category($this->acl_category);
404 }
405 }
408 function set_acl_base($base= "")
409 {
410 /* Update reference, transfer variables */
411 $first= ($base == "");
412 foreach ($this->by_object as &$obj){
413 if ($first){
414 $first= FALSE;
415 $base= $obj->acl_base;
416 } else {
417 $obj->set_acl_base($base);
418 }
419 }
420 }
423 /*! \brief Checks if one of the used tab plugins supports multiple edit.
424 @param boolean Returns TRUE if at least one plugins supports multiple edit.
425 */
426 function multiple_support_available()
427 {
428 foreach($this->by_object as $name => $obj){
429 if($obj->multiple_support){
430 return(TRUE);
431 }
432 }
433 return(FALSE);
434 }
437 /*! \brief Enables multiple edit support for the given tab.
438 All unsupported plugins will be disabled.
439 @param boolean Returns TRUE if at least one plugin supports multiple edit
440 */
441 function enable_multiple_support()
442 {
443 if(!$this->multiple_support_available()){
444 return(FALSE);
445 }else{
446 $this->multiple_support_active = TRUE;
447 foreach($this->by_object as $name => $obj){
448 if($obj->multiple_support){
449 $this->by_object[$name]->enable_multiple_support();
450 }else{
451 unset($this->by_object[$name]);
452 unset($this->by_name[$name]);
453 }
454 }
455 }
456 return(TRUE);
457 }
459 function setReadOnly($s = TRUE)
460 {
461 foreach($this->by_object as $name => $obj){
462 $this->by_object[$name]->read_only = $s;
463 }
464 $this->read_only = $s;
465 }
466 }
467 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
468 ?>