1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Checkboxes 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
|
|
|
namespace ntentan\honam\engines\php\helpers\form; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* A regular checkbox with a label. |
36
|
|
|
*/ |
37
|
|
|
class CheckableField extends Field |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* The value that this field should contain if this checkbox is checked. |
41
|
|
|
*/ |
42
|
|
|
protected $checkedValue; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor for the checkbox. |
46
|
|
|
* |
47
|
|
|
* @param string $label The label of the checkbox. |
48
|
|
|
* @param string $name The name of the checkbox used for the name='' attribute of the HTML output |
49
|
|
|
* @param string $value A value to assign to this checkbox. |
50
|
|
|
* @param string $description A description of the field. |
51
|
|
|
*/ |
52
|
1 |
|
public function __construct($label="", $name="", $value="", $description="") |
53
|
|
|
{ |
54
|
1 |
|
Element::__construct($label, $description); |
55
|
1 |
|
parent::__construct($name); |
56
|
1 |
|
$this->setCheckedValue($value); |
57
|
1 |
|
$this->setRenderLabel(false); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sets the value that should be assigned as the checked value for |
62
|
|
|
* this check box. |
63
|
|
|
* @param string $checkedValue |
64
|
|
|
* @return CheckableField |
65
|
|
|
*/ |
66
|
1 |
|
public function setCheckedValue($checkedValue) |
67
|
|
|
{ |
68
|
1 |
|
$this->checkedValue = $checkedValue; |
69
|
1 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Gets and returns the checkedValue for the check box. |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
1 |
|
public function getCheckedValue() |
77
|
|
|
{ |
78
|
1 |
|
return $this->checkedValue; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public function render() |
82
|
|
|
{ |
83
|
1 |
|
if($this->getCheckedValue() == (string)$this->getValue()) |
84
|
|
|
{ |
85
|
1 |
|
$this->setAttribute('checked', 'checked'); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
$this->setAttribute("name", $this->getName()); |
89
|
1 |
|
$this->setAttribute("class", "{$this->getAttribute('type')} {$this->getCSSClasses()}"); |
90
|
1 |
|
$this->setValue($this->getCheckedValue()); |
91
|
1 |
|
return $this->templateRenderer->render('input_checkable_element.tpl.php', array('element' => $this)); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|