init
This commit is contained in:
@@ -0,0 +1,230 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Wiki\Block\Adminhtml\Category;
|
||||
|
||||
use Esun\Wiki\Model\Category;
|
||||
use Esun\Wiki\Model\CategoryFactory;
|
||||
use Esun\Wiki\Model\ResourceModel\Category\CollectionFactory;
|
||||
use Magento\Backend\Block\Template;
|
||||
use Magento\Backend\Block\Template\Context;
|
||||
use Magento\Cms\Model\Wysiwyg\Config as WysiwygConfig;
|
||||
use Magento\Framework\Data\FormFactory;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Store\Model\System\Store as SystemStore;
|
||||
|
||||
class EditBlock extends Template
|
||||
{
|
||||
private ?Category $category = null;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
private readonly CategoryFactory $categoryFactory,
|
||||
private readonly CollectionFactory $collectionFactory,
|
||||
private readonly FormFactory $formFactory,
|
||||
private readonly WysiwygConfig $wysiwygConfig,
|
||||
private readonly SystemStore $systemStore,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $data);
|
||||
}
|
||||
|
||||
public function getCategory(): Category
|
||||
{
|
||||
if ($this->category === null) {
|
||||
$this->category = $this->categoryFactory->create();
|
||||
$id = (int) $this->getRequest()->getParam('id');
|
||||
if ($id > 0) {
|
||||
$this->category->load($id);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->category;
|
||||
}
|
||||
|
||||
public function getSaveUrl(): string
|
||||
{
|
||||
return $this->getUrl('esun_wiki/category/save');
|
||||
}
|
||||
|
||||
public function getBackUrl(): string
|
||||
{
|
||||
return $this->getUrl('esun_wiki/category/index');
|
||||
}
|
||||
|
||||
public function getPreviewUrl(): string
|
||||
{
|
||||
$id = (int) $this->getCategory()->getId();
|
||||
$customPath = trim((string) $this->getCategory()->getData('custom_url_path'), '/');
|
||||
$baseUrl = rtrim($this->_storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_LINK), '/');
|
||||
|
||||
if ($id > 0 && $customPath !== '' && $this->getCategory()->isClickable()) {
|
||||
return $baseUrl . '/' . $customPath;
|
||||
}
|
||||
|
||||
return $id > 0 && $this->getCategory()->isClickable()
|
||||
? $baseUrl . '/wiki/index/view/slug/'
|
||||
. rawurlencode((string) $this->getCategory()->getData('slug')) . '/'
|
||||
: '';
|
||||
}
|
||||
|
||||
public function getContentEditorHtml(): string
|
||||
{
|
||||
$form = $this->formFactory->create();
|
||||
$editor = $form->addField('content', 'editor', [
|
||||
'name' => 'content',
|
||||
'label' => __('Wiki Content'),
|
||||
'title' => __('Wiki Content'),
|
||||
'style' => 'width:100%;height:36em',
|
||||
'required' => false,
|
||||
'value' => (string) $this->getCategory()->getData('content'),
|
||||
'config' => $this->wysiwygConfig->getConfig([
|
||||
'add_variables' => false,
|
||||
'add_widgets' => true,
|
||||
'add_images' => true,
|
||||
'is_pagebuilder_enabled' => true,
|
||||
'pagebuilder_button' => true,
|
||||
]),
|
||||
]);
|
||||
|
||||
return $editor->getElementHtml();
|
||||
}
|
||||
|
||||
public function getLogoUrl(): string
|
||||
{
|
||||
$path = trim((string) $this->getCategory()->getData('logo_path'));
|
||||
if ($path === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
return rtrim($this->_storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA), '/')
|
||||
. '/' . ltrim($path, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int[]
|
||||
*/
|
||||
public function getSelectedStoreIds(): array
|
||||
{
|
||||
if (!$this->getCategory()->getId()) {
|
||||
return [0];
|
||||
}
|
||||
|
||||
$storeIds = array_map('intval', (array) $this->getCategory()->getData('store_ids'));
|
||||
|
||||
return $storeIds ?: [0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{value:int|string,label:string,disabled:bool}>
|
||||
*/
|
||||
public function getStoreOptions(): array
|
||||
{
|
||||
$options = [];
|
||||
foreach ($this->systemStore->getStoreValuesForForm(false, true) as $option) {
|
||||
$children = $option['value'] ?? null;
|
||||
if (!is_array($children)) {
|
||||
$options[] = [
|
||||
'value' => (int) $children,
|
||||
'label' => (string) $option['label'],
|
||||
'disabled' => false,
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
$options[] = [
|
||||
'value' => '',
|
||||
'label' => (string) $option['label'],
|
||||
'disabled' => true,
|
||||
];
|
||||
foreach ($children as $child) {
|
||||
$options[] = [
|
||||
'value' => (int) $child['value'],
|
||||
'label' => (string) $child['label'],
|
||||
'disabled' => false,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{value:int,label:string}>
|
||||
*/
|
||||
public function getParentOptions(): array
|
||||
{
|
||||
$currentId = (int) $this->getCategory()->getId();
|
||||
$excludedIds = $currentId > 0 ? $this->getExcludedParentIds($currentId) : [];
|
||||
$nodes = [];
|
||||
|
||||
foreach ($this->collectionFactory->create()->addTreeOrder() as $category) {
|
||||
$id = (int) $category->getId();
|
||||
if (isset($excludedIds[$id])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$nodes[$id] = [
|
||||
'id' => $id,
|
||||
'parent_id' => (int) $category->getData('parent_id'),
|
||||
'title' => (string) $category->getData('title'),
|
||||
'children' => [],
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($nodes as $id => $node) {
|
||||
$parentId = (int) $node['parent_id'];
|
||||
if ($parentId > 0 && isset($nodes[$parentId])) {
|
||||
$nodes[$parentId]['children'][$id] = &$nodes[$id];
|
||||
}
|
||||
}
|
||||
|
||||
$options = [['value' => 0, 'label' => (string) __('Root Category')]];
|
||||
foreach ($nodes as $id => $node) {
|
||||
if ((int) $node['parent_id'] === 0 || !isset($nodes[(int) $node['parent_id']])) {
|
||||
$this->appendOption($options, $node, 0);
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, bool>
|
||||
*/
|
||||
private function getExcludedParentIds(int $currentId): array
|
||||
{
|
||||
$childrenByParent = [];
|
||||
foreach ($this->collectionFactory->create() as $category) {
|
||||
$childrenByParent[(int) $category->getData('parent_id')][] = (int) $category->getId();
|
||||
}
|
||||
|
||||
$excluded = [$currentId => true];
|
||||
$queue = $childrenByParent[$currentId] ?? [];
|
||||
while ($queue) {
|
||||
$id = (int) array_shift($queue);
|
||||
$excluded[$id] = true;
|
||||
foreach ($childrenByParent[$id] ?? [] as $childId) {
|
||||
$queue[] = (int) $childId;
|
||||
}
|
||||
}
|
||||
|
||||
return $excluded;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array{value:int,label:string}> $options
|
||||
* @param mixed[] $node
|
||||
*/
|
||||
private function appendOption(array &$options, array $node, int $depth): void
|
||||
{
|
||||
$options[] = [
|
||||
'value' => (int) $node['id'],
|
||||
'label' => str_repeat('-- ', $depth) . (string) $node['title'],
|
||||
];
|
||||
|
||||
foreach ($node['children'] as $childNode) {
|
||||
$this->appendOption($options, $childNode, $depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Wiki\Block\Adminhtml\Homepage;
|
||||
|
||||
use Esun\Wiki\Model\Homepage;
|
||||
use Esun\Wiki\Model\HomepageFactory;
|
||||
use Esun\Wiki\Model\ResourceModel\Homepage\CollectionFactory;
|
||||
use Magento\Backend\Block\Template;
|
||||
use Magento\Backend\Block\Template\Context;
|
||||
use Magento\Cms\Model\Wysiwyg\Config as WysiwygConfig;
|
||||
use Magento\Framework\Data\FormFactory;
|
||||
use Magento\Framework\UrlInterface;
|
||||
|
||||
class EditBlock extends Template
|
||||
{
|
||||
private ?Homepage $homepage = null;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
private readonly HomepageFactory $homepageFactory,
|
||||
private readonly CollectionFactory $collectionFactory,
|
||||
private readonly FormFactory $formFactory,
|
||||
private readonly WysiwygConfig $wysiwygConfig,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $data);
|
||||
}
|
||||
|
||||
public function getHomepage(): Homepage
|
||||
{
|
||||
if ($this->homepage === null) {
|
||||
$homepage = $this->collectionFactory->create()
|
||||
->setPageSize(1)
|
||||
->getFirstItem();
|
||||
$this->homepage = $homepage->getId() ? $homepage : $this->homepageFactory->create();
|
||||
|
||||
if (!$this->homepage->getData('title')) {
|
||||
$this->homepage->setData('title', 'eSUN Wiki');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->homepage;
|
||||
}
|
||||
|
||||
public function getSaveUrl(): string
|
||||
{
|
||||
return $this->getUrl('esun_wiki/homepage/save');
|
||||
}
|
||||
|
||||
public function getCategoriesUrl(): string
|
||||
{
|
||||
return $this->getUrl('esun_wiki/category/index');
|
||||
}
|
||||
|
||||
public function getPreviewUrl(): string
|
||||
{
|
||||
return rtrim($this->_storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_LINK), '/') . '/wiki';
|
||||
}
|
||||
|
||||
public function getContentEditorHtml(): string
|
||||
{
|
||||
$form = $this->formFactory->create();
|
||||
$editor = $form->addField('content', 'editor', [
|
||||
'name' => 'content',
|
||||
'label' => __('Homepage Content'),
|
||||
'title' => __('Homepage Content'),
|
||||
'style' => 'height:36em',
|
||||
'required' => false,
|
||||
'value' => (string) $this->getHomepage()->getData('content'),
|
||||
'config' => $this->wysiwygConfig->getConfig([
|
||||
'add_variables' => false,
|
||||
'add_widgets' => true,
|
||||
'add_images' => true,
|
||||
'is_pagebuilder_enabled' => true,
|
||||
'pagebuilder_button' => true,
|
||||
]),
|
||||
]);
|
||||
|
||||
return $editor->getElementHtml();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user