NewPoll.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Poll\Controller;
  20. use App\Core\DB\DB;
  21. use App\Core\Form;
  22. use function App\Core\I18n\_m;
  23. use App\Core\Security;
  24. use App\Entity\Note;
  25. use App\Util\Common;
  26. use App\Util\Exception\InvalidFormException;
  27. use App\Util\Exception\RedirectException;
  28. use Plugin\Poll\Entity\Poll;
  29. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  30. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  31. use Symfony\Component\Form\Extension\Core\Type\TextType;
  32. use Symfony\Component\HttpFoundation\Request;
  33. /**
  34. * Create a Poll
  35. *
  36. * @package GNUsocial
  37. * @category PollPlugin
  38. *
  39. * @author Daniel Brandao <up201705812@fe.up.pt>
  40. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  41. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  42. */
  43. const MAX_OPTS = 5;
  44. const MIN_OPTS = 2;
  45. class NewPoll
  46. {
  47. /**
  48. * Create poll
  49. *
  50. * @param int $num num of options
  51. *
  52. * @throws \App\Util\Exception\NoLoggedInUser user is not logged in
  53. * @throws InvalidFormException invalid form
  54. * @throws RedirectException
  55. *
  56. * @return array template
  57. */
  58. public function newPoll(Request $request, int $num): array
  59. {
  60. $user = Common::ensureLoggedIn();
  61. $numOptions = Common::clamp($num, MIN_OPTS, MAX_OPTS);
  62. $opts[] = ['visibility', ChoiceType::class, ['label' => _m('Visibility:'), 'expanded' => true, 'choices' => [_m('Public') => 'public', _m('Instance') => 'instance', _m('Private') => 'private']]];
  63. $opts[] = ['Question', TextType::class, ['label' => _m(('Question'))]];
  64. for ($i = 1; $i <= $numOptions; ++$i) {
  65. //['Option_i', TextType::class, ['label' => _m('Option i')]],
  66. $opts[] = ['Option_' . $i, TextType::class, ['label' => _m(('Option ' . $i))]];
  67. }
  68. $opts[] = ['post_poll', SubmitType::class, ['label' => _m('Post')]];
  69. $form = Form::create($opts);
  70. $form->handleRequest($request);
  71. $opt = [];
  72. if ($form->isSubmitted()) {
  73. if ($form->isValid()) {
  74. $data = $form->getData();
  75. $note = Note::create(['actor_id' => $user->getId(), $is_local = true]);
  76. DB::persist($note);
  77. Security::sanitize($question = $data['Question']);
  78. for ($i = 1; $i <= $numOptions; ++$i) {
  79. Security::sanitize($opt[$i - 1] = $data['Option_' . $i]);
  80. }
  81. $options = implode("\n", $opt);
  82. $poll = Poll::create(['actor_id' => $user->getId(), 'question' => $question, 'options' => $options, 'note_id' => $note->getId()]);
  83. DB::persist($poll);
  84. DB::flush();
  85. throw new RedirectException('root');
  86. } else {
  87. throw new InvalidFormException();
  88. }
  89. }
  90. return ['_template' => 'poll/newpoll.html.twig', 'form' => $form->createView()];
  91. }
  92. }