[PLUGINS][Oomox] Further checks done when handling form requests. Improved documentation, fixed typos and diminished repeated calls

This commit is contained in:
Eliseu Amaro 2021-12-03 01:18:25 +00:00 committed by Diogo Peralta Cordeiro
parent ba632b4514
commit ff5f346fec
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
4 changed files with 165 additions and 129 deletions

View File

@ -1,6 +1,6 @@
<?php <?php
declare(strict_types = 1); declare(strict_types=1);
// {{{ License // {{{ License
@ -26,20 +26,23 @@ namespace Plugin\Oomox\Controller;
use App\Core\Cache; use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\ClientException; use App\Util\Exception\ClientException;
use App\Util\Exception\NoLoggedInUser; use App\Util\Exception\NoLoggedInUser;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use App\Util\Formatting; use App\Util\Formatting;
use Plugin\Oomox\Entity; use Plugin\Oomox\Entity\Oomox as EntityOomox;
use Plugin\Oomox\Oomox as PluginOomox;
use Symfony\Component\Form\Extension\Core\Type\ColorType; use Symfony\Component\Form\Extension\Core\Type\ColorType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\SubmitButton;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use function App\Core\I18n\_m;
use function is_null;
/** /**
* Oomox controller * Oomox controller
@ -53,6 +56,82 @@ use Symfony\Component\HttpFoundation\Response;
*/ */
class Oomox class Oomox
{ {
/**
* Generates a FormInterface depending on current theme settings and system-wide colour preference.
* Receives the user's Oomox entity, and wether or not its intended for dark of light theme to change its behaviour accordingly.
*
* @throws ServerException
*/
public function getOomoxForm(?EntityOomox $current_oomox_settings, bool $is_light): FormInterface
{
$theme = $is_light ? 'light' : 'dark';
$foreground = 'colour_foreground_' . $theme;
$background_hard = 'colour_background_hard_' . $theme;
$background_card = 'colour_background_card_' . $theme;
$border = 'colour_border_' . $theme;
$accent = 'colour_accent_' . $theme;
$reset = 'colour_reset_' . $theme;
$save = 'save_oomox_colours_' . $theme;
if (isset($current_oomox_settings)) {
if ($is_light) {
$current_foreground = $current_oomox_settings->getColourForegroundLight() ?: '#09090d';
$current_background_hard = $current_oomox_settings->getColourBackgroundHardLight() ?: '#ebebeb';
$current_background_card = $current_oomox_settings->getColourBackgroundCardLight() ?: '#f0f0f0';
$current_border = $current_oomox_settings->getColourBorderLight() ?: '#d5d5d5';
$current_accent = $current_oomox_settings->getColourAccentLight() ?: '#a22430';
} else {
$current_foreground = $current_oomox_settings->getColourForegroundDark() ?: '#f0f6f6';
$current_background_hard = $current_oomox_settings->getColourBackgroundHardDark() ?: '#141216';
$current_background_card = $current_oomox_settings->getColourBackgroundCardDark() ?: '#131217';
$current_border = $current_oomox_settings->getColourBorderDark() ?: '#201f25';
$current_accent = $current_oomox_settings->getColourAccentDark() ?: '#5ddbcf';
}
} else {
$current_foreground = $is_light ? '#09090d' : '#f0f6f6';
$current_background_hard = $is_light ? '#ebebeb' : '#141216';
$current_background_card = $is_light ? '#f0f0f0' : '#131217';
$current_border = $is_light ? '#d5d5d5' : '#201f25';
$current_accent = $is_light ? '#a22430' : '#5ddbcf';
}
return Form::create([
[$foreground, ColorType::class, [
'html5' => true,
'data' => $current_foreground,
'label' => _m('Foreground colour'),
'help' => _m('Choose the foreground colour'),],
],
[$background_hard, ColorType::class, [
'html5' => true,
'data' => $current_background_hard,
'label' => _m('Background colour'),
'help' => _m('Choose the background colour'),],
],
[$background_card, ColorType::class, [
'html5' => true,
'data' => $current_background_card,
'label' => _m('Card background colour'),
'help' => _m('Choose the card background colour'),],
],
[$border, ColorType::class, [
'html5' => true,
'data' => $current_border,
'label' => _m('Border colour'),
'help' => _m('Choose the borders accents'),],
],
[$accent, ColorType::class, [
'html5' => true,
'data' => $current_accent,
'label' => _m('Accent colour'),
'help' => _m('Choose the accent colour'),],
],
['hidden', HiddenType::class, []],
[$reset, SubmitType::class, ['label' => _m('Reset colours to default')]],
[$save, SubmitType::class, ['label' => _m('Submit')]],
]);
}
/** /**
* Handles Light theme settings tab * Handles Light theme settings tab
* *
@ -62,34 +141,37 @@ class Oomox
*/ */
public static function oomoxSettingsLight(Request $request): array public static function oomoxSettingsLight(Request $request): array
{ {
$user = Common::ensureLoggedIn(); $user = Common::ensureLoggedIn();
$actor_id = $user->getId(); $actor_id = $user->getId();
$current_oomox_settings = \Plugin\Oomox\Oomox::getEntity($user); $current_oomox_settings = PluginOomox::getEntity($user);
$form_light = (new self)->getOomoxForm($current_oomox_settings, true); $form_light = (new self)->getOomoxForm($current_oomox_settings, true);
$form_light->handleRequest($request); $form_light->handleRequest($request);
if ($form_light->isSubmitted() && $form_light->isValid()) { if ($form_light->isSubmitted() && $form_light->isValid()) {
/** @var SubmitButton $reset_button */
$reset_button = $form_light->get('colour_reset_light'); $reset_button = $form_light->get('colour_reset_light');
if ($reset_button->isClicked()) { if ($reset_button->isClicked()) {
$current_oomox_settings->resetTheme(true); if (isset($current_oomox_settings)) {
$current_oomox_settings?->resetTheme(true);
}
} else { } else {
$data = $form_light->getData(); $data = $form_light->getData();
$current_oomox_settings = Entity\Oomox::create( $current_oomox_settings = EntityOomox::create(
[ [
'actor_id' => $actor_id, 'actor_id' => $actor_id,
'colour_foreground_light' => $data['colour_foreground_light'], 'colour_foreground_light' => $data['colour_foreground_light'],
'colour_background_hard_light' => $data['colour_background_hard_light'], 'colour_background_hard_light' => $data['colour_background_hard_light'],
'colour_background_card_light' => $data['colour_background_card_light'], 'colour_background_card_light' => $data['colour_background_card_light'],
'colour_border_light' => $data['colour_border_light'], 'colour_border_light' => $data['colour_border_light'],
'colour_accent_light' => $data['colour_accent_light'], 'colour_accent_light' => $data['colour_accent_light'],
], ],
); );
} }
DB::merge($current_oomox_settings); DB::merge($current_oomox_settings);
DB::flush(); DB::flush();
Cache::delete(\Plugin\Oomox\Oomox::cacheKey($user)); Cache::delete(PluginOomox::cacheKey($user));
throw new RedirectException(); throw new RedirectException();
} }
@ -105,132 +187,57 @@ class Oomox
*/ */
public static function oomoxSettingsDark(Request $request): array public static function oomoxSettingsDark(Request $request): array
{ {
$user = Common::ensureLoggedIn(); $user = Common::ensureLoggedIn();
$actor_id = $user->getId(); $actor_id = $user->getId();
$current_oomox_settings = \Plugin\Oomox\Oomox::getEntity($user); $current_oomox_settings = PluginOomox::getEntity($user);
$form_dark = (new self)->getOomoxForm($current_oomox_settings, false); $form_dark = (new self)->getOomoxForm($current_oomox_settings, false);
$form_dark->handleRequest($request); $form_dark->handleRequest($request);
if ($form_dark->isSubmitted() && $form_dark->isValid()) { if ($form_dark->isSubmitted() && $form_dark->isValid()) {
$reset_button = $form_dark->get('colour_reset_dark'); $reset_button = $form_dark->get('colour_reset_dark');
/** @var SubmitButton $reset_button */
if ($reset_button->isClicked()) { if ($reset_button->isClicked()) {
$current_oomox_settings->resetTheme(false); $current_oomox_settings?->resetTheme(false);
} else { } else {
$data = $form_dark->getData(); $data = $form_dark->getData();
$current_oomox_settings = Entity\Oomox::create( $current_oomox_settings = EntityOomox::create(
[ [
'actor_id' => $actor_id, 'actor_id' => $actor_id,
'colour_foreground_dark' => $data['colour_foreground_dark'], 'colour_foreground_dark' => $data['colour_foreground_dark'],
'colour_background_hard_dark' => $data['colour_background_hard_dark'], 'colour_background_hard_dark' => $data['colour_background_hard_dark'],
'colour_background_card_dark' => $data['colour_background_card_dark'], 'colour_background_card_dark' => $data['colour_background_card_dark'],
'colour_border_dark' => $data['colour_border_dark'], 'colour_border_dark' => $data['colour_border_dark'],
'colour_accent_dark' => $data['colour_accent_dark'], 'colour_accent_dark' => $data['colour_accent_dark'],
], ],
); );
} }
DB::merge($current_oomox_settings); DB::merge($current_oomox_settings);
DB::flush(); DB::flush();
Cache::delete(\Plugin\Oomox\Oomox::cacheKey($user)); Cache::delete(PluginOomox::cacheKey($user));
throw new RedirectException(); throw new RedirectException();
} }
return ['_template' => 'oomox/oomoxSettingsDark.html.twig', 'oomoxDark' => $form_dark->createView()]; return ['_template' => 'oomox/oomoxSettingsDark.html.twig', 'oomoxDark' => $form_dark->createView()];
} }
/**
* Generates a FormInterface depending on current theme settings and system-wide colour preference.
* Receives the user's Oomox entity, and wether or not its intended for dark of light theme to change its behaviour accordingly.
*
* @throws ServerException
*/
public function getOomoxForm(?Entity\Oomox $current_oomox_settings, bool $is_light): FormInterface
{
$foreground = 'colour_foreground_' . ($is_light ? 'light' : 'dark');
$background_hard = 'colour_background_hard_' . ($is_light ? 'light' : 'dark');
$background_card = 'colour_background_card_' . ($is_light ? 'light' : 'dark');
$border = 'colour_border_' . ($is_light ? 'light' : 'dark');
$accent = 'colour_accent_' . ($is_light ? 'light' : 'dark');
$reset = 'colour_reset_' . ($is_light ? 'light' : 'dark');
$save = 'save_oomox_colours_' . ($is_light ? 'light' : 'dark');
if (isset($current_oomox_settings)) {
if ($is_light) {
$current_foreground = $current_oomox_settings->getColourForegroundLight() ?: '#09090d';
$current_background_hard = $current_oomox_settings->getColourBackgroundHardLight() ?: '#ebebeb';
$current_background_card = $current_oomox_settings->getColourBackgroundCardLight() ?: '#f0f0f0';
$current_border = $current_oomox_settings->getColourBorderLight() ?: '#d5d5d5';
$current_accent = $current_oomox_settings->getColourAccentLight() ?: '#a22430';
} else {
$current_foreground = $current_oomox_settings->getColourForegroundDark() ?: '#f0f6f6';
$current_background_hard = $current_oomox_settings->getColourBackgroundHardDark() ?: '#141216';
$current_background_card = $current_oomox_settings->getColourBackgroundCardDark() ?: '#131217';
$current_border = $current_oomox_settings->getColourBorderDark() ?: '#201f25';
$current_accent = $current_oomox_settings->getColourAccentDark() ?: '#5ddbcf';
}
} else {
$current_foreground = $is_light ? '#09090d' : '#f0f6f6';
$current_background_hard = $is_light ? '#ebebeb' : '#141216';
$current_background_card = $is_light ? '#f0f0f0' : '#131217';
$current_border = $is_light ? '#d5d5d5' : '#201f25';
$current_accent = $is_light ? '#a22430' : '#5ddbcf';
}
return Form::create([
[$foreground, ColorType::class, [
'html5' => true,
'data' => $current_foreground,
'label' => _m('Foreground colour'),
'help' => _m('Choose the foreground colour'), ],
],
[$background_hard, ColorType::class, [
'html5' => true,
'data' => $current_background_hard,
'label' => _m('Background colour'),
'help' => _m('Choose the background colour'), ],
],
[$background_card, ColorType::class, [
'html5' => true,
'data' => $current_background_card,
'label' => _m('Card background colour'),
'help' => _m('Choose the card background colour'), ],
],
[$border, ColorType::class, [
'html5' => true,
'data' => $current_border,
'label' => _m('Border colour'),
'help' => _m('Choose the borders accents'), ],
],
[$accent, ColorType::class, [
'html5' => true,
'data' => $current_accent,
'label' => _m('Accent colour'),
'help' => _m('Choose the accent colour'), ],
],
['hidden', HiddenType::class, []],
[$reset, SubmitType::class, ['label' => _m('Reset colors to default')]],
[$save, SubmitType::class, ['label' => _m('Submit')]],
]);
}
/** /**
* Renders the resulting CSS file from user options, serves that file as a response * Renders the resulting CSS file from user options, serves that file as a response
* *
* @throws ClientException * @return Response
* @throws NoLoggedInUser * @throws NoLoggedInUser
* @throws ServerException * @throws ServerException
* *
* @return Response * @throws ClientException
*/ */
public function oomoxCSS() public function oomoxCSS(): Response
{ {
$user = Common::ensureLoggedIn(); $user = Common::ensureLoggedIn();
$oomox_table = \Plugin\Oomox\Oomox::getEntity($user); $oomox_table = PluginOomox::getEntity($user);
if (\is_null($oomox_table)) { if (is_null($oomox_table)) {
throw new ClientException(_m('No custom colors defined', 404)); throw new ClientException(_m('No custom colours defined', 404));
} }
$content = Formatting::twigRenderFile('/oomox/root_override.css.twig', ['oomox' => $oomox_table]); $content = Formatting::twigRenderFile('/oomox/root_override.css.twig', ['oomox' => $oomox_table]);

View File

@ -222,16 +222,16 @@ class Oomox extends Entity
'name' => 'oomox', 'name' => 'oomox',
'fields' => [ 'fields' => [
'actor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to actor table'], 'actor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to actor table'],
'colour_foreground_light' => ['type' => 'text', 'description' => 'color hex code'], 'colour_foreground_light' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_background_hard_light' => ['type' => 'text', 'description' => 'color hex code'], 'colour_background_hard_light' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_background_card_light' => ['type' => 'text', 'description' => 'color hex code'], 'colour_background_card_light' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_border_light' => ['type' => 'text', 'description' => 'color hex code'], 'colour_border_light' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_accent_light' => ['type' => 'text', 'description' => 'color hex code'], 'colour_accent_light' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_foreground_dark' => ['type' => 'text', 'description' => 'color hex code'], 'colour_foreground_dark' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_background_hard_dark' => ['type' => 'text', 'description' => 'color hex code'], 'colour_background_hard_dark' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_background_card_dark' => ['type' => 'text', 'description' => 'color hex code'], 'colour_background_card_dark' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_border_dark' => ['type' => 'text', 'description' => 'color hex code'], 'colour_border_dark' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_accent_dark' => ['type' => 'text', 'description' => 'color hex code'], 'colour_accent_dark' => ['type' => 'text', 'description' => 'colour hex code'],
'created' => ['type' => 'datetime', 'not null' => true, 'description' => 'date this record was created', 'default' => 'CURRENT_TIMESTAMP'], 'created' => ['type' => 'datetime', 'not null' => true, 'description' => 'date this record was created', 'default' => 'CURRENT_TIMESTAMP'],
'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified', 'default' => 'CURRENT_TIMESTAMP'], 'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified', 'default' => 'CURRENT_TIMESTAMP'],
], ],

View File

@ -41,7 +41,7 @@ use Symfony\Component\HttpFoundation\Request;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
/** /**
* Profile Color plugin main class * Profile Colour plugin main class
* *
* @package GNUsocial * @package GNUsocial
* @category Oomox * @category Oomox
@ -52,10 +52,12 @@ use function App\Core\I18n\_m;
*/ */
class Oomox extends Plugin class Oomox extends Plugin
{ {
/** /**
* Map URLs to actions * Map URLs to actions
* *
* @return bool hook value; true means continue processing, false means stop * @param RouteLoader $r
* @return bool
*/ */
public function onAddRoute(RouteLoader $r): bool public function onAddRoute(RouteLoader $r): bool
{ {
@ -64,9 +66,17 @@ class Oomox extends Plugin
return Event::next; return Event::next;
} }
/** /**
* Populates an additional profile user panel section
* Used in templates/settings/base.html.twig
*
* @param Request $request
* @param array $tabs
* @return bool
* @throws RedirectException * @throws RedirectException
* @throws ServerException * @throws ServerException
* @throws \App\Util\Exception\NoLoggedInUser
*/ */
public function onPopulateProfileSettingsTabs(Request $request, array &$tabs): bool public function onPopulateProfileSettingsTabs(Request $request, array &$tabs): bool
{ {
@ -85,10 +95,22 @@ class Oomox extends Plugin
return Event::next; return Event::next;
} }
/**
* Returns Oomox cache key for the given user.
*
* @param LocalUser $user
* @return string
*/
public static function cacheKey(LocalUser $user) :string { public static function cacheKey(LocalUser $user) :string {
return "oomox-css-{$user->getId()}"; return "oomox-css-{$user->getId()}";
} }
/**
* Returns Entity\Oomox if it already exists
*
* @param LocalUser $user
* @return Entity\Oomox|null
*/
public static function getEntity(LocalUser $user): ?Entity\Oomox public static function getEntity(LocalUser $user): ?Entity\Oomox
{ {
try { try {
@ -98,6 +120,13 @@ class Oomox extends Plugin
} }
} }
/**
* Adds to array $styles the generated CSS according to user settings, if any are present.
*
* @param array $styles
* @param string $route
* @return bool
*/
public function onEndShowStyles(array &$styles, string $route) public function onEndShowStyles(array &$styles, string $route)
{ {
$user = Common::user(); $user = Common::user();

View File

@ -6,34 +6,34 @@
--big: 3rem; --big: 3rem;
} }
@media(prefers-color-scheme:dark) { @media (prefers-color-scheme: dark) {
:root { :root {
--foreground: {{ oomox.getColourForegroundDark() | default('#f0f6f6') | raw }}; --foreground: {{ oomox.getColourForegroundDark() | default('#f0f6f6') | raw }};
--background-hard: {{ oomox.getColourBackgroundHardDark() | default('#141216') | raw }}; --background-hard: {{ oomox.getColourBackgroundHardDark() | default('#141216') | raw }};
--background-card: {{ oomox.getColourBackgroundCardDark() | default('#131217') | raw }}; --background-card: {{ oomox.getColourBackgroundCardDark() | default('#131217') | raw }};
--border: {{ oomox.getColourBorderDark() | default('#201f25') | raw }}; --border: {{ oomox.getColourBorderDark() | default('#201f25') | raw }};
--accent: {{ oomox.getColourAccentDark() | default('#5ddbcf') | raw }}; --accent: {{ oomox.getColourAccentDark() | default('#5ddbcf') | raw }};
--shadow: 0px 25px 42px -10px rgba(0,0,0,0.09) !important; --shadow: 0px 25px 42px -10px rgba(0, 0, 0, 0.09) !important;
--shadow-inset-accent: inset 0 0 0 2px var(--accent); --shadow-inset-accent: inset 0 0 0 2px var(--accent);
--background-checkerboard: repeating-conic-gradient(#ffffff66 0 90deg,#ffffff33 0 180deg) 0 0/40px 40px round; --background-checkerboard: repeating-conic-gradient(#ffffff66 0 90deg, #ffffff33 0 180deg) 0 0/40px 40px round;
--gradient: linear-gradient(10deg,var(--border) 0,transparent 90%) !important; --gradient: linear-gradient(10deg, var(--border) 0, transparent 90%) !important;
--gradient-backwards: linear-gradient(290deg,var(--border) 0,var(--background-card) 100%) !important; --gradient-backwards: linear-gradient(290deg, var(--border) 0, var(--background-card) 100%) !important;
} }
} }
@media(prefers-color-scheme:light) { @media (prefers-color-scheme: light) {
:root { :root {
--foreground: {{ oomox.getColourForegroundLight() | default('#09090d') | raw }}; --foreground: {{ oomox.getColourForegroundLight() | default('#09090d') | raw }};
--background-hard: {{ oomox.getColourBackgroundHardLight() | default('#ebebeb') | raw }}; --background-hard: {{ oomox.getColourBackgroundHardLight() | default('#ebebeb') | raw }};
--background-card: {{ oomox.getColourBackgroundCardLight() | default('#f0f0f0') | raw }}; --background-card: {{ oomox.getColourBackgroundCardLight() | default('#f0f0f0') | raw }};
--border: {{ oomox.getColourBorderLight() | default('#d5d5d5') | raw }}; --border: {{ oomox.getColourBorderLight() | default('#d5d5d5') | raw }};
--accent: {{ oomox.getColourAccentLight() | default('#a22430') | raw }}; --accent: {{ oomox.getColourAccentLight() | default('#a22430') | raw }};
--shadow: 0px 25px 42px -10px rgba(1,1,1,0.09) !important; --shadow: 0px 25px 42px -10px rgba(1, 1, 1, 0.09) !important;
--shadow-inset-accent: inset 0 0 0 2px var(--accent); --shadow-inset-accent: inset 0 0 0 2px var(--accent);
--background-checkerboard: repeating-conic-gradient(#ffffff66 0 90deg,#ffffff33 0 180deg) 0 0/40px 40px round; --background-checkerboard: repeating-conic-gradient(#ffffff66 0 90deg, #ffffff33 0 180deg) 0 0/40px 40px round;
--gradient: linear-gradient(10deg,var(--background-hard) 0,transparent 60%) !important; --gradient: linear-gradient(10deg, var(--background-hard) 0, transparent 60%) !important;
--gradient-backwards: linear-gradient(290deg,var(--background-hard) 0,var(--background-card) 100%) !important; --gradient-backwards: linear-gradient(290deg, var(--background-hard) 0, var(--background-card) 100%) !important;
} }
} }