3
* Copyright Netvibes 2006-2009.
4
* This file is part of Exposition PHP Lib.
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.
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.
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/>.
24
class Widget_Preference
48
* Preference default value.
52
protected $_defaultValue;
55
* Preference onchange callback.
59
protected $_onchangeCallback;
62
* Preference options for 'list' type.
66
protected $_listOptions;
69
* Preference options for 'range' type.
73
protected $_rangeOptions;
76
* Preference constructor.
80
public function __construct($type)
82
if (in_array((string) $type, array('text', 'password', 'boolean', 'hidden', 'range', 'list', 'color', 'textarea'))) {
83
$this->_type = (string) $type;
85
$this->_type = 'text';
90
* Returns the preference type.
94
public function getType()
100
* Sets the preference name.
102
* @param string $name
104
public function setName($name)
106
$this->_name = (string) $name;
110
* Returns the preference name.
114
public function getName()
120
* Sets the preference label.
122
* @param string $label
124
public function setLabel($label)
126
$this->_label = (string) $label;
130
* Returns the preference label.
134
public function getLabel()
136
return $this->_label;
140
* Sets the preference default value.
142
* @param string $defaultValue
144
public function setDefaultValue($defaultValue)
146
$this->_defaultValue = (string) $defaultValue;
150
* Returns the preference default value.
154
public function getDefaultValue()
156
return $this->_defaultValue;
160
* Sets the preference 'onchange' callback function name.
162
* @param string $onchange
164
public function setOnchangeCallback($onchange)
166
$this->_onchangeCallback = (string) $onchange;
170
* Returns the preference 'onchange' callback function name.
174
public function getOnchangeCallback()
176
return $this->_onchangeCallback;
180
* Sets the preference list options.
182
* @param array $options
184
public function setListOptions(array $options)
186
$this->_listOptions = $options;
190
* Add a new option to the list.
192
* @param string $value
193
* @param string $label
195
public function addListOption($value, $label = '')
197
if (!is_array($this->_listOptions)) {
198
$this->_listOptions = array();
203
$this->_listOptions[(string) $label] = $value;
207
* Returns the preference list options.
211
public function getListOptions()
213
return $this->_listOptions;
217
* Sets the preference range options.
223
public function setRangeOptions($step, $min, $max)
225
$this->_rangeOptions = array('step' => (int) $step,
227
'max' => (int) $max);
231
* Returns the preference range options.
235
public function getRangeOptions()
237
return $this->_rangeOptions;
241
* Retrieves the preferences attributes in an array.
245
public function toArray()
247
$preference = array();
248
$preference['type'] = $this->_type;
249
if (!empty($this->_name)) {
250
$preference['name'] = $this->_name;
252
if (!empty($this->_label)) {
253
$preference['label'] = $this->_label;
255
if (isset($this->_defaultValue)) {
256
$preference['defaultValue'] = $this->_defaultValue;
258
if (!empty($this->_onchangeCallback)) {
259
$preference['onchange'] = $this->_onchangeCallback;
261
if (!empty($this->_listOptions)) {
263
foreach ($this->_listOptions as $label => $value) {
264
$options[] = array('label' => $label, 'value' => $value);
266
$preference['options'] = $options;
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'];