Collection.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Widget\Model\ResourceModel\Layout\Link;
  7. /**
  8. * Layout update collection model
  9. */
  10. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  11. {
  12. /**
  13. * @var \Magento\Framework\Stdlib\DateTime
  14. */
  15. protected $dateTime;
  16. /**
  17. * @param \Psr\Log\LoggerInterface $logger
  18. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  19. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  20. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  21. * @param mixed $connection
  22. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  23. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  24. */
  25. public function __construct(
  26. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  27. \Psr\Log\LoggerInterface $logger,
  28. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  29. \Magento\Framework\Event\ManagerInterface $eventManager,
  30. \Magento\Framework\Stdlib\DateTime $dateTime,
  31. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  32. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  33. ) {
  34. $this->dateTime = $dateTime;
  35. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  36. }
  37. /**
  38. * Define resource model
  39. *
  40. * @return void
  41. */
  42. protected function _construct()
  43. {
  44. parent::_construct();
  45. $this->_init(\Magento\Widget\Model\Layout\Link::class, \Magento\Widget\Model\ResourceModel\Layout\Link::class);
  46. }
  47. /**
  48. * Add filter by theme id
  49. *
  50. * @param int $themeId
  51. * @return $this
  52. */
  53. public function addThemeFilter($themeId)
  54. {
  55. $this->addFieldToFilter('theme_id', $themeId);
  56. return $this;
  57. }
  58. /**
  59. * Join with layout update table
  60. *
  61. * @param array $fields
  62. * @return $this
  63. */
  64. protected function _joinWithUpdate($fields = [])
  65. {
  66. $flagName = 'joined_with_update_table';
  67. if (!$this->getFlag($flagName)) {
  68. $this->getSelect()->join(
  69. ['update' => $this->getTable('layout_update')],
  70. 'update.layout_update_id = main_table.layout_update_id',
  71. [$fields]
  72. );
  73. $this->setFlag($flagName, true);
  74. }
  75. return $this;
  76. }
  77. /**
  78. * Filter by temporary flag
  79. *
  80. * @param bool $isTemporary
  81. * @return $this
  82. */
  83. public function addTemporaryFilter($isTemporary)
  84. {
  85. $this->addFieldToFilter('main_table.is_temporary', $isTemporary ? 1 : 0);
  86. return $this;
  87. }
  88. /**
  89. * Get links for layouts that are older than specified number of days
  90. *
  91. * @param string $days
  92. * @return $this
  93. */
  94. public function addUpdatedDaysBeforeFilter($days)
  95. {
  96. $datetime = new \DateTime('now', new \DateTimeZone('UTC'));
  97. $storeInterval = new \DateInterval('P' . $days . 'D');
  98. $datetime->sub($storeInterval);
  99. $formattedDate = $this->dateTime->formatDate($datetime->getTimestamp());
  100. $this->_joinWithUpdate();
  101. $this->addFieldToFilter(
  102. 'update.updated_at',
  103. ['notnull' => true]
  104. )->addFieldToFilter(
  105. 'update.updated_at',
  106. ['lt' => $formattedDate]
  107. );
  108. return $this;
  109. }
  110. }