--- /dev/null
+<?php
+namespace wcf\data;
+
+/**
+ * Every database object that supports polls has to implement this interface.
+ *
+ * @author Matthias Schmidt
+ * @copyright 2001-2019 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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();
+}
<?php
namespace wcf\system\poll;
+use wcf\data\IPollContainer;
use wcf\data\object\type\ObjectType;
use wcf\data\object\type\ObjectTypeCache;
use wcf\data\poll\option\PollOptionList;
*/
public function validate() {
$count = count($this->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');
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.
*/