2021-11-26 22:04:45 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types = 1);
|
|
|
|
// {{{ License
|
|
|
|
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
namespace Plugin\Oomox;
|
|
|
|
|
2021-11-26 23:44:35 +09:00
|
|
|
use App\Core\Cache;
|
2022-03-27 23:19:09 +09:00
|
|
|
use App\Core\DB;
|
2021-11-26 22:04:45 +09:00
|
|
|
use App\Core\Event;
|
|
|
|
use App\Core\Modules\Plugin;
|
2022-03-28 00:43:59 +09:00
|
|
|
use App\Core\Router;
|
2021-11-26 23:44:35 +09:00
|
|
|
use App\Entity\LocalUser;
|
2021-11-26 22:04:45 +09:00
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
2021-12-03 10:18:25 +09:00
|
|
|
* Profile Colour plugin main class
|
2021-11-26 22:04:45 +09:00
|
|
|
*
|
|
|
|
* @package GNUsocial
|
|
|
|
* @category Oomox
|
|
|
|
*
|
|
|
|
* @author Eliseu Amaro <mail@eliseuama.ro>
|
|
|
|
* @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
|
|
|
|
{
|
|
|
|
/**
|
2022-01-27 09:54:27 +09:00
|
|
|
* Maps Routes to their respective Controllers
|
2021-11-26 22:04:45 +09:00
|
|
|
*/
|
2022-03-28 00:43:59 +09:00
|
|
|
public function onAddRoute(Router $r): bool
|
2021-11-26 22:04:45 +09:00
|
|
|
{
|
2021-11-26 23:44:35 +09:00
|
|
|
$r->connect('oomox_settings', 'settings/oomox', [Controller\Oomox::class, 'oomoxSettings']);
|
|
|
|
$r->connect('oomox_css', 'plugins/oomox/colours', [Controller\Oomox::class, 'oomoxCSS']);
|
2021-11-26 22:04:45 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-03 10:18:25 +09:00
|
|
|
* Populates an additional profile user panel section
|
2022-03-31 08:09:26 +09:00
|
|
|
* Used in 'person/settings.html.twig'
|
2021-12-03 10:18:25 +09:00
|
|
|
*
|
2021-12-09 02:53:29 +09:00
|
|
|
* @throws \App\Util\Exception\NoLoggedInUser
|
2021-11-26 22:04:45 +09:00
|
|
|
* @throws RedirectException
|
|
|
|
* @throws ServerException
|
|
|
|
*/
|
2021-12-09 02:53:29 +09:00
|
|
|
public function onPopulateSettingsTabs(Request $request, string $section, array &$tabs): bool
|
2021-11-26 22:04:45 +09:00
|
|
|
{
|
2021-12-09 02:53:29 +09:00
|
|
|
if ($section === 'colours') {
|
|
|
|
$tabs[] = [
|
|
|
|
'title' => 'Light theme colours',
|
|
|
|
'desc' => 'Change the theme colours.',
|
|
|
|
'id' => 'settings-light-theme-colours-details',
|
|
|
|
'controller' => C\Oomox::oomoxSettingsLight($request),
|
|
|
|
];
|
2021-11-26 22:04:45 +09:00
|
|
|
|
2021-12-09 02:53:29 +09:00
|
|
|
$tabs[] = [
|
|
|
|
'title' => 'Dark theme colours',
|
|
|
|
'desc' => 'Change the theme colours.',
|
|
|
|
'id' => 'settings-dark-theme-colours-details',
|
|
|
|
'controller' => C\Oomox::oomoxSettingsDark($request),
|
|
|
|
];
|
|
|
|
}
|
2021-11-26 22:04:45 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-12-03 10:18:25 +09:00
|
|
|
/**
|
|
|
|
* Returns Oomox cache key for the given user.
|
|
|
|
*/
|
2021-12-09 02:53:29 +09:00
|
|
|
public static function cacheKey(LocalUser $user): string
|
|
|
|
{
|
2021-11-26 23:44:35 +09:00
|
|
|
return "oomox-css-{$user->getId()}";
|
|
|
|
}
|
2021-11-27 02:11:28 +09:00
|
|
|
|
2021-12-03 10:18:25 +09:00
|
|
|
/**
|
|
|
|
* Returns Entity\Oomox if it already exists
|
|
|
|
*/
|
2021-11-27 02:11:28 +09:00
|
|
|
public static function getEntity(LocalUser $user): ?Entity\Oomox
|
|
|
|
{
|
|
|
|
try {
|
2022-02-08 23:12:59 +09:00
|
|
|
return Cache::get(self::cacheKey($user), static fn () => DB::findOneBy('oomox', ['actor_id' => $user->getId()]));
|
2021-11-27 02:11:28 +09:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 10:18:25 +09:00
|
|
|
/**
|
|
|
|
* Adds to array $styles the generated CSS according to user settings, if any are present.
|
|
|
|
*/
|
2022-01-27 09:54:27 +09:00
|
|
|
public function onEndShowStyles(array &$styles, string $route): bool
|
2021-11-26 22:04:45 +09:00
|
|
|
{
|
2021-11-26 23:44:35 +09:00
|
|
|
$user = Common::user();
|
2022-02-08 23:12:59 +09:00
|
|
|
if ($user && Cache::get(self::cacheKey($user), static fn () => self::getEntity($user))) {
|
2021-11-26 23:44:35 +09:00
|
|
|
$styles[] = Router::url('oomox_css');
|
2021-11-26 22:04:45 +09:00
|
|
|
}
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
}
|