PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Product/Option/Type/Date.php

https://bitbucket.org/dnejedly/eaparts
PHP | 337 lines | 194 code | 30 blank | 113 comment | 56 complexity | 587050deb932cf2a129429a3fac62b3f MD5 | raw file
  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. * Catalog product option date type
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Product_Option_Type_Date extends Mage_Catalog_Model_Product_Option_Type_Default
  34. {
  35. /**
  36. * Validate user input for option
  37. *
  38. * @throws Mage_Core_Exception
  39. * @param array $values All product option values, i.e. array (option_id => mixed, option_id => mixed...)
  40. * @return Mage_Catalog_Model_Product_Option_Type_Default
  41. */
  42. public function validateUserValue($values)
  43. {
  44. parent::validateUserValue($values);
  45. $option = $this->getOption();
  46. $value = $this->getUserValue();
  47. $dateValid = true;
  48. if ($this->_dateExists()) {
  49. if ($this->useCalendar()) {
  50. $dateValid = isset($value['date']) && preg_match('/^\d{1,4}.+\d{1,4}.+\d{1,4}$/', $value['date']);
  51. } else {
  52. $dateValid = isset($value['day']) && isset($value['month']) && isset($value['year'])
  53. && $value['day'] > 0 && $value['month'] > 0 && $value['year'] > 0;
  54. }
  55. }
  56. $timeValid = true;
  57. if ($this->_timeExists()) {
  58. $timeValid = isset($value['hour']) && isset($value['minute'])
  59. && is_numeric($value['hour']) && is_numeric($value['minute']);
  60. }
  61. $isValid = $dateValid && $timeValid;
  62. if ($isValid) {
  63. $this->setUserValue(
  64. array(
  65. 'date' => isset($value['date']) ? $value['date'] : '',
  66. 'year' => isset($value['year']) ? intval($value['year']) : 0,
  67. 'month' => isset($value['month']) ? intval($value['month']) : 0,
  68. 'day' => isset($value['day']) ? intval($value['day']) : 0,
  69. 'hour' => isset($value['hour']) ? intval($value['hour']) : 0,
  70. 'minute' => isset($value['minute']) ? intval($value['minute']) : 0,
  71. 'day_part' => isset($value['day_part']) ? $value['day_part'] : '',
  72. 'date_internal' => isset($value['date_internal']) ? $value['date_internal'] : '',
  73. )
  74. );
  75. } elseif (!$isValid && $option->getIsRequire() && !$this->getSkipCheckRequiredOption()) {
  76. $this->setIsValid(false);
  77. if (!$dateValid) {
  78. Mage::throwException(Mage::helper('catalog')->__('Please specify date required option(s).'));
  79. } elseif (!$timeValid) {
  80. Mage::throwException(Mage::helper('catalog')->__('Please specify time required option(s).'));
  81. } else {
  82. Mage::throwException(Mage::helper('catalog')->__('Please specify the product required option(s).'));
  83. }
  84. } else {
  85. $this->setUserValue(null);
  86. return $this;
  87. }
  88. return $this;
  89. }
  90. /**
  91. * Prepare option value for cart
  92. *
  93. * @throws Mage_Core_Exception
  94. * @return mixed Prepared option value
  95. */
  96. public function prepareForCart()
  97. {
  98. if ($this->getIsValid() && $this->getUserValue() !== null) {
  99. $option = $this->getOption();
  100. $value = $this->getUserValue();
  101. if (isset($value['date_internal']) && $value['date_internal'] != '') {
  102. $this->_setInternalInRequest($value['date_internal']);
  103. return $value['date_internal'];
  104. }
  105. $timestamp = 0;
  106. if ($this->_dateExists()) {
  107. if ($this->useCalendar()) {
  108. $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
  109. $timestamp += Mage::app()->getLocale()->date($value['date'], $format, null, false)->getTimestamp();
  110. } else {
  111. $timestamp += mktime(0, 0, 0, $value['month'], $value['day'], $value['year']);
  112. }
  113. } else {
  114. $timestamp += mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  115. }
  116. if ($this->_timeExists()) {
  117. // 24hr hour conversion
  118. if (! $this->is24hTimeFormat()) {
  119. $pmDayPart = ('pm' == strtolower($value['day_part']));
  120. if (12 == $value['hour']) {
  121. $value['hour'] = $pmDayPart ? 12 : 0;
  122. } elseif ($pmDayPart) {
  123. $value['hour'] += 12;
  124. }
  125. }
  126. $timestamp += 60 * 60 * $value['hour'] + 60 * $value['minute'];
  127. }
  128. $date = new Zend_Date($timestamp);
  129. $result = $date->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
  130. // Save date in internal format to avoid locale date bugs
  131. $this->_setInternalInRequest($result);
  132. return $result;
  133. } else {
  134. return null;
  135. }
  136. }
  137. /**
  138. * Return formatted option value for quote option
  139. *
  140. * @param string $optionValue Prepared for cart option value
  141. * @return string
  142. */
  143. public function getFormattedOptionValue($optionValue)
  144. {
  145. if ($this->_formattedOptionValue === null) {
  146. $option = $this->getOption();
  147. if ($this->getOption()->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE) {
  148. $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);
  149. $result = Mage::app()->getLocale()->date($optionValue, Zend_Date::ISO_8601, null, false)
  150. ->toString($format);
  151. } elseif ($this->getOption()->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE_TIME) {
  152. $format = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
  153. $result = Mage::app()->getLocale()
  154. ->date($optionValue, Varien_Date::DATETIME_INTERNAL_FORMAT, null, false)->toString($format);
  155. } elseif ($this->getOption()->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_TIME) {
  156. $date = new Zend_Date($optionValue);
  157. $result = date($this->is24hTimeFormat() ? 'H:i' : 'h:i a', $date->getTimestamp());
  158. } else {
  159. $result = $optionValue;
  160. }
  161. $this->_formattedOptionValue = $result;
  162. }
  163. return $this->_formattedOptionValue;
  164. }
  165. /**
  166. * Return printable option value
  167. *
  168. * @param string $optionValue Prepared for cart option value
  169. * @return string
  170. */
  171. public function getPrintableOptionValue($optionValue)
  172. {
  173. return $this->getFormattedOptionValue($optionValue);
  174. }
  175. /**
  176. * Return formatted option value ready to edit, ready to parse
  177. *
  178. * @param string $optionValue Prepared for cart option value
  179. * @return string
  180. */
  181. public function getEditableOptionValue($optionValue)
  182. {
  183. return $this->getFormattedOptionValue($optionValue);
  184. }
  185. /**
  186. * Parse user input value and return cart prepared value
  187. *
  188. * @param string $optionValue
  189. * @param array $productOptionValues Values for product option
  190. * @return string|null
  191. */
  192. public function parseOptionValue($optionValue, $productOptionValues)
  193. {
  194. $timestamp = strtotime($optionValue);
  195. if ($timestamp === false || $timestamp == -1) {
  196. return null;
  197. }
  198. $date = new Zend_Date($timestamp);
  199. return $date->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
  200. }
  201. /**
  202. * Prepare option value for info buy request
  203. *
  204. * @param string $optionValue
  205. * @return mixed
  206. */
  207. public function prepareOptionValueForRequest($optionValue)
  208. {
  209. $confItem = $this->getConfigurationItem();
  210. $infoBuyRequest = $confItem->getOptionByCode('info_buyRequest');
  211. try {
  212. $value = unserialize($infoBuyRequest->getValue());
  213. if (is_array($value) && isset($value['options']) && isset($value['options'][$this->getOption()->getId()])) {
  214. return $value['options'][$this->getOption()->getId()];
  215. } else {
  216. return array('date_internal' => $optionValue);
  217. }
  218. } catch (Exception $e) {
  219. return array('date_internal' => $optionValue);
  220. }
  221. }
  222. /**
  223. * Use Calendar on frontend or not
  224. *
  225. * @return boolean
  226. */
  227. public function useCalendar()
  228. {
  229. return (bool)$this->getConfigData('use_calendar');
  230. }
  231. /**
  232. * Time Format
  233. *
  234. * @return boolean
  235. */
  236. public function is24hTimeFormat()
  237. {
  238. return (bool)($this->getConfigData('time_format') == '24h');
  239. }
  240. /**
  241. * Year range start
  242. *
  243. * @return mixed
  244. */
  245. public function getYearStart()
  246. {
  247. $_range = explode(',', $this->getConfigData('year_range'));
  248. if (isset($_range[0]) && !empty($_range[0])) {
  249. return $_range[0];
  250. } else {
  251. return date('Y');
  252. }
  253. }
  254. /**
  255. * Year range end
  256. *
  257. * @return mixed
  258. */
  259. public function getYearEnd()
  260. {
  261. $_range = explode(',', $this->getConfigData('year_range'));
  262. if (isset($_range[1]) && !empty($_range[1])) {
  263. return $_range[1];
  264. } else {
  265. return date('Y');
  266. }
  267. }
  268. /**
  269. * Save internal value of option in infoBuy_request
  270. *
  271. * @param string $internalValue Datetime value in internal format
  272. * @return Mage_Catalog_Model_Product_Option_Type_Date
  273. */
  274. protected function _setInternalInRequest($internalValue)
  275. {
  276. $requestOptions = $this->getRequest()->getOptions();
  277. if (!isset($requestOptions[$this->getOption()->getId()])) {
  278. $requestOptions[$this->getOption()->getId()] = array();
  279. }
  280. $requestOptions[$this->getOption()->getId()]['date_internal'] = $internalValue;
  281. $this->getRequest()->setOptions($requestOptions);
  282. }
  283. /**
  284. * Does option have date?
  285. *
  286. * @return boolean
  287. */
  288. protected function _dateExists()
  289. {
  290. return in_array($this->getOption()->getType(), array(
  291. Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE,
  292. Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE_TIME
  293. ));
  294. }
  295. /**
  296. * Does option have time?
  297. *
  298. * @return boolean
  299. */
  300. protected function _timeExists()
  301. {
  302. return in_array($this->getOption()->getType(), array(
  303. Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE_TIME,
  304. Mage_Catalog_Model_Product_Option::OPTION_TYPE_TIME
  305. ));
  306. }
  307. }