PageRenderTime 29ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-catalog/Model/Product/Option/Type/Date.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 384 lines | 244 code | 31 blank | 109 comment | 54 complexity | e407a337c0ce4e02ac154e0812aaf7d3 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\Product\Option\Type;
  7. /**
  8. * Catalog product option date type
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Date extends \Magento\Catalog\Model\Product\Option\Type\DefaultType
  13. {
  14. /**
  15. * @var string
  16. */
  17. protected $_formattedOptionValue = null;
  18. /**
  19. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  20. */
  21. protected $_localeDate;
  22. /**
  23. * @param \Magento\Checkout\Model\Session $checkoutSession
  24. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  25. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  26. * @param array $data
  27. */
  28. public function __construct(
  29. \Magento\Checkout\Model\Session $checkoutSession,
  30. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  31. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  32. array $data = []
  33. ) {
  34. $this->_localeDate = $localeDate;
  35. parent::__construct($checkoutSession, $scopeConfig, $data);
  36. }
  37. /**
  38. * Validate user input for option
  39. *
  40. * @param array $values All product option values, i.e. array (option_id => mixed, option_id => mixed...)
  41. * @return $this
  42. * @throws \Magento\Framework\Exception\LocalizedException
  43. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  44. * @SuppressWarnings(PHPMD.NPathComplexity)
  45. */
  46. public function validateUserValue($values)
  47. {
  48. parent::validateUserValue($values);
  49. $option = $this->getOption();
  50. $value = $this->getUserValue();
  51. $dateValid = true;
  52. if ($this->_dateExists()) {
  53. if ($this->useCalendar()) {
  54. $dateValid = isset($value['date']) && preg_match('/^\d{1,4}.+\d{1,4}.+\d{1,4}$/', $value['date']);
  55. } else {
  56. $dateValid = isset(
  57. $value['day']
  58. ) && isset(
  59. $value['month']
  60. ) && isset(
  61. $value['year']
  62. ) && $value['day'] > 0 && $value['month'] > 0 && $value['year'] > 0;
  63. }
  64. }
  65. $timeValid = true;
  66. if ($this->_timeExists()) {
  67. $timeValid = isset(
  68. $value['hour']
  69. ) && isset(
  70. $value['minute']
  71. ) && is_numeric(
  72. $value['hour']
  73. ) && is_numeric(
  74. $value['minute']
  75. );
  76. }
  77. $isValid = $dateValid && $timeValid;
  78. if ($isValid) {
  79. $this->setUserValue(
  80. [
  81. 'date' => isset($value['date']) ? $value['date'] : '',
  82. 'year' => isset($value['year']) ? intval($value['year']) : 0,
  83. 'month' => isset($value['month']) ? intval($value['month']) : 0,
  84. 'day' => isset($value['day']) ? intval($value['day']) : 0,
  85. 'hour' => isset($value['hour']) ? intval($value['hour']) : 0,
  86. 'minute' => isset($value['minute']) ? intval($value['minute']) : 0,
  87. 'day_part' => isset($value['day_part']) ? $value['day_part'] : '',
  88. 'date_internal' => isset($value['date_internal']) ? $value['date_internal'] : '',
  89. ]
  90. );
  91. } elseif (!$isValid && $option->getIsRequire() && !$this->getSkipCheckRequiredOption()) {
  92. $this->setIsValid(false);
  93. if (!$dateValid) {
  94. throw new \Magento\Framework\Exception\LocalizedException(
  95. __('Please specify date required option(s).')
  96. );
  97. } elseif (!$timeValid) {
  98. throw new \Magento\Framework\Exception\LocalizedException(
  99. __('Please specify time required option(s).')
  100. );
  101. } else {
  102. throw new \Magento\Framework\Exception\LocalizedException(
  103. __('Please specify product\'s required option(s).')
  104. );
  105. }
  106. } else {
  107. $this->setUserValue(null);
  108. }
  109. return $this;
  110. }
  111. /**
  112. * Prepare option value for cart
  113. *
  114. * @return string|null Prepared option value
  115. * @throws \Magento\Framework\Exception\LocalizedException
  116. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  117. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  118. */
  119. public function prepareForCart()
  120. {
  121. if ($this->getIsValid() && $this->getUserValue() !== null) {
  122. $option = $this->getOption();
  123. $value = $this->getUserValue();
  124. if (isset($value['date_internal']) && $value['date_internal'] != '') {
  125. $this->_setInternalInRequest($value['date_internal']);
  126. return $value['date_internal'];
  127. }
  128. $timestamp = 0;
  129. if ($this->_dateExists()) {
  130. if ($this->useCalendar()) {
  131. $timestamp += (new \DateTime($value['date']))->getTimestamp();
  132. } else {
  133. $timestamp += mktime(0, 0, 0, $value['month'], $value['day'], $value['year']);
  134. }
  135. } else {
  136. $timestamp += mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  137. }
  138. if ($this->_timeExists()) {
  139. // 24hr hour conversion
  140. if (!$this->is24hTimeFormat()) {
  141. $pmDayPart = 'pm' == strtolower($value['day_part']);
  142. if (12 == $value['hour']) {
  143. $value['hour'] = $pmDayPart ? 12 : 0;
  144. } elseif ($pmDayPart) {
  145. $value['hour'] += 12;
  146. }
  147. }
  148. $timestamp += 60 * 60 * $value['hour'] + 60 * $value['minute'];
  149. }
  150. $date = (new \DateTime())->setTimestamp($timestamp);
  151. $result = $date->format('Y-m-d H:i:s');
  152. // Save date in internal format to avoid locale date bugs
  153. $this->_setInternalInRequest($result);
  154. return $result;
  155. } else {
  156. return null;
  157. }
  158. }
  159. /**
  160. * Return formatted option value for quote option
  161. *
  162. * @param string $optionValue Prepared for cart option value
  163. * @return string
  164. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  165. */
  166. public function getFormattedOptionValue($optionValue)
  167. {
  168. if ($this->_formattedOptionValue === null) {
  169. if ($this->getOption()->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_DATE) {
  170. $result = $this->_localeDate->formatDateTime(
  171. new \DateTime($optionValue),
  172. \IntlDateFormatter::MEDIUM,
  173. \IntlDateFormatter::NONE,
  174. null,
  175. 'UTC'
  176. );
  177. } elseif ($this->getOption()->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_DATE_TIME) {
  178. $result = $this->_localeDate->formatDateTime(
  179. new \DateTime($optionValue),
  180. \IntlDateFormatter::SHORT,
  181. \IntlDateFormatter::SHORT,
  182. null,
  183. 'UTC'
  184. );
  185. } elseif ($this->getOption()->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_TIME) {
  186. $result = $this->_localeDate->formatDateTime(
  187. new \DateTime($optionValue),
  188. \IntlDateFormatter::NONE,
  189. \IntlDateFormatter::SHORT,
  190. null,
  191. 'UTC'
  192. );
  193. } else {
  194. $result = $optionValue;
  195. }
  196. $this->_formattedOptionValue = $result;
  197. }
  198. return $this->_formattedOptionValue;
  199. }
  200. /**
  201. * Return printable option value
  202. *
  203. * @param string $optionValue Prepared for cart option value
  204. * @return string
  205. */
  206. public function getPrintableOptionValue($optionValue)
  207. {
  208. return $this->getFormattedOptionValue($optionValue);
  209. }
  210. /**
  211. * Return formatted option value ready to edit, ready to parse
  212. *
  213. * @param string $optionValue Prepared for cart option value
  214. * @return string
  215. */
  216. public function getEditableOptionValue($optionValue)
  217. {
  218. return $this->getFormattedOptionValue($optionValue);
  219. }
  220. /**
  221. * Parse user input value and return cart prepared value
  222. *
  223. * @param string $optionValue
  224. * @param array $productOptionValues Values for product option
  225. * @return string|null
  226. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  227. */
  228. public function parseOptionValue($optionValue, $productOptionValues)
  229. {
  230. try {
  231. $date = new \DateTime($optionValue);
  232. } catch (\Exception $e) {
  233. return null;
  234. }
  235. return $date->format('Y-m-d H:i:s');
  236. }
  237. /**
  238. * Prepare option value for info buy request
  239. *
  240. * @param string $optionValue
  241. * @return array
  242. */
  243. public function prepareOptionValueForRequest($optionValue)
  244. {
  245. $confItem = $this->getConfigurationItem();
  246. $infoBuyRequest = $confItem->getOptionByCode('info_buyRequest');
  247. try {
  248. $value = unserialize($infoBuyRequest->getValue());
  249. if (is_array($value) && isset($value['options']) && isset($value['options'][$this->getOption()->getId()])
  250. ) {
  251. return $value['options'][$this->getOption()->getId()];
  252. } else {
  253. return ['date_internal' => $optionValue];
  254. }
  255. } catch (\Exception $e) {
  256. return ['date_internal' => $optionValue];
  257. }
  258. }
  259. /**
  260. * Use Calendar on frontend or not
  261. *
  262. * @return boolean
  263. */
  264. public function useCalendar()
  265. {
  266. return (bool)$this->getConfigData('use_calendar');
  267. }
  268. /**
  269. * Time Format
  270. *
  271. * @return boolean
  272. */
  273. public function is24hTimeFormat()
  274. {
  275. return (bool)($this->getConfigData('time_format') == '24h');
  276. }
  277. /**
  278. * Year range start
  279. *
  280. * @return string|false
  281. */
  282. public function getYearStart()
  283. {
  284. $_range = explode(',', $this->getConfigData('year_range'));
  285. if (isset($_range[0]) && !empty($_range[0])) {
  286. return $_range[0];
  287. } else {
  288. return date('Y');
  289. }
  290. }
  291. /**
  292. * Year range end
  293. *
  294. * @return string|false
  295. */
  296. public function getYearEnd()
  297. {
  298. $_range = explode(',', $this->getConfigData('year_range'));
  299. if (isset($_range[1]) && !empty($_range[1])) {
  300. return $_range[1];
  301. } else {
  302. return date('Y');
  303. }
  304. }
  305. /**
  306. * Save internal value of option in infoBuy_request
  307. *
  308. * @param string $internalValue Datetime value in internal format
  309. * @return void
  310. */
  311. protected function _setInternalInRequest($internalValue)
  312. {
  313. $requestOptions = $this->getRequest()->getOptions();
  314. if (!isset($requestOptions[$this->getOption()->getId()])) {
  315. $requestOptions[$this->getOption()->getId()] = [];
  316. }
  317. if (!is_array($requestOptions[$this->getOption()->getId()])) {
  318. $requestOptions[$this->getOption()->getId()] = [];
  319. }
  320. $requestOptions[$this->getOption()->getId()]['date_internal'] = $internalValue;
  321. $this->getRequest()->setOptions($requestOptions);
  322. }
  323. /**
  324. * Does option have date?
  325. *
  326. * @return boolean
  327. */
  328. protected function _dateExists()
  329. {
  330. return in_array(
  331. $this->getOption()->getType(),
  332. [
  333. \Magento\Catalog\Model\Product\Option::OPTION_TYPE_DATE,
  334. \Magento\Catalog\Model\Product\Option::OPTION_TYPE_DATE_TIME
  335. ]
  336. );
  337. }
  338. /**
  339. * Does option have time?
  340. *
  341. * @return boolean
  342. */
  343. protected function _timeExists()
  344. {
  345. return in_array(
  346. $this->getOption()->getType(),
  347. [
  348. \Magento\Catalog\Model\Product\Option::OPTION_TYPE_DATE_TIME,
  349. \Magento\Catalog\Model\Product\Option::OPTION_TYPE_TIME
  350. ]
  351. );
  352. }
  353. }