Poll.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\Pages;
  10. use ForkBB\Models\Page;
  11. use ForkBB\Models\Topic\Topic;
  12. use ForkBB\Models\Poll\Poll as PollModel;
  13. use function \ForkBB\__;
  14. class Poll extends Page
  15. {
  16. /**
  17. * Голосование
  18. */
  19. public function vote(array $args, string $method): Page
  20. {
  21. $tid = $args['tid'];
  22. $topic = $this->c->topics->load($tid);
  23. if (
  24. ! $topic instanceof Topic
  25. || ! $topic->poll instanceof PollModel
  26. ) {
  27. return $this->c->Message->message('Bad request');
  28. }
  29. $this->c->Lang->load('poll');
  30. $v = $this->c->Validator->reset()
  31. ->addValidators([
  32. ])->addRules([
  33. 'token' => 'token:Poll',
  34. 'poll_vote.*.*' => 'required|integer',
  35. 'vote' => 'required|string',
  36. ])->addAliases([
  37. ])->addArguments([
  38. 'token' => $args,
  39. ])->addMessages([
  40. 'poll_vote.*.*' => 'The poll structure is broken',
  41. ]);
  42. if (! $v->validation($_POST)) {
  43. $message = $this->c->Message;
  44. $message->fIswev = $v->getErrors();
  45. return $message->message('');
  46. } elseif (null !== ($result = $topic->poll->vote($v->poll_vote))) {
  47. return $this->c->Message->message($result);
  48. } else {
  49. return $this->c->Redirect->url($topic->link)->message('You voted');
  50. }
  51. }
  52. }