示例#1
0
 /**
  * @see	\wcf\data\AbstractDatabaseObjectAction::update()
  */
 public function update()
 {
     parent::update();
     // read current poll
     $pollEditor = reset($this->objects);
     // get current options
     $optionList = new PollOptionList();
     $optionList->getConditionBuilder()->add("poll_option.pollID = ?", array($pollEditor->pollID));
     $optionList->sqlOrderBy = "poll_option.showOrder ASC";
     $optionList->readObjects();
     $options = $optionList->getObjects();
     $newOptions = $updateOptions = array();
     foreach ($this->parameters['options'] as $showOrder => $option) {
         // check if editing an existing option
         if ($option['optionID']) {
             // check if an update is required
             if ($options[$option['optionID']]->showOrder != $showOrder || $options[$option['optionID']]->optionValue != $option['optionValue']) {
                 $updateOptions[$option['optionID']] = array('optionValue' => $option['optionValue'], 'showOrder' => $showOrder);
             }
             // remove option
             unset($options[$option['optionID']]);
         } else {
             $newOptions[] = array('optionValue' => $option['optionValue'], 'showOrder' => $showOrder);
         }
     }
     if (!empty($newOptions) || !empty($updateOptions) || !empty($options)) {
         WCF::getDB()->beginTransaction();
         // check if new options should be created
         if (!empty($newOptions)) {
             $sql = "INSERT INTO\twcf" . WCF_N . "_poll_option\n\t\t\t\t\t\t\t(pollID, optionValue, showOrder)\n\t\t\t\t\tVALUES\t\t(?, ?, ?)";
             $statement = WCF::getDB()->prepareStatement($sql);
             foreach ($newOptions as $option) {
                 $statement->execute(array($pollEditor->pollID, $option['optionValue'], $option['showOrder']));
             }
         }
         // check if existing options should be updated
         if (!empty($updateOptions)) {
             $sql = "UPDATE\twcf" . WCF_N . "_poll_option\n\t\t\t\t\tSET\toptionValue = ?,\n\t\t\t\t\t\tshowOrder = ?\n\t\t\t\t\tWHERE\toptionID = ?";
             $statement = WCF::getDB()->prepareStatement($sql);
             foreach ($updateOptions as $optionID => $option) {
                 $statement->execute(array($option['optionValue'], $option['showOrder'], $optionID));
             }
         }
         // check if options should be removed
         if (!empty($options)) {
             $sql = "DELETE FROM\twcf" . WCF_N . "_poll_option\n\t\t\t\t\tWHERE\t\toptionID = ?";
             $statement = WCF::getDB()->prepareStatement($sql);
             foreach ($options as $option) {
                 $statement->execute(array($option->optionID));
             }
         }
         // force recalculation of poll stats
         $pollEditor->calculateVotes();
         WCF::getDB()->commitTransaction();
     }
 }