123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- <?php
- /**
- * Copyright © 2015 Ihor Vansach (ihor@magefan.com). All rights reserved.
- * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
- *
- * Glory to Ukraine! Glory to the heroes!
- */
- namespace Magefan\Blog\Block\Adminhtml\Post\Edit\Tab;
- use Magento\Backend\Block\Widget\Grid\Column;
- use Magento\Backend\Block\Widget\Grid\Extended;
- /**
- * Admin blog post edit form related posts tab
- */
- class RelatedPosts extends Extended implements \Magento\Backend\Block\Widget\Tab\TabInterface
- {
- /**
- * Core registry
- *
- * @var \Magento\Framework\Registry
- */
- protected $_coreRegistry = null;
- /**
- * @var \Magento\Catalog\Model\Post\Attribute\Source\Status
- */
- protected $_status;
- /**
- * @param \Magento\Backend\Block\Template\Context $context
- * @param \Magento\Backend\Helper\Data $backendHelper
- * @param \Magento\Catalog\Model\Post\Attribute\Source\Status $status
- * @param \Magento\Framework\Registry $coreRegistry
- * @param array $data
- */
- public function __construct(
- \Magento\Backend\Block\Template\Context $context,
- \Magento\Backend\Helper\Data $backendHelper,
- \Magento\Catalog\Model\Product\Attribute\Source\Status $status,
- \Magento\Framework\Registry $coreRegistry,
- array $data = []
- ) {
- $this->_status = $status;
- $this->_coreRegistry = $coreRegistry;
- parent::__construct($context, $backendHelper, $data);
- }
- /**
- * Set grid params
- *
- * @return void
- */
- protected function _construct()
- {
- parent::_construct();
- $this->setId('related_posts_section');
- $this->setDefaultSort('post_id');
- $this->setUseAjax(true);
- if ($this->getPost() && $this->getPost()->getId()) {
- $this->setDefaultFilter(['in_posts' => 1]);
- }
- if ($this->isReadonly()) {
- $this->setFilterVisibility(false);
- }
- }
- /**
- * Retrieve currently edited post model
- *
- * @return array|null
- */
- public function getPost()
- {
- return $this->_coreRegistry->registry('current_model');
- }
- /**
- * Add filter
- *
- * @param Column $column
- * @return $this
- */
- protected function _addColumnFilterToCollection($column)
- {
- // Set custom filter for in post flag
- if ($column->getId() == 'in_posts') {
- $postIds = $this->_getSelectedPosts();
- if (empty($postIds)) {
- $postIds = 0;
- }
- if ($column->getFilter()->getValue()) {
- $this->getCollection()->addFieldToFilter('post_id', ['in' => $postIds]);
- } else {
- if ($postIds) {
- $this->getCollection()->addFieldToFilter('post_id', ['nin' => $postIds]);
- }
- }
- } else {
- parent::_addColumnFilterToCollection($column);
- }
- return $this;
- }
- /**
- * Prepare collection
- *
- * @return Extended
- */
- protected function _prepareCollection()
- {
- $post = $this->getPost();
- $collection = $post->getCollection()
- ->addFieldToFilter('post_id', array('neq' => $post->getId()));
- $this->setCollection($collection);
- return parent::_prepareCollection();
- }
- /**
- * Checks when this block is readonly
- *
- * @return bool
- */
- public function isReadonly()
- {
- return false;
- }
- /**
- * Add columns to grid
- *
- * @return $this
- */
- protected function _prepareColumns()
- {
- if (!$this->isReadonly()) {
- $this->addColumn(
- 'in_posts',
- [
- 'type' => 'checkbox',
- 'name' => 'in_posts',
- 'values' => $this->_getSelectedPosts(),
- 'align' => 'center',
- 'index' => 'post_id',
- 'header_css_class' => 'col-select',
- 'column_css_class' => 'col-select'
- ]
- );
- }
- $this->addColumn(
- 'post_id',
- [
- 'header' => __('ID'),
- 'sortable' => true,
- 'index' => 'post_id',
- 'header_css_class' => 'col-id',
- 'column_css_class' => 'col-id'
- ]
- );
- $this->addColumn(
- 'title',
- [
- 'header' => __('Title'),
- 'index' => 'title',
- 'header_css_class' => 'col-name',
- 'column_css_class' => 'col-name'
- ]
- );
- $this->addColumn(
- 'identifier',
- [
- 'header' => __('URL Key'),
- 'index' => 'identifier',
- 'header_css_class' => 'col-name',
- 'column_css_class' => 'col-name'
- ]
- );
- /**
- * Check is single store mode
- */
- if (!$this->_storeManager->isSingleStoreMode()) {
- $this->addColumn(
- 'store_id',
- [
- 'header' => __('Store View'),
- 'index' => 'store_id',
- 'type' => 'store',
- 'store_all' => true,
- 'store_view' => true,
- 'sortable' => false,
- ]
- );
- }
- $this->addColumn(
- 'is_active',
- [
- 'header' => __('Status'),
- 'index' => 'is_active',
- 'type' => 'options',
- 'options' => $this->_status->getOptionArray(),
- 'header_css_class' => 'col-status',
- 'column_css_class' => 'col-status',
- 'frame_callback' => array(
- $this->getLayout()->createBlock('Magefan\Blog\Block\Adminhtml\Grid\Column\Statuses'),
- 'decorateStatus'
- ),
- ]
- );
- $this->addColumn(
- 'position',
- [
- 'header' => __('Position'),
- 'name' => 'position',
- 'type' => 'number',
- 'validate_class' => 'validate-number',
- 'index' => 'position',
- 'editable' => true,
- 'edit_only' => false,
- 'sortable' => false,
- 'filter' => false,
- 'header_css_class' => 'col-position',
- 'column_css_class' => 'col-position'
- ]
- );
- return parent::_prepareColumns();
- }
- /**
- * Rerieve grid URL
- *
- * @return string
- */
- public function getGridUrl()
- {
- return $this->getData(
- 'grid_url'
- ) ? $this->getData(
- 'grid_url'
- ) : $this->getUrl(
- 'blog/post/relatedPostsGrid',
- ['_current' => true]
- );
- }
- /**
- * Retrieve selected related posts
- *
- * @return array
- */
- protected function _getSelectedPosts()
- {
- $posts = $this->getPostsRelated();
- if (!is_array($posts)) {
- $posts = array_keys($this->getSelectedRelatedPosts());
- }
- return $posts;
- }
- /**
- * Retrieve related posts
- *
- * @return array
- */
- public function getSelectedRelatedPosts()
- {
- $posts = [];
- foreach ($this->_coreRegistry->registry('current_model')->getRelatedPosts() as $post) {
- $posts[$post->getId()] = ['position' => $post->getPosition()];
- }
- return $posts;
- }
- public function getTabLabel()
- {
- return __('Related Posts');
- }
- /**
- * Prepare title for tab
- *
- * @return \Magento\Framework\Phrase
- */
- public function getTabTitle()
- {
- return __('Related Posts');
- }
- /**
- * Returns status flag about this tab can be shown or not
- *
- * @return bool
- */
- public function canShowTab()
- {
- return true;
- }
- /**
- * Returns status flag about this tab hidden or not
- *
- * @return bool
- */
- public function isHidden()
- {
- return false;
- }
- }
|