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;
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 $baseobject= NULL;
48 foreach ($data as &$tab){
50 if (!plugin_available($tab['CLASS'])){
51 continue;
52 }
54 $this->by_name[$tab['CLASS']]= $tab['NAME'];
56 if ($baseobject === NULL){
57 $baseobject= new $tab['CLASS']($this->config, $this->dn);
58 $baseobject->enable_CSN_check();
59 $this->by_object[$tab['CLASS']]= $baseobject;
60 } else {
61 $this->by_object[$tab['CLASS']]= new $tab['CLASS']($this->config, $this->dn, $baseobject);
62 }
64 $this->by_object[$tab['CLASS']]->parent= &$this;
65 $this->by_object[$tab['CLASS']]->set_acl_category($acl_category);
67 /* Initialize current */
68 if ($this->current == ""){
69 $this->current= $tab['CLASS'];
70 }
71 }
72 }
75 function execute()
76 {
77 /* Rotate current to last */
78 $this->last= $this->current;
80 /* Look for pressed tab button */
81 foreach ($this->by_object as $class => &$obj){
82 if (isset($_POST[$class]) || (isset($_POST['arg']) && $_POST['arg'] == "$class")){
83 $this->current= $class;
84 break;
85 }
86 }
88 /* Save last tab object */
89 if ($this->last == $this->current){
90 $this->save_object(TRUE);
91 } else {
92 $this->save_object(FALSE);
93 }
95 /* Build tab line */
96 $display= $this->gen_tabs();
98 /* Show object */
99 $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";
100 $display.= "<tr><td>\n";
102 /* If multiple edit is enabled for this tab,
103 we have tho display different templates */
104 if(!$this->multiple_support_active){
105 $display.= $this->by_object[$this->current]->execute();
106 }else{
107 $display.= $this->by_object[$this->current]->multiple_execute();
108 }
110 /* Footer for tabbed dialog */
111 $display.= "</td></tr></table>";
113 return ($display);
114 }
116 function save_object($save_current= FALSE)
117 {
118 /* Save last tab */
119 if ($this->last != ""){
120 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
121 $this->last, "Saving");
123 if(!$this->multiple_support_active){
124 $this->by_object[$this->last]->save_object ();
125 }else{
126 $this->by_object[$this->last]->multiple_save_object();
127 }
128 }
130 /* Skip if curent and last are the same object */
131 if ($this->last == $this->current){
132 return;
133 }
135 $obj= @$this->by_object[$this->current];
136 $this->disabled= $obj->parent->disabled;
138 if ($save_current){
139 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
140 $this->current, "Saving (current)");
142 if(!$this->multiple_support_active){
143 $obj->save_object();
144 }else{
145 $obj->multiple_save_object();
146 }
147 }
148 }
150 function gen_tabs()
151 {
152 $display= "<input type=\"hidden\" name=\"arg\" value=\"\">";
153 $display.= "<table summary=\"\" cellpadding=0 cellspacing=0 border=0 style=\"width:100%;\"><tr>";
154 $index= 0;
155 $style= array("tab_left", "tab_active", "tab_near_active", "tab_right");
156 foreach ($this->by_name as $class => $name){
158 /* Activate right tabs with style "tab_right"
159 Activate near current with style "tab_near_active" */
160 if ($index == 2 || $index == 1){
161 $index++;
162 }
164 /* Activate current tab with style "tab_active " */
165 if ($class == $this->current){
166 $index++;
167 }
169 /* Paint tab */
170 $display.= "<td style=\"vertical-align:bottom;width:1px;white-space:nowrap;\">";
172 /* Shorten string if its too long for the tab headers*/
173 $title= _($name);
174 if (mb_strlen($title, 'UTF-8') > 28){
175 $title= mb_substr($title,0, 25, 'UTF-8')."...";
176 }
178 /* nobr causes w3c warnings so we use to keep the tab name in one line */
179 $title= preg_replace("/ /"," ",$title);
181 /* Take care about notifications */
182 $obj = $this->by_object[$class];
183 if ( $this->by_object[$class]->pl_notify && ($obj->is_account || $obj->ignore_account)){
184 $notify= "id=\"notify\"";
185 } else {
186 $notify= "";
187 }
189 if (session::get('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 msg_dialog::display(_("Warning"), sprintf(_("Delete process has been canceled by plugin '%s': %s"), $key, $reason), WARNING_DIALOG);
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 if(!$this->multiple_support_active){
252 $msg = $obj->check();
253 }else{
254 $msg = $obj->multiple_check();
255 }
257 if (count($msg)){
258 $obj->pl_notify= TRUE;
259 if(!$current_set){
260 $current_set = TRUE;
261 $this->current= $key;
262 $messages = $msg;
263 }
264 }else{
265 $obj->pl_notify= FALSE;
266 }
267 }else{
268 $obj->pl_notify= FALSE;
269 }
270 }
271 return ($messages);
272 }
274 function save($ignore_account= FALSE)
275 {
276 /* Save all plugins */
277 foreach ($this->by_object as $key => &$obj){
278 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
279 $key, "Saving");
281 $obj->dn= $this->dn;
283 if ($obj->is_account || $ignore_account || $obj->ignore_account){
284 if ($obj->save() == 1){
285 return (1);
286 }
287 } else {
288 $obj->remove_from_parent();
289 }
290 }
291 return (0);
292 }
294 function adapt_from_template($dn, $skip= array())
295 {
296 foreach ($this->by_object as $key => &$obj){
297 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
298 $key, "Adapting");
299 $obj->parent= &$this;
300 $obj->adapt_from_template($dn, $skip);
301 }
302 }
305 /* Save attributes posted by copy & paste dialog
306 */
307 function saveCopyDialog()
308 {
309 foreach ($this->by_object as &$obj){
310 if($obj->is_account || $obj->ignore_account){
311 $obj->saveCopyDialog();
312 }
313 }
314 }
317 /* return copy & paste dialog
318 */
319 function getCopyDialog()
320 {
321 $ret = "";
322 $this->SubDialog = false;
323 foreach ($this->by_object as &$obj){
324 if($obj->is_account || $obj->ignore_account){
325 $tmp = $obj->getCopyDialog();
326 if($tmp['status'] == "SubDialog"){
327 $this->SubDialog = true;
328 return($tmp['string']);
329 }else{
330 if(!empty($tmp['string'])){
331 $ret .= $tmp['string'];
332 $ret .= "<p class='seperator'> </p>";
333 }
334 }
335 }
336 }
337 return($ret);
338 }
341 function addSpecialTabs()
342 {
343 $this->by_name['acl']= _("ACL");
344 $this->by_object['acl']= new acl($this->config, $this, $this->dn);
345 $this->by_object['acl']->parent= &$this;
346 $this->by_name['reference']= _("References");
347 $this->by_object['reference']= new reference($this->config, $this->dn);
348 $this->by_object['reference']->parent= &$this;
349 }
352 function set_acl_base($base= "")
353 {
354 /* Update reference, transfer variables */
355 $first= ($base == "");
356 foreach ($this->by_object as &$obj){
357 if ($first){
358 $first= FALSE;
359 $base= $obj->acl_base;
360 } else {
361 $obj->set_acl_base($base);
362 }
363 }
364 }
367 /*! \brief Checks if one of the used tab plugins supports multiple edit.
368 @param boolean Returns TRUE if at least one plugins supports multiple edit.
369 */
370 function multiple_support_available()
371 {
372 foreach($this->by_object as $name => $obj){
373 if($obj->multiple_support){
374 return(TRUE);
375 }
376 }
377 return(FALSE);
378 }
381 /*! \brief Enables multiple edit support for the given tab.
382 All unsupported plugins will be disabled.
383 @param boolean Returns TRUE if at least one plugin supports multiple edit
384 */
385 function enable_multiple_support()
386 {
387 if(!$this->multiple_support_available()){
388 return(FALSE);
389 }else{
390 $this->multiple_support_active = TRUE;
391 foreach($this->by_object as $name => $obj){
392 if($obj->multiple_support){
393 $this->by_object[$name]->enable_multiple_support();
394 }else{
395 unset($this->by_object[$name]);
396 unset($this->by_name[$name]);
397 }
398 }
399 }
400 return(TRUE);
401 }
402 }
403 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
404 ?>