NewPoll.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\Poll\Controller;
  19. use App\Core\DB\DB;
  20. use App\Core\Form;
  21. use function App\Core\I18n\_m;
  22. use App\Core\Security;
  23. use App\Entity\Note;
  24. use App\Entity\Poll;
  25. use App\Util\Common;
  26. use App\Util\Exception\InvalidFormException;
  27. use App\Util\Exception\RedirectException;
  28. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  29. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  30. use Symfony\Component\Form\Extension\Core\Type\TextType;
  31. use Symfony\Component\HttpFoundation\Request;
  32. /**
  33. * Create a Poll
  34. *
  35. * @package GNUsocial
  36. * @category PollPlugin
  37. *
  38. * @author Daniel Brandao <up201705812@fe.up.pt>
  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. const MAX_OPTS = 5;
  43. const MIN_OPTS = 2;
  44. class NewPoll
  45. {
  46. /**
  47. * Create poll
  48. *
  49. * @param Request $request
  50. * @param int $num num of options
  51. *
  52. * @throws InvalidFormException invalid form
  53. * @throws RedirectException
  54. * @throws \App\Util\Exception\NoLoggedInUser user is not logged in
  55. *
  56. * @return array template
  57. */
  58. public function newPoll(Request $request, int $num)
  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(['gsactor_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(['gsactor_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. }