172 lines
5.1 KiB
PHP
Raw Normal View History

2023-04-05 08:27:08 +02:00
<?php
/**
2024-12-04 15:09:22 +01:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2023-04-05 08:27:08 +02:00
*
2024-12-04 15:09:22 +01:00
* @copyright (C) 2022 Marat Bedoev, bestlife AG
* @copyright (C) 2023 O3-Shop (https://www.o3-shop.com)
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
2023-04-05 08:27:08 +02:00
*/
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;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2023-04-05 08:27:08 +02:00
class Loader
{
protected Config $config;
protected Language $language;
protected Configuration $configuration;
2023-04-05 08:27:08 +02:00
public function __construct(Config $config, Language $language)
{
$this->config = $config;
$this->language = $language;
2024-12-04 14:44:41 +01:00
$this->configuration = oxNew(Configuration::class, $this);
$this->configuration->build();
2023-04-05 08:27:08 +02:00
}
/**
* @return string
*/
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
try {
$engine = $this->getTemplateRenderer()->getTemplateEngine();
2024-12-04 14:44:41 +01:00
return $engine->render('@' . Constants::OXID_MODULE_ID . '/admin/editorswitch');
} catch (NotFoundExceptionInterface|ContainerExceptionInterface) {
return '';
}
2023-04-05 08:27:08 +02:00
}
/**
* @return bool
*/
protected function isEnabledForCurrentController(): bool
{
try {
/** @var ModuleSettingService $service */
2024-12-04 14:44:41 +01:00
$service = ContainerFactory::getInstance()->getContainer()->get(ModuleSettingServiceInterface::class);
/** @var string[] $aEnabledClasses */
2024-12-04 14:44:41 +01:00
$aEnabledClasses = $service->getCollection("aTinyMCE_classes", Constants::OXID_MODULE_ID);
2024-12-04 14:44:41 +01:00
return in_array($this->getShopConfig()->getActiveView()->getClassKey(), $aEnabledClasses);
} catch (ContainerExceptionInterface|NotFoundExceptionInterface) {
return false;
}
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-04 14:44:41 +01:00
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;
}
/**
2024-12-04 14:25:51 +01:00
* @return string[]
2023-04-05 08:27:08 +02:00
*/
public function getScripts(): array
2023-04-05 08:27:08 +02:00
{
if (!$this->isEnabledForCurrentController()) {
return [];
}
2024-12-04 14:25:51 +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'",
$this->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
);
return [
$sCopyLongDescFromTinyMCE,
$sUrlConverter,
2024-12-04 14:44:41 +01:00
$sInit,
];
2023-04-05 08:27:08 +02:00
}
/**
2024-12-04 14:25:51 +01:00
* @return string[]
2023-04-05 08:27:08 +02:00
*/
public function getIncludes(): array
2023-04-05 08:27:08 +02:00
{
if (!$this->isEnabledForCurrentController()) {
return [];
}
2024-12-03 16:00:24 +01:00
try {
return [
Registry::getConfig()->getActiveView()->getViewConfig()->getModuleUrl(
Constants::OXID_MODULE_ID,
'out/tinymce/tinymce.min.js'
2024-12-04 14:44:41 +01:00
),
];
} catch (FileException) {
return [];
}
2023-04-05 08:27:08 +02:00
}
2024-12-03 16:00:24 +01:00
/**
* @return TemplateRenderer
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
2024-12-03 16:00:24 +01:00
protected function getTemplateRenderer(): TemplateRenderer
{
return ContainerFactory::getInstance()->getContainer()
->get(TemplateRendererBridgeInterface::class)
->getTemplateRenderer();
2024-12-03 16:00:24 +01:00
}
2023-04-10 22:47:06 +02:00
}