173 lines
5.7 KiB
PHP
Raw Normal View History

2023-04-05 08:27:08 +02:00
<?php
/**
* This file is part of O3-Shop TinyMCE editor module.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
*
* @copyright Copyright (c) 2022 Marat Bedoev, bestlife AG
2023-04-05 08:27:08 +02:00
* @copyright Copyright (c) 2023 O3-Shop (https://www.o3-shop.com)
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
*/
declare(strict_types=1);
namespace O3\TinyMCE\Application\Core\TinyMCE;
2024-12-03 14:43:55 +01:00
use O3\TinyMCE\Application\Model\Constants;
2023-04-05 08:27:08 +02:00
use OxidEsales\Eshop\Core\Config;
2023-04-07 23:34:59 +02:00
use OxidEsales\Eshop\Core\Exception\FileException;
2023-04-05 08:27:08 +02:00
use OxidEsales\Eshop\Core\Language;
use OxidEsales\Eshop\Core\Registry;
2024-12-03 16:00:24 +01:00
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingService;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRenderer;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRendererBridgeInterface;
2023-04-05 08:27:08 +02:00
class Loader
{
protected Config $config;
protected Language $language;
public function __construct(Config $config, Language $language)
{
$this->config = $config;
$this->language = $language;
}
/**
* @return string
2023-04-07 23:34:59 +02:00
* @throws FileException
2023-04-05 08:27:08 +02:00
*/
2023-04-05 08:53:11 +02:00
public function getEditorCode(): string
2023-04-05 08:27:08 +02:00
{
2023-04-10 22:47:06 +02:00
if (!$this->isEnabledForCurrentController()) {
return '';
}
2023-04-05 08:27:08 +02:00
2023-04-08 22:47:11 +02:00
if ($this->contentIsPlain()) {
/** @var string $message */
$message = $this->language->translateString('TINYMCE_PLAINCMS');
return $message;
}
2023-04-05 08:27:08 +02:00
$configuration = oxNew(Configuration::class, $this);
$configuration->build();
$this->registerScripts($configuration);
$this->registerIncludes();
2024-12-03 16:00:24 +01:00
$engine = $this->getTemplateRenderer()->getTemplateEngine();
return $engine->render('@' . Constants::OXID_MODULE_ID.'/admin/editorswitch');
2023-04-05 08:27:08 +02:00
}
/**
* @return bool
*/
protected function isEnabledForCurrentController(): bool
{
2024-12-03 16:00:24 +01:00
/** @var ModuleSettingService $service */
$service = ContainerFactory::getInstance()->getContainer()->get(ModuleSettingServiceInterface::class);
2023-04-08 22:47:11 +02:00
/** @var string[] $aEnabledClasses */
2024-12-03 16:00:24 +01:00
$aEnabledClasses = $service->getCollection("aTinyMCE_classes", Constants::OXID_MODULE_ID);
2023-04-05 08:27:08 +02:00
2023-04-10 22:47:06 +02:00
return in_array($this->getShopConfig()->getActiveView()->getClassKey(), $aEnabledClasses);
2023-04-05 08:27:08 +02:00
}
/**
* @return bool
*/
protected function contentIsPlain(): bool
{
// D3 disabled, because isPlain method doesn't exist in OXID eShop
return false;
2024-12-03 14:12:24 +01:00
// /** @var BaseModel|Content $oEditObject */
// $oEditObject = $this->getShopConfig()->getActiveView()->getViewDataElement("edit");
// return $oEditObject instanceof Content && $oEditObject->isPlain();
2023-04-05 08:27:08 +02:00
}
/**
* @return Language
*/
public function getLanguage(): Language
{
return $this->language;
}
/**
* @return Config
*/
public function getShopConfig(): Config
{
return $this->config;
}
/**
* @param Configuration $configuration
*
* @return void
*/
protected function registerScripts(Configuration $configuration): void
{
2024-12-03 16:00:24 +01:00
$sCopyLongDescFromTinyMCE = file_get_contents(__DIR__.'/../../../assets/out/scripts/copyLongDesc.js');
$sUrlConverter = file_get_contents(__DIR__.'/../../../assets/out/scripts/urlConverter.js');
2023-04-05 08:27:08 +02:00
$sInit = str_replace(
"'CONFIG':'VALUES'",
$configuration->getConfig(),
2024-12-03 16:00:24 +01:00
(string) file_get_contents(__DIR__.'/../../../assets/out/scripts/init.js')
2023-04-05 08:27:08 +02:00
);
2024-12-03 16:00:24 +01:00
$engine = $this->getTemplateRenderer()->getTemplateEngine();
$globals = $engine->getGlobals();
$sSuffix = ($globals['__oxid_include_dynamic']) ? '_dynamic' : '';
$aScript = (array) Registry::getConfig()->getGlobalParameter('scripts' . $sSuffix);
2023-04-05 08:27:08 +02:00
$aScript[] = $sCopyLongDescFromTinyMCE;
$aScript[] = $sUrlConverter;
$aScript[] = $sInit;
2024-12-03 16:00:24 +01:00
Registry::getConfig()->setGlobalParameter('scripts' . $sSuffix, $aScript);
2023-04-05 08:27:08 +02:00
}
/**
* @return void
2023-04-07 23:34:59 +02:00
* @throws FileException
2023-04-05 08:27:08 +02:00
*/
protected function registerIncludes(): void
{
2024-12-03 16:00:24 +01:00
$engine = $this->getTemplateRenderer()->getTemplateEngine();
$globals = $engine->getGlobals();
$sSuffix = ($globals['__oxid_include_dynamic']) ? '_dynamic' : '';
2023-04-05 08:27:08 +02:00
2023-04-08 22:47:11 +02:00
/** @var array<int, string[]> $aInclude */
2023-04-05 08:27:08 +02:00
$aInclude = (array) Registry::getConfig()->getGlobalParameter('includes' . $sSuffix);
2023-04-07 22:48:34 +02:00
$aInclude[3][] = Registry::getConfig()->getActiveView()->getViewConfig()->getModuleUrl(
2024-12-03 16:00:24 +01:00
Constants::OXID_MODULE_ID,
'assets/out/tinymce/tinymce.min.js'
2023-04-07 22:48:34 +02:00
);
2024-12-03 16:00:24 +01:00
2023-04-05 08:27:08 +02:00
Registry::getConfig()->setGlobalParameter('includes' . $sSuffix, $aInclude);
}
2024-12-03 16:00:24 +01:00
protected function getTemplateRenderer(): TemplateRenderer
{
return ContainerFactory::getInstance()->getContainer()
->get(TemplateRendererBridgeInterface::class)
->getTemplateRenderer();
}
2023-04-10 22:47:06 +02:00
}