. // }}} namespace Plugin\Oomox; use App\Core\Cache; use App\Core\DB; use App\Core\Event; use App\Core\Modules\Plugin; use App\Core\Router; use App\Entity\LocalUser; use App\Util\Common; use App\Util\Exception\NotFoundException; use App\Util\Exception\RedirectException; use App\Util\Exception\ServerException; use Plugin\Oomox\Controller as C; use Symfony\Component\HttpFoundation\Request; /** * Profile Colour plugin main class * * @package GNUsocial * @category Oomox * * @author Eliseu Amaro * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ class Oomox extends Plugin { /** * Maps Routes to their respective Controllers */ public function onAddRoute(Router $r): bool { $r->connect('oomox_settings', 'settings/oomox', [Controller\Oomox::class, 'oomoxSettings']); $r->connect('oomox_css', 'plugins/oomox/colours', [Controller\Oomox::class, 'oomoxCSS']); return Event::next; } /** * Populates an additional profile user panel section * Used in templates/settings/base.html.twig * * @throws \App\Util\Exception\NoLoggedInUser * @throws RedirectException * @throws ServerException */ public function onPopulateSettingsTabs(Request $request, string $section, array &$tabs): bool { if ($section === 'colours') { $tabs[] = [ 'title' => 'Light theme colours', 'desc' => 'Change the theme colours.', 'id' => 'settings-light-theme-colours-details', 'controller' => C\Oomox::oomoxSettingsLight($request), ]; $tabs[] = [ 'title' => 'Dark theme colours', 'desc' => 'Change the theme colours.', 'id' => 'settings-dark-theme-colours-details', 'controller' => C\Oomox::oomoxSettingsDark($request), ]; } return Event::next; } /** * Returns Oomox cache key for the given user. */ public static function cacheKey(LocalUser $user): string { return "oomox-css-{$user->getId()}"; } /** * Returns Entity\Oomox if it already exists */ public static function getEntity(LocalUser $user): ?Entity\Oomox { try { return Cache::get(self::cacheKey($user), static fn () => DB::findOneBy('oomox', ['actor_id' => $user->getId()])); } catch (NotFoundException $e) { return null; } } /** * Adds to array $styles the generated CSS according to user settings, if any are present. */ public function onEndShowStyles(array &$styles, string $route): bool { $user = Common::user(); if ($user && Cache::get(self::cacheKey($user), static fn () => self::getEntity($user))) { $styles[] = Router::url('oomox_css'); } return Event::next; } }