PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/magento/app/code/core/Mage/Catalog/Block/Product/View/Options/Type/Date.php

https://bitbucket.org/jit_bec/shopifine
PHP | 231 lines | 123 code | 24 blank | 84 comment | 11 complexity | 99cb79fa55ba71272e9db2de760a68d2 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Product options text type block
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Block_Product_View_Options_Type_Date extends Mage_Catalog_Block_Product_View_Options_Abstract
  34. {
  35. /**
  36. * Fill date and time options with leading zeros or not
  37. *
  38. * @var boolean
  39. */
  40. protected $_fillLeadingZeros = true;
  41. protected function _prepareLayout()
  42. {
  43. if ($head = $this->getLayout()->getBlock('head')) {
  44. $head->setCanLoadCalendarJs(true);
  45. }
  46. return parent::_prepareLayout();
  47. }
  48. /**
  49. * Use JS calendar settings
  50. *
  51. * @return boolean
  52. */
  53. public function useCalendar()
  54. {
  55. return Mage::getSingleton('catalog/product_option_type_date')->useCalendar();
  56. }
  57. /**
  58. * Date input
  59. *
  60. * @return string Formatted Html
  61. */
  62. public function getDateHtml()
  63. {
  64. if ($this->useCalendar()) {
  65. return $this->getCalendarDateHtml();
  66. } else {
  67. return $this->getDropDownsDateHtml();
  68. }
  69. }
  70. /**
  71. * JS Calendar html
  72. *
  73. * @return string Formatted Html
  74. */
  75. public function getCalendarDateHtml()
  76. {
  77. $option = $this->getOption();
  78. $value = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $option->getId() . '/date');
  79. //$require = $this->getOption()->getIsRequire() ? ' required-entry' : '';
  80. $require = '';
  81. $yearStart = Mage::getSingleton('catalog/product_option_type_date')->getYearStart();
  82. $yearEnd = Mage::getSingleton('catalog/product_option_type_date')->getYearEnd();
  83. $calendar = $this->getLayout()
  84. ->createBlock('core/html_date')
  85. ->setId('options_'.$this->getOption()->getId().'_date')
  86. ->setName('options['.$this->getOption()->getId().'][date]')
  87. ->setClass('product-custom-option datetime-picker input-text' . $require)
  88. ->setImage($this->getSkinUrl('images/calendar.gif'))
  89. ->setFormat(Mage::app()->getLocale()->getDateStrFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT))
  90. ->setValue($value)
  91. ->setYearsRange('[' . $yearStart . ', ' . $yearEnd . ']');
  92. if (!$this->getSkipJsReloadPrice()) {
  93. $calendar->setExtraParams('onchange="opConfig.reloadPrice()"');
  94. }
  95. return $calendar->getHtml();
  96. }
  97. /**
  98. * Date (dd/mm/yyyy) html drop-downs
  99. *
  100. * @return string Formatted Html
  101. */
  102. public function getDropDownsDateHtml()
  103. {
  104. $fieldsSeparator = '&nbsp;';
  105. $fieldsOrder = Mage::getSingleton('catalog/product_option_type_date')->getConfigData('date_fields_order');
  106. $fieldsOrder = str_replace(',', $fieldsSeparator, $fieldsOrder);
  107. $monthsHtml = $this->_getSelectFromToHtml('month', 1, 12);
  108. $daysHtml = $this->_getSelectFromToHtml('day', 1, 31);
  109. $yearStart = Mage::getSingleton('catalog/product_option_type_date')->getYearStart();
  110. $yearEnd = Mage::getSingleton('catalog/product_option_type_date')->getYearEnd();
  111. $yearsHtml = $this->_getSelectFromToHtml('year', $yearStart, $yearEnd);
  112. $translations = array(
  113. 'd' => $daysHtml,
  114. 'm' => $monthsHtml,
  115. 'y' => $yearsHtml
  116. );
  117. return strtr($fieldsOrder, $translations);
  118. }
  119. /**
  120. * Time (hh:mm am/pm) html drop-downs
  121. *
  122. * @return string Formatted Html
  123. */
  124. public function getTimeHtml()
  125. {
  126. if (Mage::getSingleton('catalog/product_option_type_date')->is24hTimeFormat()) {
  127. $hourStart = 0;
  128. $hourEnd = 23;
  129. $dayPartHtml = '';
  130. } else {
  131. $hourStart = 1;
  132. $hourEnd = 12;
  133. $dayPartHtml = $this->_getHtmlSelect('day_part')
  134. ->setOptions(array(
  135. 'am' => Mage::helper('catalog')->__('AM'),
  136. 'pm' => Mage::helper('catalog')->__('PM')
  137. ))
  138. ->getHtml();
  139. }
  140. $hoursHtml = $this->_getSelectFromToHtml('hour', $hourStart, $hourEnd);
  141. $minutesHtml = $this->_getSelectFromToHtml('minute', 0, 59);
  142. return $hoursHtml . '&nbsp;<b>:</b>&nbsp;' . $minutesHtml . '&nbsp;' . $dayPartHtml;
  143. }
  144. /**
  145. * Return drop-down html with range of values
  146. *
  147. * @param string $name Id/name of html select element
  148. * @param int $from Start position
  149. * @param int $to End position
  150. * @param int $value Value selected
  151. * @return string Formatted Html
  152. */
  153. protected function _getSelectFromToHtml($name, $from, $to, $value = null)
  154. {
  155. $options = array(
  156. array('value' => '', 'label' => '-')
  157. );
  158. for ($i = $from; $i <= $to; $i++) {
  159. $options[] = array('value' => $i, 'label' => $this->_getValueWithLeadingZeros($i));
  160. }
  161. return $this->_getHtmlSelect($name, $value)
  162. ->setOptions($options)
  163. ->getHtml();
  164. }
  165. /**
  166. * HTML select element
  167. *
  168. * @param string $name Id/name of html select element
  169. * @return Mage_Core_Block_Html_Select
  170. */
  171. protected function _getHtmlSelect($name, $value = null)
  172. {
  173. $option = $this->getOption();
  174. // $require = $this->getOption()->getIsRequire() ? ' required-entry' : '';
  175. $require = '';
  176. $select = $this->getLayout()->createBlock('core/html_select')
  177. ->setId('options_' . $this->getOption()->getId() . '_' . $name)
  178. ->setClass('product-custom-option datetime-picker' . $require)
  179. ->setExtraParams()
  180. ->setName('options[' . $option->getId() . '][' . $name . ']');
  181. $extraParams = 'style="width:auto"';
  182. if (!$this->getSkipJsReloadPrice()) {
  183. $extraParams .= ' onchange="opConfig.reloadPrice()"';
  184. }
  185. $select->setExtraParams($extraParams);
  186. if (is_null($value)) {
  187. $value = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $option->getId() . '/' . $name);
  188. }
  189. if (!is_null($value)) {
  190. $select->setValue($value);
  191. }
  192. return $select;
  193. }
  194. /**
  195. * Add Leading Zeros to number less than 10
  196. *
  197. * @param int
  198. * @return string
  199. */
  200. protected function _getValueWithLeadingZeros($value)
  201. {
  202. if (!$this->_fillLeadingZeros) {
  203. return $value;
  204. }
  205. return $value < 10 ? '0'.$value : $value;
  206. }
  207. }