From 908e40a36a87ba7039c067cd8343087d701bef70 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Wed, 5 Aug 2020 15:34:58 +0200 Subject: [PATCH] Add upload for custom style assets Resolves #3364 --- .../install/files/acp/templates/styleAdd.tpl | 17 ++++++++++ .../files/lib/acp/form/StyleAddForm.class.php | 24 ++++++++++++-- .../lib/acp/form/StyleEditForm.class.php | 8 +++-- .../lib/data/style/StyleAction.class.php | 33 ++++++++++++++++++- wcfsetup/install/lang/de.xml | 2 ++ wcfsetup/install/lang/en.xml | 2 ++ 6 files changed, 81 insertions(+), 5 deletions(-) diff --git a/wcfsetup/install/files/acp/templates/styleAdd.tpl b/wcfsetup/install/files/acp/templates/styleAdd.tpl index 58e682c54b..6c1663876c 100644 --- a/wcfsetup/install/files/acp/templates/styleAdd.tpl +++ b/wcfsetup/install/files/acp/templates/styleAdd.tpl @@ -287,6 +287,23 @@ {/if} + +
+
+ {@$__wcf->getUploadHandler()->renderField('customAssets')} + {if $errorField == 'customAssets'} + + {if $errorType == 'empty'} + {lang}wcf.global.form.error.empty{/lang} + {else} + {lang}wcf.acp.style.customAssets.error.{$errorType}{/lang} + {/if} + + {/if} + {lang}wcf.acp.style.customAssets.description{/lang} +
+ + {event name='fileFields'} diff --git a/wcfsetup/install/files/lib/acp/form/StyleAddForm.class.php b/wcfsetup/install/files/lib/acp/form/StyleAddForm.class.php index d29a6a7327..5fc9d02758 100644 --- a/wcfsetup/install/files/lib/acp/form/StyleAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/StyleAddForm.class.php @@ -206,11 +206,17 @@ class StyleAddForm extends AbstractForm { public $scrollOffsets = []; /** - * @var mixed[] + * @var (null|UploadFile)[] * @since 5.3 */ public $uploads = []; + /** + * @var UploadFile[] + * @since 5.3 + */ + public $customAssets = []; + /** * @inheritDoc */ @@ -297,6 +303,13 @@ class StyleAddForm extends AbstractForm { $field->maxFiles = 1; $handler->registerUploadField($field); } + + // This field is special cased, because it may contain arbitrary data. + $field = new UploadField('customAssets'); + $field->setImageOnly(true); + $field->setAllowSvgImage(true); + $field->maxFiles = null; + $handler->registerUploadField($field); } /** @@ -372,6 +385,12 @@ class StyleAddForm extends AbstractForm { $this->uploads[$field] = $files[0]; } } + + $this->customAssets = [ + 'removed' => UploadHandler::getInstance()->getRemovedFiledByFieldId('customAssets'), + 'added' => UploadHandler::getInstance()->getFilesByFieldId('customAssets'), + ]; + } /** @@ -754,8 +773,9 @@ class StyleAddForm extends AbstractForm { 'apiVersion' => $this->apiVersion ]), 'uploads' => $this->uploads, + 'customAssets' => $this->customAssets, 'tmpHash' => $this->tmpHash, - 'variables' => $this->variables + 'variables' => $this->variables, ]); $returnValues = $this->objectAction->executeAction(); $style = $returnValues['returnValues']; diff --git a/wcfsetup/install/files/lib/acp/form/StyleEditForm.class.php b/wcfsetup/install/files/lib/acp/form/StyleEditForm.class.php index 8e262b0afc..ba45bbc34d 100644 --- a/wcfsetup/install/files/lib/acp/form/StyleEditForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/StyleEditForm.class.php @@ -181,8 +181,11 @@ class StyleEditForm extends StyleAddForm { break; } } - } + + UploadHandler::getInstance()->registerFilesByField('customAssets', array_map(function ($filename) { + return new UploadFile($filename, basename($filename), false, true, true); + }, glob($this->style->getAssetPath().'custom/*'))); } } @@ -217,8 +220,9 @@ class StyleEditForm extends StyleAddForm { 'apiVersion' => $this->apiVersion ]), 'uploads' => $this->uploads, + 'customAssets' => $this->customAssets, 'tmpHash' => $this->tmpHash, - 'variables' => $this->variables + 'variables' => $this->variables, ]); $this->objectAction->executeAction(); diff --git a/wcfsetup/install/files/lib/data/style/StyleAction.class.php b/wcfsetup/install/files/lib/data/style/StyleAction.class.php index ec0878bdd8..0d7915767a 100644 --- a/wcfsetup/install/files/lib/data/style/StyleAction.class.php +++ b/wcfsetup/install/files/lib/data/style/StyleAction.class.php @@ -86,7 +86,10 @@ class StyleAction extends AbstractDatabaseObjectAction implements IToggleAction // handle the cover photo $this->updateCoverPhoto($style); - + + // handle custom assets + $this->updateCustomAssets($style); + return $style; } @@ -109,6 +112,9 @@ class StyleAction extends AbstractDatabaseObjectAction implements IToggleAction // handle the cover photo $this->updateCoverPhoto($style->getDecoratedObject()); + // handle custom assets + $this->updateCustomAssets($style->getDecoratedObject()); + // reset stylesheet StyleHandler::getInstance()->resetStylesheet($style->getDecoratedObject()); } @@ -443,6 +449,31 @@ BROWSERCONFIG; } } + /** + * @since 5.2 + */ + protected function updateCustomAssets(Style $style) { + $customAssetPath = $style->getAssetPath().'custom/'; + + if (!empty($this->parameters['customAssets']['removed'])) { + /** @var \wcf\system\file\upload\UploadFile $file */ + foreach ($this->parameters['customAssets']['removed'] as $file) { + unlink($file->getLocation()); + } + } + if (!empty($this->parameters['customAssets']['added'])) { + if (!is_dir($customAssetPath)) { + FileUtil::makePath($customAssetPath); + } + + /** @var \wcf\system\file\upload\UploadFile $file */ + foreach ($this->parameters['customAssets']['added'] as $file) { + rename($file->getLocation(), $customAssetPath.$file->getFilename()); + $file->setProcessed($customAssetPath.$file->getFilename()); + } + } + } + /** * Validates parameters to assign a new default style. */ diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index 7dabd27ccf..0a8d7485fe 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -2878,6 +2878,8 @@ Kein Abschnitt darf leer sein und alle Abschnitten dürfen nur folgende Zeichen + + custom/-Ordner innerhalb des #{$style_image_path} gespeichert.{/literal}]]> {$style->styleName} wirklich löschen?]]> diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index d45d828408..9ab54b07a0 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -2805,6 +2805,8 @@ If you have already bought the licenses for the listed apps, th + + custom/ folder within the #{$style_image_path}.{/literal}]]> {$style->styleName}?]]> -- 2.20.1