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:
35: class Mage_Catalog_Block_Product_View_Options_Type_Date extends Mage_Catalog_Block_Product_View_Options_Abstract
36: {
37:
38: 39: 40: 41: 42:
43: protected $_fillLeadingZeros = true;
44:
45: protected function _prepareLayout()
46: {
47: if ($head = $this->getLayout()->getBlock('head')) {
48: $head->setCanLoadCalendarJs(true);
49: }
50: return parent::_prepareLayout();
51: }
52:
53: 54: 55: 56: 57:
58: public function useCalendar()
59: {
60: return Mage::getSingleton('catalog/product_option_type_date')->useCalendar();
61: }
62:
63: 64: 65: 66: 67:
68: public function getDateHtml()
69: {
70: if ($this->useCalendar()) {
71: return $this->getCalendarDateHtml();
72: } else {
73: return $this->getDropDownsDateHtml();
74: }
75: }
76:
77: 78: 79: 80: 81:
82: public function getCalendarDateHtml()
83: {
84: $option = $this->getOption();
85: $value = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $option->getId() . '/date');
86:
87:
88: $require = '';
89:
90: $yearStart = Mage::getSingleton('catalog/product_option_type_date')->getYearStart();
91: $yearEnd = Mage::getSingleton('catalog/product_option_type_date')->getYearEnd();
92:
93: $calendar = $this->getLayout()
94: ->createBlock('core/html_date')
95: ->setId('options_'.$this->getOption()->getId().'_date')
96: ->setName('options['.$this->getOption()->getId().'][date]')
97: ->setClass('product-custom-option datetime-picker input-text' . $require)
98: ->setImage($this->getSkinUrl('images/calendar.gif'))
99: ->setFormat(Mage::app()->getLocale()->getDateStrFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT))
100: ->setValue($value)
101: ->setYearsRange('[' . $yearStart . ', ' . $yearEnd . ']');
102: if (!$this->getSkipJsReloadPrice()) {
103: $calendar->setExtraParams('onchange="opConfig.reloadPrice()"');
104: }
105:
106: return $calendar->getHtml();
107: }
108:
109: 110: 111: 112: 113:
114: public function getDropDownsDateHtml()
115: {
116: $fieldsSeparator = ' ';
117: $fieldsOrder = Mage::getSingleton('catalog/product_option_type_date')->getConfigData('date_fields_order');
118: $fieldsOrder = str_replace(',', $fieldsSeparator, $fieldsOrder);
119:
120: $monthsHtml = $this->_getSelectFromToHtml('month', 1, 12);
121: $daysHtml = $this->_getSelectFromToHtml('day', 1, 31);
122:
123: $yearStart = Mage::getSingleton('catalog/product_option_type_date')->getYearStart();
124: $yearEnd = Mage::getSingleton('catalog/product_option_type_date')->getYearEnd();
125: $yearsHtml = $this->_getSelectFromToHtml('year', $yearStart, $yearEnd);
126:
127: $translations = array(
128: 'd' => $daysHtml,
129: 'm' => $monthsHtml,
130: 'y' => $yearsHtml
131: );
132: return strtr($fieldsOrder, $translations);
133: }
134:
135: 136: 137: 138: 139:
140: public function getTimeHtml()
141: {
142: if (Mage::getSingleton('catalog/product_option_type_date')->is24hTimeFormat()) {
143: $hourStart = 0;
144: $hourEnd = 23;
145: $dayPartHtml = '';
146: } else {
147: $hourStart = 1;
148: $hourEnd = 12;
149: $dayPartHtml = $this->_getHtmlSelect('day_part')
150: ->setOptions(array(
151: 'am' => Mage::helper('catalog')->__('AM'),
152: 'pm' => Mage::helper('catalog')->__('PM')
153: ))
154: ->getHtml();
155: }
156: $hoursHtml = $this->_getSelectFromToHtml('hour', $hourStart, $hourEnd);
157: $minutesHtml = $this->_getSelectFromToHtml('minute', 0, 59);
158:
159: return $hoursHtml . ' <b>:</b> ' . $minutesHtml . ' ' . $dayPartHtml;
160: }
161:
162: 163: 164: 165: 166: 167: 168: 169: 170:
171: protected function _getSelectFromToHtml($name, $from, $to, $value = null)
172: {
173: $options = array(
174: array('value' => '', 'label' => '-')
175: );
176: for ($i = $from; $i <= $to; $i++) {
177: $options[] = array('value' => $i, 'label' => $this->_getValueWithLeadingZeros($i));
178: }
179: return $this->_getHtmlSelect($name, $value)
180: ->setOptions($options)
181: ->getHtml();
182: }
183:
184: 185: 186: 187: 188: 189:
190: protected function _getHtmlSelect($name, $value = null)
191: {
192: $option = $this->getOption();
193:
194:
195: $require = '';
196: $select = $this->getLayout()->createBlock('core/html_select')
197: ->setId('options_' . $this->getOption()->getId() . '_' . $name)
198: ->setClass('product-custom-option datetime-picker' . $require)
199: ->setExtraParams()
200: ->setName('options[' . $option->getId() . '][' . $name . ']');
201:
202: $extraParams = 'style="width:auto"';
203: if (!$this->getSkipJsReloadPrice()) {
204: $extraParams .= ' onchange="opConfig.reloadPrice()"';
205: }
206: $select->setExtraParams($extraParams);
207:
208: if (is_null($value)) {
209: $value = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $option->getId() . '/' . $name);
210: }
211: if (!is_null($value)) {
212: $select->setValue($value);
213: }
214:
215: return $select;
216: }
217:
218: 219: 220: 221: 222: 223:
224: protected function _getValueWithLeadingZeros($value)
225: {
226: if (!$this->_fillLeadingZeros) {
227: return $value;
228: }
229: return $value < 10 ? '0'.$value : $value;
230: }
231: }
232: