Add PollManager method to create poll based on external data
authorMatthias Schmidt <gravatronics@live.com>
Sun, 24 Feb 2019 13:28:36 +0000 (14:28 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 24 Feb 2019 13:28:36 +0000 (14:28 +0100)
See #2852

wcfsetup/install/files/lib/data/IPollContainer.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/poll/PollManager.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 (file)
index 0000000..a898c69
--- /dev/null
@@ -0,0 +1,27 @@
+<?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();
+}
index 2abd0223995ab1a197c86e95450cd2db421ffdfc..a648e3c0487543e2e6d8ef91ee76210a38f0da2f 100644 (file)
@@ -1,5 +1,6 @@
 <?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;
@@ -205,12 +206,12 @@ class PollManager extends SingletonFactory {
         */
        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');
@@ -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.
         */