source: branches/2.3/preferences/preferences.php @ 3018

Revision 3018, 18.5 KB checked in by amuller, 14 years ago (diff)

Ticket #1135 - Aplicando alterações do branches 2.0 no branches 2.2

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2        /**************************************************************************\
3        * phpGroupWare - Preferences                                               *
4        * http://www.phpgroupware.org                                              *
5        * --------------------------------------------                             *
6        *  This program is free software; you can redistribute it and/or modify it *
7        *  under the terms of the GNU General Public License as published by the   *
8        *  Free Software Foundation; either version 2 of the License, or (at your  *
9        *  option) any later version.                                              *
10        \**************************************************************************/
11
12
13        $GLOBALS['phpgw_info']['flags'] = array(
14                'noheader'                => True,
15                'noappheader'             => True,
16                'nonavbar'                => True,
17                'currentapp'              => @addslashes($_GET['appname']) ? addslashes($_GET['appname']) : 'preferences',
18                'enable_nextmatchs_class' => True
19        );
20        include('../header.inc.php');
21       
22        if ($_POST['cancel'])
23        {
24                $GLOBALS['phpgw']->redirect_link('/preferences/index.php');
25        }
26       
27        $user    = get_var('user',Array('POST'));
28        $forced  = get_var('forced',Array('POST'));
29        $default = get_var('default',Array('POST'));
30
31        $t = CreateObject('phpgwapi.Template',$GLOBALS['phpgw']->common->get_tpl_dir('preferences'));
32        $t->set_file(array(
33                'preferences' => 'preferences.tpl'
34        ));
35        $t->set_block('preferences','list','lists');
36        $t->set_block('preferences','row','rowhandle');
37        $t->set_block('preferences','script','scripthandle');
38        $t->set_block('preferences','help_row','help_rowhandle');
39        $t->set_var(array('rowhandle' => '','help_rowhandle' => '','messages' => ''));
40       
41        if ($_GET['appname'] != 'preferences')
42        {
43                $GLOBALS['phpgw']->translation->add_app('preferences'); // we need the prefs translations too
44        }
45
46        /* Make things a little easier to follow */
47        /* Some places we will need to change this if there in common */
48        function check_app()
49        {
50                if ($_GET['appname'] == 'preferences')
51                {
52                        return 'common';
53                }
54                else
55                {
56                        return ($_GET['appname'] == 'expressoMail1_2'?'expressoMail':$_GET['appname']);             
57                }
58        }
59
60        function is_forced_value($_appname,$preference_name)
61        {
62                if (isset($GLOBALS['phpgw']->preferences->forced[$_appname][$preference_name]) && $GLOBALS['type'] != 'forced')
63                {
64                        return True;
65                }
66                else
67                {
68                        return False;
69                }
70        }
71
72        function create_password_box($label_name,$preference_name,$help='',$size = '',$max_size = '',$run_lang=True)
73        {
74                global $user,$forced,$default;
75               
76                $_appname = check_app();
77                if (is_forced_value($_appname,$preference_name))
78                {
79                        return True;
80                }
81                create_input_box($label_name,$preference_name.'][pw',$help,'',$size,$max_size,'password',$run_lang);
82        }
83       
84        function create_input_box($label,$name,$help='',$default='',$size = '',$max_size = '',$type='',
85                $run_lang=True)
86        {
87                global $t,$prefs;
88
89                $charSet = $GLOBALS['phpgw']->translation->charset();
90
91                $_appname = check_app();
92                if (is_forced_value($_appname,$name))
93                {
94                        return True;
95                }
96
97                if ($type)      // used to specify password
98                {
99                        $options = " TYPE='$type'";
100                }
101                if ($size)
102                {
103                        $options .= " SIZE='$size'";
104                }
105                if ($maxsize)
106                {
107                        $options .= " MAXSIZE='$maxsize'";
108                }
109
110                if (isset($prefs[$name]) || $GLOBALS['type'] != 'user')
111                {
112                        $default = $prefs[$name];
113                }
114               
115                if ($GLOBALS['type'] == 'user')
116                {
117                        $def_text = !$GLOBALS['phpgw']->preferences->user[$_appname][$name] ? $GLOBALS['phpgw']->preferences->data[$_appname][$name] : $GLOBALS['phpgw']->preferences->default[$_appname][$name];
118
119                        if (isset($notifys[$name]))     // translate the substitution names
120                        {
121                                $def_text = $GLOBALS['phpgw']->preferences->lang_notify($def_text,$notifys[$name]);
122                        }
123                        $def_text = $def_text != '' ? ' <i><font size="-1">'.lang('default').':&nbsp;'.$def_text.'</font></i>' : '';
124                }
125        $t->set_var('row_id', "${GLOBALS[type]}[$name]");
126                $t->set_var('row_value',"<input name=\"${GLOBALS[type]}[$name]\"value=\"".
127                        @htmlentities($default,ENT_COMPAT,$charSet)."\"$options>$def_text");
128                $t->set_var('row_name',lang($label));
129                $GLOBALS['phpgw']->nextmatchs->template_alternate_row_color($t);
130
131                $t->fp('rows',process_help($help,$run_lang) ? 'help_row' : 'row',True);
132        }
133       
134        function process_help($help,$run_lang=True)
135        {
136                global $t,$show_help,$has_help;
137
138                if (!empty($help))
139                {
140                        $has_help = True;
141                       
142                        if ($show_help)
143                        {
144                                $t->set_var('help_value',$run_lang ? lang($help) : $help);
145                               
146                                return True;
147                        }
148                }
149                return False;
150        }
151
152        function create_check_box($label,$name,$help='',$default='',$run_lang=True,$checkbox_prop='',$visible=True)
153        {
154                // checkboxes itself can't be use as they return nothing if uncheckt !!!
155                global $prefs;
156               
157                if ($GLOBALS['type'] != 'user')
158                {
159                        $default = '';  // no defaults for default or forced prefs
160                }
161                if (isset($prefs[$name]))
162                {
163                        $prefs[$name] = (int)(!!$prefs[$name]); // to care for '' and 'True'
164                }
165               
166                return create_select_box($label,$name,array(
167                        '0' => lang('No'),
168                        '1' => lang('Yes')
169                ),$help,$default,$run_lang,$checkbox_prop,$visible);
170        }
171
172        function create_option_string($selected,$values)
173        {
174                while (is_array($values) && list($var,$value) = each($values))
175                {
176                        $s .= '<option value="' . $var . '"';
177                        if ("$var" == "$selected")      // the "'s are necessary to force a string-compare
178                        {
179                                $s .= ' selected';
180                        }
181                        $s .= '>' . $value . '</option>';
182                }
183                return $s;
184        }
185
186        /* for creating different sections with a title */
187        function create_section($title='',$value = '')
188        {
189                global $t;
190
191                        $t->set_var('row_value','');
192                        $t->set_var('row_name','<span class="prefSection">'.lang($title,$value).'</span>');
193                        $GLOBALS['phpgw']->nextmatchs->template_alternate_row_color($t);
194
195                        $t->fp('rows',process_help($help) ? 'help_row' : 'row',True);
196        }
197
198        function create_script($script_code){
199            global $t;
200            $t->set_var('script_code',$script_code);
201            $t->fp('scripthandle','script',True);
202        }
203
204        function create_html_code($name,$code,$appendcode)
205        {
206                global $t,$prefs;
207        $t->set_var('row_id', "${GLOBALS[type]}[$name]");
208                $t->set_var('row_value',$code.$prefs[$name].$appendcode);
209                $t->set_var('row_name',lang("signature"));
210                $GLOBALS['phpgw']->nextmatchs->template_alternate_row_color($t);
211                $t->fp('rows','row',True);
212        }
213
214        function create_select_box($label,$name,$values,$help='',$default='',$run_lang=True,$select_prop = '',$visible=True)
215        {
216                global $t,$prefs;
217
218                $_appname = check_app();
219                if (is_forced_value($_appname,$name))
220                {
221                        return True;
222                }
223               
224                if (isset($prefs[$name]) || $GLOBALS['type'] != 'user')
225                {
226                        $default = $prefs[$name];
227                }
228
229                switch ($GLOBALS['type'])
230                {
231                        case 'user':
232                                $s = '<option value="">' . lang('Use default') . '</option>';
233                                break;
234                        case 'default':
235                                $s = '<option value="">' . lang('No default') . '</option>';
236                                break;
237                        case 'forced':
238                                $s = '<option value="**NULL**">' . lang('Users choice') . '</option>';
239                                break;
240                }
241                $s .= create_option_string($default,$values);
242                if ($GLOBALS['type'] == 'user')
243                {
244                        $def_text = $GLOBALS['phpgw']->preferences->default[$_appname][$name];
245                        $def_text = $def_text != '' ? ' <i><font size="-1">'.lang('default').':&nbsp;'.$values[$def_text].'</font></i>' : '';
246                }
247        $t->set_var('row_id', "${GLOBALS[type]}[$name]");
248                $t->set_var('row_value',"<select name=\"${GLOBALS[type]}[$name]\" $select_prop>$s</select>$def_text");
249                $t->set_var('row_name',lang($label));
250        if ($visible)
251        {
252            $t->set_var('row_visibility', '');
253        }
254        else
255        {
256            $t->set_var('row_visibility', 'style="display: none;"');
257        }
258       
259                $GLOBALS['phpgw']->nextmatchs->template_alternate_row_color($t);
260       
261                $t->fp('rows',process_help($help,$run_lang) ? 'help_row' : 'row',True);
262        }
263       
264        /*!
265        @function create_notify
266        @abstract creates text-area or inputfield with subtitution-variables
267        @syntax create_notify($label,$name,$rows,$cols,$help='',$default='',$vars2='')
268        @param $label untranslated label
269        @param $name name of the pref
270        @param $rows, $cols of the textarea or input-box ($rows==1)
271        @param $help untranslated help-text
272        @param $default default-value
273        @param $vars2 array with extra substitution-variables of the form key => help-text
274        */
275        function create_notify($label,$name,$rows,$cols,$help='',$default='',$vars2='',$subst_help=True,$run_lang=True)
276        {
277                global $t,$prefs,$notifys;
278
279                $vars = $GLOBALS['phpgw']->preferences->vars;
280                if (is_array($vars2))
281                {
282                        $vars += $vars2;
283                }
284                $prefs[$name] = $GLOBALS['phpgw']->preferences->lang_notify($prefs[$name],$vars);
285
286                $notifys[$name] = $vars;        // this gets saved in the app_session for re-translation
287
288                $help = $help && $run_lang ? lang($help) : $help;
289                if ($subst_help)
290                {
291                        $help .= '<p><b>'.lang('Substitutions and their meanings:').'</b>';
292                        foreach($vars as $var => $var_help)
293                        {
294                                $lname = ($lname = lang($var)) == $var.'*' ? $var : $lname;
295                                $help .= "<br>\n".'<b>$$'.$lname.'$$</b>: '.$var_help;
296                        }
297                        $help .= "</p>\n";
298                }
299                if ($row == 1)
300                {
301                        create_input_box($label,$name,$help,$default,$cols,'','',False);
302                }
303                else
304                {
305                        create_text_area($label,$name,$rows,$cols,$help,$default,False);
306                }
307        }
308
309        function create_text_area($label,$name,$rows,$cols,$help='',$default='',$run_lang=True)
310        {
311                global $t,$prefs,$notifys;
312               
313                $charSet = $GLOBALS['phpgw']->translation->charset();
314
315                $_appname = check_app();
316                if (is_forced_value($_appname,$name))
317                {
318                        return True;
319                }
320               
321                if (isset($prefs[$name]) || $GLOBALS['type'] != 'user')
322                {
323                        $default = $prefs[$name];
324                }
325
326                if ($GLOBALS['type'] == 'user')
327                {
328                        $def_text = !$GLOBALS['phpgw']->preferences->user[$_appname][$name] ? $GLOBALS['phpgw']->preferences->data[$_appname][$name] : $GLOBALS['phpgw']->preferences->default[$_appname][$name];
329
330                        if (isset($notifys[$name]))     // translate the substitution names
331                        {
332                                $def_text = $GLOBALS['phpgw']->preferences->lang_notify($def_text,$notifys[$name]);
333                        }
334                        $def_text = $def_text != '' ? '<br><i><font size="-1"><b>'.lang('default').'</b>:<br>'.nl2br($def_text).'</font></i>' : '';
335                }
336        $t->set_var('row_id', "${GLOBALS[type]}[$name]");
337                $t->set_var('row_value',"<textarea rows=\"$rows\" cols=\"$cols\" name=\"${GLOBALS[type]}[$name]\">".
338                        htmlentities($default,ENT_COMPAT,$charSet)."</textarea>$def_text");
339                $t->set_var('row_name',lang($label));
340                $GLOBALS['phpgw']->nextmatchs->template_alternate_row_color($t);
341
342                $t->fp('rows',process_help($help,$run_lang) ? 'help_row' : 'row',True);
343        }
344
345        function process_array(&$repository,$array,$notifys,$prefix='')
346        {
347                $_appname = check_app();
348
349                $prefs = &$repository[$_appname];
350
351                if ($prefix != '')
352                {
353                        $prefix_arr = explode('/',$prefix);
354                        foreach ($prefix_arr as $pre)
355                        {
356                                $prefs = &$prefs[$pre];
357                        }
358                }
359                unset($prefs['']);
360                //echo "array:<pre>"; print_r($array); echo "</pre>\n";
361                while (is_array($array) && list($var,$value) = each($array))
362                {
363                        if (isset($value) && $value != '' && $value != '**NULL**')
364                        {
365                                if (is_array($value))
366                                {
367                                        $value = $value['pw'];
368                                        if (empty($value))
369                                        {
370                                                continue;       // dont write empty password-fields
371                                        }
372                                }
373                                $prefs[$var] = stripslashes($value);
374
375                                if ($notifys[$var])     // need to translate the key-words back
376                                {
377                                        $prefs[$var] = $GLOBALS['phpgw']->preferences->lang_notify($prefs[$var],$notifys[$var],True);
378                                }
379                        }
380                        else
381                        {
382                                unset($prefs[$var]);
383                        }
384                }
385                //echo "prefix='$prefix', prefs=<pre>"; print_r($repository[$_appname]); echo "</pre>\n";
386
387                // the following hook can be used to verify the prefs
388                // if you return something else than False, it is treated as an error-msg and
389                // displayed to the user (the prefs get not saved !!!)
390                //
391                if ($error = $GLOBALS['phpgw']->hooks->single(array(
392                        'location' => 'verify_settings',
393                        'prefs'    => $repository[$_appname],
394                        'prefix'   => $prefix,
395                        'type'     => $GLOBALS['type']
396                ),$_GET['appname']))
397                {
398                        return $error;
399                }
400               
401                $GLOBALS['phpgw']->preferences->save_repository(True,$GLOBALS['type']);
402               
403                return False;
404        }
405
406        /* Only check this once */
407        if ($GLOBALS['phpgw']->acl->check('run',1,'admin'))
408        {
409                /* Don't use a global variable for this ... */
410                define('HAS_ADMIN_RIGHTS',1);
411        }
412
413        /* Makes the ifs a little nicer, plus ... this will change once the ACL manager is in place */
414        /* and is able to create less powerfull admins.  This will handle the ACL checks for that (jengo) */
415        function is_admin()
416        {
417                global $prefix;
418
419                if (HAS_ADMIN_RIGHTS == 1 && empty($prefix))    // tabs only without prefix
420                {
421                        return True;
422                }
423                else
424                {
425                        return False;
426                }
427        }
428       
429        function show_list($header = '&nbsp;')
430        {
431                global $t,$list_shown;
432
433                $t->set_var('list_header',$header);
434                $t->parse('lists','list',$list_shown);
435
436                $t->set_var('rows','');
437                $list_shown = True;
438        }
439
440        $session_data = $GLOBALS['phpgw']->session->appsession('session_data','preferences');
441
442        $prefix = get_var('prefix',array('GET'),$session_data['appname'] == $_GET['appname'] ? $session_data['prefix'] : '');
443       
444        if (is_admin())
445        {
446                /* This is where we will keep track of our postion. */
447                /* Developers won't have to pass around a variable then */
448
449                $GLOBALS['type'] = get_var('type',Array('GET','POST'),$session_data['type']);
450
451                if (empty($GLOBALS['type']))
452                {
453                        $GLOBALS['type'] = 'user';
454                }
455        }
456        else
457        {
458                $GLOBALS['type'] = 'user';
459        }
460        $show_help = "$session_data[show_help]" != '' && $session_data['appname'] == $_GET['appname'] ?
461                $session_data['show_help'] : (int)$GLOBALS['phpgw_info']['user']['preferences']['common']['show_help'];
462
463        if ($toggle_help = get_var('toggle_help','POST'))
464        {
465                $show_help = (int)(!$show_help);
466        }
467        $has_help = 0;
468
469        if ($_POST['submit'])
470        {
471                /* Don't use a switch here, we need to check some permissions durring the ifs */
472                if ($GLOBALS['type'] == 'user' || !($GLOBALS['type']))
473                {
474                        $error = process_array($GLOBALS['phpgw']->preferences->user,$user,$session_data['notifys'],$prefix);
475                }
476
477                if ($GLOBALS['type'] == 'default' && is_admin())
478                {
479                        $error = process_array($GLOBALS['phpgw']->preferences->default, $default,$session_data['notifys']);
480                }
481
482                if ($GLOBALS['type'] == 'forced' && is_admin())
483                {
484                        $error = process_array($GLOBALS['phpgw']->preferences->forced, $forced,$session_data['notifys']);
485                }
486
487                if (!is_admin() || $error)
488                {
489                        $GLOBALS['phpgw']->redirect_link('/'.$_GET['appname'].'/');
490                }
491               
492                if ($GLOBALS['type'] == 'user' && $_GET['appname'] == 'preferences' && $user['show_help'] != '')
493                {
494                        $show_help = $user['show_help'];        // use it, if admin changes his help-prefs
495                }
496        }
497        $GLOBALS['phpgw']->session->appsession('session_data','preferences',array(
498                'type'      => $GLOBALS['type'],        // save our state in the app-session
499                'show_help' => $show_help,
500                'prefix'    => $prefix,
501                'appname'   => $_GET['appname']         // we use this to reset prefix on appname-change
502        ));
503        // changes for the admin itself, should have immediate feedback ==> redirect
504        if (!$error && $_POST['submit'] && $GLOBALS['type'] == 'user' && $_GET['appname'] == 'preferences') {
505                $GLOBALS['phpgw']->redirect_link('/preferences/preferences.php','appname='.$_GET['appname']);
506        }
507
508        $GLOBALS['phpgw_info']['flags']['app_header'] = $_GET['appname'] == 'preferences' ?
509                lang('Preferences') : lang('%1 - Preferences',$GLOBALS['phpgw_info']['apps'][$_GET['appname']]['title']);
510        $GLOBALS['phpgw']->common->phpgw_header();
511        echo parse_navbar();
512
513        $t->set_var('messages',$error);
514        $t->set_var('action_url',$GLOBALS['phpgw']->link('/preferences/preferences.php','appname=' . $_GET['appname']));
515        $t->set_var('th_bg',  $GLOBALS['phpgw_info']['theme']['th_bg']);
516        $t->set_var('th_text',$GLOBALS['phpgw_info']['theme']['th_text']);
517        $t->set_var('row_on', $GLOBALS['phpgw_info']['theme']['row_on']);
518        $t->set_var('row_off',$GLOBALS['phpgw_info']['theme']['row_off']);
519
520        switch ($GLOBALS['type'])       // set up some globals to be used by the hooks
521        {
522                case 'forced': 
523                        $prefs = &$GLOBALS['phpgw']->preferences->forced[check_app()];
524                        break;
525                case 'default':
526                        $prefs = &$GLOBALS['phpgw']->preferences->default[check_app()];
527                        break;
528                default:
529                        $prefs = &$GLOBALS['phpgw']->preferences->user[check_app()];
530                        // use prefix if given in the url, used for email extra-accounts
531                        if ($prefix != '')
532                        {
533                                $prefix_arr = explode('/',$prefix);
534                                foreach ($prefix_arr as $pre)
535                                {
536                                        $prefs = &$prefs[$pre];
537                                }
538                        }
539        }
540        //echo "prefs=<pre>"; print_r($prefs); echo "</pre>\n";
541       
542        $notifys = array();
543        if (!$GLOBALS['phpgw']->hooks->single('settings',$_GET['appname']))
544        {
545                $t->set_block('preferences','form','formhandle');       // skip the form
546                $t->set_var('formhandle','');
547               
548                $t->set_var('messages',lang('Error: There was a problem finding the preference file for %1 in %2',
549                        $GLOBALS['phpgw_info']['navbar'][$_GET['appname']]['title'],PHPGW_SERVER_ROOT . SEP
550                        . $_GET['appname'] . SEP . 'inc' . SEP . 'hook_settings.inc.php'));
551        }
552        $tmpl_settings = PHPGW_TEMPLATE_DIR.'/hook_settings.inc.php';
553        if ($_GET['appname'] == 'preferences' && file_exists($tmpl_settings))
554        {
555                include($tmpl_settings);
556        }
557
558        if (count($notifys))    // there have been notifys in the hook, we need to save in the session
559        {
560                $GLOBALS['phpgw']->session->appsession('session_data','preferences',array(
561                        'type'      => $GLOBALS['type'],        // save our state in the app-session
562                        'show_help' => $show_help,
563                        'prefix'    => $prefix,
564                        'appname'   => $_GET['appname'],        // we use this to reset prefix on appname-change
565                        'notifys'   => $notifys
566                ));
567                //echo "notifys:<pre>"; print_r($notifys); echo "</pre>\n";
568        }
569        if (is_admin())
570        {
571                $tabs[] = array(
572                        'label' => lang('Your preferences'),
573                        'link'  => $GLOBALS['phpgw']->link('/preferences/preferences.php','appname=' . $_GET['appname'] . "&type=user")
574                );
575                $tabs[] = array(
576                        'label' => lang('Default preferences'),
577                        'link'  => $GLOBALS['phpgw']->link('/preferences/preferences.php','appname=' . $_GET['appname'] . "&type=default")
578                );
579                $tabs[] = array(
580                        'label' => lang('Forced preferences'),
581                        'link'  => $GLOBALS['phpgw']->link('/preferences/preferences.php','appname=' . $_GET['appname'] . "&type=forced")
582                );
583
584                switch($GLOBALS['type'])
585                {
586                        case 'user':    $selected = 0; break;
587                        case 'default': $selected = 1; break;
588                        case 'forced':  $selected = 2; break;
589                }
590                $t->set_var('tabs',$GLOBALS['phpgw']->common->create_tabs($tabs,$selected));
591        }
592        $t->set_var('lang_submit', lang('save'));
593        $t->set_var('lang_cancel', lang('cancel'));
594        $t->set_var('show_help',(int)$show_help);
595        $t->set_var('help_button',$has_help ? '<input type="submit" name="toggle_help" value="'.
596                ($show_help ? lang('help off') : lang('help')).'">' : '');
597
598        if (!$list_shown)
599        {
600                show_list();
601        }
602        $t->pfp('phpgw_body','preferences');
603       
604        //echo '<pre style="text-align: left;">'; print_r($GLOBALS['phpgw']->preferences->data); echo "</pre>\n";
605       
606        $GLOBALS['phpgw']->common->phpgw_footer();
607?>
Note: See TracBrowser for help on using the repository browser.