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 definitions found to initialize '%s', please check your configuration file."),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'!", $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 // Rotate current to last
118 $this->last= $this->current;
120 // Look for pressed tab button
121 foreach ($this->by_object as $class => &$obj){
122 if (isset($_POST[$class]) || (isset($_POST['arg']) && $_POST['arg'] == "$class")){
123 $this->current= $class;
124 break;
125 }
126 }
128 // Save last tab object
129 if ($this->last == $this->current){
130 $this->save_object(TRUE);
131 } else {
132 $this->save_object(FALSE);
133 }
135 /* If multiple edit is enabled for this tab,
136 we have tho display different templates */
137 if(!$this->multiple_support_active){
138 $display= $this->by_object[$this->current]->execute();
139 }else{
140 $display= $this->by_object[$this->current]->multiple_execute();
141 }
142 $tabs= $this->gen_tabs();
143 $display =
144 "\n {$tabs}".
145 "\n <input type='hidden' name='arg' value=''>".
146 "\n <div class='tab-content'>".
147 "\n {$display}".
148 "\n </div>";
150 return ($display);
151 }
153 function save_object($save_current= FALSE)
154 {
155 /* Save last tab */
156 if ($this->last != ""){
157 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
158 $this->last, "Saving");
160 if(!$this->multiple_support_active){
161 $this->by_object[$this->last]->save_object ();
162 }else{
163 $this->by_object[$this->last]->multiple_save_object();
164 }
165 }
167 /* Skip if curent and last are the same object */
168 if ($this->last == $this->current){
169 return;
170 }
172 $obj= @$this->by_object[$this->current];
173 $this->disabled= $obj->parent->disabled;
175 if ($save_current){
176 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
177 $this->current, "Saving (current)");
179 if(!$this->multiple_support_active){
180 $obj->save_object();
181 }else{
182 $obj->multiple_save_object();
183 }
184 }
185 }
188 function is_modal_dialog()
189 {
190 return($this->by_object[$this->current]->is_modal_dialog());
191 }
193 function gen_tabs()
194 {
195 if($this->is_modal_dialog()) return("");
197 $display = "\n <div class='tabs'>";
198 $display.= "\n <ul>";
200 foreach ($this->by_name as $class => $name){
202 // Shorten string if its too long for the tab headers
203 $title= _($name);
204 if (mb_strlen($title, 'UTF-8') > 28){
205 $title= mb_substr($title,0, 25, 'UTF-8')."...";
206 }
208 // nobr causes w3c warnings so we use to keep the tab name in one line
209 $title= str_replace(" "," ",$title);
211 // Take care about notifications
212 $obj = $this->by_object[$class];
213 $tabClass = ($this->current == $class) ? "current" :"";
214 if ( $this->by_object[$class]->pl_notify && ($obj->is_account || $obj->ignore_account)){
215 $tabClass .= " info";
216 }
217 if(!empty($tabClass)) $tabClass="class='{$tabClass}'";
218 $onClick = "document.mainform.arg.value='{$class}'; document.mainform.submit();";
219 $display.= "\n <li {$tabClass} onClick=\"{$onClick}\">{$title}</li>";
220 }
221 $display.="\n </ul>";
222 $display.="\n </div>";
223 return($display);
224 }
227 function set_acl($acl)
228 {
229 /* Look for attribute in ACL */
230 trigger_error("Don't use tabs::set_acl() its obsolete.");
231 }
233 function delete()
234 {
235 /* Check if all plugins will ACK for deletion */
236 foreach (array_reverse($this->by_object) as $key => $obj){
237 $reason= $obj->allow_remove();
238 if ($reason != ""){
239 msg_dialog::display(_("Warning"), sprintf(_("Delete process has been canceled by plugin '%s': %s"), $key, $reason), WARNING_DIALOG);
240 return;
241 }
242 }
244 /* Delete for all plugins */
245 foreach (array_reverse($this->by_object) as $obj){
246 $obj->remove_from_parent();
247 }
248 }
250 function password_change_needed()
251 {
252 /* Ask all plugins for needed password changes */
253 foreach ($this->by_object as &$obj){
254 if ($obj->password_change_needed()){
255 return TRUE;
256 }
257 }
259 return FALSE;
260 }
262 function check($ignore_account= FALSE)
263 {
264 $this->save_object(TRUE);
265 $messages= array();
267 $current_set = FALSE;
269 /* Check all plugins */
270 foreach ($this->by_object as $key => &$obj){
271 if ($obj->is_account || $ignore_account || $obj->ignore_account){
272 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$key, "Checking");
274 if(!$this->multiple_support_active){
275 $msg = $obj->check();
276 }else{
277 $msg = $obj->multiple_check();
278 }
280 if (count($msg)){
281 $obj->pl_notify= TRUE;
282 if(!$current_set){
283 $current_set = TRUE;
284 $this->current= $key;
285 $messages = $msg;
286 }
287 }else{
288 $obj->pl_notify= FALSE;
289 }
290 }else{
291 $obj->pl_notify= FALSE;
292 }
293 }
294 return ($messages);
295 }
297 function save($ignore_account= FALSE)
298 {
299 /* Save all plugins */
300 foreach ($this->by_object as $key => &$obj){
301 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
302 $key, "Saving");
304 $obj->dn= $this->dn;
306 if(!$obj instanceof plugin && !$obj instanceOf management){
307 trigger_error("Something went wrong while saving ".$obj->dn.". Object class '".get_class($obj)."'.");
308 }else{
310 if ($obj->is_account || $ignore_account || $obj->ignore_account){
311 if ($obj->save() == 1){
312 return (1);
313 }
314 } else {
315 $obj->remove_from_parent();
316 }
317 }
318 }
319 return (0);
320 }
322 function adapt_from_template($dn, $skip= array())
323 {
324 foreach ($this->by_object as $key => &$obj){
325 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
326 $key, "Adapting");
327 $obj->parent= &$this;
328 $obj->adapt_from_template($dn, $skip);
329 }
330 }
333 /* Save attributes posted by copy & paste dialog
334 */
335 function saveCopyDialog()
336 {
337 foreach ($this->by_object as &$obj){
338 if($obj->is_account || $obj->ignore_account){
339 $obj->saveCopyDialog();
340 }
341 }
342 }
345 /* return copy & paste dialog
346 */
347 function getCopyDialog()
348 {
349 $ret = "";
350 $this->SubDialog = false;
351 foreach ($this->by_object as &$obj){
352 if($obj->is_account || $obj->ignore_account){
353 $tmp = $obj->getCopyDialog();
354 if($tmp['status'] == "SubDialog"){
355 $this->SubDialog = true;
356 return($tmp['string']);
357 }else{
358 if(!empty($tmp['string'])){
359 $ret .= $tmp['string'];
360 $ret .= "<p class='seperator'> </p>";
361 }
362 }
363 }
364 }
365 return($ret);
366 }
369 function addSpecialTabs()
370 {
371 if(!$this->hide_acls){
372 $this->by_name['acl']= _("ACL");
373 $this->by_object['acl']= new acl($this->config, $this, $this->dn);
374 $this->by_object['acl']->parent= &$this;
375 }
376 if(!$this->hide_refs){
377 $this->by_name['reference']= _("References");
378 $this->by_object['reference']= new reference($this->config, $this->dn);
379 $this->by_object['reference']->parent= &$this;
380 }
381 }
384 function set_acl_base($base= "")
385 {
386 /* Update reference, transfer variables */
387 $first= ($base == "");
388 foreach ($this->by_object as &$obj){
389 if ($first){
390 $first= FALSE;
391 $base= $obj->acl_base;
392 } else {
393 $obj->set_acl_base($base);
394 }
395 }
396 }
399 /*! \brief Checks if one of the used tab plugins supports multiple edit.
400 @param boolean Returns TRUE if at least one plugins supports multiple edit.
401 */
402 function multiple_support_available()
403 {
404 foreach($this->by_object as $name => $obj){
405 if($obj->multiple_support){
406 return(TRUE);
407 }
408 }
409 return(FALSE);
410 }
413 /*! \brief Enables multiple edit support for the given tab.
414 All unsupported plugins will be disabled.
415 @param boolean Returns TRUE if at least one plugin supports multiple edit
416 */
417 function enable_multiple_support()
418 {
419 if(!$this->multiple_support_available()){
420 return(FALSE);
421 }else{
422 $this->multiple_support_active = TRUE;
423 foreach($this->by_object as $name => $obj){
424 if($obj->multiple_support){
425 $this->by_object[$name]->enable_multiple_support();
426 }else{
427 unset($this->by_object[$name]);
428 unset($this->by_name[$name]);
429 }
430 }
431 }
432 return(TRUE);
433 }
435 function setReadOnly($s = TRUE)
436 {
437 foreach($this->by_object as $name => $obj){
438 $this->by_object[$name]->read_only = $s;
439 }
440 }
441 }
442 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
443 ?>