1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Selection Lists for forms |
4
|
|
|
* |
5
|
|
|
* Ntentan Framework |
6
|
|
|
* Copyright (c) 2008-2012 James Ekow Abaka Ainooson |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
9
|
|
|
* a copy of this software and associated documentation files (the |
10
|
|
|
* "Software"), to deal in the Software without restriction, including |
11
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
12
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
13
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
14
|
|
|
* the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be |
17
|
|
|
* included in all copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
20
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
21
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
22
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
23
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
24
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
25
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
26
|
|
|
* |
27
|
|
|
* @author James Ainooson <[email protected]> |
28
|
|
|
* @copyright Copyright 2010 James Ekow Abaka Ainooson |
29
|
|
|
* @license MIT |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
namespace ntentan\honam\helpers\form; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* A selection list class for the forms helper. This class renders an HTML |
37
|
|
|
* select form object with its associated options. |
38
|
|
|
*/ |
39
|
|
|
class SelectionList extends Field |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* An array of options to display with this selection list |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $options = array(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* When set true, this selection list would allow multiple selections |
49
|
|
|
* @var boolean |
50
|
|
|
*/ |
51
|
|
|
protected $multiple; |
52
|
|
|
|
53
|
|
|
protected $default; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constructs a new selection list. This constructor could be invoked through |
57
|
|
|
* the form helper's $this->form->get_* method as $this->form->get_selection_list(). |
58
|
|
|
* |
59
|
|
|
* @param string $label The label for the selection list |
60
|
|
|
* @param string $name The name of the selection list |
61
|
|
|
* @param string $description A brief description for the selection list |
62
|
|
|
*/ |
63
|
1 |
|
public function __construct($label="", $name="", $description="") |
64
|
|
|
{ |
65
|
1 |
|
Field::__construct($name); |
66
|
1 |
|
Element::__construct($label, $description); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Sets whether multiple selections are allowed. This method automatically |
71
|
|
|
* appends the array symbol '[]' to the name of the selection list object. |
72
|
|
|
* @param boolean $multiple |
73
|
|
|
* @return SelectionList |
74
|
|
|
*/ |
75
|
1 |
|
public function setMultiple($multiple) |
76
|
|
|
{ |
77
|
1 |
|
$this->name.="[]"; |
78
|
1 |
|
$this->multiple = $multiple; |
79
|
1 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Add an option to the selection list. |
84
|
|
|
* @param string $label |
85
|
|
|
* @param string $value |
86
|
|
|
* @return SelectionList |
87
|
|
|
*/ |
88
|
1 |
|
public function addOption($label="", $value="") |
89
|
|
|
{ |
90
|
1 |
|
if($value==="") $value=$label; |
91
|
1 |
|
$this->options[$value] = $label; |
92
|
1 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* An alias for SelectionList::addOption |
97
|
|
|
* @param string $label |
98
|
|
|
* @param string $value |
99
|
|
|
* @return SelectionList |
100
|
|
|
*/ |
101
|
1 |
|
public function option($label='', $value='') |
102
|
|
|
{ |
103
|
1 |
|
$this->addOption($label, $value); |
104
|
1 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
public function initial($default) |
108
|
|
|
{ |
109
|
1 |
|
$this->default = $default; |
110
|
1 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
public function render() |
114
|
|
|
{ |
115
|
1 |
|
$keys = array_keys($this->options); |
116
|
1 |
|
array_unshift($keys, ''); |
117
|
1 |
|
array_unshift($this->options, $this->default); |
118
|
1 |
|
$this->options = array_combine($keys, $this->options); |
119
|
|
|
|
120
|
1 |
|
$this->setAttribute('name', $this->name); |
121
|
|
|
|
122
|
1 |
|
if($this->multiple) |
123
|
|
|
{ |
124
|
1 |
|
$this->setAttribute('multiple', 'multiple'); |
125
|
|
|
} |
126
|
1 |
|
$this->setAttribute('class', "select {$this->getCSSClasses()}"); |
127
|
|
|
|
128
|
1 |
|
return \ntentan\honam\TemplateEngine::render( |
129
|
1 |
|
'select_element.tpl.php', |
130
|
1 |
|
array('element' => $this) |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Set the options using a key value pair datastructure represented in the form of |
136
|
|
|
* a structured array. |
137
|
|
|
* |
138
|
|
|
* @param array $options An array of options |
139
|
|
|
* |
140
|
|
|
* @return SelectionList |
141
|
|
|
*/ |
142
|
1 |
|
public function setOptions($options = []) |
143
|
|
|
{ |
144
|
1 |
|
if(is_a($options, "\\ntentan\\honam\\template_engines\\php\\Variable")) |
145
|
|
|
{ |
146
|
|
|
$options = $options->unescape(); |
|
|
|
|
147
|
|
|
} |
148
|
1 |
|
$this->options += $options; |
149
|
1 |
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Return the array of options |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
1 |
|
public function getOptions() |
157
|
|
|
{ |
158
|
1 |
|
return $this->options; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.