From: Matthias Schmidt Date: Sun, 24 Feb 2019 13:28:36 +0000 (+0100) Subject: Add PollManager method to create poll based on external data X-Git-Tag: 5.2.0_Alpha_1~278 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=f984c90d1af705a4d397c1e8a231b210976e2121;p=GitHub%2FWoltLab%2FWCF.git Add PollManager method to create poll based on external data See #2852 --- diff --git a/wcfsetup/install/files/lib/data/IPollContainer.class.php b/wcfsetup/install/files/lib/data/IPollContainer.class.php new file mode 100644 index 0000000000..a898c69f63 --- /dev/null +++ b/wcfsetup/install/files/lib/data/IPollContainer.class.php @@ -0,0 +1,27 @@ + + * @package WoltLabSuite\Core\Data + * @since 5.2 + */ +interface IPollContainer extends IPollObject { + /** + * Returns the id of the poll container. + * + * @return integer + */ + public function getObjectID(); + + /** + * Returns the id of the poll that belongs to this object or `null` if there is no such poll. + * + * @return null|integer + */ + public function getPollID(); +} diff --git a/wcfsetup/install/files/lib/system/poll/PollManager.class.php b/wcfsetup/install/files/lib/system/poll/PollManager.class.php index 2abd022399..a648e3c048 100644 --- a/wcfsetup/install/files/lib/system/poll/PollManager.class.php +++ b/wcfsetup/install/files/lib/system/poll/PollManager.class.php @@ -1,5 +1,6 @@ pollOptions); - + // if no question and no options are given, ignore poll completely if (empty($this->pollData['question']) && !$count) { return; } - + // no question given if (empty($this->pollData['question'])) { throw new UserInputException('pollQuestion'); @@ -298,6 +299,45 @@ class PollManager extends SingletonFactory { return $this->poll->pollID; } + /** + * Saves the poll for the given poll container using the given poll data and returns the id + * of the relevant poll or `null` if the poll container has no poll after the update. + * + * This method either creates a new poll or updates or deletes an existing poll. + * + * @param IPollContainer $pollContainer object the poll belongs to + * @param array $pollData poll data + * @return null|integer id of the poll + * @since 5.2 + */ + public function savePoll(IPollContainer $pollContainer, array $pollData) { + // backup internal data + $backupData = [ + 'objectID' => $this->objectID, + 'poll' => $this->poll, + 'pollData' => $this->pollData, + 'pollOptions' => $this->pollOptions + ]; + + if ($pollContainer->getPollID() !== null) { + $this->poll = new Poll($pollContainer->getPollID()); + } + + $this->pollOptions = $pollData['options']; + unset($pollData['options']); + $this->pollData = $pollData; + + $pollID = $this->save($pollContainer->getObjectID()); + + // restore internal data + $this->objectID = $backupData['objectID']; + $this->poll = $backupData['poll']; + $this->pollData = $backupData['pollData']; + $this->pollOptions = $backupData['pollOptions']; + + return $pollID ?: null; + } + /** * Assigns variables for poll management or display. */