[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

@ -26,20 +26,23 @@ namespace Plugin\Oomox\Controller;
use App\Core\Cache;
use App\Core\DB\DB;
use App\Core\Form;
use function App\Core\I18n\_m;
use App\Util\Common;
use App\Util\Exception\ClientException;
use App\Util\Exception\NoLoggedInUser;
use App\Util\Exception\RedirectException;
use App\Util\Exception\ServerException;
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\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\SubmitButton;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use function App\Core\I18n\_m;
use function is_null;
/**
* Oomox controller
@ -53,108 +56,22 @@ use Symfony\Component\HttpFoundation\Response;
*/
class Oomox
{
/**
* Handles Light theme settings tab
*
* @throws NoLoggedInUser
* @throws RedirectException
* @throws ServerException
*/
public static function oomoxSettingsLight(Request $request): array
{
$user = Common::ensureLoggedIn();
$actor_id = $user->getId();
$current_oomox_settings = \Plugin\Oomox\Oomox::getEntity($user);
$form_light = (new self)->getOomoxForm($current_oomox_settings, true);
$form_light->handleRequest($request);
if ($form_light->isSubmitted() && $form_light->isValid()) {
$reset_button = $form_light->get('colour_reset_light');
if ($reset_button->isClicked()) {
$current_oomox_settings->resetTheme(true);
} else {
$data = $form_light->getData();
$current_oomox_settings = Entity\Oomox::create(
[
'actor_id' => $actor_id,
'colour_foreground_light' => $data['colour_foreground_light'],
'colour_background_hard_light' => $data['colour_background_hard_light'],
'colour_background_card_light' => $data['colour_background_card_light'],
'colour_border_light' => $data['colour_border_light'],
'colour_accent_light' => $data['colour_accent_light'],
],
);
}
DB::merge($current_oomox_settings);
DB::flush();
Cache::delete(\Plugin\Oomox\Oomox::cacheKey($user));
throw new RedirectException();
}
return ['_template' => 'oomox/oomoxSettingsLight.html.twig', 'oomoxLight' => $form_light->createView()];
}
/**
* Handles the Dark theme settings tab
*
* @throws NoLoggedInUser
* @throws RedirectException
* @throws ServerException
*/
public static function oomoxSettingsDark(Request $request): array
{
$user = Common::ensureLoggedIn();
$actor_id = $user->getId();
$current_oomox_settings = \Plugin\Oomox\Oomox::getEntity($user);
$form_dark = (new self)->getOomoxForm($current_oomox_settings, false);
$form_dark->handleRequest($request);
if ($form_dark->isSubmitted() && $form_dark->isValid()) {
$reset_button = $form_dark->get('colour_reset_dark');
if ($reset_button->isClicked()) {
$current_oomox_settings->resetTheme(false);
} else {
$data = $form_dark->getData();
$current_oomox_settings = Entity\Oomox::create(
[
'actor_id' => $actor_id,
'colour_foreground_dark' => $data['colour_foreground_dark'],
'colour_background_hard_dark' => $data['colour_background_hard_dark'],
'colour_background_card_dark' => $data['colour_background_card_dark'],
'colour_border_dark' => $data['colour_border_dark'],
'colour_accent_dark' => $data['colour_accent_dark'],
],
);
}
DB::merge($current_oomox_settings);
DB::flush();
Cache::delete(\Plugin\Oomox\Oomox::cacheKey($user));
throw new RedirectException();
}
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
public function getOomoxForm(?EntityOomox $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');
$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) {
@ -210,27 +127,117 @@ class Oomox
'help' => _m('Choose the accent colour'),],
],
['hidden', HiddenType::class, []],
[$reset, SubmitType::class, ['label' => _m('Reset colors to default')]],
[$reset, SubmitType::class, ['label' => _m('Reset colours to default')]],
[$save, SubmitType::class, ['label' => _m('Submit')]],
]);
}
/**
* Handles Light theme settings tab
*
* @throws NoLoggedInUser
* @throws RedirectException
* @throws ServerException
*/
public static function oomoxSettingsLight(Request $request): array
{
$user = Common::ensureLoggedIn();
$actor_id = $user->getId();
$current_oomox_settings = PluginOomox::getEntity($user);
$form_light = (new self)->getOomoxForm($current_oomox_settings, true);
$form_light->handleRequest($request);
if ($form_light->isSubmitted() && $form_light->isValid()) {
/** @var SubmitButton $reset_button */
$reset_button = $form_light->get('colour_reset_light');
if ($reset_button->isClicked()) {
if (isset($current_oomox_settings)) {
$current_oomox_settings?->resetTheme(true);
}
} else {
$data = $form_light->getData();
$current_oomox_settings = EntityOomox::create(
[
'actor_id' => $actor_id,
'colour_foreground_light' => $data['colour_foreground_light'],
'colour_background_hard_light' => $data['colour_background_hard_light'],
'colour_background_card_light' => $data['colour_background_card_light'],
'colour_border_light' => $data['colour_border_light'],
'colour_accent_light' => $data['colour_accent_light'],
],
);
}
DB::merge($current_oomox_settings);
DB::flush();
Cache::delete(PluginOomox::cacheKey($user));
throw new RedirectException();
}
return ['_template' => 'oomox/oomoxSettingsLight.html.twig', 'oomoxLight' => $form_light->createView()];
}
/**
* Handles the Dark theme settings tab
*
* @throws NoLoggedInUser
* @throws RedirectException
* @throws ServerException
*/
public static function oomoxSettingsDark(Request $request): array
{
$user = Common::ensureLoggedIn();
$actor_id = $user->getId();
$current_oomox_settings = PluginOomox::getEntity($user);
$form_dark = (new self)->getOomoxForm($current_oomox_settings, false);
$form_dark->handleRequest($request);
if ($form_dark->isSubmitted() && $form_dark->isValid()) {
$reset_button = $form_dark->get('colour_reset_dark');
/** @var SubmitButton $reset_button */
if ($reset_button->isClicked()) {
$current_oomox_settings?->resetTheme(false);
} else {
$data = $form_dark->getData();
$current_oomox_settings = EntityOomox::create(
[
'actor_id' => $actor_id,
'colour_foreground_dark' => $data['colour_foreground_dark'],
'colour_background_hard_dark' => $data['colour_background_hard_dark'],
'colour_background_card_dark' => $data['colour_background_card_dark'],
'colour_border_dark' => $data['colour_border_dark'],
'colour_accent_dark' => $data['colour_accent_dark'],
],
);
}
DB::merge($current_oomox_settings);
DB::flush();
Cache::delete(PluginOomox::cacheKey($user));
throw new RedirectException();
}
return ['_template' => 'oomox/oomoxSettingsDark.html.twig', 'oomoxDark' => $form_dark->createView()];
}
/**
* Renders the resulting CSS file from user options, serves that file as a response
*
* @throws ClientException
* @return Response
* @throws NoLoggedInUser
* @throws ServerException
*
* @return Response
* @throws ClientException
*/
public function oomoxCSS()
public function oomoxCSS(): Response
{
$user = Common::ensureLoggedIn();
$oomox_table = \Plugin\Oomox\Oomox::getEntity($user);
if (\is_null($oomox_table)) {
throw new ClientException(_m('No custom colors defined', 404));
$oomox_table = PluginOomox::getEntity($user);
if (is_null($oomox_table)) {
throw new ClientException(_m('No custom colours defined', 404));
}
$content = Formatting::twigRenderFile('/oomox/root_override.css.twig', ['oomox' => $oomox_table]);

View File

@ -222,16 +222,16 @@ class Oomox extends Entity
'name' => 'oomox',
'fields' => [
'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_background_hard_light' => ['type' => 'text', 'description' => 'color hex code'],
'colour_background_card_light' => ['type' => 'text', 'description' => 'color hex code'],
'colour_border_light' => ['type' => 'text', 'description' => 'color hex code'],
'colour_accent_light' => ['type' => 'text', 'description' => 'color hex code'],
'colour_foreground_dark' => ['type' => 'text', 'description' => 'color hex code'],
'colour_background_hard_dark' => ['type' => 'text', 'description' => 'color hex code'],
'colour_background_card_dark' => ['type' => 'text', 'description' => 'color hex code'],
'colour_border_dark' => ['type' => 'text', 'description' => 'color hex code'],
'colour_accent_dark' => ['type' => 'text', 'description' => 'color hex code'],
'colour_foreground_light' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_background_hard_light' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_background_card_light' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_border_light' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_accent_light' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_foreground_dark' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_background_hard_dark' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_background_card_dark' => ['type' => 'text', 'description' => 'colour hex code'],
'colour_border_dark' => ['type' => 'text', 'description' => 'colour 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'],
'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;
/**
* Profile Color plugin main class
* Profile Colour plugin main class
*
* @package GNUsocial
* @category Oomox
@ -52,10 +52,12 @@ use function App\Core\I18n\_m;
*/
class Oomox extends Plugin
{
/**
* 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
{
@ -64,9 +66,17 @@ class Oomox extends Plugin
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 ServerException
* @throws \App\Util\Exception\NoLoggedInUser
*/
public function onPopulateProfileSettingsTabs(Request $request, array &$tabs): bool
{
@ -85,10 +95,22 @@ class Oomox extends Plugin
return Event::next;
}
/**
* Returns Oomox cache key for the given user.
*
* @param LocalUser $user
* @return string
*/
public static function cacheKey(LocalUser $user) :string {
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
{
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)
{
$user = Common::user();