DateBox::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Alpha\View\Widget;
4
5
use Alpha\Exception\IllegalArguementException;
6
use Alpha\Model\Type\Date;
7
use Alpha\Model\Type\Timestamp;
8
use Alpha\Util\Security\SecurityUtils;
9
use Alpha\Util\Config\ConfigProvider;
10
11
/**
12
 * A HTML widget for rendering a text box with calendar icon for Date/Timestamp types.
13
 *
14
 * @since 1.0
15
 *
16
 * @author John Collins <[email protected]>
17
 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
18
 * @copyright Copyright (c) 2018, John Collins (founder of Alpha Framework).
19
 * All rights reserved.
20
 *
21
 * <pre>
22
 * Redistribution and use in source and binary forms, with or
23
 * without modification, are permitted provided that the
24
 * following conditions are met:
25
 *
26
 * * Redistributions of source code must retain the above
27
 *   copyright notice, this list of conditions and the
28
 *   following disclaimer.
29
 * * Redistributions in binary form must reproduce the above
30
 *   copyright notice, this list of conditions and the
31
 *   following disclaimer in the documentation and/or other
32
 *   materials provided with the distribution.
33
 * * Neither the name of the Alpha Framework nor the names
34
 *   of its contributors may be used to endorse or promote
35
 *   products derived from this software without specific
36
 *   prior written permission.
37
 *
38
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
39
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
40
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
41
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
43
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
49
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51
 * </pre>
52
 */
53
class DateBox
54
{
55
    /**
56
     * The date or timestamp object for the widget.
57
     *
58
     * @var \Alpha\Model\Type\Date or Alpha\Model\Type\Timestamp
59
     *
60
     * @since 1.0
61
     */
62
    private $dateObject = null;
63
64
    /**
65
     * The data label for the object.
66
     *
67
     * @var string
68
     *
69
     * @since 1.0
70
     */
71
    private $label;
72
73
    /**
74
     * The name of the HTML input box.
75
     *
76
     * @var string
77
     *
78
     * @since 1.0
79
     */
80
    private $name;
81
82
    /**
83
     * The constructor.
84
     *
85
     * @param \Alpha\Model\Type\Date or Alpha\Model\Type\Timestamp $object The date or timestamp object that will be edited by this widget.
86
     * @param string                                              $label  The data label for the object.
87
     * @param string                                              $name   The name of the HTML input box.
88
     *
89
     * @since 1.0
90
     *
91
     * @throws \Alpha\Exception\IllegalArguementException
92
     */
93
    public function __construct($object, $label = '', $name = '')
94
    {
95
        $config = ConfigProvider::getInstance();
96
97
        // check the type of the object passed
98
        if ($object instanceof Date || $object instanceof Timestamp) {
99
            $this->dateObject = $object;
0 ignored issues
show
Documentation Bug introduced by
It seems like $object can also be of type object<Alpha\Model\Type\Timestamp>. However, the property $dateObject is declared as type object<Alpha\Model\Type\Date>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
100
        } else {
101
            throw new IllegalArguementException('DateBox widget can only accept a Date or Timestamp object!');
102
        }
103
104
        $this->label = $label;
105
106
        if ($config->get('security.encrypt.http.fieldnames')) {
107
            $this->name = base64_encode(SecurityUtils::encrypt($name));
108
        } else {
109
            $this->name = $name;
110
        }
111
    }
112
113
    /**
114
     * Renders the text box and icon to open the calendar pop-up.
115
     *
116
     * @return string
117
     *
118
     * @since 1.0
119
     */
120
    public function render()
121
    {
122
        $value = $this->dateObject->getValue();
123
124
        if ($value == '0000-00-00') {
125
            $value = '';
126
        }
127
128
        $html = '<div class="form-group">';
129
        $html .= '  <label for="'.$this->name.'">'.$this->label.'</label>';
130
        $html .= '  <div class="input-group date">';
131
        $html .= '    <input type="text" class="form-control" name="'.$this->name.'" id="'.$this->name.'" value="'.$value.'" readonly/>';
132
        $html .= '    <span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>';
133
        $html .= '  </div>';
134
        $html .= '</div>';
135
136
        $html .= '<script language="javascript">';
137
        $html .= 'if(window.jQuery) {';
138
        $html .= '  $(\'[Id="'.$this->name.'"]\').parent().datepicker({';
139
        $html .= '      format: "yyyy-mm-dd",';
140
        $html .= '      todayBtn: "linked",';
141
        $html .= '      todayHighlight: true,';
142
        $html .= '      autoclose: true';
143
        $html .= '  });';
144
        $html .= '}';
145
        $html .= '</script>';
146
147
        return $html;
148
    }
149
}
150