AzuraCast/src/Customization.php

199 lines
5.4 KiB
PHP
Raw Permalink Normal View History

<?php
2021-07-19 07:53:45 +02:00
declare(strict_types=1);
namespace App;
2023-06-07 08:25:57 +02:00
use App\Assets\AssetTypes;
use App\Assets\BrowserIconCustomAsset;
2023-06-07 08:25:57 +02:00
use App\Container\EnvironmentAwareTrait;
2023-06-07 14:18:05 +02:00
use App\Entity\Repository\SettingsRepository;
use App\Entity\Settings;
use App\Entity\Station;
use App\Enums\SupportedLocales;
use App\Enums\SupportedThemes;
use App\Http\ServerRequest;
use App\Traits\RequestAwareTrait;
2022-07-01 09:41:04 +02:00
final class Customization
{
use RequestAwareTrait;
2023-06-07 08:25:57 +02:00
use EnvironmentAwareTrait;
2023-06-07 14:18:05 +02:00
private Settings $settings;
2022-07-01 09:41:04 +02:00
private SupportedLocales $locale;
2020-07-10 10:17:51 +02:00
private ?SupportedThemes $publicTheme;
2023-06-06 12:43:06 +02:00
private string $instanceName;
public function __construct(
2023-06-07 14:18:05 +02:00
SettingsRepository $settingsRepo
) {
$this->settings = $settingsRepo->readSettings();
$this->instanceName = $this->settings->getInstanceName() ?? '';
2023-05-16 00:13:25 +02:00
$this->publicTheme = $this->settings->getPublicTheme();
$this->locale = SupportedLocales::default();
}
public function setRequest(?ServerRequest $request): void
{
$this->request = $request;
if (null !== $request) {
// Register current theme
$queryParams = $request->getQueryParams();
if (!empty($queryParams['theme'])) {
$theme = SupportedThemes::tryFrom($queryParams['theme']);
if (null !== $theme && $theme !== SupportedThemes::Browser) {
$this->publicTheme = $theme;
}
}
2020-07-10 10:17:51 +02:00
// Register locale
$this->locale = SupportedLocales::createFromRequest($this->environment, $request);
}
}
public function getLocale(): SupportedLocales
{
2020-07-10 10:17:51 +02:00
return $this->locale;
}
/**
* Get the instance name for this AzuraCast instance.
*/
2020-07-10 10:17:51 +02:00
public function getInstanceName(): string
{
2020-07-10 10:17:51 +02:00
return $this->instanceName;
}
/**
* Get the theme name to be used in public (non-logged-in) pages.
*/
public function getPublicTheme(): ?SupportedThemes
{
return (SupportedThemes::Browser !== $this->publicTheme)
? $this->publicTheme
: null;
}
/**
* Return the administrator-supplied custom CSS for public (minimal layout) pages, if specified.
*/
2020-07-08 09:03:50 +02:00
public function getCustomPublicCss(): string
{
2021-07-30 08:20:14 +02:00
$publicCss = $this->settings->getPublicCustomCss() ?? '';
2023-06-07 08:25:57 +02:00
$background = AssetTypes::Background->createObject($this->environment);
2021-07-30 08:20:14 +02:00
if ($background->isUploaded()) {
$backgroundUrl = $background->getUrl();
$publicCss .= <<<CSS
[data-bs-theme] body.page-minimal {
2022-05-08 20:05:02 +02:00
background-image: url('{$backgroundUrl}');
2021-07-30 08:20:14 +02:00
}
CSS;
}
return $publicCss;
}
2023-06-07 14:18:05 +02:00
public function getStationCustomPublicCss(Station $station): string
2023-01-03 23:55:49 +01:00
{
$publicCss = $station->getBrandingConfig()->getPublicCustomCss() ?? '';
2023-06-07 08:25:57 +02:00
$background = AssetTypes::Background->createObject($this->environment, $station);
2023-01-03 23:55:49 +01:00
if ($background->isUploaded()) {
$backgroundUrl = $background->getUrl();
$publicCss .= <<<CSS
[data-bs-theme] body.page-minimal {
2023-01-03 23:55:49 +01:00
background-image: url('{$backgroundUrl}');
}
CSS;
}
return $publicCss;
}
/**
* Return the administrator-supplied custom JS for public (minimal layout) pages, if specified.
*/
2020-07-08 09:03:50 +02:00
public function getCustomPublicJs(): string
{
return $this->settings->getPublicCustomJs() ?? '';
}
2023-06-07 14:18:05 +02:00
public function getStationCustomPublicJs(Station $station): string
2023-01-03 23:55:49 +01:00
{
return $station->getBrandingConfig()->getPublicCustomJs() ?? '';
}
/**
* Return the administrator-supplied custom CSS for internal (full layout) pages, if specified.
*/
2020-07-08 09:03:50 +02:00
public function getCustomInternalCss(): string
{
return $this->settings->getInternalCustomCss() ?? '';
}
2021-07-30 08:20:14 +02:00
public function getBrowserIconUrl(int $size = 256): string
{
2023-06-07 08:25:57 +02:00
/** @var BrowserIconCustomAsset $browserIcon */
$browserIcon = AssetTypes::BrowserIcon->createObject($this->environment);
return $browserIcon->getUrlForSize($size);
2021-07-30 08:20:14 +02:00
}
/**
* Return whether to show or hide album art on public pages.
*/
public function hideAlbumArt(): bool
{
return $this->settings->getHideAlbumArt();
}
/**
* Return the calculated page title given branding settings and the application environment.
*
* @param string|null $title
*/
2021-07-19 07:53:45 +02:00
public function getPageTitle(?string $title = null): string
{
if (!$this->hideProductName()) {
if ($title) {
$title .= ' - ' . $this->environment->getAppName();
} else {
$title = $this->environment->getAppName();
}
}
if (!$this->environment->isProduction()) {
2022-01-17 05:45:07 +01:00
$title = '(' . $this->environment->getAppEnvironmentEnum()->getName() . ') ' . $title;
}
2021-07-19 07:53:45 +02:00
return $title ?? '';
}
/**
2019-09-04 20:00:51 +02:00
* Return whether to show or hide the AzuraCast name from public-facing pages.
*/
2019-09-04 20:00:51 +02:00
public function hideProductName(): bool
{
return $this->settings->getHideProductName();
}
public function enableAdvancedFeatures(): bool
{
return $this->settings->getEnableAdvancedFeatures();
}
public function useStaticNowPlaying(): bool
{
return $this->settings->getEnableStaticNowPlaying();
}
}