Completed
Push — dev ( 8a2d43...215a79 )
by James Ekow Abaka
08:03
created

CheckableField::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
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\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
    public function __construct($label="", $name="", $value="", $description="")
53
    {
54
        Element::__construct($label, $description);
55
        parent::__construct($name);
56
        $this->setCheckedValue($value);
57
        $this->setRenderLabel(false);
58
    }
59
60
    /**
61
     * Sets the value that should be assigned as the checked value for
62
     * this check box.
63
     * @param $checkedValue The value to be assigned.
64
     * @return Checkbox
65
     */
66
    public function setCheckedValue($checkedValue)
67
    {
68
        $this->checkedValue = $checkedValue;
69
        return $this;
70
    }
71
72
    /**
73
     * Gets and returns the checkedValue for the check box.
74
     * @return string
75
     */
76
    public function getCheckedValue()
77
    {
78
        return $this->checkedValue;
79
    }
80
81
    public function render()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
82
    {
83
        if($this->getCheckedValue() == (string)$this->getValue())
84
        {
85
            $this->setAttribute('checked', 'checked');
86
        }
87
        
88
        $this->setAttribute("name", $this->getName());
89
        $this->setAttribute("class", "{$this->getAttribute('type')} {$this->getCSSClasses()}");        
90
        $this->setValue($this->getCheckedValue());
91
        return $this->templateRenderer->render('input_checkable_element.tpl.php', array('element' => $this));
92
    }
93
}
94
95