From 5c1f7926dde450cf85101ebb61e2782bf7b07793 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 21 Jul 2020 12:05:39 +0200 Subject: [PATCH] Use modern UploadHandler to update style logos --- .../install/files/acp/templates/styleAdd.tpl | 33 +++++++-------- .../files/lib/acp/form/StyleAddForm.class.php | 41 ++++++++++++++++--- .../lib/acp/form/StyleEditForm.class.php | 13 ++++++ .../lib/data/style/StyleAction.class.php | 20 +++++++++ 4 files changed, 86 insertions(+), 21 deletions(-) diff --git a/wcfsetup/install/files/acp/templates/styleAdd.tpl b/wcfsetup/install/files/acp/templates/styleAdd.tpl index 088cca0183..9aa280641b 100644 --- a/wcfsetup/install/files/acp/templates/styleAdd.tpl +++ b/wcfsetup/install/files/acp/templates/styleAdd.tpl @@ -52,8 +52,6 @@ 'wcf.acp.style.favicon.error.invalidExtension': '{lang}wcf.acp.style.favicon.error.invalidExtension{/lang}', 'wcf.acp.style.image.error.invalidExtension': '{lang}wcf.acp.style.image.error.invalidExtension{/lang}' }); - new WCF.ACP.Style.LogoUpload('{$tmpHash}'); - new WCF.ACP.Style.LogoUploadMobile('{$tmpHash}'); {if $action == 'edit'} new WCF.ACP.Style.CopyStyle({@$style->styleID}); @@ -420,14 +418,23 @@
-
- -
- -
-
- + {@$__wcf->getUploadHandler()->renderField('pageLogo')} {lang}wcf.acp.style.globals.pageLogo.description{/lang} +
@@ -447,13 +454,7 @@
-
- -
-
-
-
- + {@$__wcf->getUploadHandler()->renderField('pageLogoMobile')} {lang}wcf.acp.style.globals.pageLogoMobile.description{/lang}
diff --git a/wcfsetup/install/files/lib/acp/form/StyleAddForm.class.php b/wcfsetup/install/files/lib/acp/form/StyleAddForm.class.php index b6a550bf9d..89afd3d3f1 100644 --- a/wcfsetup/install/files/lib/acp/form/StyleAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/StyleAddForm.class.php @@ -222,6 +222,9 @@ class StyleAddForm extends AbstractForm { I18nHandler::getInstance()->register('styleDescription'); $this->setVariables(); + + $this->rebuildUploadFields(); + if (empty($_POST)) { $this->readStyleVariables(); } @@ -234,8 +237,6 @@ class StyleAddForm extends AbstractForm { if (empty($this->tmpHash)) { $this->tmpHash = StringUtil::getRandomID(); } - - $this->rebuildUploadFields(); } protected function rebuildUploadFields() { @@ -256,6 +257,24 @@ class StyleAddForm extends AbstractForm { $field->setImageOnly(true); $field->maxFiles = 1; $handler->registerUploadField($field); + + if ($handler->isRegisteredFieldId('pageLogo')) { + $handler->unregisterUploadField('pageLogo'); + } + $field = new UploadField('pageLogo'); + $field->setImageOnly(true); + $field->setAllowSvgImage(true); + $field->maxFiles = 1; + $handler->registerUploadField($field); + + if ($handler->isRegisteredFieldId('pageLogoMobile')) { + $handler->unregisterUploadField('pageLogoMobile'); + } + $field = new UploadField('pageLogoMobile'); + $field->setImageOnly(true); + $field->setAllowSvgImage(true); + $field->maxFiles = 1; + $handler->registerUploadField($field); } /** @@ -322,7 +341,7 @@ class StyleAddForm extends AbstractForm { if (isset($_POST['scrollOffsets']) && is_array($_POST['scrollOffsets'])) $this->scrollOffsets = ArrayUtil::toIntegerArray($_POST['scrollOffsets']); $this->uploads = []; - foreach (['image', 'image2x'] as $field) { + foreach (['image', 'image2x', 'pageLogo', 'pageLogoMobile'] as $field) { $removedFiles = UploadHandler::getInstance()->getRemovedFiledByFieldId($field); if (!empty($removedFiles)) { $this->uploads[$field] = null; @@ -485,6 +504,20 @@ class StyleAddForm extends AbstractForm { $adapter->writeImage($thumbnail, $fileLocation); } } + + // pageLogo + $field = 'pageLogo'; + $files = UploadHandler::getInstance()->getFilesByFieldId($field); + if (count($files) > 1) { + throw new UserInputException($field, 'invalid'); + } + + // pageLogoMobile + $field = 'pageLogoMobile'; + $files = UploadHandler::getInstance()->getFilesByFieldId($field); + if (count($files) > 1) { + throw new UserInputException($field, 'invalid'); + } } /** @@ -651,10 +684,8 @@ class StyleAddForm extends AbstractForm { $this->specialVariables = [ 'individualScss', 'overrideScss', - 'pageLogo', 'pageLogoWidth', 'pageLogoHeight', - 'pageLogoMobile', 'useFluidLayout', 'useGoogleFont', 'wcfFontFamilyGoogle', diff --git a/wcfsetup/install/files/lib/acp/form/StyleEditForm.class.php b/wcfsetup/install/files/lib/acp/form/StyleEditForm.class.php index f3da9407c3..f0f927bc18 100644 --- a/wcfsetup/install/files/lib/acp/form/StyleEditForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/StyleEditForm.class.php @@ -104,6 +104,19 @@ class StyleEditForm extends StyleAddForm { $this->variables['overrideScss'] = $tmp['preset']; $this->variables['overrideScssCustom'] = $tmp['custom']; } + + if ($this->variables['pageLogo']) { + $file = new UploadFile($this->style->getAssetPath().$this->variables['pageLogo'], basename($this->variables['pageLogo']), true, true, true); + UploadHandler::getInstance()->registerFilesByField('pageLogo', [ + $file, + ]); + } + if ($this->variables['pageLogoMobile']) { + $file = new UploadFile($this->style->getAssetPath().$this->variables['pageLogoMobile'], basename($this->variables['pageLogoMobile']), true, true, true); + UploadHandler::getInstance()->registerFilesByField('pageLogoMobile', [ + $file, + ]); + } } /** diff --git a/wcfsetup/install/files/lib/data/style/StyleAction.class.php b/wcfsetup/install/files/lib/data/style/StyleAction.class.php index 7dee8e47b0..9280b444a1 100644 --- a/wcfsetup/install/files/lib/data/style/StyleAction.class.php +++ b/wcfsetup/install/files/lib/data/style/StyleAction.class.php @@ -173,6 +173,26 @@ class StyleAction extends AbstractDatabaseObjectAction implements IToggleAction, return; } + foreach (['pageLogo', 'pageLogoMobile'] as $type) { + if (array_key_exists($type, $this->parameters['uploads'])) { + /** @var \wcf\system\file\upload\UploadFile $file */ + $file = $this->parameters['uploads'][$type]; + + if ($file !== null) { + $fileLocation = $file->getLocation(); + $extension = pathinfo($file->getFilename(), PATHINFO_EXTENSION); + $newName = $type.'.'.$extension; + $newLocation = $style->getAssetPath().$newName; + rename($fileLocation, $newLocation); + $this->parameters['variables'][$type] = $newName; + $file->setProcessed($newLocation); + } + else { + $this->parameters['variables'][$type] = ''; + } + } + } + $sql = "SELECT variableID, variableName, defaultValue FROM wcf".WCF_N."_style_variable"; $statement = WCF::getDB()->prepareStatement($sql); -- 2.20.1