Collection.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. /**
  3. * Copyright © 2015 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Model\ResourceModel\Post;
  9. /**
  10. * Blog post collection
  11. */
  12. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  13. {
  14. /**
  15. * @var \Magento\Store\Model\StoreManagerInterface
  16. */
  17. protected $_storeManager;
  18. /**
  19. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  20. */
  21. protected $_date;
  22. /**
  23. * @var int
  24. */
  25. protected $_storeId;
  26. /**
  27. * @var \Magefan\Blog\Model\Category
  28. */
  29. protected $category;
  30. /**
  31. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  32. * @param \Psr\Log\LoggerInterface $logger
  33. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  34. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  35. * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
  36. * @param Magento\Store\Model\StoreManagerInterface $storeManager
  37. * @param null|\Zend_Db_Adapter_Abstract $connection
  38. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  39. */
  40. public function __construct(
  41. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  42. \Psr\Log\LoggerInterface $logger,
  43. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  44. \Magento\Framework\Event\ManagerInterface $eventManager,
  45. \Magento\Framework\Stdlib\DateTime\DateTime $date,
  46. \Magento\Store\Model\StoreManagerInterface $storeManager,
  47. $connection = null,
  48. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  49. ) {
  50. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  51. $this->_date = $date;
  52. $this->_storeManager = $storeManager;
  53. }
  54. /**
  55. * Constructor
  56. * Configures collection
  57. *
  58. * @return void
  59. */
  60. protected function _construct()
  61. {
  62. parent::_construct();
  63. $this->_init('Magefan\Blog\Model\Post', 'Magefan\Blog\Model\ResourceModel\Post');
  64. $this->_map['fields']['post_id'] = 'main_table.post_id';
  65. $this->_map['fields']['store'] = 'store_table.store_id';
  66. $this->_map['fields']['category'] = 'category_table.category_id';
  67. $this->_map['fields']['tag'] = 'tag_table.tag_id';
  68. }
  69. /**
  70. * Add field filter to collection
  71. *
  72. * @param string|array $field
  73. * @param null|string|array $condition
  74. * @return $this
  75. */
  76. public function addFieldToFilter($field, $condition = null)
  77. {
  78. if ($field === 'store_id' || $field === 'store_ids') {
  79. return $this->addStoreFilter($condition);
  80. }
  81. return parent::addFieldToFilter($field, $condition);
  82. }
  83. /**
  84. * Add store filter to collection
  85. * @param array|int|\Magento\Store\Model\Store $store
  86. * @param boolean $withAdmin
  87. * @return $this
  88. */
  89. public function addStoreFilter($store, $withAdmin = true)
  90. {
  91. if ($store === null) {
  92. return $this;
  93. }
  94. if (!$this->getFlag('store_filter_added')) {
  95. if ($store instanceof \Magento\Store\Model\Store) {
  96. $this->_storeId = $store->getId();
  97. $store = [$store->getId()];
  98. }
  99. if (!is_array($store)) {
  100. $this->_storeId = $store;
  101. $store = [$store];
  102. }
  103. if (in_array(\Magento\Store\Model\Store::DEFAULT_STORE_ID, $store)) {
  104. return $this;
  105. }
  106. if ($withAdmin) {
  107. $store[] = \Magento\Store\Model\Store::DEFAULT_STORE_ID;
  108. }
  109. $this->addFilter('store', ['in' => $store], 'public');
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Add category filter to collection
  115. * @param array|int|\Magefan\Blog\Model\Category $category
  116. * @return $this
  117. */
  118. public function addCategoryFilter($category)
  119. {
  120. if (!$this->getFlag('category_filter_added')) {
  121. if ($category instanceof \Magefan\Blog\Model\Category) {
  122. $this->category = $category;
  123. $categories = $category->getChildrenIds();
  124. $categories[] = $category->getId();
  125. } else {
  126. $categories = $category;
  127. if (!is_array($categories)) {
  128. $categories = [$categories];
  129. }
  130. }
  131. $this->addFilter('category', ['in' => $categories], 'public');
  132. }
  133. return $this;
  134. }
  135. /**
  136. * Add archive filter to collection
  137. * @param int $year
  138. * @param int $month
  139. * @return $this
  140. */
  141. public function addArchiveFilter($year, $month)
  142. {
  143. $this->getSelect()
  144. ->where('YEAR(publish_time) = ?', $year)
  145. ->where('MONTH(publish_time) = ?', $month);
  146. return $this;
  147. }
  148. /**
  149. * Add search filter to collection
  150. * @param string $term
  151. * @return $this
  152. */
  153. public function addSearchFilter($term)
  154. {
  155. $this->addFieldToFilter(
  156. ['title', 'content_heading', 'content'],
  157. [
  158. ['like' => '%' . $term . '%'],
  159. ['like' => '%' . $term . '%'],
  160. ['like' => '% ' . $term . ' %']
  161. ]
  162. );
  163. return $this;
  164. }
  165. /**
  166. * Add tag filter to collection
  167. * @param array|int|\Magefan\Blog\Model\Tag $tag
  168. * @return $this
  169. */
  170. public function addTagFilter($tag)
  171. {
  172. if (!$this->getFlag('tag_filter_added')) {
  173. if ($tag instanceof \Magefan\Blog\Model\Tag) {
  174. $tag = [$tag->getId()];
  175. }
  176. if (!is_array($tag)) {
  177. $tag = [$tag];
  178. }
  179. $this->addFilter('tag', ['in' => $tag], 'public');
  180. }
  181. return $this;
  182. }
  183. /**
  184. * Add author filter to collection
  185. * @param array|int|\Magefan\Blog\Model\Author $author
  186. * @return $this
  187. */
  188. public function addAuthorFilter($author)
  189. {
  190. if (!$this->getFlag('author_filter_added')) {
  191. if ($author instanceof \Magefan\Blog\Model\Author) {
  192. $author = [$author->getId()];
  193. }
  194. if (!is_array($author)) {
  195. $author = [$author];
  196. }
  197. $this->addFilter('author_id', ['in' => $author], 'public');
  198. }
  199. return $this;
  200. }
  201. /**
  202. * Add is_active filter to collection
  203. * @return $this
  204. */
  205. public function addActiveFilter()
  206. {
  207. return $this
  208. ->addFieldToFilter('is_active', 1)
  209. ->addFieldToFilter('publish_time', ['lteq' => $this->_date->gmtDate()]);
  210. }
  211. /**
  212. * Get SQL for get record count
  213. *
  214. * Extra GROUP BY strip added.
  215. *
  216. * @return \Magento\Framework\DB\Select
  217. */
  218. public function getSelectCountSql()
  219. {
  220. $countSelect = parent::getSelectCountSql();
  221. $countSelect->reset(\Magento\Framework\DB\Select::GROUP);
  222. return $countSelect;
  223. }
  224. /**
  225. * Perform operations after collection load
  226. *
  227. * @return $this
  228. */
  229. protected function _afterLoad()
  230. {
  231. $items = $this->getColumnValues('post_id');
  232. if (count($items)) {
  233. $connection = $this->getConnection();
  234. $tableName = $this->getTable('magefan_blog_post_store');
  235. $select = $connection->select()
  236. ->from(['cps' => $tableName])
  237. ->where('cps.post_id IN (?)', $items);
  238. $result = [];
  239. foreach ($connection->fetchAll($select) as $item) {
  240. if (!isset($result[$item['post_id']])) {
  241. $result[$item['post_id']] = [];
  242. }
  243. $result[$item['post_id']][] = $item['store_id'];
  244. }
  245. if ($result) {
  246. foreach ($this as $item) {
  247. $postId = $item->getData('post_id');
  248. if (!isset($result[$postId])) {
  249. continue;
  250. }
  251. if ($result[$postId] == 0) {
  252. $stores = $this->_storeManager->getStores(false, true);
  253. $storeId = current($stores)->getId();
  254. } else {
  255. $storeId = $result[$item->getData('post_id')];
  256. }
  257. $item->setData('_first_store_id', $storeId);
  258. $item->setData('store_ids', $result[$postId]);
  259. }
  260. }
  261. foreach ($this as $item) {
  262. if ($this->_storeId) {
  263. $item->setStoreId($this->_storeId);
  264. }
  265. if ($this->category) {
  266. $item->setData('parent_category', $this->category);
  267. }
  268. }
  269. $map = [
  270. 'category' => 'categories',
  271. 'tag' => 'tags',
  272. ];
  273. foreach ($map as $key => $property) {
  274. $tableName = $this->getTable('magefan_blog_post_' . $key);
  275. $select = $connection->select()
  276. ->from(['cps' => $tableName])
  277. ->where('cps.post_id IN (?)', $items);
  278. $result = $connection->fetchAll($select);
  279. if ($result) {
  280. $data = [];
  281. foreach($result as $item) {
  282. $data[$item['post_id']][] = $item[$key . '_id'];
  283. }
  284. foreach ($this as $item) {
  285. $postId = $item->getData('post_id');
  286. if (isset($data[$postId])) {
  287. $item->setData($property, $data[$postId]);
  288. }
  289. }
  290. }
  291. }
  292. }
  293. $this->_previewFlag = false;
  294. return parent::_afterLoad();
  295. }
  296. /**
  297. * Join store relation table if there is store filter
  298. *
  299. * @return void
  300. */
  301. protected function _renderFiltersBefore()
  302. {
  303. foreach(['store', 'category', 'tag'] as $key) {
  304. if ($this->getFilter($key)) {
  305. $this->getSelect()->join(
  306. [$key.'_table' => $this->getTable('magefan_blog_post_'.$key)],
  307. 'main_table.post_id = '.$key.'_table.post_id',
  308. []
  309. )->group(
  310. 'main_table.post_id'
  311. );
  312. }
  313. }
  314. parent::_renderFiltersBefore();
  315. }
  316. }