ProfileColor.php 3.0 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;
  22. use App\Core\Event;
  23. use App\Core\Modules\Plugin;
  24. use App\Core\Router;
  25. use App\Util\Exception\RedirectException;
  26. use App\Util\Exception\ServerException;
  27. use App\Util\Formatting;
  28. use EventResult;
  29. use Plugin\ProfileColor\Controller as C;
  30. use Symfony\Component\HttpFoundation\Request;
  31. /**
  32. * Profile Color plugin main class
  33. *
  34. * @package GNUsocial
  35. * @category ProfileColor
  36. *
  37. * @author Daniel Brandao <up201705812@fe.up.pt>
  38. * @author Eliseu Amaro <mail@eliseuama.ro>
  39. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  40. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  41. */
  42. class ProfileColor extends Plugin
  43. {
  44. /**
  45. * Map URLs to actions
  46. */
  47. public function onAddRoute(Router $r): EventResult
  48. {
  49. $r->connect('settings_profile_color', 'settings/color', [Controller\ProfileColor::class, 'profileColorSettings']);
  50. return Event::next;
  51. }
  52. /**
  53. * @param SettingsTabsType $tabs
  54. * @throws RedirectException
  55. * @throws ServerException
  56. */
  57. public function onPopulateSettingsTabs(Request $request, string $section, array &$tabs): EventResult
  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): EventResult
  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. }