1: <?php
2:
3: /**
4: * A HTML widget for rendering a text box with calendar icon for Date/Timestamp types
5: *
6: * @package alpha::view::widgets
7: * @since 1.0
8: * @author John Collins <dev@alphaframework.org>
9: * @version $Id: DateBox.inc 1548 2012-07-29 17:07:07Z alphadevx $
10: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
11: * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
12: * All rights reserved.
13: *
14: * <pre>
15: * Redistribution and use in source and binary forms, with or
16: * without modification, are permitted provided that the
17: * following conditions are met:
18: *
19: * * Redistributions of source code must retain the above
20: * copyright notice, this list of conditions and the
21: * following disclaimer.
22: * * Redistributions in binary form must reproduce the above
23: * copyright notice, this list of conditions and the
24: * following disclaimer in the documentation and/or other
25: * materials provided with the distribution.
26: * * Neither the name of the Alpha Framework nor the names
27: * of its contributors may be used to endorse or promote
28: * products derived from this software without specific
29: * prior written permission.
30: *
31: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
36: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
41: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
42: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44: * </pre>
45: *
46: */
47: class DateBox {
48: /**
49: * The date or timestamp object for the widget.
50: *
51: * @var Date/Timestamp
52: * @since 1.0
53: */
54: var $dateObject = null;
55:
56: /**
57: * The data label for the object
58: *
59: * @var string
60: * @since 1.0
61: */
62: var $label;
63:
64: /**
65: * The name of the HTML input box
66: *
67: * @var string
68: * @since 1.0
69: */
70: var $name;
71:
72: /**
73: * The constructor
74: *
75: * @param Date/Timestamp $object The date or timestamp object that will be edited by this widget.
76: * @param string $label The data label for the object.
77: * @param string $name The name of the HTML input box.
78: * @since 1.0
79: * @throws IllegalArguementException
80: */
81: public function __construct($object, $label="", $name="") {
82:
83: // check the type of the object passed
84: if($object instanceof Date || $object instanceof Timestamp) {
85: $this->dateObject = $object;
86: }else{
87: throw new IllegalArguementException('DateBox widget can only accept a Date or Timestamp object!');
88: }
89:
90: $this->label = $label;
91: $this->name = $name;
92: }
93:
94: /**
95: * Renders the text box and icon to open the calendar pop-up
96: *
97: * @param boolean $tableTags
98: * @return string
99: * @since 1.0
100: */
101: public function render($tableTags=true) {
102: global $config;
103:
104: $html = '';
105:
106: /*
107: * decide on the size of the text box and the height of the widget pop-up,
108: * depending on the dateObject type
109: */
110: if(strtoupper(get_class($this->dateObject)) == "TIMESTAMP") {
111: $size = 18;
112: $cal_height = 230;
113: }else{
114: $size = 10;
115: $cal_height = 230;
116: }
117:
118: $value = $this->dateObject->getValue();
119: if($value == '0000-00-00')
120: $value = '';
121:
122: if($tableTags) {
123: $html .= '<tr><th style="width:25%;">';
124: $html .= $this->label;
125: $html .= '</th>';
126:
127: $html .= '<td>';
128: $html .= '<input type="text" size="'.$size.'" class="readonly" name="'.$this->name.'" id="'.$this->name.'" value="'.$value.'" readonly/>';
129: $html .= '<script language="javascript">';
130: if($this->dateObject instanceof Timestamp)
131: $html .= "$('#".$this->name."').datepicker({dateFormat:'yy-mm-dd HH:II:SS',showOn:'button',buttonImage:'".$config->get('app.url')."alpha/images/icons/calendar.png'})";
132: else
133: $html .= '$(document).ready(function(){$(\'#'.$this->name.'\').datepicker({dateFormat:\'yy-mm-dd\',defaultDate:\''.$value.'\',showOn:\'button\',buttonImageOnly:\'true\',buttonImage:\''.$config->get('app.url').'alpha/images/icons/calendar.png\'})});';
134: $html .= '</script>';
135: $html .= '</td></tr>';
136: }else{
137: $html .= '<input type="text" size="'.$size.'" class="readonly" name="'.$this->name.'" id="'.$this->name.'" value="'.$value.'" readonly/>';
138: $html .= '<script language="javascript">';
139: if($this->dateObject instanceof Timestamp)
140: $html .= "$('#".$this->name."').datepicker({dateFormat:'yy-mm-dd HH:II:SS',showOn:'button',buttonImage:'".$config->get('app.url')."alpha/images/icons/calendar.png'})";
141: else
142: $html .= '$(document).ready(function(){$(\'#'.$this->name.'\').datepicker({dateFormat:\'yy-mm-dd\',defaultDate:\''.$value.'\',showOn:\'button\',buttonImageOnly:\'true\',buttonImage:\''.$config->get('app.url').'alpha/images/icons/calendar.png\'})});';
143: $html .= '</script>';
144: }
145:
146: return $html;
147: }
148: }
149:
150: ?>