From: joshuaruesweg Date: Thu, 7 Jan 2021 14:53:15 +0000 (+0100) Subject: Add user rank image upload X-Git-Tag: 5.4.0_Alpha_1~462^2~1 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=9517b7a020af4077256e154677bfb52682496eff;p=GitHub%2FWoltLab%2FWCF.git Add user rank image upload --- diff --git a/wcfsetup/install/files/acp/templates/userRankAdd.tpl b/wcfsetup/install/files/acp/templates/userRankAdd.tpl index 705ab7e79c..7f21dfeb50 100644 --- a/wcfsetup/install/files/acp/templates/userRankAdd.tpl +++ b/wcfsetup/install/files/acp/templates/userRankAdd.tpl @@ -96,13 +96,16 @@
- + {@$__wcf->getUploadHandler()->renderField('rankImage')} {if $errorField == 'rankImage'} - {lang}wcf.acp.user.rank.image.error.{@$errorType}{/lang} + {if $errorType == 'empty'} + {lang}wcf.global.form.error.empty{/lang} + {else} + {lang}wcf.acp.user.rank.image.error.{$errorType}{/lang} + {/if} {/if} - {lang}wcf.acp.user.rank.rankImage.description{/lang}
@@ -119,13 +122,6 @@ - {if $action == 'edit' && $rank->rankImage} -
-
-
{@$rank->getImage()}
-
- {/if} -
diff --git a/wcfsetup/install/files/lib/acp/form/UserRankAddForm.class.php b/wcfsetup/install/files/lib/acp/form/UserRankAddForm.class.php index 155a88f832..67e7a0d027 100644 --- a/wcfsetup/install/files/lib/acp/form/UserRankAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/UserRankAddForm.class.php @@ -6,6 +6,9 @@ use wcf\data\user\rank\UserRankEditor; use wcf\data\user\UserProfile; use wcf\form\AbstractForm; use wcf\system\exception\UserInputException; +use wcf\system\file\upload\UploadField; +use wcf\system\file\upload\UploadFile; +use wcf\system\file\upload\UploadHandler; use wcf\system\language\I18nHandler; use wcf\system\Regex; use wcf\system\request\LinkHandler; @@ -67,8 +70,7 @@ class UserRankAddForm extends AbstractForm { public $requiredPoints = 0; /** - * path to user rank image - * @var string + * @deprecated since 5.4 */ public $rankImage = ''; @@ -109,6 +111,16 @@ class UserRankAddForm extends AbstractForm { 'custom' /* not a real value */ ]; + /** + * @var UploadFile[] + */ + public $removedRankImages; + + /** + * @var UploadFile|bool + */ + public $rankImageFile; + /** * @inheritDoc */ @@ -116,6 +128,19 @@ class UserRankAddForm extends AbstractForm { parent::readParameters(); I18nHandler::getInstance()->register('rankTitle'); + + $this->rebuildUploadField(); + } + + protected function rebuildUploadField(): void { + if (UploadHandler::getInstance()->isRegisteredFieldId('rankImage')) { + UploadHandler::getInstance()->unregisterUploadField('rankImage'); + } + $field = new UploadField('rankImage'); + $field->setImageOnly(true); + $field->setAllowSvgImage(true); + $field->maxFiles = 1; + UploadHandler::getInstance()->registerUploadField($field); } /** @@ -131,10 +156,13 @@ class UserRankAddForm extends AbstractForm { if (isset($_POST['customCssClassName'])) $this->customCssClassName = StringUtil::trim($_POST['customCssClassName']); if (isset($_POST['groupID'])) $this->groupID = intval($_POST['groupID']); if (isset($_POST['requiredPoints'])) $this->requiredPoints = intval($_POST['requiredPoints']); - if (isset($_POST['rankImage'])) $this->rankImage = StringUtil::trim($_POST['rankImage']); if (isset($_POST['repeatImage'])) $this->repeatImage = intval($_POST['repeatImage']); if (isset($_POST['requiredGender'])) $this->requiredGender = intval($_POST['requiredGender']); if (isset($_POST['hideTitle'])) $this->hideTitle = intval($_POST['hideTitle']); + + $this->removedRankImages = UploadHandler::getInstance()->getRemovedFiledByFieldId('rankImage'); + $rankImageFiles = UploadHandler::getInstance()->getFilesByFieldId('rankImage'); + $this->rankImageFile = reset($rankImageFiles); } /** @@ -180,7 +208,7 @@ class UserRankAddForm extends AbstractForm { $this->requiredGender = 0; } - if ($this->hideTitle && !$this->rankImage) { + if ($this->hideTitle && !$this->rankImageFile) { throw new UserInputException('hideTitle', 'rankImage'); } } @@ -192,16 +220,18 @@ class UserRankAddForm extends AbstractForm { parent::save(); // save label - $this->objectAction = new UserRankAction([], 'create', ['data' => array_merge($this->additionalFields, [ - 'rankTitle' => $this->rankTitle, - 'cssClassName' => $this->cssClassName == 'custom' ? $this->customCssClassName : $this->cssClassName, - 'groupID' => $this->groupID, - 'requiredPoints' => $this->requiredPoints, - 'rankImage' => $this->rankImage, - 'repeatImage' => $this->repeatImage, - 'requiredGender' => $this->requiredGender, - 'hideTitle' => ($this->hideTitle ? 1 : 0) - ])]); + $this->objectAction = new UserRankAction([], 'create', [ + 'data' => array_merge($this->additionalFields, [ + 'rankTitle' => $this->rankTitle, + 'cssClassName' => $this->cssClassName == 'custom' ? $this->customCssClassName : $this->cssClassName, + 'groupID' => $this->groupID, + 'requiredPoints' => $this->requiredPoints, + 'repeatImage' => $this->repeatImage, + 'requiredGender' => $this->requiredGender, + 'hideTitle' => ($this->hideTitle ? 1 : 0) + ]), + 'rankImageFile' => $this->rankImageFile, + ]); $returnValues = $this->objectAction->executeAction(); $rankID = $returnValues['returnValues']->rankID; @@ -222,6 +252,7 @@ class UserRankAddForm extends AbstractForm { $this->repeatImage = 1; I18nHandler::getInstance()->reset(); + $this->rebuildUploadField(); // show success message WCF::getTPL()->assign([ diff --git a/wcfsetup/install/files/lib/acp/form/UserRankEditForm.class.php b/wcfsetup/install/files/lib/acp/form/UserRankEditForm.class.php index 3b41aba600..182fe59223 100644 --- a/wcfsetup/install/files/lib/acp/form/UserRankEditForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/UserRankEditForm.class.php @@ -4,6 +4,7 @@ use wcf\data\user\rank\UserRank; use wcf\data\user\rank\UserRankAction; use wcf\form\AbstractForm; use wcf\system\exception\IllegalLinkException; +use wcf\system\file\upload\UploadHandler; use wcf\system\language\I18nHandler; use wcf\system\WCF; @@ -62,16 +63,19 @@ class UserRankEditForm extends UserRankAddForm { } // update label - $this->objectAction = new UserRankAction([$this->rank], 'update', ['data' => array_merge($this->additionalFields, [ - 'rankTitle' => $this->rankTitle, - 'cssClassName' => $this->cssClassName == 'custom' ? $this->customCssClassName : $this->cssClassName, - 'groupID' => $this->groupID, - 'requiredPoints' => $this->requiredPoints, - 'rankImage' => $this->rankImage, - 'repeatImage' => $this->repeatImage, - 'requiredGender' => $this->requiredGender, - 'hideTitle' => $this->hideTitle - ])]); + $this->objectAction = new UserRankAction([$this->rank], 'update', [ + 'data' => array_merge($this->additionalFields, [ + 'rankTitle' => $this->rankTitle, + 'cssClassName' => $this->cssClassName == 'custom' ? $this->customCssClassName : $this->cssClassName, + 'groupID' => $this->groupID, + 'requiredPoints' => $this->requiredPoints, + 'repeatImage' => $this->repeatImage, + 'requiredGender' => $this->requiredGender, + 'hideTitle' => $this->hideTitle + ]), + 'rankImageFile' => $this->rankImageFile, + 'rankImageFile__removedFiles' => $this->removedRankImages, + ]); $this->objectAction->executeAction(); $this->saved(); @@ -102,6 +106,12 @@ class UserRankEditForm extends UserRankAddForm { $this->repeatImage = $this->rank->repeatImage; $this->rankImage = $this->rank->rankImage; $this->hideTitle = $this->rank->hideTitle; + + if ($this->rank->getImageFile()) { + UploadHandler::getInstance()->registerFilesByField('rankImage', [ + $this->rank->getImageFile() + ]); + } } } diff --git a/wcfsetup/install/files/lib/data/user/rank/UserRank.class.php b/wcfsetup/install/files/lib/data/user/rank/UserRank.class.php index 513e4c4259..c22359b026 100644 --- a/wcfsetup/install/files/lib/data/user/rank/UserRank.class.php +++ b/wcfsetup/install/files/lib/data/user/rank/UserRank.class.php @@ -2,6 +2,7 @@ namespace wcf\data\user\rank; use wcf\data\DatabaseObject; use wcf\data\ITitledObject; +use wcf\system\file\upload\UploadFile; use wcf\system\WCF; use wcf\util\StringUtil; @@ -24,6 +25,9 @@ use wcf\util\StringUtil; * @property-read integer $hideTitle hides the generic title of the rank, but not custom titles, `0` to show the title at all times */ class UserRank extends DatabaseObject implements ITitledObject { + + public const RANK_IMAGE_DIR = 'images/rank/'; + /** * Returns the image of this user rank. * @@ -31,7 +35,7 @@ class UserRank extends DatabaseObject implements ITitledObject { */ public function getImage() { if ($this->rankImage) { - $image = ''; + $image = ''; if ($this->repeatImage > 1) $image = str_repeat($image, $this->repeatImage); return $image; } @@ -55,4 +59,17 @@ class UserRank extends DatabaseObject implements ITitledObject { public function showTitle() { return !$this->rankImage || !$this->hideTitle; } + + /** + * Returns the currently uploaded rank image or null, if the rank has no image. + * + * @since 5.4 + */ + public function getImageFile(): ?UploadFile { + if ($this->rankImage) { + return new UploadFile(WCF_DIR . self::RANK_IMAGE_DIR . $this->rankImage, $this->rankImage, true, true, true); + } + + return null; + } } diff --git a/wcfsetup/install/files/lib/data/user/rank/UserRankAction.class.php b/wcfsetup/install/files/lib/data/user/rank/UserRankAction.class.php index 9a75d72b78..686a940dfe 100644 --- a/wcfsetup/install/files/lib/data/user/rank/UserRankAction.class.php +++ b/wcfsetup/install/files/lib/data/user/rank/UserRankAction.class.php @@ -1,6 +1,8 @@ * @package WoltLabSuite\Core\Data\User\Rank * - * @method UserRank create() * @method UserRankEditor[] getObjects() * @method UserRankEditor getSingleObject() */ @@ -24,4 +25,75 @@ class UserRankAction extends AbstractDatabaseObjectAction { * @inheritDoc */ protected $requireACP = ['delete']; + + /** + * @inheritDoc + */ + public function create() { + /** @var UserRank $rank */ + $rank = parent::create(); + + if (isset($this->parameters['rankImageFile']) && $this->parameters['rankImageFile']) { + if (!($this->parameters['rankImageFile'] instanceof UploadFile)) { + throw new InvalidObjectArgument($this->parameters['rankImageFile'], UploadFile::class, "The parameter 'rankImageFile'"); + } + + if (!$this->parameters['rankImageFile']->isProcessed()) { + $fileName = $rank->rankID . '-' . $this->parameters['rankImageFile']->getFilename(); + + rename($this->parameters['rankImageFile']->getLocation(), WCF_DIR . UserRank::RANK_IMAGE_DIR . $fileName); + $this->parameters['rankImageFile']->setProcessed(WCF_DIR . UserRank::RANK_IMAGE_DIR . $fileName); + + $updateData['rankImage'] = $fileName; + + $rankEditor = new UserRankEditor($rank); + $rankEditor->update($updateData); + } + } + + return $rank; + } + + /** + * @inheritDoc + */ + public function update() { + if (isset($this->parameters['rankImageFile__removedFiles']) && is_array($this->parameters['rankImageFile__removedFiles'])) { + foreach ($this->parameters['rankImageFile__removedFiles'] as $file) { + if (!($file instanceof UploadFile)) { + throw new InvalidObjectArgument($this->parameters['rankImageFile__removedFiles'], UploadFile::class, "An array values of 'rankImageFile__removedFiles'"); + } + + @unlink($file->getLocation()); + } + } + + if (isset($this->parameters['rankImageFile'])) { + if (count($this->objects) > 1) { + throw new \BadMethodCallException("The parameter 'rankImageFile' can only be processed, if there is only one object to update."); + } + + $object = reset($this->objects); + + if (!$this->parameters['rankImageFile']) { + $this->parameters['data']['rankImage'] = ""; + } + else { + if (!($this->parameters['rankImageFile'] instanceof UploadFile)) { + throw new InvalidObjectArgument($this->parameters['rankImageFile'], UploadFile::class, "The parameter 'rankImageFile'"); + } + + if (!$this->parameters['rankImageFile']->isProcessed()) { + $fileName = $object->rankID . '-' . $this->parameters['rankImageFile']->getFilename(); + + rename($this->parameters['rankImageFile']->getLocation(), WCF_DIR . UserRank::RANK_IMAGE_DIR . $fileName); + $this->parameters['rankImageFile']->setProcessed(WCF_DIR . UserRank::RANK_IMAGE_DIR . $fileName); + + $this->parameters['data']['rankImage'] = $fileName; + } + } + } + + parent::update(); + } } diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index a220ab02bc..185fe9e7df 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -3142,7 +3142,6 @@ Wenn {if LANGUAGE_USE_INFORMAL_VARIANT}du{else}Sie{/if} unter „Konfiguration - diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index ac810dfb59..4d81f8e35a 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -3070,7 +3070,6 @@ You can define the default sender in “Configuration » Options » General » E -