Address.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\ResourceModel\Order;
  7. use Magento\Sales\Model\ResourceModel\EntityAbstract as SalesResource;
  8. use Magento\Sales\Model\Spi\OrderAddressResourceInterface;
  9. use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
  10. /**
  11. * Flat sales order address resource
  12. */
  13. class Address extends SalesResource implements OrderAddressResourceInterface
  14. {
  15. /**
  16. * Event prefix
  17. *
  18. * @var string
  19. */
  20. protected $_eventPrefix = 'sales_order_address_resource';
  21. /**
  22. * @var \Magento\Sales\Model\Order\Address\Validator
  23. */
  24. protected $_validator;
  25. /**
  26. * @var \Magento\Sales\Model\ResourceModel\GridPool
  27. */
  28. protected $gridPool;
  29. /**
  30. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  31. * @param \Magento\Sales\Model\ResourceModel\Attribute $attribute
  32. * @param \Magento\SalesSequence\Model\Manager $sequenceManager
  33. * @param Snapshot $entitySnapshot
  34. * @param \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite $entityRelationComposite
  35. * @param \Magento\Sales\Model\Order\Address\Validator $validator
  36. * @param \Magento\Sales\Model\ResourceModel\GridPool $gridPool
  37. * @param string $connectionName
  38. */
  39. public function __construct(
  40. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  41. Snapshot $entitySnapshot,
  42. \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite $entityRelationComposite,
  43. \Magento\Sales\Model\ResourceModel\Attribute $attribute,
  44. \Magento\SalesSequence\Model\Manager $sequenceManager,
  45. \Magento\Sales\Model\Order\Address\Validator $validator,
  46. \Magento\Sales\Model\ResourceModel\GridPool $gridPool,
  47. $connectionName = null
  48. ) {
  49. $this->_validator = $validator;
  50. $this->gridPool = $gridPool;
  51. parent::__construct(
  52. $context,
  53. $entitySnapshot,
  54. $entityRelationComposite,
  55. $attribute,
  56. $sequenceManager,
  57. $connectionName
  58. );
  59. }
  60. /**
  61. * Resource initialization
  62. *
  63. * @return void
  64. */
  65. protected function _construct()
  66. {
  67. $this->_init('sales_order_address', 'entity_id');
  68. }
  69. /**
  70. * Return configuration for all attributes
  71. *
  72. * @return array
  73. */
  74. public function getAllAttributes()
  75. {
  76. $attributes = [
  77. 'city' => __('City'),
  78. 'company' => __('Company'),
  79. 'country_id' => __('Country'),
  80. 'email' => __('Email'),
  81. 'firstname' => __('First Name'),
  82. 'lastname' => __('Last Name'),
  83. 'region_id' => __('State/Province'),
  84. 'street' => __('Street Address'),
  85. 'telephone' => __('Phone Number'),
  86. 'postcode' => __('Zip/Postal Code'),
  87. ];
  88. asort($attributes);
  89. return $attributes;
  90. }
  91. /**
  92. * Performs validation before save
  93. *
  94. * @param \Magento\Framework\Model\AbstractModel $object
  95. * @return $this
  96. * @throws \Magento\Framework\Exception\LocalizedException
  97. */
  98. protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
  99. {
  100. parent::_beforeSave($object);
  101. if (!$object->getParentId() && $object->getOrder()) {
  102. $object->setParentId($object->getOrder()->getId());
  103. }
  104. // Init customer address id if customer address is assigned
  105. $customerData = $object->getCustomerAddressData();
  106. if ($customerData) {
  107. $object->setCustomerAddressId($customerData->getId());
  108. }
  109. $warnings = $this->_validator->validate($object);
  110. if (!empty($warnings)) {
  111. throw new \Magento\Framework\Exception\LocalizedException(
  112. __("We can't save the address:\n%1", implode("\n", $warnings))
  113. );
  114. }
  115. return $this;
  116. }
  117. }