1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_XmlConnect_Block_Adminhtml_Mobile_Form_Element_Datetime
35: extends Varien_Data_Form_Element_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_value;
43:
44: public function __construct($attributes=array())
45: {
46: parent::__construct($attributes);
47: $this->setType('text');
48: $this->setExtType('textfield');
49: if (isset($attributes['value'])) {
50: $this->setValue($attributes['value']);
51: }
52: }
53:
54: 55: 56: 57: 58: 59: 60:
61: protected function _toTimestamp($value)
62: {
63: $value = (int)$value;
64: if ($value > 3155760000) {
65: $value = 0;
66: }
67: return $value;
68: }
69:
70: 71: 72: 73: 74: 75: 76: 77: 78: 79:
80: public function setValue($value, $format = null, $locale = null)
81: {
82: if (empty($value)) {
83: $this->_value = '';
84: return $this;
85: }
86: if ($value instanceof Zend_Date) {
87: $this->_value = $value;
88: return $this;
89: }
90: if (preg_match('/^[0-9]+$/', $value)) {
91: $this->_value = new Zend_Date($this->_toTimestamp($value));
92:
93: return $this;
94: }
95:
96: if (null === $format) {
97: $format = Varien_Date::DATETIME_INTERNAL_FORMAT;
98: if ($this->getInputFormat()) {
99: $format = $this->getInputFormat();
100: }
101: }
102:
103: if (null === $locale) {
104: if (!$locale = $this->getLocale()) {
105: $locale = null;
106: }
107: }
108: try {
109: $this->_value = new Zend_Date($value, $format, $locale);
110: } catch (Exception $e) {
111: $this->_value = '';
112: }
113: return $this;
114: }
115:
116: 117: 118: 119: 120: 121: 122:
123: public function getValue($format = null)
124: {
125: if (empty($this->_value)) {
126: return '';
127: }
128: if (null === $format) {
129: $format = $this->getFormat() . " " . $this->getFormatT();
130: }
131: return $this->_value->toString($format);
132: }
133:
134: 135: 136: 137: 138:
139: public function getValueInstance()
140: {
141: if (empty($this->_value)) {
142: return null;
143: }
144: return $this->_value;
145: }
146:
147: 148: 149: 150: 151: 152: 153: 154:
155: public function getElementHtml()
156: {
157: $this->addClass('input-text');
158:
159: $html = sprintf(
160: '<input name="%s" id="%s" value="%s" %s style="width:110px !important;" />'
161: .' <img src="%s" alt="" class="v-middle" id="%s_trig" title="%s" style="%s" />',
162: $this->getName(),
163: $this->getHtmlId(),
164: $this->_escape($this->getValue()),
165: $this->serialize($this->getHtmlAttributes()),
166: $this->getImage(),
167: $this->getHtmlId(),
168: 'Select Date',
169: ($this->getDisabled() ? 'display:none;' : '')
170: );
171: $outputFormat = $this->getFormat();
172: $outputTimeFormat = $this->getFormatT();
173: if (empty($outputFormat)) {
174: Mage::throwException(
175: $this->__('Output format is not specified. Please, specify "format" key in constructor, or set it using setFormat().')
176: );
177: }
178: $displayFormat = Varien_Date::convertZendToStrFtime($outputFormat, true, false);
179: $displayTimeFormat = Varien_Date::convertZendToStrFtime($outputTimeFormat, false, true);
180:
181: $html .= sprintf('
182: <script type="text/javascript">
183: //<![CDATA[
184: Calendar.setup({
185: inputField: "%s",
186: ifFormat: "%s",
187: showsTime: %s,
188: button: "%s_trig",
189: align: "Bl",
190: singleClick : false,
191: timeFormat: 12
192: });
193: //]]>
194: </script>',
195: $this->getHtmlId(),
196: $displayFormat . " " . $displayTimeFormat,
197: $this->getTime() ? 'true' : 'false',
198: $this->getHtmlId()
199: );
200:
201: $html .= $this->getAfterElementHtml();
202:
203: return $html;
204: }
205: }
206: