Collection.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\CheckoutAgreements\Model\ResourceModel\Agreement\Grid;
  8. /**
  9. * CheckoutAgreement Grid Collection
  10. */
  11. class Collection extends \Magento\CheckoutAgreements\Model\ResourceModel\Agreement\Collection
  12. {
  13. /**
  14. * @inheritdoc
  15. */
  16. public function load($printQuery = false, $logQuery = false)
  17. {
  18. if ($this->isLoaded()) {
  19. return $this;
  20. }
  21. parent::load($printQuery, $logQuery);
  22. $this->addStoresToResult();
  23. return $this;
  24. }
  25. /**
  26. * Add stores to result
  27. *
  28. * @return void
  29. */
  30. private function addStoresToResult()
  31. {
  32. $stores = $this->getStoresForAgreements();
  33. if (!empty($stores)) {
  34. $storesByAgreementId = [];
  35. foreach ($stores as $storeData) {
  36. $storesByAgreementId[$storeData['agreement_id']][] = $storeData['store_id'];
  37. }
  38. foreach ($this as $item) {
  39. $agreementId = $item->getData('agreement_id');
  40. if (!isset($storesByAgreementId[$agreementId])) {
  41. continue;
  42. }
  43. $item->setData('stores', $storesByAgreementId[$agreementId]);
  44. }
  45. }
  46. }
  47. /**
  48. * Get stores for agreements
  49. *
  50. * @return array
  51. */
  52. private function getStoresForAgreements()
  53. {
  54. $agreementId = $this->getColumnValues('agreement_id');
  55. if (!empty($agreementId)) {
  56. $select = $this->getConnection()->select()->from(
  57. ['agreement_store' => $this->getResource()->getTable('checkout_agreement_store')]
  58. )->where(
  59. 'agreement_store.agreement_id IN (?)',
  60. $agreementId
  61. );
  62. return $this->getConnection()->fetchAll($select);
  63. }
  64. return [];
  65. }
  66. }