source: branches/1.2/preferences/preferences.php @ 43

Revision 43, 17.3 KB checked in by niltonneto, 17 years ago (diff)

Vide change_log.txt

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