123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- declare(strict_types = 1);
- namespace Plugin\Poll;
- use App\Core\DB\DB;
- use App\Core\Event;
- use App\Core\Form;
- use function App\Core\I18n\_m;
- use App\Core\Modules\NoteHandlerPlugin;
- use App\Core\Router\RouteLoader;
- use App\Entity\Note;
- use App\Util\Common;
- use App\Util\Exception\InvalidFormException;
- use App\Util\Exception\NotFoundException;
- use App\Util\Exception\RedirectException;
- use App\Util\Exception\ServerException;
- use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
- use Symfony\Component\Form\Extension\Core\Type\HiddenType;
- use Symfony\Component\Form\Extension\Core\Type\SubmitType;
- use Symfony\Component\HttpFoundation\Request;
- class Poll extends NoteHandlerPlugin
- {
-
- public function onAddRoute(RouteLoader $r): bool
- {
- $r->connect('newpoll', 'main/poll/new/{num<\\d+>?3}', [Controller\NewPoll::class, 'newpoll']);
- return Event::next;
- }
-
-
- public function onStartShowStyles(array &$styles): bool
- {
- $styles[] = 'poll/poll.css';
- return Event::next;
- }
-
- public function onShowNoteContent(Request $request, Note $note, array &$otherContent): bool
- {
- $responses = null;
- $formView = null;
- try {
- $poll = DB::findOneBy('poll', ['note_id' => $note->getId()]);
- } catch (NotFoundException $e) {
- return Event::next;
- }
- if (Common::isLoggedIn() && !Entity\PollResponse::exits($poll->getId(), Common::ensureLoggedIn()->getId())) {
- $opts = $poll->getOptionsArr();
- $options = [];
- for ($i = 1; $i <= \count($opts); ++$i) {
- $options[$opts[$i - 1]] = $i;
- }
- $formOptions = [
- ['Options' . $poll->getId(), ChoiceType::class, [
- 'choices' => $options,
- 'expanded' => true,
- ]],
- ['note_id', HiddenType::class, ['data' => $note->getId()]],
- ['pollresponse', SubmitType::class, ['label' => _m('Vote')]],
- ];
- $form = Form::create($formOptions);
- $formView = $form->createView();
- $ret = self::noteActionHandle($request, $form, $note, 'pollresponse', function ($note, $data) {
- $user = Common::ensureLoggedIn();
- try {
- $poll = DB::findOneBy('poll', ['note_id' => $note->getId()]);
- } catch (NotFoundException $e) {
- return Event::next;
- }
- if (Entity\PollResponse::exits($poll->getId(), $user->getId())) {
- return Event::next;
- }
- $selection = array_values($data)[1];
- if (!$poll->isValidSelection($selection)) {
- throw new InvalidFormException();
- }
- if (Entity\PollResponse::exits($poll->getId(), $user->getId())) {
- throw new ServerException('User already responded to poll');
- }
- $pollResponse = Entity\PollResponse::create(['poll_id' => $poll->getId(), 'actor_id' => $user->getId(), 'selection' => $selection]);
- DB::persist($pollResponse);
- DB::flush();
- throw new RedirectException();
- });
- if ($ret != null) {
- return $ret;
- }
- } else {
- $responses = $poll->countResponses();
- }
- $otherContent[] = ['name' => 'Poll', 'vars' => ['question' => $poll->getQuestion(), 'responses' => $responses, 'form' => $formView]];
- return Event::next;
- }
- }
|