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 $multiple_entries = FALSE;
38 function tabs(&$config, $data, $dn, $acl_category= "")
39 {
40 /* Save dn */
41 $this->dn= $dn;
42 $this->config= &$config;
44 $baseobject= NULL;
46 /* Check specified dn
47 */
48 if(is_string($dn)){
50 }elseif(is_array($dn)&&count($dn) == 1){
51 $this->dn = $dn[key($dn)];
52 }elseif(is_array($dn)&&count($dn) > 1){
53 $this->multiple_entries = TRUE;
54 }
56 foreach ($data as &$tab){
58 /* Check we want to handle multiple obejcts at once
59 * and if this is supported by enabled plugins
60 */
61 $tmp = get_class_vars($tab['CLASS']);
62 if($this->multiple_entries && !$tmp['multiple_support']){
63 continue;
64 }
66 $this->by_name[$tab['CLASS']]= $tab['NAME'];
68 if ($baseobject === NULL){
69 $baseobject= new $tab['CLASS']($this->config, $this->dn);
70 $this->by_object[$tab['CLASS']]= $baseobject;
71 } else {
72 $this->by_object[$tab['CLASS']]= new $tab['CLASS']($this->config, $this->dn, $baseobject);
73 }
75 $this->by_object[$tab['CLASS']]->parent= &$this;
76 $this->by_object[$tab['CLASS']]->set_acl_category($acl_category);
78 /* Initialize current */
79 if ($this->current == ""){
80 $this->current= $tab['CLASS'];
81 }
82 }
84 /* Return false if tabs */
85 if(!count($this->by_object)){
86 return(FALSE);
87 }
88 }
90 function execute()
91 {
92 /* Rotate current to last */
93 $this->last= $this->current;
95 /* Look for pressed tab button */
96 foreach ($this->by_object as $class => &$obj){
97 if (isset($_POST[$class]) || (isset($_POST['arg']) && $_POST['arg'] == "$class")){
98 $this->current= $class;
99 break;
100 }
101 }
103 /* Save last tab object */
104 if ($this->last == $this->current){
105 $this->save_object(TRUE);
106 } else {
107 $this->save_object(FALSE);
108 }
110 /* Build tab line */
111 $display= $this->gen_tabs();
113 /* Show object */
114 $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";
115 $display.= "<tr><td>\n";
117 $display.= $this->by_object[$this->current]->execute();
119 /* Footer for tabbed dialog */
120 $display.= "</td></tr></table>";
122 return ($display);
123 }
125 function save_object($save_current= FALSE)
126 {
127 /* Save last tab */
128 if ($this->last != ""){
129 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
130 $this->last, "Saving");
132 $this->by_object[$this->last]->save_object ();
133 }
135 /* Skip if curent and last are the same object */
136 if ($this->last == $this->current){
137 return;
138 }
140 $obj= @$this->by_object[$this->current];
141 $this->disabled= $obj->parent->disabled;
143 if ($save_current){
144 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
145 $this->current, "Saving (current)");
147 $obj->save_object ();
148 }
149 }
151 function gen_tabs()
152 {
153 $display= "<input type=\"hidden\" name=\"arg\" value=\"\">";
154 $display.= "<table summary=\"\" cellpadding=0 cellspacing=0 border=0 style=\"width:100%;\"><tr>";
155 $index= 0;
156 $style= array("tab_left", "tab_active", "tab_near_active", "tab_right");
157 foreach ($this->by_name as $class => $name){
159 /* Activate right tabs with style "tab_right"
160 Activate near current with style "tab_near_active" */
161 if ($index == 2 || $index == 1){
162 $index++;
163 }
165 /* Activate current tab with style "tab_active " */
166 if ($class == $this->current){
167 $index++;
168 }
170 /* Paint tab */
171 $display.= "<td style=\"vertical-align:bottom;width:1px;white-space:nowrap;\">";
173 /* Shorten string if its too long for the tab headers*/
174 $title= _($name);
175 if (mb_strlen($title, 'UTF-8') > 28){
176 $title= mb_substr($title,0, 25, 'UTF-8')."...";
177 }
179 /* nobr causes w3c warnings so we use to keep the tab name in one line */
180 $title= preg_replace("/ /"," ",$title);
182 /* Take care about notifications */
183 if ($this->by_object[$class]->pl_notify){
184 $notify= "id=\"notify\"";
185 } else {
186 $notify= "";
187 }
189 if ($_SESSION['js']==FALSE){
190 $display.= "<div ".$notify." class=\"$style[$index]\"><input type=\"submit\" name=\"$class\"".
191 " class=\"$style[$index]\" value=\"$title\"";
192 } else {
193 $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";
194 }
195 $display.= "></div></td>";
196 }
197 $display.= "<td style=\"vertical-align:bottom;\">\n";
198 $display.= "<div class=\"tab_border\"> </div></td></tr></table>";
200 return($display);
201 }
204 function set_acl($acl)
205 {
206 /* Look for attribute in ACL */
207 trigger_error("Don't use tabs::set_acl() its obsolete.");
208 }
210 function delete()
211 {
212 /* Check if all plugins will ACK for deletion */
213 foreach (array_reverse($this->by_object) as $key => $obj){
214 $reason= $obj->allow_remove();
215 if ($reason != ""){
216 print_red(sprintf(_("Delete process has been canceled by plugin '%s': %s"), $key, $reason));
217 return;
218 }
219 }
221 /* Delete for all plugins */
222 foreach (array_reverse($this->by_object) as $obj){
223 $obj->remove_from_parent();
224 }
225 }
227 function password_change_needed()
228 {
229 /* Ask all plugins for needed password changes */
230 foreach ($this->by_object as &$obj){
231 if ($obj->password_change_needed()){
232 return TRUE;
233 }
234 }
236 return FALSE;
237 }
239 function check($ignore_account= FALSE)
240 {
241 $this->save_object(TRUE);
242 $messages= array();
244 $current_set = FALSE;
246 /* Check all plugins */
247 foreach ($this->by_object as $key => &$obj){
248 if ($obj->is_account || $ignore_account || $obj->ignore_account){
249 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$key, "Checking");
251 $msg = $obj->check();
253 if (count($msg)){
254 $obj->pl_notify= TRUE;
255 if(!$current_set){
256 $current_set = TRUE;
257 $this->current= $key;
258 $messages = $msg;
259 }
260 }else{
261 $obj->pl_notify= FALSE;
262 }
263 }else{
264 $obj->pl_notify= FALSE;
265 }
266 }
267 return ($messages);
268 }
270 function save($ignore_account= FALSE)
271 {
272 if($this->multiple_entries){
273 foreach ($this->by_object as $key => &$obj){
274 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
275 $key, "Saving");
276 $obj->save();
277 }
278 }else{
279 /* Save all plugins */
280 foreach ($this->by_object as $key => &$obj){
281 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
282 $key, "Saving");
284 $obj->dn= $this->dn;
286 if ($obj->is_account || $ignore_account || $obj->ignore_account){
287 if ($obj->save() == 1){
288 return (1);
289 }
290 } else {
291 $obj->remove_from_parent();
292 }
293 }
294 }
295 return (0);
296 }
298 function adapt_from_template($dn)
299 {
300 foreach ($this->by_object as $key => &$obj){
301 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
302 $key, "Adapting");
303 $obj->parent= &$this;
304 $obj->adapt_from_template($dn);
305 }
306 }
309 /* Save attributes posted by copy & paste dialog
310 */
311 function saveCopyDialog()
312 {
313 foreach ($this->by_object as &$obj){
314 if($obj->is_account){
315 $obj->saveCopyDialog();
316 }
317 }
318 }
321 /* return copy & paste dialog
322 */
323 function getCopyDialog()
324 {
325 $ret = "";
326 $this->SubDialog = false;
327 foreach ($this->by_object as &$obj){
328 if($obj->is_account){
329 $tmp = $obj->getCopyDialog();
330 if($tmp['status'] == "SubDialog"){
331 $this->SubDialog = true;
332 return($tmp['string']);
333 }else{
334 if(!empty($tmp['string'])){
335 $ret .= $tmp['string'];
336 $ret .= "<p class='seperator'> </p>";
337 }
338 }
339 }
340 }
341 return($ret);
342 }
345 function addSpecialTabs()
346 {
347 if(!$this->multiple_entries){
348 $this->by_name['acl']= _("ACL");
349 $this->by_object['acl']= new acl($this->config, $this, $this->dn);
350 $this->by_object['acl']->parent= &$this;
351 $this->by_name['reference']= _("References");
352 $this->by_object['reference']= new reference($this->config, $this->dn);
353 $this->by_object['reference']->parent= &$this;
354 }
355 }
358 function set_acl_base($base= "")
359 {
360 /* Update reference, transfer variables */
361 $first= ($base == "");
362 foreach ($this->by_object as &$obj){
363 if ($first){
364 $first= FALSE;
365 $base= $obj->acl_base;
366 } else {
367 $obj->set_acl_base($base);
368 }
369 }
370 }
372 }
373 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
374 ?>