Rules.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\ResourceModel;
  3. use Dotdigitalgroup\Email\Model\ResourceModel\Cron\Collection;
  4. use Dotdigitalgroup\Email\Model\Config\Json;
  5. use Dotdigitalgroup\Email\Setup\Schema;
  6. class Rules extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  7. {
  8. /**
  9. * @var Json
  10. */
  11. protected $serializer;
  12. /**
  13. * Initialize resource.
  14. *
  15. * @return null
  16. */
  17. public function _construct()
  18. {
  19. $this->_init(Schema::EMAIL_RULES_TABLE, 'id');
  20. }
  21. /**
  22. * Rules constructor.
  23. *
  24. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  25. * @param Json $serializer
  26. * @param null $connectionName
  27. */
  28. public function __construct(
  29. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  30. Json $serializer,
  31. $connectionName = null
  32. ) {
  33. $this->serializer = $serializer;
  34. parent::__construct(
  35. $context,
  36. $connectionName
  37. );
  38. }
  39. protected function _afterLoad(\Magento\Framework\Model\AbstractModel $object)
  40. {
  41. $object->setCondition($this->serializer->unserialize($object->getConditions()));
  42. return parent::_afterLoad($object);
  43. }
  44. /**
  45. * Join tables on collection by type.
  46. *
  47. * @param Collection $collection
  48. * @param string $type
  49. *
  50. * @return Collection
  51. */
  52. public function joinTablesOnCollectionByType($collection, $type)
  53. {
  54. if ($type == \Dotdigitalgroup\Email\Model\Rules::ABANDONED) {
  55. $collection->getSelect()
  56. ->joinLeft(
  57. ['quote_address' => $this->getTable('quote_address')],
  58. 'main_table.entity_id = quote_address.quote_id',
  59. ['shipping_method', 'country_id', 'city', 'region_id']
  60. )->joinLeft(
  61. ['quote_payment' => $this->getTable('quote_payment')],
  62. 'main_table.entity_id = quote_payment.quote_id',
  63. ['method']
  64. )->where('address_type = ?', 'shipping');
  65. } elseif ($type == \Dotdigitalgroup\Email\Model\Rules::REVIEW) {
  66. $collection->getSelect()
  67. ->join(
  68. ['order_address' => $this->getTable('sales_order_address')],
  69. 'main_table.entity_id = order_address.parent_id',
  70. ['country_id', 'city', 'region_id']
  71. )->join(
  72. ['order_payment' => $this->getTable('sales_order_payment')],
  73. 'main_table.entity_id = order_payment.parent_id',
  74. ['method']
  75. )->where('order_address.address_type = ?', 'shipping');
  76. }
  77. return $collection;
  78. }
  79. }