Collection.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CheckoutAgreements\Model\ResourceModel\Agreement;
  7. /**
  8. * Resource Model for Agreement Collection
  9. */
  10. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  11. {
  12. /**
  13. * @var array
  14. */
  15. protected $_map = [
  16. 'fields' => [
  17. 'agreement_id' => 'main_table.agreement_id'
  18. ]
  19. ];
  20. /**
  21. * Is store filter with admin store
  22. *
  23. * @var bool
  24. */
  25. protected $_isStoreFilterWithAdmin = true;
  26. /**
  27. * Initialize resource
  28. *
  29. * @return void
  30. */
  31. protected function _construct()
  32. {
  33. $this->_init(
  34. \Magento\CheckoutAgreements\Model\Agreement::class,
  35. \Magento\CheckoutAgreements\Model\ResourceModel\Agreement::class
  36. );
  37. }
  38. /**
  39. * Filter collection by specified store ids
  40. *
  41. * @param int|\Magento\Store\Model\Store $store
  42. * @return $this
  43. */
  44. public function addStoreFilter($store)
  45. {
  46. // check and prepare data
  47. if ($store instanceof \Magento\Store\Model\Store) {
  48. $store = [$store->getId()];
  49. } elseif (is_numeric($store)) {
  50. $store = [$store];
  51. }
  52. $alias = 'store_table_' . implode('_', $store);
  53. if ($this->getFlag($alias)) {
  54. return $this;
  55. }
  56. $storeFilter = [$store];
  57. if ($this->_isStoreFilterWithAdmin) {
  58. $storeFilter[] = 0;
  59. }
  60. // add filter
  61. $this->getSelect()->join(
  62. [$alias => $this->getTable('checkout_agreement_store')],
  63. 'main_table.agreement_id = ' . $alias . '.agreement_id',
  64. []
  65. )->where(
  66. $alias . '.store_id IN (?)',
  67. $storeFilter
  68. )->group(
  69. 'main_table.agreement_id'
  70. );
  71. $this->setFlag($alias, true);
  72. return $this;
  73. }
  74. /**
  75. * Make store filter using admin website or not
  76. *
  77. * @param bool $value
  78. * @return $this
  79. * @codeCoverageIgnore
  80. */
  81. public function setIsStoreFilterWithAdmin($value)
  82. {
  83. $this->_isStoreFilterWithAdmin = (bool)$value;
  84. return $this;
  85. }
  86. }