Startdate.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\Attribute\Backend;
  7. /**
  8. *
  9. * Speical Start Date attribute backend
  10. *
  11. * @api
  12. *
  13. * @author Magento Core Team <core@magentocommerce.com>
  14. * @since 100.0.2
  15. */
  16. class Startdate extends \Magento\Eav\Model\Entity\Attribute\Backend\Datetime
  17. {
  18. /**
  19. * Date model
  20. *
  21. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  22. */
  23. protected $_date;
  24. /**
  25. * Constructor
  26. *
  27. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  28. * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
  29. */
  30. public function __construct(
  31. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  32. \Magento\Framework\Stdlib\DateTime\DateTime $date
  33. ) {
  34. $this->_date = $date;
  35. parent::__construct($localeDate);
  36. }
  37. /**
  38. * Get attribute value for save.
  39. *
  40. * @param \Magento\Framework\DataObject $object
  41. * @return string|bool
  42. */
  43. protected function _getValueForSave($object)
  44. {
  45. $attributeName = $this->getAttribute()->getName();
  46. $startDate = $object->getData($attributeName);
  47. return $startDate;
  48. }
  49. /**
  50. * Before save hook.
  51. * Prepare attribute value for save
  52. *
  53. * @param \Magento\Framework\DataObject $object
  54. * @return $this
  55. */
  56. public function beforeSave($object)
  57. {
  58. $startDate = $this->_getValueForSave($object);
  59. if ($startDate === false) {
  60. return $this;
  61. }
  62. $object->setData($this->getAttribute()->getName(), $startDate);
  63. parent::beforeSave($object);
  64. return $this;
  65. }
  66. /**
  67. * Product from date attribute validate function.
  68. * In case invalid data throws exception.
  69. *
  70. * @param \Magento\Framework\DataObject $object
  71. * @throws \Magento\Eav\Model\Entity\Attribute\Exception
  72. * @return bool
  73. */
  74. public function validate($object)
  75. {
  76. $attr = $this->getAttribute();
  77. $maxDate = $attr->getMaxValue();
  78. $startDate = $this->_getValueForSave($object);
  79. if ($startDate === false) {
  80. return true;
  81. }
  82. if ($maxDate) {
  83. $date = $this->_date;
  84. $value = $date->timestamp($startDate);
  85. $maxValue = $date->timestamp($maxDate);
  86. if ($value > $maxValue) {
  87. $message = __('Make sure the To Date is later than or the same as the From Date.');
  88. $eavExc = new \Magento\Eav\Model\Entity\Attribute\Exception($message);
  89. $eavExc->setAttributeCode($attr->getName());
  90. throw $eavExc;
  91. }
  92. }
  93. return true;
  94. }
  95. }