00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Catalog_Block_Product_View_Options_Type_Date extends Mage_Catalog_Block_Product_View_Options_Abstract
00036 {
00037
00038
00039
00040
00041
00042
00043 protected $_fillLeadingZeros = true;
00044
00045 protected function _prepareLayout()
00046 {
00047 if ($head = $this->getLayout()->getBlock('head')) {
00048 $head->setCanLoadCalendarJs(true);
00049 }
00050 return parent::_prepareLayout();
00051 }
00052
00053
00054
00055
00056
00057
00058 public function useCalendar()
00059 {
00060 return Mage::getSingleton('catalog/product_option_type_date')->useCalendar();
00061 }
00062
00063
00064
00065
00066
00067
00068 public function getDateHtml()
00069 {
00070 if ($this->useCalendar()) {
00071 return $this->getCalendarDateHtml();
00072 } else {
00073 return $this->getDropDownsDateHtml();
00074 }
00075 }
00076
00077
00078
00079
00080
00081
00082 public function getCalendarDateHtml()
00083 {
00084
00085 $require = '';
00086 $calendar = $this->getLayout()
00087 ->createBlock('core/html_date')
00088 ->setId('options_'.$this->getOption()->getId().'_date')
00089 ->setName('options['.$this->getOption()->getId().'][date]')
00090 ->setClass('product-custom-option datetime-picker input-text' . $require)
00091 ->setImage(Mage::getDesign()->getSkinUrl('images/grid-cal.gif'))
00092 ->setExtraParams('onchange="opConfig.reloadPrice()"')
00093 ->setFormat(Mage::app()->getLocale()->getDateStrFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT));
00094
00095 return $calendar->getHtml();
00096 }
00097
00098
00099
00100
00101
00102
00103 public function getDropDownsDateHtml()
00104 {
00105 $_fieldsSeparator = ' ';
00106 $_fieldsOrder = Mage::getSingleton('catalog/product_option_type_date')->getConfigData('date_fields_order');
00107 $_fieldsOrder = str_replace(',', $_fieldsSeparator, $_fieldsOrder);
00108
00109 $monthsHtml = $this->_getSelectFromToHtml('month', 1, 12);
00110 $daysHtml = $this->_getSelectFromToHtml('day', 1, 31);
00111
00112 $_yearStart = Mage::getSingleton('catalog/product_option_type_date')->getYearStart();
00113 $_yearEnd = Mage::getSingleton('catalog/product_option_type_date')->getYearEnd();
00114 $yearsHtml = $this->_getSelectFromToHtml('year', $_yearStart, $_yearEnd);
00115
00116 $_translations = array(
00117 'd' => $daysHtml,
00118 'm' => $monthsHtml,
00119 'y' => $yearsHtml
00120 );
00121 return strtr($_fieldsOrder, $_translations);
00122 }
00123
00124
00125
00126
00127
00128
00129 public function getTimeHtml()
00130 {
00131 if (Mage::getSingleton('catalog/product_option_type_date')->is24hTimeFormat()) {
00132 $hourStart = 0;
00133 $hourEnd = 23;
00134 $dayPartHtml = '';
00135 } else {
00136 $hourStart = 1;
00137 $hourEnd = 12;
00138 $dayPartHtml = $this->_getHtmlSelect('day_part')
00139 ->setOptions(array(
00140 'am' => Mage::helper('catalog')->__('AM'),
00141 'pm' => Mage::helper('catalog')->__('PM')
00142 ))
00143 ->getHtml();
00144 }
00145 $hoursHtml = $this->_getSelectFromToHtml('hour', $hourStart, $hourEnd);
00146 $minutesHtml = $this->_getSelectFromToHtml('minute', 0, 59);
00147
00148 return $hoursHtml . ' <b>:</b> ' . $minutesHtml . ' ' . $dayPartHtml;
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 protected function _getSelectFromToHtml($name, $from, $to, $value = null)
00161 {
00162 $options = array(
00163 array('value' => '', 'label' => '-')
00164 );
00165 for ($i = $from; $i <= $to; $i++) {
00166 $options[] = array('value' => $i, 'label' => $this->_getValueWithLeadingZeros($i));
00167 }
00168 return $this->_getHtmlSelect($name, $value)
00169 ->setOptions($options)
00170 ->getHtml();
00171 }
00172
00173
00174
00175
00176
00177
00178
00179 protected function _getHtmlSelect($name, $value = null)
00180 {
00181
00182 $require = '';
00183 $select = $this->getLayout()->createBlock('core/html_select')
00184 ->setId('options_' . $this->getOption()->getId() . '_' . $name)
00185 ->setClass('product-custom-option datetime-picker' . $require)
00186 ->setExtraParams('style="width:auto;" onchange="opConfig.reloadPrice()"')
00187 ->setName('options[' . $this->getOption()->getId() . '][' . $name . ']');
00188 if (!is_null($value)) {
00189 $select->setValue($value);
00190 }
00191 return $select;
00192 }
00193
00194
00195
00196
00197
00198
00199
00200 protected function _getValueWithLeadingZeros($value)
00201 {
00202 if (!$this->_fillLeadingZeros) {
00203 return $value;
00204 }
00205 return $value < 10 ? '0'.$value : $value;
00206 }
00207 }