This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace Esun\Wiki\Controller\Adminhtml\Category;
use Esun\ImageManager\Model\MediaUploader;
use Esun\Wiki\Model\CategoryFactory;
use Esun\Wiki\Model\ResourceModel\Category\CollectionFactory;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Esun_Wiki::categories_delete';
public function __construct(
Action\Context $context,
private readonly CategoryFactory $categoryFactory,
private readonly CollectionFactory $collectionFactory,
private readonly MediaUploader $mediaUploader
) {
parent::__construct($context);
}
public function execute()
{
try {
$category = $this->categoryFactory->create()->load((int) $this->getRequest()->getParam('id'));
if (!$category->getId()) {
throw new LocalizedException(__('The wiki category no longer exists.'));
}
$childrenCount = $this->collectionFactory->create()
->addFieldToFilter('parent_id', (int) $category->getId())
->getSize();
if ($childrenCount > 0) {
throw new LocalizedException(
__('This wiki category contains %1 child category(s). Move or delete them first.', $childrenCount)
);
}
$logoPath = (string) $category->getData('logo_path');
$category->delete();
if ($logoPath !== '') {
$this->mediaUploader->delete($logoPath);
}
$this->messageManager->addSuccessMessage(__('The wiki category has been deleted.'));
} catch (\Throwable $exception) {
$this->messageManager->addErrorMessage($exception->getMessage());
}
return $this->resultRedirectFactory->create()->setPath('esun_wiki/category/index');
}
}
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Esun\Wiki\Controller\Adminhtml\Category;
use Esun\Wiki\Model\CategoryFactory;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Edit extends Action
{
public const ADMIN_RESOURCE = 'Esun_Wiki::categories_save';
public function __construct(
Action\Context $context,
private readonly PageFactory $pageFactory,
private readonly CategoryFactory $categoryFactory
) {
parent::__construct($context);
}
public function execute()
{
$id = (int) $this->getRequest()->getParam('id');
if ($id > 0 && !$this->categoryFactory->create()->load($id)->getId()) {
$this->messageManager->addErrorMessage(__('The wiki category no longer exists.'));
return $this->resultRedirectFactory->create()->setPath('esun_wiki/category/index');
}
$page = $this->pageFactory->create();
$page->getConfig()->getTitle()->prepend($id ? __('Edit Wiki Category') : __('New Wiki Category'));
return $page;
}
}
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Esun\Wiki\Controller\Adminhtml\Category;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
public const ADMIN_RESOURCE = 'Esun_Wiki::categories';
public function __construct(
Action\Context $context,
private readonly PageFactory $pageFactory
) {
parent::__construct($context);
}
public function execute()
{
$page = $this->pageFactory->create();
$page->getConfig()->getTitle()->prepend(__('Wiki Knowledge Base'));
return $page;
}
}
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace Esun\Wiki\Controller\Adminhtml\Category;
class NewAction extends Edit
{
}
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace Esun\Wiki\Controller\Adminhtml\Category;
use Esun\MultiLocaleTranslation\Model\Queue\SelectionRegistry;
use Esun\Wiki\Model\ResourceModel\Category\CollectionFactory;
use Magento\Backend\App\Action;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Ui\Component\MassAction\Filter;
class PrepareTranslation extends Action implements HttpPostActionInterface
{
public const ADMIN_RESOURCE = 'Esun_MultiLocaleTranslation::queue_add';
public function __construct(
Action\Context $context,
private readonly SelectionRegistry $selectionRegistry,
private readonly Filter $filter,
private readonly CollectionFactory $collectionFactory
) {
parent::__construct($context);
}
public function execute(): Redirect
{
try {
if (!$this->_authorization->isAllowed('Esun_MultiLocaleTranslation::translate_wiki')) {
throw new \InvalidArgumentException((string) __('You are not allowed to translate Wiki content.'));
}
$collection = $this->filter->getCollection($this->collectionFactory->create());
$categoryIds = array_values(array_unique(array_map('intval', $collection->getAllIds())));
if ($categoryIds === []) {
throw new \InvalidArgumentException((string) __('Select at least one Wiki category.'));
}
$token = $this->selectionRegistry->save('wiki_category', $categoryIds);
return $this->resultRedirectFactory->create()->setPath(
'localetranslate/queue/new',
['token' => $token]
);
} catch (\Throwable $exception) {
$this->messageManager->addErrorMessage($exception->getMessage());
return $this->resultRedirectFactory->create()->setPath('esun_wiki/category/index');
}
}
}
@@ -0,0 +1,268 @@
<?php
declare(strict_types=1);
namespace Esun\Wiki\Controller\Adminhtml\Category;
use Esun\ImageManager\Model\MediaUploader;
use Esun\Wiki\Model\CategoryFactory;
use Esun\Wiki\Model\ResourceModel\Category as CategoryResource;
use Esun\Wiki\Model\ResourceModel\Category\CollectionFactory;
use Magento\Backend\App\Action;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Model\StoreManagerInterface;
class Save extends Action
{
public const ADMIN_RESOURCE = 'Esun_Wiki::categories_save';
public function __construct(
Action\Context $context,
private readonly CategoryFactory $categoryFactory,
private readonly CategoryResource $categoryResource,
private readonly CollectionFactory $collectionFactory,
private readonly MediaUploader $mediaUploader,
private readonly StoreManagerInterface $storeManager,
private readonly ResourceConnection $resourceConnection,
private readonly TypeListInterface $cacheTypeList
) {
parent::__construct($context);
}
public function execute()
{
$id = (int) $this->getRequest()->getParam('category_id');
$redirect = $this->resultRedirectFactory->create();
$uploadedLogo = null;
try {
$category = $this->categoryFactory->create();
if ($id > 0) {
$category->load($id);
if (!$category->getId()) {
throw new LocalizedException(__('The wiki category no longer exists.'));
}
}
$this->assertNavigationSettingsSchema();
$title = trim((string) $this->getRequest()->getParam('title'));
$slug = $this->normalizeSlug((string) $this->getRequest()->getParam('slug'));
$customUrlPath = $this->normalizeCustomUrlPath(
(string) $this->getRequest()->getParam('custom_url_path')
);
$parentId = (int) $this->getRequest()->getParam('parent_id');
$parentId = $parentId > 0 ? $parentId : null;
$storeIds = $this->normalizeStoreIds((array) $this->getRequest()->getParam('store_ids', []));
if ($title === '') {
throw new LocalizedException(__('Title is required.'));
}
if ($slug === '') {
$slug = $this->normalizeSlug($title);
}
if ($slug === '') {
throw new LocalizedException(__('Slug is required.'));
}
$this->assertUniqueSlug($slug, $id);
$this->assertUniqueCustomUrlPath($customUrlPath, $id);
$this->assertValidParent($parentId, $id);
if ($this->getRequest()->getParam('show_download_center') === '1'
&& ($this->getRequest()->getParam('is_active') === '0'
|| $this->getRequest()->getParam('is_clickable') === '0')
) {
throw new LocalizedException(
__('The download center page must be enabled and allow opening its content.')
);
}
if ($this->getRequest()->getParam('show_parameter_comparison') === '1'
&& ($this->getRequest()->getParam('is_active') === '0'
|| $this->getRequest()->getParam('is_clickable') === '0')
) {
throw new LocalizedException(
__('The parameter comparison page must be enabled and allow opening its content.')
);
}
$oldLogoPath = (string) $category->getData('logo_path');
$logoPath = $oldLogoPath;
$uploadedLogo = $this->mediaUploader->save('logo');
if ($uploadedLogo !== null) {
$logoPath = $uploadedLogo;
} elseif ($this->getRequest()->getParam('delete_logo')) {
$logoPath = '';
}
$category->addData([
'parent_id' => $parentId,
'title' => $title,
'slug' => $slug,
'custom_url_path' => $customUrlPath !== '' ? $customUrlPath : null,
'logo_path' => $logoPath,
'summary' => trim((string) $this->getRequest()->getParam('summary')),
'content' => (string) $this->getRequest()->getParam('content'),
'meta_title' => trim((string) $this->getRequest()->getParam('meta_title')),
'meta_description' => trim((string) $this->getRequest()->getParam('meta_description')),
'meta_keywords' => trim((string) $this->getRequest()->getParam('meta_keywords')),
'is_active' => $this->getRequest()->getParam('is_active') === '0' ? 0 : 1,
'is_default_open' => (string) $this->getRequest()->getParam('is_default_open') === '1' ? 1 : 0,
'is_clickable' => (string) $this->getRequest()->getParam('is_clickable') === '0' ? 0 : 1,
'show_download_center' => (string) $this->getRequest()->getParam('show_download_center') === '1' ? 1 : 0,
'show_parameter_comparison' => (string) $this->getRequest()->getParam('show_parameter_comparison') === '1' ? 1 : 0,
'sort_order' => (int) $this->getRequest()->getParam('sort_order'),
'store_ids' => $storeIds,
])->save();
if ((int) $category->getData('show_download_center') === 1) {
$this->categoryResource->clearOtherDownloadCenters((int) $category->getId());
}
if ((int) $category->getData('show_parameter_comparison') === 1) {
$this->categoryResource->clearOtherParameterComparisons((int) $category->getId());
}
$this->cacheTypeList->cleanType('block_html');
$this->cacheTypeList->cleanType('full_page');
if ($oldLogoPath !== '' && $oldLogoPath !== $logoPath) {
$this->mediaUploader->delete($oldLogoPath);
}
$this->messageManager->addSuccessMessage(__('The wiki category has been saved.'));
if ($this->getRequest()->getParam('back')) {
return $redirect->setPath('esun_wiki/category/edit', ['id' => (int) $category->getId()]);
}
return $redirect->setPath('esun_wiki/category/index');
} catch (\Throwable $exception) {
if ($uploadedLogo !== null) {
$this->mediaUploader->delete($uploadedLogo);
}
$this->messageManager->addErrorMessage($exception->getMessage());
return $redirect->setPath(
$id ? 'esun_wiki/category/edit' : 'esun_wiki/category/new',
$id ? ['id' => $id] : []
);
}
}
/**
* @param mixed[] $requestedStoreIds
* @return int[]
*/
private function normalizeStoreIds(array $requestedStoreIds): array
{
$storeIds = array_values(array_unique(array_filter(
array_map('intval', $requestedStoreIds),
static fn (int $storeId): bool => $storeId >= 0
)));
if (in_array(0, $storeIds, true)) {
return [0];
}
$validStoreIds = array_map('intval', array_keys($this->storeManager->getStores(false)));
$storeIds = array_values(array_intersect($storeIds, $validStoreIds));
if (!$storeIds) {
throw new LocalizedException(__('Select at least one Store View.'));
}
return $storeIds;
}
private function normalizeSlug(string $slug): string
{
$slug = strtolower(trim($slug));
$slug = (string) preg_replace('/[^a-z0-9_-]+/', '-', $slug);
return trim($slug, '-_');
}
private function assertUniqueSlug(string $slug, int $currentId): void
{
$collection = $this->collectionFactory->create()
->addFieldToFilter('slug', $slug);
if ($currentId > 0) {
$collection->addFieldToFilter('category_id', ['neq' => $currentId]);
}
if ($collection->getSize() > 0) {
throw new LocalizedException(__('The slug "%1" is already used by another wiki category.', $slug));
}
}
private function normalizeCustomUrlPath(string $path): string
{
$parsedPath = parse_url(trim($path), PHP_URL_PATH);
$path = strtolower(trim(is_string($parsedPath) ? $parsedPath : '', '/'));
$path = (string) preg_replace('#[^a-z0-9/_-]+#', '-', $path);
$path = (string) preg_replace('#/+#', '/', $path);
return trim($path, '-_/');
}
private function assertUniqueCustomUrlPath(string $path, int $currentId): void
{
if ($path === '') {
return;
}
$collection = $this->collectionFactory->create()
->addFieldToFilter('custom_url_path', $path);
if ($currentId > 0) {
$collection->addFieldToFilter('category_id', ['neq' => $currentId]);
}
if ($collection->getSize() > 0) {
throw new LocalizedException(__('The custom URL "%1" is already used by another wiki category.', $path));
}
}
private function assertValidParent(?int $parentId, int $currentId): void
{
if ($parentId === null || $currentId === 0) {
return;
}
if ($parentId === $currentId) {
throw new LocalizedException(__('A wiki category cannot be its own parent.'));
}
$categories = [];
foreach ($this->collectionFactory->create() as $category) {
$categories[(int) $category->getId()] = (int) $category->getData('parent_id');
}
$cursor = $parentId;
while ($cursor > 0) {
if ($cursor === $currentId) {
throw new LocalizedException(__('A child category cannot be selected as the parent.'));
}
$cursor = $categories[$cursor] ?? 0;
}
}
private function assertNavigationSettingsSchema(): void
{
$table = $this->resourceConnection->getTableName('esun_wiki_category');
$columns = $this->resourceConnection->getConnection()->describeTable($table);
if (!isset(
$columns['is_default_open'],
$columns['is_clickable'],
$columns['show_download_center'],
$columns['show_parameter_comparison'],
$columns['meta_title'],
$columns['meta_description'],
$columns['meta_keywords'],
$columns['custom_url_path']
)) {
throw new LocalizedException(__(
'The Wiki page settings are not installed. Run "bin/magento setup:upgrade" and try again.'
));
}
}
}
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Esun\Wiki\Controller\Adminhtml\Homepage;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Edit extends Action
{
public const ADMIN_RESOURCE = 'Esun_Wiki::homepage';
public function __construct(
Action\Context $context,
private readonly PageFactory $pageFactory
) {
parent::__construct($context);
}
public function execute()
{
$page = $this->pageFactory->create();
$page->getConfig()->getTitle()->prepend(__('Edit Wiki Homepage'));
return $page;
}
}
@@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace Esun\Wiki\Controller\Adminhtml\Homepage;
use Esun\Wiki\Model\HomepageFactory;
use Esun\Wiki\Model\ResourceModel\Homepage as HomepageResource;
use Esun\Wiki\Model\ResourceModel\Homepage\CollectionFactory;
use Magento\Backend\App\Action;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\LocalizedException;
class Save extends Action
{
public const ADMIN_RESOURCE = 'Esun_Wiki::homepage';
public function __construct(
Action\Context $context,
private readonly HomepageFactory $homepageFactory,
private readonly HomepageResource $homepageResource,
private readonly CollectionFactory $collectionFactory,
private readonly ResourceConnection $resourceConnection,
private readonly TypeListInterface $cacheTypeList
) {
parent::__construct($context);
}
public function execute()
{
$redirect = $this->resultRedirectFactory->create();
try {
$this->assertSchema();
$title = trim((string) $this->getRequest()->getParam('title'));
if ($title === '') {
throw new LocalizedException(__('Title is required.'));
}
$homepage = $this->collectionFactory->create()
->setPageSize(1)
->getFirstItem();
if (!$homepage->getId()) {
$homepage = $this->homepageFactory->create();
}
$homepage->addData([
'title' => $title,
'content' => (string) $this->getRequest()->getParam('content'),
'meta_title' => trim((string) $this->getRequest()->getParam('meta_title')),
'meta_description' => trim((string) $this->getRequest()->getParam('meta_description')),
'meta_keywords' => trim((string) $this->getRequest()->getParam('meta_keywords')),
]);
$this->homepageResource->save($homepage);
$this->cacheTypeList->cleanType('block_html');
$this->cacheTypeList->cleanType('full_page');
$this->messageManager->addSuccessMessage(__('The wiki homepage has been saved.'));
} catch (\Throwable $exception) {
$this->messageManager->addErrorMessage($exception->getMessage());
}
return $redirect->setPath('esun_wiki/homepage/edit');
}
private function assertSchema(): void
{
$connection = $this->resourceConnection->getConnection();
$table = $this->resourceConnection->getTableName('esun_wiki_homepage');
if (!$connection->isTableExists($table)) {
throw new LocalizedException(__(
'The Wiki homepage is not installed. Run "bin/magento setup:upgrade" and try again.'
));
}
$columns = $connection->describeTable($table);
if (!isset(
$columns['title'],
$columns['content'],
$columns['meta_title'],
$columns['meta_description'],
$columns['meta_keywords']
)) {
throw new LocalizedException(__(
'The Wiki homepage schema is incomplete. Run "bin/magento setup:upgrade" and try again.'
));
}
}
}