~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/ecosystem/workspace/ecosystem/uwa-server-original/server/lib/Exposition/Widget/Preference.php

  • Committer: parra
  • Date: 2010-03-15 02:39:02 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1433
merged gelee at svn

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Copyright Netvibes 2006-2009.
 
4
 * This file is part of Exposition PHP Lib.
 
5
 * 
 
6
 * Exposition PHP Lib is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU Lesser General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 * 
 
11
 * Exposition PHP Lib 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 Lesser General Public License for more details.
 
15
 * 
 
16
 * You should have received a copy of the GNU Lesser General Public License
 
17
 * along with Exposition PHP Lib.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
 
 
21
/**
 
22
 * Widget Preference.
 
23
 */
 
24
class Widget_Preference
 
25
{
 
26
    /**
 
27
     * Preference type.
 
28
     *
 
29
     * @var string
 
30
     */
 
31
    protected $_type;
 
32
 
 
33
    /**
 
34
     * Preference name.
 
35
     *
 
36
     * @var string
 
37
     */
 
38
    protected $_name;
 
39
 
 
40
    /**
 
41
     * Preference label.
 
42
     *
 
43
     * @var string
 
44
     */
 
45
    protected $_label;
 
46
 
 
47
    /**
 
48
     * Preference default value.
 
49
     *
 
50
     * @var string
 
51
     */
 
52
    protected $_defaultValue;
 
53
 
 
54
    /**
 
55
     * Preference onchange callback.
 
56
     *
 
57
     * @var string
 
58
     */
 
59
    protected $_onchangeCallback;
 
60
 
 
61
    /**
 
62
     * Preference options for 'list' type.
 
63
     *
 
64
     * @var array
 
65
     */
 
66
    protected $_listOptions;
 
67
 
 
68
    /**
 
69
     * Preference options for 'range' type.
 
70
     *
 
71
     * @var array
 
72
     */
 
73
    protected $_rangeOptions;
 
74
 
 
75
    /**
 
76
     * Preference constructor.
 
77
     *
 
78
     * @param string $type
 
79
     */
 
80
    public function __construct($type)
 
81
    {
 
82
        if (in_array((string) $type, array('text', 'password', 'boolean', 'hidden', 'range', 'list', 'color', 'textarea'))) {
 
83
            $this->_type = (string) $type;
 
84
        } else {
 
85
            $this->_type = 'text';
 
86
        }
 
87
    }
 
88
 
 
89
    /**
 
90
     * Returns the preference type.
 
91
     *
 
92
     * @return string
 
93
     */
 
94
    public function getType()
 
95
    {
 
96
        return $this->_type;
 
97
    }
 
98
 
 
99
    /**
 
100
     * Sets the preference name.
 
101
     *
 
102
     * @param string $name
 
103
     */
 
104
    public function setName($name)
 
105
    {
 
106
        $this->_name = (string) $name;
 
107
    }
 
108
 
 
109
    /**
 
110
     * Returns the preference name.
 
111
     *
 
112
     * @return string
 
113
     */
 
114
    public function getName()
 
115
    {
 
116
        return $this->_name;
 
117
    }
 
118
 
 
119
    /**
 
120
     * Sets the preference label.
 
121
     *
 
122
     * @param string $label
 
123
     */
 
124
    public function setLabel($label)
 
125
    {
 
126
        $this->_label = (string) $label;
 
127
    }
 
128
 
 
129
    /**
 
130
     * Returns the preference label.
 
131
     *
 
132
     * @return string
 
133
     */
 
134
    public function getLabel()
 
135
    {
 
136
        return $this->_label;
 
137
    }
 
138
 
 
139
    /**
 
140
     * Sets the preference default value.
 
141
     *
 
142
     * @param string $defaultValue
 
143
     */
 
144
    public function setDefaultValue($defaultValue)
 
145
    {
 
146
        $this->_defaultValue = (string) $defaultValue;
 
147
    }
 
148
 
 
149
    /**
 
150
     * Returns the preference default value.
 
151
     *
 
152
     * @return string
 
153
     */
 
154
    public function getDefaultValue()
 
155
    {
 
156
        return $this->_defaultValue;
 
157
    }
 
158
 
 
159
    /**
 
160
     * Sets the preference 'onchange' callback function name.
 
161
     *
 
162
     * @param string $onchange
 
163
     */
 
164
    public function setOnchangeCallback($onchange)
 
165
    {
 
166
        $this->_onchangeCallback = (string) $onchange;
 
167
    }
 
168
 
 
169
    /**
 
170
     * Returns the preference 'onchange' callback function name.
 
171
     *
 
172
     * @return string
 
173
     */
 
174
    public function getOnchangeCallback()
 
175
    {
 
176
        return $this->_onchangeCallback;
 
177
    }
 
178
 
 
179
    /**
 
180
     * Sets the preference list options.
 
181
     *
 
182
     * @param array $options
 
183
     */
 
184
    public function setListOptions(array $options)
 
185
    {
 
186
        $this->_listOptions = $options;
 
187
    }
 
188
 
 
189
    /**
 
190
     * Add a new option to the list.
 
191
     *
 
192
     * @param string $value
 
193
     * @param string $label
 
194
     */
 
195
    public function addListOption($value, $label = '')
 
196
    {
 
197
        if (!is_array($this->_listOptions)) {
 
198
            $this->_listOptions = array();
 
199
        }
 
200
        if (empty($label)) {
 
201
            $label = $value;
 
202
        }
 
203
        $this->_listOptions[(string) $label] = $value;
 
204
    }
 
205
 
 
206
    /**
 
207
     * Returns the preference list options.
 
208
     *
 
209
     * @return array
 
210
     */
 
211
    public function getListOptions()
 
212
    {
 
213
        return $this->_listOptions;
 
214
    }
 
215
 
 
216
    /**
 
217
     * Sets the preference range options.
 
218
     *
 
219
     * @param int $step
 
220
     * @param int $min
 
221
     * @param int $max
 
222
     */
 
223
    public function setRangeOptions($step, $min, $max)
 
224
    {
 
225
        $this->_rangeOptions = array('step' => (int) $step,
 
226
                                     'min'  => (int) $min,
 
227
                                     'max'  => (int) $max);
 
228
    }
 
229
 
 
230
    /**
 
231
     * Returns the preference range options.
 
232
     *
 
233
     * @return array
 
234
     */
 
235
    public function getRangeOptions()
 
236
    {
 
237
        return $this->_rangeOptions;
 
238
    }
 
239
 
 
240
    /**
 
241
     * Retrieves the preferences attributes in an array.
 
242
     *
 
243
     * @return array
 
244
     */
 
245
    public function toArray()
 
246
    {
 
247
        $preference = array();
 
248
        $preference['type'] = $this->_type;
 
249
        if (!empty($this->_name)) {
 
250
            $preference['name'] = $this->_name;
 
251
        }
 
252
        if (!empty($this->_label)) {
 
253
            $preference['label'] = $this->_label;
 
254
        }
 
255
        if (isset($this->_defaultValue)) {
 
256
            $preference['defaultValue'] = $this->_defaultValue;
 
257
        }
 
258
        if (!empty($this->_onchangeCallback)) {
 
259
            $preference['onchange'] = $this->_onchangeCallback;
 
260
        }
 
261
        if (!empty($this->_listOptions)) {
 
262
            $options = array();
 
263
            foreach ($this->_listOptions as $label => $value) {
 
264
                $options[] = array('label' => $label, 'value' => $value);
 
265
            }
 
266
            $preference['options'] = $options;
 
267
        }
 
268
        if (!empty($this->_rangeOptions)) {
 
269
            $preference['step'] = (string) $this->_rangeOptions['step'];
 
270
            $preference['min']  = (string) $this->_rangeOptions['min'];
 
271
            $preference['max']  = (string) $this->_rangeOptions['max'];
 
272
        }
 
273
        return $preference;
 
274
    }
 
275
}