ProfileColor.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\Controller;
  20. use App\Core\DB\DB;
  21. use App\Core\Form;
  22. use function App\Core\I18n\_m;
  23. use App\Util\Common;
  24. use App\Util\Exception\RedirectException;
  25. use App\Util\Exception\ServerException;
  26. use Plugin\ProfileColor\Entity;
  27. use Symfony\Component\Form\Extension\Core\Type\ColorType;
  28. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  29. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  30. use Symfony\Component\HttpFoundation\Request;
  31. /**
  32. * Profile Color controller
  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
  43. {
  44. /**
  45. * Change Profile color background
  46. *
  47. * @throws RedirectException
  48. * @throws ServerException
  49. */
  50. public static function profileColorSettings(Request $request): array
  51. {
  52. $actor = Common::actor();
  53. $actor_id = $actor->getId();
  54. $current_profile_color = DB::find('profile_color', ['actor_id' => $actor_id]);
  55. $form = Form::create([
  56. ['background_color', ColorType::class, [
  57. 'html5' => true,
  58. 'data' => $current_profile_color ? $current_profile_color->getBackground() : '#000000',
  59. 'label' => _m('Profile background color'),
  60. 'help' => _m('Choose your Profile background color'), ],
  61. ],
  62. ['foreground_color', ColorType::class, [
  63. 'html5' => true,
  64. 'data' => $current_profile_color ? $current_profile_color->getColor() : '#000000',
  65. 'label' => _m('Profile foreground color'),
  66. 'help' => _m('Choose your Profile foreground color'), ],
  67. ],
  68. ['hidden', HiddenType::class, []],
  69. ['save_profile_color', SubmitType::class, ['label' => _m('Submit')]],
  70. ]);
  71. $form->handleRequest($request);
  72. if ($form->isSubmitted() && $form->isValid()) {
  73. if ($current_profile_color !== null) {
  74. DB::remove($current_profile_color);
  75. DB::flush();
  76. }
  77. $data = $form->getData();
  78. $current_profile_color = Entity\ProfileColor::create(['actor_id' => $actor_id, 'color' => $data['foreground_color'], 'background' => $data['background_color']]);
  79. DB::persist($current_profile_color);
  80. DB::flush();
  81. throw new RedirectException();
  82. }
  83. return ['_template' => 'profileColor/profileColorSettings.html.twig', 'profile_color' => $form->createView()];
  84. }
  85. }