Collection.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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\Update;
  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. * Name prefix of events that are dispatched by model
  18. *
  19. * @var string
  20. */
  21. protected $_eventPrefix = 'layout_update_collection';
  22. /**
  23. * Name of event parameter
  24. *
  25. * @var string
  26. */
  27. protected $_eventObject = 'layout_update_collection';
  28. /**
  29. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  30. * @param \Psr\Log\LoggerInterface $logger
  31. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  32. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  33. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  34. * @param mixed $connection
  35. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  36. */
  37. public function __construct(
  38. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  39. \Psr\Log\LoggerInterface $logger,
  40. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  41. \Magento\Framework\Event\ManagerInterface $eventManager,
  42. \Magento\Framework\Stdlib\DateTime $dateTime,
  43. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  44. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  45. ) {
  46. $this->dateTime = $dateTime;
  47. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  48. }
  49. /**
  50. * Define resource model
  51. *
  52. * @return void
  53. */
  54. protected function _construct()
  55. {
  56. parent::_construct();
  57. $this->_init(
  58. \Magento\Widget\Model\Layout\Update::class,
  59. \Magento\Widget\Model\ResourceModel\Layout\Update::class
  60. );
  61. }
  62. /**
  63. * Add filter by theme id
  64. *
  65. * @param int $themeId
  66. * @return $this
  67. */
  68. public function addThemeFilter($themeId)
  69. {
  70. $this->_joinWithLink();
  71. $this->getSelect()->where('link.theme_id = ?', $themeId);
  72. return $this;
  73. }
  74. /**
  75. * Add filter by store id
  76. *
  77. * @param int $storeId
  78. * @return $this
  79. */
  80. public function addStoreFilter($storeId)
  81. {
  82. $this->_joinWithLink();
  83. $this->getSelect()->where('link.store_id = ?', $storeId);
  84. return $this;
  85. }
  86. /**
  87. * Join with layout link table
  88. *
  89. * @return $this
  90. */
  91. protected function _joinWithLink()
  92. {
  93. $flagName = 'joined_with_link_table';
  94. if (!$this->getFlag($flagName)) {
  95. $this->getSelect()->join(
  96. ['link' => $this->getTable('layout_link')],
  97. 'link.layout_update_id = main_table.layout_update_id',
  98. ['store_id', 'theme_id']
  99. );
  100. $this->setFlag($flagName, true);
  101. }
  102. return $this;
  103. }
  104. /**
  105. * Left Join with layout link table
  106. *
  107. * @param array $fields
  108. * @return $this
  109. */
  110. protected function _joinLeftWithLink($fields = [])
  111. {
  112. $flagName = 'joined_left_with_link_table';
  113. if (!$this->getFlag($flagName)) {
  114. $this->getSelect()->joinLeft(
  115. ['link' => $this->getTable('layout_link')],
  116. 'link.layout_update_id = main_table.layout_update_id',
  117. [$fields]
  118. );
  119. $this->setFlag($flagName, true);
  120. }
  121. return $this;
  122. }
  123. /**
  124. * Get layouts that are older then specified number of days
  125. *
  126. * @param string $days
  127. * @return $this
  128. */
  129. public function addUpdatedDaysBeforeFilter($days)
  130. {
  131. $datetime = new \DateTime('now', new \DateTimeZone('UTC'));
  132. $storeInterval = new \DateInterval('P' . $days . 'D');
  133. $datetime->sub($storeInterval);
  134. $formattedDate = $this->dateTime->formatDate($datetime->getTimestamp());
  135. $this->addFieldToFilter(
  136. 'main_table.updated_at',
  137. ['notnull' => true]
  138. )->addFieldToFilter(
  139. 'main_table.updated_at',
  140. ['lt' => $formattedDate]
  141. );
  142. return $this;
  143. }
  144. /**
  145. * Get layouts without links
  146. *
  147. * @return $this
  148. */
  149. public function addNoLinksFilter()
  150. {
  151. $this->_joinLeftWithLink();
  152. $this->addFieldToFilter('link.layout_update_id', ['null' => true]);
  153. return $this;
  154. }
  155. /**
  156. * Delete updates in collection
  157. *
  158. * @return $this
  159. */
  160. public function delete()
  161. {
  162. /** @var $update \Magento\Widget\Model\Layout\Update */
  163. foreach ($this->getItems() as $update) {
  164. $update->delete();
  165. }
  166. return $this;
  167. }
  168. }