From f984c90d1af705a4d397c1e8a231b210976e2121 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sun, 24 Feb 2019 14:28:36 +0100 Subject: [PATCH] Add PollManager method to create poll based on external data See #2852 --- .../files/lib/data/IPollContainer.class.php | 27 ++++++++++++ .../lib/system/poll/PollManager.class.php | 44 ++++++++++++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 wcfsetup/install/files/lib/data/IPollContainer.class.php 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. */ -- 2.20.1