From: Joshua Rüsweg Date: Sun, 9 Jul 2017 11:40:01 +0000 (+0200) Subject: Add trophy edit form X-Git-Tag: 3.1.0_Alpha_1~292 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=e6f03104b8747653c564ac18d21c3d9f830e9d31;p=GitHub%2FWoltLab%2FWCF.git Add trophy edit form See #2315 --- diff --git a/wcfsetup/install/files/acp/templates/trophyAdd.tpl b/wcfsetup/install/files/acp/templates/trophyAdd.tpl index a0d39ede1f..129ed330bd 100644 --- a/wcfsetup/install/files/acp/templates/trophyAdd.tpl +++ b/wcfsetup/install/files/acp/templates/trophyAdd.tpl @@ -167,7 +167,7 @@ {lang}wcf.acp.trophy.type.imageUpload.description{/lang}
-
{if $action == 'add'}{if !$uploadedImageURL|empty}{/if}{else}{if $trophy->type == 2}{/if}{/if}
+
{if $action == 'add'}{if !$uploadedImageURL|empty}{/if}{else}{if $trophy->type == 1}{/if}{/if}
diff --git a/wcfsetup/install/files/lib/acp/form/TrophyAddForm.class.php b/wcfsetup/install/files/lib/acp/form/TrophyAddForm.class.php index 16f232bb54..f0863c21ea 100644 --- a/wcfsetup/install/files/lib/acp/form/TrophyAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/TrophyAddForm.class.php @@ -204,6 +204,30 @@ class TrophyAddForm extends AbstractAcpForm { throw new UserInputException('categoryID'); } + $this->validateType(); + + if ($this->awardAutomatically) { + $hasData = false; + foreach ($this->conditions as $conditions) { + foreach ($conditions as $condition) { + $condition->getProcessor()->validate(); + + if (!$hasData && $condition->getProcessor()->getData() !== null) { + $hasData = true; + } + } + } + + if (!$hasData) { + throw new UserInputException('conditions'); + } + } + } + + /** + * Validates the trophy type. + */ + protected function validateType() { switch ($this->type) { case Trophy::TYPE_IMAGE: $fileExtension = WCF::getSession()->getVar('trophyImage-'.$this->tmpHash); @@ -231,23 +255,6 @@ class TrophyAddForm extends AbstractAcpForm { } break; } - - if ($this->awardAutomatically) { - $hasData = false; - foreach ($this->conditions as $conditions) { - foreach ($conditions as $condition) { - $condition->getProcessor()->validate(); - - if (!$hasData && $condition->getProcessor()->getData() !== null) { - $hasData = true; - } - } - } - - if (!$hasData) { - throw new UserInputException('conditions'); - } - } } /** @@ -278,7 +285,14 @@ class TrophyAddForm extends AbstractAcpForm { // transform conditions array into one-dimensional array $conditions = []; foreach ($this->conditions as $groupedObjectTypes) { - $conditions = array_merge($conditions, $groupedObjectTypes); + foreach ($groupedObjectTypes as $objectTypes) { + if (is_array($objectTypes)) { + $conditions = array_merge($conditions, $objectTypes); + } + else { + $conditions[] = $objectTypes; + } + } } ConditionHandler::getInstance()->createConditions($this->objectAction->getReturnValues()['returnValues']->trophyID, $conditions); diff --git a/wcfsetup/install/files/lib/acp/form/TrophyEditForm.class.php b/wcfsetup/install/files/lib/acp/form/TrophyEditForm.class.php new file mode 100644 index 0000000000..2ec278765e --- /dev/null +++ b/wcfsetup/install/files/lib/acp/form/TrophyEditForm.class.php @@ -0,0 +1,201 @@ + + * @package WoltLabSuite\Core\Acp\Form + * @since 3.1 + */ +class TrophyEditForm extends TrophyAddForm { + /** + * @inheritDoc + */ + public $activeMenuItem = 'wcf.acp.menu.link.trophy'; + + /** + * @inheritDoc + */ + public $action = 'edit'; + + /** + * trophy id + * @var int + */ + public $trophyID = 0; + + /** + * trophy object + * @var Trophy + */ + public $trophy; + + /** + * @inheritDoc + */ + public function readParameters() { + if (!empty($_REQUEST['id'])) $this->trophyID = intval($_REQUEST['id']); + $this->trophy = new Trophy($this->trophyID); + + if (!$this->trophy->trophyID) { + throw new IllegalLinkException(); + } + + parent::readParameters(); + } + + /** + * @inheritDoc + */ + public function readData() { + parent::readData(); + + if (empty($_POST)) { + $this->readDataI18n($this->trophy); + + $this->categoryID = $this->trophy->categoryID; + $this->type = $this->trophy->type; + $this->isDisabled = $this->trophy->isDisabled; + if ($this->trophy->type != Trophy::TYPE_BADGE) { + $this->iconName = 'trophy'; + $this->iconColor = 'rgba(255, 255, 255, 1)'; + $this->badgeColor = 'rgba(50, 92, 132, 1)'; + } + $this->awardAutomatically = $this->trophy->awardAutomatically; + + $conditions = $this->trophy->getConditions(); + $conditionsByObjectTypeID = []; + foreach ($conditions as $condition) { + $conditionsByObjectTypeID[$condition->objectTypeID] = $condition; + } + + foreach ($this->conditions as $objectTypes1) { + foreach ($objectTypes1 as $objectTypes2) { + if (is_array($objectTypes2)) { + foreach ($objectTypes2 as $objectType) { + if (isset($conditionsByObjectTypeID[$objectType->objectTypeID])) { + $conditionsByObjectTypeID[$objectType->objectTypeID]->getObjectType()->getProcessor()->setData($conditionsByObjectTypeID[$objectType->objectTypeID]); + } + } + } + else if (isset($conditionsByObjectTypeID[$objectTypes2->objectTypeID])) { + $conditionsByObjectTypeID[$objectTypes2->objectTypeID]->getObjectType()->getProcessor()->setData($conditionsByObjectTypeID[$objectTypes2->objectTypeID]); + } + } + } + } + } + + /** @noinspection PhpMissingParentCallCommonInspection */ + /** + * @inheritDoc + */ + protected function validateType() { + switch ($this->type) { + case Trophy::TYPE_IMAGE: + if (empty($this->trophy->iconFile) || !file_exists(WCF_DIR.'images/trophy/'.$this->trophy->iconFile)) { + throw new UserInputException('imageUpload'); + } + break; + + case Trophy::TYPE_BADGE: + if (empty($this->iconName)) { + throw new UserInputException('iconName'); + } + + if (empty($this->iconColor)) { + throw new UserInputException('iconColor'); + } + + if (empty($this->badgeColor)) { + throw new UserInputException('badgeColor'); + } + break; + } + } + + /** + * @inheritDoc + */ + public function save() { + AbstractAcpForm::save(); + + $this->beforeSaveI18n($this->trophy); + + $data = []; + if ($this->type == Trophy::TYPE_IMAGE) { + $data['iconName'] = ''; + $data['iconColor'] = ''; + $data['badgeColor'] = ''; + } else if ($this->type == Trophy::TYPE_BADGE) { + // delete old image icon + if (is_file(WCF_DIR.'images/trophy/'.$this->trophy->iconFile)) { + @unlink(WCF_DIR.'images/trophy/'.$this->trophy->iconFile); + } + + $data['iconName'] = $this->iconName; + $data['iconColor'] = $this->iconColor; + $data['badgeColor'] = $this->badgeColor; + $data['iconFile'] = ''; + } + + $this->objectAction = new TrophyAction([$this->trophy], 'update', ['data' => array_merge($this->additionalFields, $data, [ + 'title' => $this->title, + 'description' => $this->description, + 'categoryID' => $this->categoryID, + 'type' => $this->type, + 'isDisabled' => $this->isDisabled, + 'awardAutomatically' => $this->awardAutomatically + ])]); + $this->objectAction->executeAction(); + + // transform conditions array into one-dimensional array + $conditions = []; + foreach ($this->conditions as $groupedObjectTypes) { + foreach ($groupedObjectTypes as $objectTypes) { + if (is_array($objectTypes)) { + $conditions = array_merge($conditions, $objectTypes); + } + else { + $conditions[] = $objectTypes; + } + } + } + + if ($this->awardAutomatically) { + ConditionHandler::getInstance()->updateConditions($this->trophy->trophyID, $this->trophy->getConditions(), $conditions); + } + else { + ConditionHandler::getInstance()->deleteConditions(TrophyConditionHandler::CONDITION_DEFINITION_NAME, [$this->trophy->trophyID]); + } + + $this->saved(); + + // show success message + WCF::getTPL()->assign('success', true); + } + + /** + * @inheritDoc + */ + public function assignVariables() { + parent::assignVariables(); + + I18nHandler::getInstance()->assignVariables(!empty($_POST)); + + WCF::getTPL()->assign([ + 'trophy' => $this->trophy + ]); + } +} diff --git a/wcfsetup/install/files/lib/data/trophy/Trophy.class.php b/wcfsetup/install/files/lib/data/trophy/Trophy.class.php index 0b97ab10e9..1a9b54b635 100644 --- a/wcfsetup/install/files/lib/data/trophy/Trophy.class.php +++ b/wcfsetup/install/files/lib/data/trophy/Trophy.class.php @@ -4,6 +4,7 @@ use wcf\data\trophy\category\TrophyCategory; use wcf\data\trophy\category\TrophyCategoryCache; use wcf\data\DatabaseObject; use wcf\data\ITitledLinkObject; +use wcf\system\condition\ConditionHandler; use wcf\system\event\EventHandler; use wcf\system\request\IRouteController; use wcf\system\request\LinkHandler; @@ -140,4 +141,13 @@ class Trophy extends DatabaseObject implements ITitledLinkObject, IRouteControll public function getDescription() { return nl2br(StringUtil::encodeHTML(WCF::getLanguage()->get($this->description)), false); } + + /** + * Returns the conditions of the trophy. + * + * @return Condition[] + */ + public function getConditions() { + return ConditionHandler::getInstance()->getConditions('com.woltlab.wcf.condition.trophy', $this->trophyID); + } } diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index 8ec3ee54aa..046d9beedd 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -852,6 +852,7 @@ + diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index fdf3d6bde6..21be25455f 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -835,6 +835,7 @@ +