ProfileColor.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace Plugin\ProfileColor\Controller;
  19. use App\Core\DB\DB;
  20. use App\Core\Form;
  21. use function App\Core\I18n\_m;
  22. use App\Util\Common;
  23. use App\Util\Exception\RedirectException;
  24. use Plugin\ProfileColor\Entity;
  25. use Symfony\Component\Form\Extension\Core\Type\ColorType;
  26. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  27. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  28. use Symfony\Component\HttpFoundation\Request;
  29. /**
  30. * Profile Color controller
  31. *
  32. * @package GNUsocial
  33. * @category ProfileColor
  34. *
  35. * @author Daniel Brandao <up201705812@fe.up.pt>
  36. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  37. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  38. */
  39. class ProfileColor
  40. {
  41. /**
  42. * Add/change profile color
  43. *
  44. * @param Request $request
  45. *
  46. * @throws RedirectException
  47. *
  48. * @return array template
  49. */
  50. public static function profileColorSettings(Request $request)
  51. {
  52. $user = Common::user();
  53. $actor_id = $user->getId();
  54. $pcolor = DB::find('profile_color', ['gsactor_id' => $actor_id]);
  55. $color = '#000000';
  56. if ($pcolor != null) {
  57. $color = $pcolor->getColor();
  58. }
  59. $form = Form::create([
  60. ['color', ColorType::class, ['data' => $color, 'label' => _m('Profile Color'), 'help' => _m('Choose your Profile Color')] ],
  61. ['hidden', HiddenType::class, []],
  62. ['save_profile_color', SubmitType::class, ['label' => _m('Submit')]],
  63. ]);
  64. $form->handleRequest($request);
  65. if ($form->isSubmitted() && $form->isValid()) {
  66. $data = $form->getData();
  67. if ($pcolor != null) {
  68. DB::remove($pcolor);
  69. DB::flush();
  70. }
  71. $pcolor = Entity\ProfileColor::create(['gsactor_id' => $actor_id, 'color' => $data['color']]);
  72. DB::persist($pcolor);
  73. DB::flush();
  74. throw new RedirectException();
  75. }
  76. return ['_template' => 'profilecolor/profilecolor.html.twig', 'form' => $form->createView()];
  77. }
  78. }