File: UI.php

package info (click to toggle)
horde2 2.2.8-1sarge3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,832 kB
  • ctags: 2,897
  • sloc: php: 12,784; sh: 954; sql: 149; makefile: 104; perl: 97; xml: 24; pascal: 6
file content (205 lines) | stat: -rw-r--r-- 7,279 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
 * Class for auto-generating the preferences user interface and
 * processing the forms.
 *
 *
 * $Horde: horde/lib/Prefs/UI.php,v 1.2.2.8 2003/02/18 00:07:56 jan Exp $
 *
 * Copyright 2002-2003 Chuck Hagenbuch <chuck@horde.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  Chuck Hagenbuch <chuck@horde.org>
 * @version $Revision: 1.2.2.8 $
 * @since   Horde 2.1
 * @package horde.prefs
 */
class PrefsUI {

    /**
     * Determine whether or not a preferences group is editable.
     *
     * @access public
     *
     * @param string $group  The preferences group to check.
     *
     * @return boolean  Whether or not the group is editable.
     */
    function groupIsEditable($group)
    {
        global $prefs, $prefGroups;
        static $results;

        if (!isset($results)) {
            $results = array();
        }

        if (!array_key_exists($group, $results)) {
            $results[$group] = false;
            foreach ($prefGroups[$group]['members'] as $pref) {
                if (!$prefs->isLocked($pref)) {
                    $results[$group] = true;
                    return true;
                }
            }
        } else {
            return $results[$group];
        }

        return false;
    }

    /**
     * Handle a preferences form submission if there is one, updating
     * any preferences which have been changed.
     *
     * @param optional string $group  The preferences group that was edited.
     *
     * @return boolean  Whether preferences have been updated.
     */
    function handleForm($group = null)
    {
        global $prefs, $prefGroups, $_prefs, $notification, $registry;

        $updated = false;

        /* Run through the action handlers */
        $actionID = Horde::getFormData('actionID', NOOP);
        switch ($actionID) {

        case HORDE_UPDATE_PREFS:
            if (isset($group) && PrefsUI::groupIsEditable($group)) {
                $updated = false;

                foreach ($prefGroups[$group]['members'] as $pref) {
                    if (!$prefs->isLocked($pref) ||
                        ($_prefs[$pref]['type'] == 'special')) {
                        switch ($_prefs[$pref]['type']) {

                        /* These either aren't set or are set in other parts
                           of the UI. */
                        case 'implicit':
                        case 'link':
                            break;

                        case 'select':
                        case 'text':
                        case 'textarea':
                            $prefs->setValue($pref, Horde::getFormData($pref));
                            $updated = true;
                            break;

                        case 'enum':
                            $val = Horde::getFormData($pref);
                            if (isset($_prefs[$pref]['enum'][$val])) {
                                $prefs->setValue($pref, $val);
                                $updated = true;
                            } else {
                                $notification->push(_("An illegal value was specified."), 'horde.error');
                            }
                            break;

                        case 'number':
                            $num = Horde::getFormData($pref);
                            if (intval($num) != $num) {
                                $notification->push(_("This value must be a number."), 'horde.error');
                            } elseif ($num == 0) {
                                $notification->push(_("This number must be at least one."), 'horde.error');
                            } else {
                                $prefs->setValue($pref, $num);
                                $updated = true;
                            }
                            break;

                        case 'checkbox':
                            $val = Horde::getFormData($pref);
                            $prefs->setValue($pref, isset($val) ? 1 : 0);
                            $updated = true;
                            break;

                        case 'special':
                            /* Code for special elements must be written
                               specifically for each application. */
                            if (function_exists('handle_' . $pref)) {
                                $updated = call_user_func('handle_' . $pref, $updated);
                            }
                            break;

                        }
                    }
                }

                // Do anything that you need to do as a result of certain
                // preferences changing.
                if ($prefs->isDirty('language')) {
                    Lang::setLang($prefs->getValue('language'));
                    Lang::setTextdomain($registry->getApp(), $registry->getParam('fileroot') . '/locale', Lang::getCharset());
                }

                if ($updated) {
                    if (function_exists('prefs_callback')) {
                        prefs_callback();
                    }
                    $prefs->store();
                    $notification->push(_("Your options have been updated."), 'horde.message');
                    $group = null;
                }
            }
            break;
        }

        return $updated;
    }

    /**
     * Generate the UI for the preferences interface, either for a
     * specific group, or the group selection interface.
     *
     * @param optional string $group  The group to generate the UI for.
     */
    function generateUI($group = null)
    {
        global $prefs, $prefGroups, $_prefs, $registry;

        /* Assign variables to hold select lists. */
        if (!$prefs->isLocked('language')) {
            $GLOBALS['language_options'] = &$GLOBALS['nls']['languages'];
        }

        if (!empty($group) &&
            PrefsUI::groupIsEditable($group)) {
            include $registry->getParam('templates', 'horde') . '/prefs/begin.inc';
            foreach ($prefGroups[$group]['members'] as $pref) {
                if (!$prefs->isLocked($pref)) {
                    switch ($_prefs[$pref]['type']) {

                    case 'implicit':
                        break;

                    case 'special':
                        include $registry->getParam('templates', $registry->getApp()) . "/prefs/$pref.inc";
                        break;

                    default:
                        include $registry->getParam('templates', 'horde') . '/prefs/' . $_prefs[$pref]['type'] . '.inc';
                        break;

                    }
                }
            }
            include $registry->getParam('templates', 'horde') . '/prefs/end.inc';
        } else {
            $columns = array();
            foreach ($prefGroups as $group => $gvals) {
                $col = $gvals['column'];
                unset($gvals['column']);
                $columns[$col][$group] = $gvals;
            }
            $span = round(100 / count($columns));
            include $registry->getParam('templates', 'horde') . '/prefs/overview.inc';
        }
    }

}