ProfileColor.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\NotFoundException;
  26. use App\Util\Exception\RedirectException;
  27. use App\Util\Exception\ServerException;
  28. use App\Util\Formatting;
  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. * @return bool hook value; true means continue processing, false means stop
  48. */
  49. public function onAddRoute(RouteLoader $r): bool
  50. {
  51. $r->connect('settings_profile_color', 'settings/color', [Controller\ProfileColor::class, 'profileColorSettings']);
  52. return Event::next;
  53. }
  54. /**
  55. * @throws RedirectException
  56. * @throws ServerException
  57. */
  58. public function onPopulateSettingsTabs(Request $request, string $section, array &$tabs): bool
  59. {
  60. if ($section === 'colours') {
  61. $tabs[] = [
  62. 'title' => 'Profile Colour',
  63. 'desc' => 'Change your profile colour.',
  64. 'id' => 'settings-profile-colour-details',
  65. 'controller' => C\ProfileColor::profileColorSettings($request),
  66. ];
  67. }
  68. return Event::next;
  69. }
  70. /**
  71. * Renders profileColorView, which changes the background color of that profile.
  72. */
  73. public function onAppendCardProfile(array $vars, array &$res): bool
  74. {
  75. $actor = $vars['actor'];
  76. if ($actor !== null) {
  77. $actor_id = $actor->getId();
  78. try {
  79. $profile_color_tab = Cache::get("profile-color-{$actor_id}", DB::findOneBy('profile_color', ['actor_id' => $actor_id]));
  80. } catch (NotFoundException $e) {
  81. return Event::next;
  82. }
  83. $color = DB::findBy('profile_color', ['actor_id' => $actor_id])[0];
  84. if ($color !== null) {
  85. $color = $color->getColor();
  86. $res[] = Formatting::twigRenderFile('/profileColor/profileColorView.html.twig', ['profile_color' => $profile_color_tab, 'actor' => $actor_id]);
  87. }
  88. }
  89. return Event::next;
  90. }
  91. }