1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2003 Cajus Pollmeier
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 class tabs
22 {
23 var $dn;
24 var $config;
25 var $acl;
26 var $is_template;
27 var $is_new= FALSE;
29 var $last= "";
30 var $current= "";
31 var $disabled= "";
32 var $by_name= array();
33 var $by_object= array();
34 var $SubDialog = false;
36 var $entryCSN = "";
38 function tabs(&$config, $data, $dn, $acl_category= "")
39 {
40 /* Save dn */
41 $this->dn= $dn;
42 $this->config= &$config;
44 $baseobject= NULL;
46 $this->entryCSN = getEntryCSN($dn);
48 foreach ($data as &$tab){
49 $this->by_name[$tab['CLASS']]= $tab['NAME'];
51 if ($baseobject === NULL){
52 $baseobject= new $tab['CLASS']($this->config, $this->dn);
53 $this->by_object[$tab['CLASS']]= $baseobject;
54 } else {
55 $this->by_object[$tab['CLASS']]= new $tab['CLASS']($this->config, $this->dn, $baseobject);
56 }
58 $this->by_object[$tab['CLASS']]->parent= &$this;
59 $this->by_object[$tab['CLASS']]->set_acl_category($acl_category);
61 /* Initialize current */
62 if ($this->current == ""){
63 $this->current= $tab['CLASS'];
64 }
65 }
66 }
69 function multiple_support_available()
70 {
71 foreach($this->by_object as $name => $obj){
72 if($obj->multiple_support){
73 return(TRUE);
74 }
75 }
76 return(FALSE);
77 }
79 function enable_multiple_support()
80 {
81 if(!$this->multiple_support_available()){
82 return(FALSE);
83 }else{
84 foreach($this->by_object as $name => $obj){
85 if($obj->multiple_support){
86 $this->by_object[$name]->multiple_support_active = TRUE;
87 }else{
88 unset($this->by_object[$name]);
89 unset($this->by_name[$name]);
90 }
91 }
92 }
93 return(TRUE);
94 }
97 function execute()
98 {
99 /* Rotate current to last */
100 $this->last= $this->current;
102 /* Look for pressed tab button */
103 foreach ($this->by_object as $class => &$obj){
104 if (isset($_POST[$class]) || (isset($_POST['arg']) && $_POST['arg'] == "$class")){
105 $this->current= $class;
106 break;
107 }
108 }
110 /* Save last tab object */
111 if ($this->last == $this->current){
112 $this->save_object(TRUE);
113 } else {
114 $this->save_object(FALSE);
115 }
117 /* Build tab line */
118 $display= $this->gen_tabs();
120 /* Show object */
121 $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";
122 $display.= "<tr><td>\n";
124 $display.= $this->by_object[$this->current]->execute();
126 /* Footer for tabbed dialog */
127 $display.= "</td></tr></table>";
129 return ($display);
130 }
132 function save_object($save_current= FALSE)
133 {
134 /* Update entry CSN if it is empty. */
135 if(empty($this->entryCSN)){
136 $this->entryCSN = getEntryCSN($this->dn);
137 }
139 /* Save last tab */
140 if ($this->last != ""){
141 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
142 $this->last, "Saving");
144 $this->by_object[$this->last]->save_object ();
145 }
147 /* Skip if curent and last are the same object */
148 if ($this->last == $this->current){
149 return;
150 }
152 $obj= @$this->by_object[$this->current];
153 $this->disabled= $obj->parent->disabled;
155 if ($save_current){
156 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
157 $this->current, "Saving (current)");
159 $obj->save_object ();
160 }
161 }
163 function gen_tabs()
164 {
165 $display= "<input type=\"hidden\" name=\"arg\" value=\"\">";
166 $display.= "<table summary=\"\" cellpadding=0 cellspacing=0 border=0 style=\"width:100%;\"><tr>";
167 $index= 0;
168 $style= array("tab_left", "tab_active", "tab_near_active", "tab_right");
169 foreach ($this->by_name as $class => $name){
171 /* Activate right tabs with style "tab_right"
172 Activate near current with style "tab_near_active" */
173 if ($index == 2 || $index == 1){
174 $index++;
175 }
177 /* Activate current tab with style "tab_active " */
178 if ($class == $this->current){
179 $index++;
180 }
182 /* Paint tab */
183 $display.= "<td style=\"vertical-align:bottom;width:1px;white-space:nowrap;\">";
185 /* Shorten string if its too long for the tab headers*/
186 $title= _($name);
187 if (mb_strlen($title, 'UTF-8') > 28){
188 $title= mb_substr($title,0, 25, 'UTF-8')."...";
189 }
191 /* nobr causes w3c warnings so we use to keep the tab name in one line */
192 $title= preg_replace("/ /"," ",$title);
194 /* Take care about notifications */
195 $obj = $this->by_object[$class];
196 if ( $this->by_object[$class]->pl_notify && ($obj->is_account || $obj->ignore_account)){
197 $notify= "id=\"notify\"";
198 } else {
199 $notify= "";
200 }
202 if ($_SESSION['js']==FALSE){
203 $display.= "<div ".$notify." class=\"$style[$index]\"><input type=\"submit\" name=\"$class\"".
204 " class=\"$style[$index]\" value=\"$title\"";
205 } else {
206 $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";
207 }
208 $display.= "></div></td>";
209 }
210 $display.= "<td style=\"vertical-align:bottom;\">\n";
211 $display.= "<div class=\"tab_border\"> </div></td></tr></table>";
213 return($display);
214 }
217 function set_acl($acl)
218 {
219 /* Look for attribute in ACL */
220 trigger_error("Don't use tabs::set_acl() its obsolete.");
221 }
223 function delete()
224 {
225 /* Check if all plugins will ACK for deletion */
226 foreach (array_reverse($this->by_object) as $key => $obj){
227 $reason= $obj->allow_remove();
228 if ($reason != ""){
229 print_red(sprintf(_("Delete process has been canceled by plugin '%s': %s"), $key, $reason));
230 return;
231 }
232 }
234 /* Delete for all plugins */
235 foreach (array_reverse($this->by_object) as $obj){
236 $obj->remove_from_parent();
237 }
238 }
240 function password_change_needed()
241 {
242 /* Ask all plugins for needed password changes */
243 foreach ($this->by_object as &$obj){
244 if ($obj->password_change_needed()){
245 return TRUE;
246 }
247 }
249 return FALSE;
250 }
252 function check($ignore_account= FALSE)
253 {
254 $this->save_object(TRUE);
255 $messages= array();
257 $current_set = FALSE;
259 /* Check entryCSN */
260 $current_csn = getEntryCSN($this->dn);
261 if($current_csn != $this->entryCSN && !empty($this->entryCSN) && !empty($current_csn)){
262 $this->entryCSN = $current_csn;
263 $messages[] = _("The object has changed since opened in GOsa. Please ensure that nobody has done serious changes that may get lost if you save this entry.");
264 }
266 /* Check all plugins */
267 foreach ($this->by_object as $key => &$obj){
268 if ($obj->is_account || $ignore_account || $obj->ignore_account){
269 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$key, "Checking");
271 $msg = $obj->check();
273 if (count($msg)){
274 $obj->pl_notify= TRUE;
275 if(!$current_set){
276 $current_set = TRUE;
277 $this->current= $key;
278 $messages = $msg;
279 }
280 }else{
281 $obj->pl_notify= FALSE;
282 }
283 }else{
284 $obj->pl_notify= FALSE;
285 }
286 }
287 $messages = array_unique($messages);
288 return ($messages);
289 }
291 function save($ignore_account= FALSE)
292 {
293 /* Save all plugins */
294 $this->entryCSN = "";
295 foreach ($this->by_object as $key => &$obj){
296 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
297 $key, "Saving");
299 $obj->dn= $this->dn;
301 if ($obj->is_account || $ignore_account || $obj->ignore_account){
302 if ($obj->save() == 1){
303 return (1);
304 }
305 } else {
306 $obj->remove_from_parent();
307 }
308 }
309 return (0);
310 }
312 function adapt_from_template($dn)
313 {
314 foreach ($this->by_object as $key => &$obj){
315 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
316 $key, "Adapting");
317 $obj->parent= &$this;
318 $obj->adapt_from_template($dn);
319 }
320 }
323 /* Save attributes posted by copy & paste dialog
324 */
325 function saveCopyDialog()
326 {
327 foreach ($this->by_object as &$obj){
328 if($obj->is_account || $obj->ignore_account){
329 $obj->saveCopyDialog();
330 }
331 }
332 }
335 /* return copy & paste dialog
336 */
337 function getCopyDialog()
338 {
339 $ret = "";
340 $this->SubDialog = false;
341 foreach ($this->by_object as &$obj){
342 if($obj->is_account || $obj->ignore_account){
343 $tmp = $obj->getCopyDialog();
344 if($tmp['status'] == "SubDialog"){
345 $this->SubDialog = true;
346 return($tmp['string']);
347 }else{
348 if(!empty($tmp['string'])){
349 $ret .= $tmp['string'];
350 $ret .= "<p class='seperator'> </p>";
351 }
352 }
353 }
354 }
355 return($ret);
356 }
359 function addSpecialTabs()
360 {
361 $this->by_name['acl']= _("ACL");
362 $this->by_object['acl']= new acl($this->config, $this, $this->dn);
363 $this->by_object['acl']->parent= &$this;
364 $this->by_name['reference']= _("References");
365 $this->by_object['reference']= new reference($this->config, $this->dn);
366 $this->by_object['reference']->parent= &$this;
367 }
370 function set_acl_base($base= "")
371 {
372 /* Update reference, transfer variables */
373 $first= ($base == "");
374 foreach ($this->by_object as &$obj){
375 if ($first){
376 $first= FALSE;
377 $base= $obj->acl_base;
378 } else {
379 $obj->set_acl_base($base);
380 }
381 }
382 }
383 }
384 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
385 ?>