From e0399cd5cf4f5645d1b89095bbc8d750801e2149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20B=C3=A9jean?= <nicolas@bejean.eu> Date: Fri, 8 May 2020 17:15:37 +0200 Subject: [PATCH] =?UTF-8?q?D=C3=A9veloppement=20des=20UI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ui/Component/DataProvider.php | 131 ++++++++++++++++++ .../Listing/Column/CategoryWidget/Options.php | 42 ++++++ .../Listing/Column/CategoryWidgetActions.php | 114 +++++++++++++++ Ui/Component/Listing/Column/Store.php | 31 +++++ 4 files changed, 318 insertions(+) create mode 100755 Ui/Component/DataProvider.php create mode 100755 Ui/Component/Listing/Column/CategoryWidget/Options.php create mode 100755 Ui/Component/Listing/Column/CategoryWidgetActions.php create mode 100755 Ui/Component/Listing/Column/Store.php diff --git a/Ui/Component/DataProvider.php b/Ui/Component/DataProvider.php new file mode 100755 index 0000000..04de63e --- /dev/null +++ b/Ui/Component/DataProvider.php @@ -0,0 +1,131 @@ +<?php +namespace NicolasBejean\CategoryWidget\Ui\Component; + +use \Magento\Cms\Ui\Component\AddFilterInterface; +use \Magento\Framework\Api\Filter; +use \Magento\Framework\Api\FilterBuilder; +use \Magento\Framework\Api\Search\SearchCriteriaBuilder; +use \Magento\Framework\App\ObjectManager; +use \Magento\Framework\App\RequestInterface; +use \Magento\Framework\AuthorizationInterface; +use \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider as UiComponentDataProvider; +use \Magento\Framework\View\Element\UiComponent\DataProvider\Reporting; + +/** + * Class DataProvider + * + * @category PHP + * @package NicolasBejean\CategoryWidget\Ui\Component + * @author Nicolas Béjean <nicolas@bejean.eu> + * @license https://github.com/nicolasbejean/category-widget/blob/master/licence.txt BSD Licence + * @link https://www.bejean.eu + */ +class DataProvider extends UiComponentDataProvider +{ + /** @var string */ + private $isAllowedResource; + + /** + * @var AuthorizationInterface + */ + private $authorization; + + /** + * @var AddFilterInterface[] + */ + private $additionalFilterPool; + + /** + * @param string $title + * @param string $primaryFieldName + * @param string $requestFieldName + * @param Reporting $reporting + * @param SearchCriteriaBuilder $searchCriteriaBuilder + * @param RequestInterface $request + * @param FilterBuilder $filterBuilder + * @param array $meta + * @param array $data + * @param array $additionalFilterPool + * @SuppressWarnings(PHPMD.ExcessiveParameterList) + */ + public function __construct( + string $title, + string $primaryFieldName, + string $requestFieldName, + Reporting $reporting, + SearchCriteriaBuilder $searchCriteriaBuilder, + RequestInterface $request, + FilterBuilder $filterBuilder, + array $meta = [], + array $data = [], + array $additionalFilterPool = [] + ) { + parent::__construct( + $title, + $primaryFieldName, + $requestFieldName, + $reporting, + $searchCriteriaBuilder, + $request, + $filterBuilder, + $meta, + $data + ); + + $this->isAllowedResource = 'NicolasBejean_CategoryWidget::add'; + $this->meta = array_replace_recursive($meta, $this->prepareMetadata()); + $this->additionalFilterPool = $additionalFilterPool; + } + + /** + * @return bool + */ + private function getAuthorizationInstance() + { + if ($this->authorization === null) { + $this->authorization = ObjectManager::getInstance()->get(AuthorizationInterface::class); + } + + return $this->authorization->isAllowed($this->isAllowedResource); + } + + /** + * Prepares Meta + * + * @return array + */ + public function prepareMetadata() + { + $metadata = []; + + if (!$this->getAuthorizationInstance()) { + $metadata = [ + 'categorywidget_item_columns' => [ + 'arguments' => [ + 'data' => [ + 'config' => [ + 'editorConfig' => [ + 'enabled' => false + ] + ] + ] + ] + ] + ]; + } + + return $metadata; + } + + /** + * @inheritdoc + */ + public function addFilter(Filter $filter) + { + if (!empty($this->additionalFilterPool[$filter->getField()])) { + $this->additionalFilterPool[$filter->getField()]->addFilter($this->searchCriteriaBuilder, $filter); + } else { + parent::addFilter($filter); + } + } +} diff --git a/Ui/Component/Listing/Column/CategoryWidget/Options.php b/Ui/Component/Listing/Column/CategoryWidget/Options.php new file mode 100755 index 0000000..7ce68e9 --- /dev/null +++ b/Ui/Component/Listing/Column/CategoryWidget/Options.php @@ -0,0 +1,42 @@ +<?php +namespace NicolasBejean\CategoryWidget\Ui\Component\Listing\Column\CategoryWidget; + +use \Magento\Store\Ui\Component\Listing\Column\Store\Options as StoreOptions; + +/** + * Class Options + * + * @category PHP + * @package NNicolasBejean\CategoryWidget\Ui\Component\Listing\Column\CategoryWidget + * @author Nicolas Béjean <nicolas@bejean.eu> + * @license https://github.com/nicolasbejean/category-widget/blob/master/licence.txt BSD Licence + * @link https://www.bejean.eu + */ +class Options extends StoreOptions +{ + /** + * All Store Views value + */ + const ALL_STORE_VIEWS = '0'; + + /** + * Get options + * + * @return array + */ + public function toOptionArray() + { + if ($this->options !== null) { + return $this->options; + } + + $this->currentOptions['All Store Views']['label'] = __('All Store Views'); + $this->currentOptions['All Store Views']['value'] = self::ALL_STORE_VIEWS; + + $this->generateCurrentOptions(); + + $this->options = array_values($this->currentOptions); + + return $this->options; + } +} diff --git a/Ui/Component/Listing/Column/CategoryWidgetActions.php b/Ui/Component/Listing/Column/CategoryWidgetActions.php new file mode 100755 index 0000000..7e5f04d --- /dev/null +++ b/Ui/Component/Listing/Column/CategoryWidgetActions.php @@ -0,0 +1,114 @@ +<?php +namespace NicolasBejean\CategoryWidget\Ui\Component\Listing\Column; + +use \Magento\Framework\UrlInterface; +use \Magento\Framework\View\Element\UiComponent\ContextInterface; +use \Magento\Framework\View\Element\UiComponentFactory; +use \Magento\Ui\Component\Listing\Columns\Column; +use \Magento\Framework\App\ObjectManager; +use \Magento\Framework\Escaper; + +/** + * Class CategoryWidgetActions + * + * @category PHP + * @package NicolasBejean\CategoryWidget\Ui\Component\Listing\Column + * @author Nicolas Béjean <nicolas@bejean.eu> + * @license https://github.com/nicolasbejean/category-widget/blob/master/licence.txt BSD Licence + * @link https://www.bejean.eu + */ +class CategoryWidgetActions extends Column +{ + /** + * Url path + */ + const URL_PATH_EDIT = 'nicolasbejeancategorywidget/categorywidget/edit'; + const URL_PATH_DELETE = 'nicolasbejeancategorywidget/categorywidget/delete'; + + /** + * @var UrlInterface + */ + protected $urlBuilder; + + /** + * @var Escaper + */ + private $escaper; + + /** + * Constructor + * + * @param ContextInterface $context + * @param UiComponentFactory $uiComponentFactory + * @param UrlInterface $urlBuilder + * @param array $components + * @param array $data + */ + public function __construct( + ContextInterface $context, + UiComponentFactory $uiComponentFactory, + UrlInterface $urlBuilder, + array $components = [], + array $data = [] + ) { + $this->urlBuilder = $urlBuilder; + + parent::__construct($context, $uiComponentFactory, $components, $data); + } + + /** + * Prepare Data Source + * + * @param array $dataSource + * @return array + */ + public function prepareDataSource(array $dataSource) + { + if (isset($dataSource['data']['items'])) { + foreach ($dataSource['data']['items'] as & $item) { + if (isset($item['entity_id'])) { + $identifier = $this->getEscaperHtml($item['identifier']); + $item[$this->getData('name')] = [ + 'edit' => [ + 'href' => $this->urlBuilder->getUrl( + static::URL_PATH_EDIT, + [ + 'entity_id' => $item['entity_id'] + ] + ), + 'label' => __('Edit') + ], + 'delete' => [ + 'href' => $this->urlBuilder->getUrl( + static::URL_PATH_DELETE, + [ + 'entity_id' => $item['entity_id'] + ] + ), + 'label' => __('Delete'), + 'confirm' => [ + 'identifier' => __('Delete %1', $identifier), + 'message' => __('Are you sure you want to delete a %1 record?', $identifier) + ], + 'post' => true + ] + ]; + } + } + } + + return $dataSource; + } + + /** + * @param string|array $data + * @return array|string + */ + private function getEscaperHtml($data) + { + if (!$this->escaper) { + $this->escaper = ObjectManager::getInstance()->get(Escaper::class); + return $this->escaper->escapeHtml($data); + } + } +} diff --git a/Ui/Component/Listing/Column/Store.php b/Ui/Component/Listing/Column/Store.php new file mode 100755 index 0000000..75c93e8 --- /dev/null +++ b/Ui/Component/Listing/Column/Store.php @@ -0,0 +1,31 @@ +<?php +namespace NicolasBejean\CategoryWidget\Ui\Component\Listing\Column; + +use \Magento\Store\Ui\Component\Listing\Column\Store as ColumnStore; + +/** + * Class Store + * + * @category PHP + * @package NNicolasBejean\CategoryWidget\Ui\Component\Listing\Column + * @author Nicolas Béjean <nicolas@bejean.eu> + * @license https://github.com/nicolasbejean/category-widget/blob/master/licence.txt BSD Licence + * @link https://www.bejean.eu + */ +class Store extends ColumnStore +{ + /** + * Fix magento bug with function empty + * + * @param array $item + * @return string + */ + protected function prepareItem(array $item) + { + if ($item[$this->storeKey] == 0) { + $item[$this->storeKey] = [0]; + } + + return parent::prepareItem($item); + } +} -- GitLab