3 use wcf\system\exception\IllegalLinkException
;
4 use wcf\system\exception\PermissionDeniedException
;
5 use wcf\system\exception\SystemException
;
6 use wcf\system\exception\UserInputException
;
7 use wcf\system\poll\PollManager
;
9 use wcf\util\ArrayUtil
;
11 use wcf\util\StringUtil
;
14 * Handles poll interaction.
16 * @author Alexander Ebert
17 * @copyright 2001-2018 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package WoltLabSuite\Core\Action
21 class PollAction
extends AJAXProxyAction
{
25 public $loginRequired = true;
31 public $optionIDs = [];
35 * @var \wcf\data\poll\Poll
47 * @var \wcf\data\IPollObject
49 public $relatedObject = null;
54 public function readParameters() {
56 throw new IllegalLinkException();
59 AbstractSecureAction
::readParameters();
61 if (isset($_POST['actionName'])) $this->actionName
= StringUtil
::trim($_POST['actionName']);
62 if (isset($_POST['pollID'])) $this->pollID
= intval($_POST['pollID']);
64 $polls = PollManager
::getInstance()->getPolls([$this->pollID
]);
65 if (!isset($polls[$this->pollID
])) {
66 throw new UserInputException('pollID');
68 $this->poll
= $polls[$this->pollID
];
70 // load related object
71 $this->relatedObject
= PollManager
::getInstance()->getRelatedObject($this->poll
);
72 if ($this->relatedObject
=== null) {
73 if ($this->poll
->objectID
) {
74 throw new SystemException("Missing related object for poll id '".$this->poll
->pollID
."'");
78 $this->poll
->setRelatedObject($this->relatedObject
);
82 switch ($this->actionName
) {
84 if (!$this->poll
->canSeeResult()) {
85 throw new PermissionDeniedException();
91 if (!$this->poll
->canVote()) {
92 throw new PermissionDeniedException();
97 throw new SystemException("Unknown action '".$this->actionName
."'");
101 if (isset($_POST['optionIDs']) && is_array($_POST['optionIDs'])) {
102 $this->optionIDs
= ArrayUtil
::toIntegerArray($_POST['optionIDs']);
103 if (count($this->optionIDs
) > $this->poll
->maxVotes
) {
104 throw new PermissionDeniedException();
108 foreach ($this->poll
->getOptions() as $option) {
109 $optionIDs[] = $option->optionID
;
112 foreach ($this->optionIDs
as $optionID) {
113 if (!in_array($optionID, $optionIDs)) {
114 throw new PermissionDeniedException();
123 public function execute() {
124 AbstractAction
::execute();
127 'actionName' => $this->actionName
,
128 'pollID' => $this->pollID
131 switch ($this->actionName
) {
133 $this->getResult($returnValues);
137 $this->getVote($returnValues);
141 $this->vote($returnValues);
147 // send JSON-encoded response
148 header('Content-type: application/json');
149 echo JSON
::encode($returnValues);
154 * Renders the result template.
156 * @param array $returnValues
158 public function getResult(array &$returnValues) {
159 WCF
::getTPL()->assign([
160 'poll' => $this->poll
163 $returnValues['resultTemplate'] = WCF
::getTPL()->fetch('pollResult');
167 * Renders the vote template.
169 * @param array $returnValues
171 public function getVote(array &$returnValues) {
172 WCF
::getTPL()->assign([
173 'poll' => $this->poll
176 $returnValues['voteTemplate'] = WCF
::getTPL()->fetch('pollVote');
182 * @param mixed[] $returnValues
184 protected function vote(array &$returnValues) {
185 $pollAction = new \wcf\data\poll\
PollAction([$this->poll
], 'vote', ['optionIDs' => $this->optionIDs
]);
186 $pollAction->executeAction();
188 // update poll object
189 $polls = PollManager
::getInstance()->getPolls([$this->pollID
]);
190 $this->poll
= $polls[$this->pollID
];
191 if ($this->relatedObject
!== null) {
192 $this->poll
->setRelatedObject($this->relatedObject
);
195 // render result template
196 $this->getResult($returnValues);
198 // render vote template if votes are changeable
199 if ($this->poll
->isChangeable
) {
200 $this->getVote($returnValues);
203 $returnValues['canVote'] = $this->poll
->isChangeable ?
1 : 0;