ProfileColor.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Plugin\ProfileColor;
  20. use App\Core\Cache;
  21. use App\Core\DB\DB;
  22. use App\Core\Event;
  23. use App\Core\Modules\Plugin;
  24. use App\Core\Router\RouteLoader;
  25. use App\Util\Exception\RedirectException;
  26. use App\Util\Exception\ServerException;
  27. use App\Util\Formatting;
  28. use Plugin\ProfileColor\Controller as C;
  29. use Symfony\Component\HttpFoundation\Request;
  30. /**
  31. * Profile Color plugin main class
  32. *
  33. * @package GNUsocial
  34. * @category ProfileColor
  35. *
  36. * @author Daniel Brandao <up201705812@fe.up.pt>
  37. * @author Eliseu Amaro <mail@eliseuama.ro>
  38. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  39. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  40. */
  41. class ProfileColor extends Plugin
  42. {
  43. /**
  44. * Map URLs to actions
  45. *
  46. * @return bool hook value; true means continue processing, false means stop
  47. */
  48. public function onAddRoute(RouteLoader $r): bool
  49. {
  50. $r->connect('settings_profile_color', 'settings/color', [Controller\ProfileColor::class, 'profileColorSettings']);
  51. return Event::next;
  52. }
  53. /**
  54. * @throws RedirectException
  55. * @throws ServerException
  56. */
  57. public function onPopulateSettingsTabs(Request $request, string $section, array &$tabs): bool
  58. {
  59. if ($section === 'colours') {
  60. $tabs[] = [
  61. 'title' => 'Profile Colour',
  62. 'desc' => 'Change your profile colour.',
  63. 'id' => 'settings-profile-colour-details',
  64. 'controller' => C\ProfileColor::profileColorSettings($request),
  65. ];
  66. }
  67. return Event::next;
  68. }
  69. /**
  70. * Renders profileColorView, which changes the background color of that profile.
  71. */
  72. public function onAppendCardProfile(array $vars, array &$res): bool
  73. {
  74. $actor = $vars['actor'];
  75. if ($actor !== null) {
  76. $actor_id = $actor->getId();
  77. $profile_color = Cache::get("profile-color-{$actor_id}", fn () => DB::findOneBy('profile_color', ['actor_id' => $actor_id], return_null: true));
  78. if (!\is_null($profile_color)) {
  79. $res[] = Formatting::twigRenderFile('/profileColor/profileColorView.html.twig', ['profile_color' => $profile_color, 'actor' => $actor_id]);
  80. }
  81. }
  82. return Event::next;
  83. }
  84. }