1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* The actual widgets, used by the form builder, you can extend replace this to handle your usecase better |
5
|
|
|
* |
6
|
|
|
* @package Jam |
7
|
|
|
* @category Form |
8
|
|
|
* @author Ivan Kerin |
9
|
|
|
* @copyright (c) 2011-2012 Despark Ltd. |
10
|
|
|
* @license http://www.opensource.org/licenses/isc-license.txt |
11
|
|
|
*/ |
12
|
|
|
abstract class Kohana_Jam_Form_General extends Jam_Form { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The template for a single html row |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $_template = '<div class="row :name-field :type-field :with-errors">:label<div class="input"><div class="field-wrapper">:field:errors</div>:help</div></div>'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Getter / setter for the template |
22
|
|
|
* @param string|null $template |
23
|
|
|
* @return Jam_Form|string |
24
|
|
|
*/ |
25
|
1 |
|
public function template($template = NULL) |
26
|
|
|
{ |
27
|
1 |
|
if ($template !== NULL) |
28
|
|
|
{ |
29
|
|
|
$this->_template = $template; |
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
1 |
|
return $this->_template; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Generate a html row with the input field, label, help and error messages |
37
|
|
|
* |
38
|
|
|
* @param string $type the html field (input, hidden, textarea ...) |
39
|
|
|
* @param string $name the name of the attribute of the bound Jam_Model |
40
|
|
|
* @param array $options options for the input field, you can also change the 'label', and add 'help' message |
41
|
|
|
* @param array $attributes html attributes for the input field |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
1 |
|
public function row($type, $name, array $options = array(), array $attributes = array(), $template = NULL) |
45
|
|
|
{ |
46
|
1 |
|
$errors = Arr::get($options, 'errors', $this->errors($name)); |
47
|
|
|
|
48
|
1 |
|
$field = call_user_func(array($this, $type), $name, $options, $attributes); |
49
|
|
|
|
50
|
1 |
|
$help = Arr::get($options, 'help'); |
51
|
|
|
|
52
|
|
|
$slots = array( |
53
|
1 |
|
':name' => $name, |
54
|
1 |
|
':type' => $type, |
55
|
1 |
|
':label' => $this->label($name, Arr::get($options, 'label')), |
56
|
1 |
|
':with-errors' => $errors ? 'with-errors' : '', |
57
|
1 |
|
':errors' => $errors, |
58
|
1 |
|
':help' => $help ? "<span class=\"help-block\">{$help}</span>" : '', |
59
|
1 |
|
':field' => $field, |
60
|
|
|
); |
61
|
|
|
|
62
|
1 |
|
return strtr($template ? $template : $this->template(), $slots); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return the html for the errors for a given field |
67
|
|
|
* @param string $name |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
1 |
|
public function errors($name) |
71
|
|
|
{ |
72
|
1 |
|
$errors = join(', ', Arr::flatten( (array) $this->object()->errors($name))); |
73
|
1 |
|
return $errors ? "<span class=\"form-error\">{$errors}</span>" : ''; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Return an html label linked to a given input field |
78
|
|
|
* @param string $name the name of the Jam_Model attribute |
79
|
|
|
* @param string $label the text of the label, can be autogenerated if NULL |
80
|
|
|
* @param array $attributes of the input field, extract the id from there |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
2 |
|
public function label($name, $label = NULL, array $attributes = array()) |
84
|
|
|
{ |
85
|
2 |
|
$field_attributes = $this->default_attributes($name, $attributes); |
86
|
|
|
|
87
|
2 |
|
if ($label === NULL) |
88
|
|
|
{ |
89
|
2 |
|
if ($this->meta()->field($name) AND $this->meta()->field($name)->label !== NULL) |
90
|
|
|
{ |
91
|
2 |
|
$label = UTF8::ucfirst($this->meta()->field($name)->label); |
92
|
|
|
} |
93
|
|
|
else |
94
|
|
|
{ |
95
|
|
|
$label = UTF8::ucfirst(Inflector::humanize($name)); |
96
|
|
|
} |
97
|
|
|
} |
98
|
2 |
|
return Form::label($field_attributes['id'], $label, $attributes); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* HTML input text field |
103
|
|
|
* |
104
|
|
|
* @param string $name the name of the Jam_Model attribute |
105
|
|
|
* @param array $options Not Used - for compatibility |
106
|
|
|
* @param array $attributes HTML attributes for the field |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
2 |
|
public function input($name, array $options = array(), array $attributes = array()) |
|
|
|
|
110
|
|
|
{ |
111
|
2 |
|
$attributes = $this->default_attributes($name, $attributes); |
112
|
|
|
|
113
|
2 |
|
return Form::input($attributes['name'], $this->object()->$name, $attributes); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* HTML input hidden field |
118
|
|
|
* |
119
|
|
|
* @param string $name the name of the Jam_Model attribute |
120
|
|
|
* @param array $options Not Used - for compatibility |
121
|
|
|
* @param array $attributes HTML attributes for the field |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
1 |
|
public function hidden($name, array $options = array(), array $attributes = array()) |
|
|
|
|
125
|
|
|
{ |
126
|
1 |
|
$attributes = $this->default_attributes($name, $attributes); |
127
|
|
|
|
128
|
1 |
|
return Form::hidden($attributes['name'], Jam_Form::list_id($this->object()->$name), $attributes); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* HTML input hidden field for multiple values, renders all the nesessary hidden fields |
134
|
|
|
* @param string $name |
135
|
|
|
* @param array $options Not Used - for compatibility |
136
|
|
|
* @param array $attributes HTML attributes for all the fields |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function hidden_list($name, array $options = array(), array $attributes = array()) |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$attributes = $this->default_attributes($name, $attributes); |
142
|
|
|
$ids = Jam_Form::list_id($this->object()->$name); |
143
|
|
|
$html = ''; |
144
|
|
|
foreach ($ids as $index => $id) |
145
|
|
|
{ |
146
|
|
|
$html .= Form::hidden($attributes['name']."[$index]", $id, $attributes); |
147
|
|
|
} |
148
|
|
|
return $html; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* HTML input checkbox field |
153
|
|
|
* |
154
|
|
|
* @param string $name the name of the Jam_Model attribute |
155
|
|
|
* @param array $options can change the value of the checkbox with 'value', defaults to 1 |
156
|
|
|
* @param array $attributes HTML attributes for the field |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
1 |
|
public function checkbox($name, array $options = array(), array $attributes = array()) |
160
|
|
|
{ |
161
|
1 |
|
$attributes = $this->default_attributes($name, $attributes); |
162
|
|
|
|
163
|
1 |
|
$value = Arr::get($options, 'value', 1); |
164
|
1 |
|
$empty = Arr::get($options, 'empty', 0); |
165
|
1 |
|
$disabled = (in_array('disabled', $attributes) OR isset($attributes['disabled'])); |
166
|
|
|
|
167
|
|
|
return |
168
|
1 |
|
Form::hidden($attributes['name'], $empty, $disabled ? array('disabled') : array()) |
169
|
1 |
|
.Form::checkbox($attributes['name'], $value, $this->object()->$name == $value, $attributes); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* HTML input radio field |
174
|
|
|
* |
175
|
|
|
* @param string $name the name of the Jam_Model attribute |
176
|
|
|
* @param array $options must provide the value of the radio with 'value' |
177
|
|
|
* @param array $attributes HTML attributes for the field |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
1 |
|
public function radio($name, array $options = array(), array $attributes = array()) |
181
|
|
|
{ |
182
|
1 |
|
$value = Arr::get($options, 'value', ''); |
183
|
|
|
|
184
|
1 |
|
if ( ! isset($attributes['id'])) |
185
|
|
|
{ |
186
|
1 |
|
$attributes['id'] = $this->default_id($name).'_'.URL::title($value); |
187
|
|
|
} |
188
|
|
|
|
189
|
1 |
|
$attributes = $this->default_attributes($name, $attributes); |
190
|
|
|
|
191
|
1 |
|
return Form::radio($attributes['name'], $value, Jam_Form::list_id($this->object()->$name, TRUE) == Jam_Form::list_id($value), $attributes); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function checkboxes($name, array $options = array(), array $attributes = array()) |
195
|
|
|
{ |
196
|
|
|
$attributes = $this->default_attributes($name, $attributes); |
197
|
|
|
|
198
|
|
|
if ( ! isset($options['choices'])) |
199
|
|
|
throw new Kohana_Exception("Checkboxes tag widget requires a 'choices' option"); |
200
|
|
|
|
201
|
|
|
$choices = Jam_Form::list_choices($options['choices']); |
202
|
|
|
$values = Arr::get($options, 'value', (array) Jam_Form::list_choices($this->object()->$name)); |
203
|
|
|
$html = ''; |
204
|
|
|
|
205
|
|
|
foreach ($choices as $key => $title) |
206
|
|
|
{ |
207
|
|
|
$id = $attributes['id'].'_'.$key; |
208
|
|
|
$html .= '<li>' |
209
|
|
|
.Form::label($id, Form::checkbox($attributes['name']."[]", $key, array_key_exists($key, $values), array("id" => $id))."<span>$title</span>") |
210
|
|
|
.'</li>'; |
211
|
|
|
} |
212
|
|
|
return "<ul ".HTML::attributes($attributes).">$html</ul>"; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function radios($name, array $options = array(), array $attributes = array()) |
216
|
|
|
{ |
217
|
|
|
$attributes = $this->default_attributes($name, $attributes); |
218
|
|
|
|
219
|
|
|
if ( ! isset($options['choices'])) |
220
|
|
|
throw new Kohana_Exception("Radios tag widget requires a 'choices' option"); |
221
|
|
|
|
222
|
|
|
$choices = Jam_Form::list_choices($options['choices']); |
223
|
|
|
|
224
|
|
|
$radios = array(); |
225
|
|
|
|
226
|
|
|
foreach ($choices as $key => $title) |
227
|
|
|
{ |
228
|
|
|
$id = $attributes['id'].'_'.$key; |
229
|
|
|
$radios[] = |
230
|
|
|
'<li>' |
231
|
|
|
.$this->radio($name, array('value' => $key), array('id' => $id)) |
232
|
|
|
.$this->label($name, $title, array('id' => $id)) |
233
|
|
|
.'</li>'; |
234
|
|
|
} |
235
|
|
|
return "<ul ".HTML::attributes($attributes).">".join("\n", $radios)."</ul>"; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* HTML input file field |
240
|
|
|
* |
241
|
|
|
* @param string $name the name of the Jam_Model attribute |
242
|
|
|
* @param array $options temp_source = TRUE, set this to add a spection hidden input to preserve the file upload on fiald validation |
243
|
|
|
* @param array $attributes HTML attributes for the field |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
1 |
|
public function file($name, array $options = array(), array $attributes = array()) |
247
|
|
|
{ |
248
|
1 |
|
$attributes = $this->default_attributes($name, $attributes); |
249
|
1 |
|
$disabled = Arr::get($attributes, 'disabled', NULL); |
250
|
|
|
|
251
|
|
|
return |
252
|
1 |
|
Form::file($attributes['name'], $attributes) |
253
|
1 |
|
.(Arr::get($options, 'temp_source', FALSE) |
254
|
|
|
? Form::hidden($attributes['name'], $this->object()->$name->temp_source(), Arr::get($options, 'temp_attributes', array('class' => 'hidden-input', 'disabled' => $disabled))) |
255
|
1 |
|
: '' |
256
|
|
|
); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* HTML input password field |
261
|
|
|
* @param string $name the name of the Jam_Model attribute |
262
|
|
|
* @param array $options Not Used - for compatibility |
263
|
|
|
* @param array $attributes HTML attributes for the field |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
|
|
public function password($name, array $options = array(), array $attributes = array()) |
|
|
|
|
267
|
|
|
{ |
268
|
|
|
$attributes = $this->default_attributes($name, $attributes); |
269
|
|
|
|
270
|
|
|
return Form::password($attributes['name'], '', $attributes); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* HTML input textarea field |
276
|
|
|
* |
277
|
|
|
* @param string $name the name of the Jam_Model attribute |
278
|
|
|
* @param array $options Not Used - for compatibility |
279
|
|
|
* @param array $attributes HTML attributes for the field |
280
|
|
|
* @return string |
281
|
|
|
*/ |
282
|
1 |
|
public function textarea($name, array $options = array(), array $attributes = array()) |
|
|
|
|
283
|
|
|
{ |
284
|
1 |
|
$attributes = $this->default_attributes($name, $attributes); |
285
|
|
|
|
286
|
1 |
|
return Form::textarea($attributes['name'], $this->object()->$name, $attributes); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* HTML input select field |
291
|
|
|
* available options |
292
|
|
|
* - choices - this can be Jam_Query_Builder_Collection or a simple array |
293
|
|
|
* - include_blank - bool|string - include an empty option |
294
|
|
|
* |
295
|
|
|
* @param string $name the name of the Jam_Model attribute |
296
|
|
|
* @param array $options set the options of the select with 'choices' and 'include_blank' |
297
|
|
|
* @param array $attributes HTML attributes for the field |
298
|
|
|
* @return string |
299
|
|
|
*/ |
300
|
1 |
|
public function select($name, array $options = array(), array $attributes = array()) |
301
|
|
|
{ |
302
|
1 |
|
$attributes = $this->default_attributes($name, $attributes); |
303
|
|
|
|
304
|
1 |
|
if ( ! isset($options['choices'])) |
305
|
|
|
throw new Kohana_Exception("Select tag widget requires a 'choices' option"); |
306
|
|
|
|
307
|
1 |
|
$choices = Jam_Form::list_choices($options['choices']); |
308
|
|
|
|
309
|
1 |
|
if ($blank = Arr::get($options, 'include_blank')) |
310
|
|
|
{ |
311
|
1 |
|
Arr::unshift($choices, '', ($blank === TRUE) ? " -- Select -- " : $blank); |
312
|
|
|
} |
313
|
|
|
|
314
|
1 |
|
if ($additional = Arr::get($options, 'additional')) |
315
|
|
|
{ |
316
|
|
|
$choices = Arr::merge($choices, $additional); |
317
|
|
|
} |
318
|
|
|
|
319
|
1 |
|
$selected = Jam_Form::list_id($this->object()->$name); |
320
|
|
|
|
321
|
1 |
|
return Form::select($attributes['name'], $choices, $selected, $attributes); |
322
|
|
|
} |
323
|
|
|
} // End Kohana_Jam_Field |
324
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.