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 <dev@alphaframework.org>
17: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
18: * @copyright Copyright (c) 2015, 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;
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: $config = ConfigProvider::getInstance();
123:
124: $html = '';
125:
126: /*
127: * decide on the size of the text box and the height of the widget pop-up,
128: * depending on the dateObject type
129: */
130: if (mb_strtoupper(get_class($this->dateObject)) == 'TIMESTAMP') {
131: $size = 18;
132: $cal_height = 230;
133: } else {
134: $size = 10;
135: $cal_height = 230;
136: }
137:
138: $value = $this->dateObject->getValue();
139:
140: if ($value == '0000-00-00') {
141: $value = '';
142: }
143:
144: $html = '<div class="form-group">';
145: $html .= ' <label for="'.$this->name.'">'.$this->label.'</label>';
146: $html .= ' <div class="input-group date">';
147: $html .= ' <input type="text" class="form-control" name="'.$this->name.'" id="'.$this->name.'" value="'.$value.'" readonly/>';
148: $html .= ' <span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>';
149: $html .= ' </div>';
150: $html .= '</div>';
151:
152: $html .= '<script language="javascript">';
153: $html .= 'if(window.jQuery) {';
154: $html .= ' $(\'[Id="'.$this->name.'"]\').parent().datepicker({';
155: $html .= ' format: "yyyy-mm-dd",';
156: $html .= ' todayBtn: "linked",';
157: $html .= ' todayHighlight: true,';
158: $html .= ' autoclose: true';
159: $html .= ' });';
160: $html .= '}';
161: $html .= '</script>';
162:
163: return $html;
164: }
165: }
166: