Add trophy edit form
authorJoshua Rüsweg <josh@bastelstu.be>
Sun, 9 Jul 2017 11:40:01 +0000 (13:40 +0200)
committerJoshua Rüsweg <josh@bastelstu.be>
Sun, 9 Jul 2017 11:40:14 +0000 (13:40 +0200)
See #2315

wcfsetup/install/files/acp/templates/trophyAdd.tpl
wcfsetup/install/files/lib/acp/form/TrophyAddForm.class.php
wcfsetup/install/files/lib/acp/form/TrophyEditForm.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/data/trophy/Trophy.class.php
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

index a0d39ede1f8e85ce041af61cca42b81cb7004a9d..129ed330bd40284d74ee645a29e80e6fa981eedd 100644 (file)
                                                <small>{lang}wcf.acp.trophy.type.imageUpload.description{/lang}</small>
                                        </div>
                                        <div class="col-md-6">
-                                               <div id="uploadIconFileContent">{if $action == 'add'}{if !$uploadedImageURL|empty}<img src="{$uploadedImageURL}">{/if}{else}{if $trophy->type == 2}<img src="{$__wcf->getPath()}images/trophy/{$trophy->iconFile}">{/if}{/if}</div>
+                                               <div id="uploadIconFileContent">{if $action == 'add'}{if !$uploadedImageURL|empty}<img src="{$uploadedImageURL}">{/if}{else}{if $trophy->type == 1}<img src="{$__wcf->getPath()}images/trophy/{$trophy->iconFile}">{/if}{/if}</div>
                                        </div>
                                </div>
 
index 16f232bb54beb73bf5b3db50a2797d796ac8ad2b..f0863c21ea3303673bb541f82e0152705382660c 100644 (file)
@@ -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 (file)
index 0000000..2ec2787
--- /dev/null
@@ -0,0 +1,201 @@
+<?php
+namespace wcf\acp\form;
+use wcf\data\trophy\Trophy;
+use wcf\data\trophy\TrophyAction;
+use wcf\system\condition\ConditionHandler;
+use wcf\system\exception\IllegalLinkException;
+use wcf\system\exception\UserInputException;
+use wcf\system\language\I18nHandler;
+use wcf\system\trophy\condition\TrophyConditionHandler;
+use wcf\system\WCF;
+
+/**
+ * Represents the trophy edit form.
+ *
+ * @author     Joshua Ruesweg
+ * @copyright  2001-2017 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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
+               ]);
+       }
+}
index 0b97ab10e983ee7a262e45054d528af6149ad5af..1a9b54b63542bfa4be5e09c644b065d00e52bb56 100644 (file)
@@ -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);
+       }
 }
index 8ec3ee54aa11c6ef56594b964b559f53931471f6..046d9beedd39ab244599de0eba62cc5376ab7feb 100644 (file)
                <item name="wcf.acp.menu.link.trophy.category.add"><![CDATA[Kategorie hinzufügen]]></item>
                <item name="wcf.acp.menu.link.trophy.list"><![CDATA[Trophäen]]></item>
                <item name="wcf.acp.menu.link.trophy.add"><![CDATA[Trophäe hinzufügen]]></item>
+               <item name="wcf.acp.menu.link.trophy.edit"><![CDATA[Trophäe bearbeiten]]></item>
        </category>
        
        <category name="wcf.acp.notice">
index fdf3d6bde6ca16cbb5dab43dd05276df14980305..21be25455fd7253fcd193153e25837fa6f7c6bd6 100644 (file)
                <item name="wcf.acp.menu.link.trophy.category.add"><![CDATA[Add Category]]></item>
                <item name="wcf.acp.menu.link.trophy.list"><![CDATA[Trophies]]></item>
                <item name="wcf.acp.menu.link.trophy.add"><![CDATA[Add Trophy]]></item>
+               <item name="wcf.acp.menu.link.trophy.edit"><![CDATA[Edit Trophy]]></item>
        </category>
        
        <category name="wcf.acp.notice">