| Server IP : 85.158.181.41 / Your IP : 216.73.217.12 Web Server : Apache System : Linux cloud9-vm129 6.1.178+1-ph #ph SMP PREEMPT_DYNAMIC Wed Jul 29 09:00:54 UTC 2026 x86_64 User : moncbefd ( 1024) PHP Version : 7.3.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/moncbefd/www.moneta.at/GXModules/Gambio/StyleEdit/Core/Components/Theme/ |
Upload File : |
<?php
/*--------------------------------------------------------------------------------------------------
ThemeConfigurationTest.php 2019-12-17
Gambio GmbH
http://www.gambio.de
Copyright (c) 2019 Gambio GmbH
Released under the GNU General Public License (Version 2)
[http://www.gnu.org/licenses/gpl-2.0.html]
--------------------------------------------------------------------------------------------------
*/
namespace Gambio\StyleEdit\Core\Components\Theme;
use Exception;
use Gambio\StyleEdit\Adapters\Interfaces\ThemeContentImporterAdapterInterface;
use Gambio\StyleEdit\Core\Components\BasicController;
use Gambio\StyleEdit\Core\Components\Theme\Entities\Interfaces\RequestedThemeInterface;
use Gambio\StyleEdit\Core\Components\Theme\Entities\PreviewThemeSettings;
use Gambio\StyleEdit\Core\Components\Theme\Entities\ThemeConfiguration;
use Gambio\StyleEdit\Core\Components\Theme\Services\ThemeExtenderService;
use Gambio\StyleEdit\Core\SingletonPrototype;
use Gambio\StyleEdit\Core\TranslatedException;
use GoogleFontDownloader;
use GoogleFontManager;
use ReflectionException;
/**
* Class ThemeController
* @package Gambio\StyleEdit\Core\Components\Theme
*/
class ThemeController extends BasicController
{
/**
* @var ThemeExtenderService
*/
protected $themeExtender;
/**
* ThemeController constructor.
*
* @param ThemeExtenderService $themeExtender
*/
public function __construct(ThemeExtenderService $themeExtender)
{
$this->themeExtender = $themeExtender;
}
/**
* @param array $uri
*
* @return mixed|void
* @throws Exception
*/
public function get(array $uri)
{
if (count($uri) === 4 && end($uri) === 'font') {
$this->additionalGoogleFonts();
} elseif (count($uri) === 3) {
if ($this->currentThemeId()) {
$this->outputTheme();
} else {
throw new TranslatedException('StyleEdit.exceptions.invalid-theme-id', [end($uri)], 404);
}
} elseif (count($uri) === 2) {
$this->outputThemeList();
}
}
/**
*
*/
protected function additionalGoogleFonts(): void
{
$themeId = $this->currentThemeId();
$masterFontVariable = \StyleEditServiceFactory::service()->getMasterFontVariableName();
$reader = \StyleEditServiceFactory::service()->getStyleEditReader($themeId);
$googleFontUrl = $reader->findSettingValueByName($masterFontVariable);
$regularExpression = '/[=\|]([\w\+]+)\:?/m';
$result = [];
if (preg_match_all($regularExpression, $googleFontUrl, $matches, PREG_SET_ORDER, 0)) {
// only use the grouped value
// and replace any '+' for a whitespace character
$matches = array_map(static function ($item) {
return str_replace('+', ' ', end($item));
},
$matches);
$requiredFonts = $this->fontManager($googleFontUrl)->requiredFonts();
// remove fonts that are all ready in the select input
$matches = array_values(array_filter($matches,
static function ($item) use ($requiredFonts) {
return !in_array($item, $requiredFonts, true);
}));
if (count($matches)) {
$result = $matches;
}
}
// Print fonts that are available, but are not in select input
$this->outputJson($result);
}
/**
* @throws Exception
*/
protected function outputTheme(): void
{
$this->outputJson($this->themeService()->getConfigurationById($this->currentThemeId()));
}
/**
* @throws Exception
*/
protected function outputThemeList(): void
{
$themeCollection = $this->getThemesList();
$gambioThemes = [];
$duplicatedThemes = [];
foreach ($themeCollection as $theme) {
/** @var ThemeConfiguration $theme */
if ($theme->author() === "Gambio GmbH" && !preg_match('/\-\s(Copy|Kopie)$/', $theme->title())) {
$gambioThemes[] = $theme;
} else {
$duplicatedThemes[] = $theme;
}
}
//put the gambio themes inthe begning of the list
$themeList = array_merge($gambioThemes, $duplicatedThemes);
$this->outputJson($this->themeService()->sortByActive($themeList));
}
/**
* @param string $fontUrl
*
* @return GoogleFontManager
*/
protected function fontManager(string $fontUrl): \GoogleFontManager
{
return new GoogleFontManager(new GoogleFontDownloader, $fontUrl);
}
/**
* Save some theme
*
* @param array $uri
* @param $data
*
* @return mixed|void
* @throws Exception
*/
public function put(array $uri, $data)
{
$this->themeService()->save($data);
}
/** Duplicate a theme
*
* @param array $uri
* @param $data
*
* @return mixed|void
* @throws Exception
*/
public function post(array $uri, $data)
{
if (count($uri) >= 3 && strtoupper($uri[2]) === 'IMPORT') {
$jsonObject = json_decode($data, false);
$themeId = $jsonObject->name ?? null;
$themeTmpPath = $jsonObject->path ?? null;
$overwrite = $jsonObject->overwrite ?? false;
$response = $this->getImportThemeService()->import($themeId, $themeTmpPath, $overwrite);
$this->outputJson($response);
return;
}
$theme = $this->themeService()->getConfigurationById($this->currentThemeId());
//Ex: styleEdit/Theme/HoneyGrid/duplicate
if ($theme && count($uri) >= 4 && strtoupper($uri[3]) === 'DUPLICATE') {
$this->createExtendedTheme($theme, $data);
} elseif (count($uri) >= 4 && strtoupper($uri[3]) === 'CREATEPREVIEW') {
if (!$theme) {
throw new TranslatedException('StyleEdit.exceptions.invalid-theme-id', [end($uri)], 404);
}
if ($theme->isPreview()) {
throw new TranslatedException('StyleEdit.exceptions.cant-create-preview-of-preview',
[$theme->id()],
404);
}
$previewThemeId = $theme->id() . '_preview';
if ($this->getThemesList()->hasKey($previewThemeId)) {
throw (new TranslatedException('StyleEdit.exceptions.preview-already-exists',
[$theme->id()],
100))->withHttpStatusCode(500);
}
$jsonObject = (object)[
'title' => $theme->title() . ' Preview',
'id' => $previewThemeId,
'preview' => true
];
$configFile = $this->themeService()->duplicateTheme($theme, $jsonObject);
//needs to inherit from parent in order to replicate the behaviour of the source/original theme
$this->themeService()->patch($configFile->id(),
[
'extends' => $theme->extendsOf()
]);
$this->updateThemeList();
$path = $this->themeService()->createPreviewFolder();
$previewSettings = new PreviewThemeSettings($previewThemeId, $path, 'templates_c/temp/' . $previewThemeId);
$this->previewSettingsService()->save($previewSettings);
$this->importThemeContent($theme->id());
$this->outputJson(['id' => $previewThemeId, 'path' => $path]);
}
}
/**
* @param ThemeConfiguration $theme
* @param $data
*
* @throws TranslatedException
*/
public function createExtendedTheme(ThemeConfiguration $theme, $data)
{
$jsonObject = json_decode($data, false);
if (isset($jsonObject->id)) {
unset($jsonObject->id);
}
$id = $this->themeExtender->extendTheme($theme, $jsonObject);
$this->updateThemeList();
$this->outputJson($this->getThemesList()->get($id));
}
/**
* @param $themeId
*
* @throws Exception
*/
protected function importThemeContent($themeId): void
{
/**
* @var ThemeContentImporterAdapterInterface $importer
*/
$importer = SingletonPrototype::instance()->get(ThemeContentImporterAdapterInterface::class);
if ($importer) {
$importer->importContentFromTheme($themeId);
}
}
/**
* @param array $uri
* @param $data
*
* @return mixed|void
* @throws Exception
*/
public function delete(array $uri, $data)
{
if ($this->currentThemeId()) {
$this->themeService()->deleteTheme($this->currentThemeId());
} else {
throw new Exception('Invalid theme');
}
}
/**
* @param array $uri
* @param $data
*
* @return mixed|void
* @throws Exception
*/
public function patch(array $uri, $data)
{
$data = json_decode($data, false);
$this->themeService()->patch($this->currentThemeId(), $data);
}
/**
* @return BasicController|void
*/
public function __clone()
{
}
/**
* @param $id
*
* @return ThemeConfiguration
* @throws TranslatedException
* @throws \FileNotFoundException
*/
protected function getTheme($id)
{
return $this->themeService()->getConfigurationById($id);
}
}