RelatedPosts.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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\Block\Adminhtml\Post\Edit\Tab;
  9. use Magento\Backend\Block\Widget\Grid\Column;
  10. use Magento\Backend\Block\Widget\Grid\Extended;
  11. /**
  12. * Admin blog post edit form related posts tab
  13. */
  14. class RelatedPosts extends Extended implements \Magento\Backend\Block\Widget\Tab\TabInterface
  15. {
  16. /**
  17. * Core registry
  18. *
  19. * @var \Magento\Framework\Registry
  20. */
  21. protected $_coreRegistry = null;
  22. /**
  23. * @var \Magento\Catalog\Model\Post\Attribute\Source\Status
  24. */
  25. protected $_status;
  26. /**
  27. * @param \Magento\Backend\Block\Template\Context $context
  28. * @param \Magento\Backend\Helper\Data $backendHelper
  29. * @param \Magento\Catalog\Model\Post\Attribute\Source\Status $status
  30. * @param \Magento\Framework\Registry $coreRegistry
  31. * @param array $data
  32. */
  33. public function __construct(
  34. \Magento\Backend\Block\Template\Context $context,
  35. \Magento\Backend\Helper\Data $backendHelper,
  36. \Magento\Catalog\Model\Product\Attribute\Source\Status $status,
  37. \Magento\Framework\Registry $coreRegistry,
  38. array $data = []
  39. ) {
  40. $this->_status = $status;
  41. $this->_coreRegistry = $coreRegistry;
  42. parent::__construct($context, $backendHelper, $data);
  43. }
  44. /**
  45. * Set grid params
  46. *
  47. * @return void
  48. */
  49. protected function _construct()
  50. {
  51. parent::_construct();
  52. $this->setId('related_posts_section');
  53. $this->setDefaultSort('post_id');
  54. $this->setUseAjax(true);
  55. if ($this->getPost() && $this->getPost()->getId()) {
  56. $this->setDefaultFilter(['in_posts' => 1]);
  57. }
  58. if ($this->isReadonly()) {
  59. $this->setFilterVisibility(false);
  60. }
  61. }
  62. /**
  63. * Retrieve currently edited post model
  64. *
  65. * @return array|null
  66. */
  67. public function getPost()
  68. {
  69. return $this->_coreRegistry->registry('current_model');
  70. }
  71. /**
  72. * Add filter
  73. *
  74. * @param Column $column
  75. * @return $this
  76. */
  77. protected function _addColumnFilterToCollection($column)
  78. {
  79. // Set custom filter for in post flag
  80. if ($column->getId() == 'in_posts') {
  81. $postIds = $this->_getSelectedPosts();
  82. if (empty($postIds)) {
  83. $postIds = 0;
  84. }
  85. if ($column->getFilter()->getValue()) {
  86. $this->getCollection()->addFieldToFilter('post_id', ['in' => $postIds]);
  87. } else {
  88. if ($postIds) {
  89. $this->getCollection()->addFieldToFilter('post_id', ['nin' => $postIds]);
  90. }
  91. }
  92. } else {
  93. parent::_addColumnFilterToCollection($column);
  94. }
  95. return $this;
  96. }
  97. /**
  98. * Prepare collection
  99. *
  100. * @return Extended
  101. */
  102. protected function _prepareCollection()
  103. {
  104. $post = $this->getPost();
  105. $collection = $post->getCollection()
  106. ->addFieldToFilter('post_id', array('neq' => $post->getId()));
  107. $this->setCollection($collection);
  108. return parent::_prepareCollection();
  109. }
  110. /**
  111. * Checks when this block is readonly
  112. *
  113. * @return bool
  114. */
  115. public function isReadonly()
  116. {
  117. return false;
  118. }
  119. /**
  120. * Add columns to grid
  121. *
  122. * @return $this
  123. */
  124. protected function _prepareColumns()
  125. {
  126. if (!$this->isReadonly()) {
  127. $this->addColumn(
  128. 'in_posts',
  129. [
  130. 'type' => 'checkbox',
  131. 'name' => 'in_posts',
  132. 'values' => $this->_getSelectedPosts(),
  133. 'align' => 'center',
  134. 'index' => 'post_id',
  135. 'header_css_class' => 'col-select',
  136. 'column_css_class' => 'col-select'
  137. ]
  138. );
  139. }
  140. $this->addColumn(
  141. 'post_id',
  142. [
  143. 'header' => __('ID'),
  144. 'sortable' => true,
  145. 'index' => 'post_id',
  146. 'header_css_class' => 'col-id',
  147. 'column_css_class' => 'col-id'
  148. ]
  149. );
  150. $this->addColumn(
  151. 'title',
  152. [
  153. 'header' => __('Title'),
  154. 'index' => 'title',
  155. 'header_css_class' => 'col-name',
  156. 'column_css_class' => 'col-name'
  157. ]
  158. );
  159. $this->addColumn(
  160. 'identifier',
  161. [
  162. 'header' => __('URL Key'),
  163. 'index' => 'identifier',
  164. 'header_css_class' => 'col-name',
  165. 'column_css_class' => 'col-name'
  166. ]
  167. );
  168. /**
  169. * Check is single store mode
  170. */
  171. if (!$this->_storeManager->isSingleStoreMode()) {
  172. $this->addColumn(
  173. 'store_id',
  174. [
  175. 'header' => __('Store View'),
  176. 'index' => 'store_id',
  177. 'type' => 'store',
  178. 'store_all' => true,
  179. 'store_view' => true,
  180. 'sortable' => false,
  181. ]
  182. );
  183. }
  184. $this->addColumn(
  185. 'is_active',
  186. [
  187. 'header' => __('Status'),
  188. 'index' => 'is_active',
  189. 'type' => 'options',
  190. 'options' => $this->_status->getOptionArray(),
  191. 'header_css_class' => 'col-status',
  192. 'column_css_class' => 'col-status',
  193. 'frame_callback' => array(
  194. $this->getLayout()->createBlock('Magefan\Blog\Block\Adminhtml\Grid\Column\Statuses'),
  195. 'decorateStatus'
  196. ),
  197. ]
  198. );
  199. $this->addColumn(
  200. 'position',
  201. [
  202. 'header' => __('Position'),
  203. 'name' => 'position',
  204. 'type' => 'number',
  205. 'validate_class' => 'validate-number',
  206. 'index' => 'position',
  207. 'editable' => true,
  208. 'edit_only' => false,
  209. 'sortable' => false,
  210. 'filter' => false,
  211. 'header_css_class' => 'col-position',
  212. 'column_css_class' => 'col-position'
  213. ]
  214. );
  215. return parent::_prepareColumns();
  216. }
  217. /**
  218. * Rerieve grid URL
  219. *
  220. * @return string
  221. */
  222. public function getGridUrl()
  223. {
  224. return $this->getData(
  225. 'grid_url'
  226. ) ? $this->getData(
  227. 'grid_url'
  228. ) : $this->getUrl(
  229. 'blog/post/relatedPostsGrid',
  230. ['_current' => true]
  231. );
  232. }
  233. /**
  234. * Retrieve selected related posts
  235. *
  236. * @return array
  237. */
  238. protected function _getSelectedPosts()
  239. {
  240. $posts = $this->getPostsRelated();
  241. if (!is_array($posts)) {
  242. $posts = array_keys($this->getSelectedRelatedPosts());
  243. }
  244. return $posts;
  245. }
  246. /**
  247. * Retrieve related posts
  248. *
  249. * @return array
  250. */
  251. public function getSelectedRelatedPosts()
  252. {
  253. $posts = [];
  254. foreach ($this->_coreRegistry->registry('current_model')->getRelatedPosts() as $post) {
  255. $posts[$post->getId()] = ['position' => $post->getPosition()];
  256. }
  257. return $posts;
  258. }
  259. public function getTabLabel()
  260. {
  261. return __('Related Posts');
  262. }
  263. /**
  264. * Prepare title for tab
  265. *
  266. * @return \Magento\Framework\Phrase
  267. */
  268. public function getTabTitle()
  269. {
  270. return __('Related Posts');
  271. }
  272. /**
  273. * Returns status flag about this tab can be shown or not
  274. *
  275. * @return bool
  276. */
  277. public function canShowTab()
  278. {
  279. return true;
  280. }
  281. /**
  282. * Returns status flag about this tab hidden or not
  283. *
  284. * @return bool
  285. */
  286. public function isHidden()
  287. {
  288. return false;
  289. }
  290. }