init
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Wiki\Ui\Component\Listing\Column;
|
||||
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Framework\View\Element\UiComponent\ContextInterface;
|
||||
use Magento\Framework\View\Element\UiComponentFactory;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\Ui\Component\Listing\Columns\Column;
|
||||
|
||||
class Actions extends Column
|
||||
{
|
||||
public function __construct(
|
||||
ContextInterface $context,
|
||||
UiComponentFactory $uiComponentFactory,
|
||||
private readonly UrlInterface $urlBuilder,
|
||||
private readonly StoreManagerInterface $storeManager,
|
||||
array $components = [],
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $uiComponentFactory, $components, $data);
|
||||
}
|
||||
|
||||
public function prepareDataSource(array $dataSource): array
|
||||
{
|
||||
if (!isset($dataSource['data']['items'])) {
|
||||
return $dataSource;
|
||||
}
|
||||
|
||||
$fieldName = (string) $this->getData('name');
|
||||
foreach ($dataSource['data']['items'] as &$item) {
|
||||
if (!isset($item['category_id'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$id = (int) $item['category_id'];
|
||||
$title = (string) ($item['raw_title'] ?? $item['title'] ?? '');
|
||||
$item[$fieldName]['edit'] = [
|
||||
'href' => $this->urlBuilder->getUrl('esun_wiki/category/edit', ['id' => $id]),
|
||||
'label' => __('Edit'),
|
||||
];
|
||||
|
||||
if ((int) ($item['is_clickable'] ?? 1) === 1) {
|
||||
$item[$fieldName]['preview'] = [
|
||||
'href' => $this->getPreviewUrl($item),
|
||||
'label' => __('Preview'),
|
||||
'target' => '_blank',
|
||||
];
|
||||
}
|
||||
|
||||
$item[$fieldName]['delete'] = [
|
||||
'href' => $this->urlBuilder->getUrl('esun_wiki/category/delete', ['id' => $id]),
|
||||
'label' => __('Delete'),
|
||||
'confirm' => [
|
||||
'title' => __('Delete %1', $title),
|
||||
'message' => __('Delete this wiki category?'),
|
||||
],
|
||||
];
|
||||
}
|
||||
unset($item);
|
||||
|
||||
return $dataSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $item
|
||||
*/
|
||||
private function getPreviewUrl(array $item): string
|
||||
{
|
||||
$baseUrl = rtrim($this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_LINK), '/');
|
||||
$customPath = (string) ($item['raw_custom_url_path'] ?? '');
|
||||
if ($customPath !== '') {
|
||||
return $baseUrl . '/' . $customPath;
|
||||
}
|
||||
|
||||
return $baseUrl . '/wiki/index/view/slug/' . rawurlencode((string) ($item['slug'] ?? '')) . '/';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Wiki\Ui\DataProvider\Category;
|
||||
|
||||
use Esun\Wiki\Model\ResourceModel\Category\CollectionFactory;
|
||||
use Magento\Framework\Api\Filter;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Ui\DataProvider\AbstractDataProvider;
|
||||
|
||||
class ListingDataProvider extends AbstractDataProvider
|
||||
{
|
||||
public function __construct(
|
||||
string $name,
|
||||
string $primaryFieldName,
|
||||
string $requestFieldName,
|
||||
private readonly CollectionFactory $collectionFactory,
|
||||
RequestInterface $request,
|
||||
array $meta = [],
|
||||
array $data = []
|
||||
) {
|
||||
$this->collection = $this->collectionFactory->create();
|
||||
if (!$request->getParam('sorting')) {
|
||||
$this->collection->addTreeOrder();
|
||||
}
|
||||
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
|
||||
}
|
||||
|
||||
public function addFilter(Filter $filter): void
|
||||
{
|
||||
if ($filter->getField() !== 'fulltext') {
|
||||
parent::addFilter($filter);
|
||||
return;
|
||||
}
|
||||
|
||||
$value = '%' . (string) $filter->getValue() . '%';
|
||||
$this->collection->addFieldToFilter(
|
||||
['title', 'slug', 'custom_url_path', 'summary', 'content'],
|
||||
[
|
||||
['like' => $value],
|
||||
['like' => $value],
|
||||
['like' => $value],
|
||||
['like' => $value],
|
||||
['like' => $value],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function getData(): array
|
||||
{
|
||||
$data = parent::getData();
|
||||
if (empty($data['items'])) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$categoryMap = $this->getCategoryMap();
|
||||
foreach ($data['items'] as &$item) {
|
||||
$id = (int) ($item['category_id'] ?? 0);
|
||||
$parentId = (int) ($item['parent_id'] ?? 0);
|
||||
$title = (string) ($item['title'] ?? '');
|
||||
$depth = $this->getDepth($id, $categoryMap);
|
||||
|
||||
$item['raw_title'] = $title;
|
||||
$item['title'] = ($depth > 0 ? str_repeat("\u{00A0}", $depth * 4) . '↳ ' : '') . $title;
|
||||
$item['parent_title'] = $parentId > 0 && isset($categoryMap[$parentId])
|
||||
? $categoryMap[$parentId]['title']
|
||||
: (string) __('Root');
|
||||
|
||||
$customPath = trim((string) ($item['custom_url_path'] ?? ''), '/');
|
||||
$item['raw_custom_url_path'] = $customPath;
|
||||
$item['custom_url_path'] = $customPath !== '' ? '/' . $customPath : '—';
|
||||
}
|
||||
unset($item);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{parent_id:int,title:string}>
|
||||
*/
|
||||
private function getCategoryMap(): array
|
||||
{
|
||||
$map = [];
|
||||
foreach ($this->collectionFactory->create() as $category) {
|
||||
$map[(int) $category->getId()] = [
|
||||
'parent_id' => (int) $category->getData('parent_id'),
|
||||
'title' => (string) $category->getData('title'),
|
||||
];
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array{parent_id:int,title:string}> $categoryMap
|
||||
*/
|
||||
private function getDepth(int $id, array $categoryMap): int
|
||||
{
|
||||
$depth = 0;
|
||||
$visited = [];
|
||||
$parentId = $categoryMap[$id]['parent_id'] ?? 0;
|
||||
|
||||
while ($parentId > 0 && isset($categoryMap[$parentId]) && !isset($visited[$parentId]) && $depth < 20) {
|
||||
$visited[$parentId] = true;
|
||||
$depth++;
|
||||
$parentId = $categoryMap[$parentId]['parent_id'];
|
||||
}
|
||||
|
||||
return $depth;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user