Use modern UploadHandler to update style logos
authorTim Düsterhus <duesterhus@woltlab.com>
Tue, 21 Jul 2020 10:05:39 +0000 (12:05 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Tue, 21 Jul 2020 13:34:41 +0000 (15:34 +0200)
wcfsetup/install/files/acp/templates/styleAdd.tpl
wcfsetup/install/files/lib/acp/form/StyleAddForm.class.php
wcfsetup/install/files/lib/acp/form/StyleEditForm.class.php
wcfsetup/install/files/lib/data/style/StyleAction.class.php

index 088cca0183a6ff8b2aee644661a8691f82c7b16b..9aa280641b660901a2a6410a1fdd4b8c162135f1 100644 (file)
@@ -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});
                                <dl>
                                        <dt><label for="pageLogo">{lang}wcf.acp.style.globals.pageLogo{/lang}</label></dt>
                                        <dd>
-                                               <div class="selectedImagePreview">
-                                                       <img src="" alt="" id="styleLogo" style="max-width: 100%">
-                                               </div>
-                                               <div id="uploadLogo"></div>
-                                       </dd>
-                                       <dd>
-                                               <input type="text" name="pageLogo" id="pageLogo" value="{$variables[pageLogo]}" class="long">
+                                               {@$__wcf->getUploadHandler()->renderField('pageLogo')}
                                                <small>{lang}wcf.acp.style.globals.pageLogo.description{/lang}</small>
+                                               <script data-relocate="true">
+                                               elBySel('#pageLogouploadFileList').addEventListener('change', function (ev) {
+                                                       var img = elBySel('#pageLogouploadFileList img');
+                                                       if (!img) return;
+                                                       
+                                                       function updateSizes() {
+                                                               elById('pageLogoWidth').value = img.width;
+                                                               elById('pageLogoHeight').value = img.height;
+                                                       }
+                                                       img.addEventListener('load', updateSizes);
+                                                       if (img.complete) {
+                                                               updateSizes();
+                                                       }
+                                               })
+                                               </script>
                                        </dd>
                                </dl>
                                
                                <dl>
                                        <dt><label for="pageLogoMobile">{lang}wcf.acp.style.globals.pageLogoMobile{/lang}</label></dt>
                                        <dd>
-                                               <div class="selectedImagePreview">
-                                                       <img src="" alt="" id="styleLogoMobile" style="max-width: 100%">
-                                               </div>
-                                               <div id="uploadLogoMobile"></div>
-                                       </dd>
-                                       <dd>
-                                               <input type="text" name="pageLogoMobile" id="pageLogoMobile" value="{$variables[pageLogoMobile]}" class="long">
+                                               {@$__wcf->getUploadHandler()->renderField('pageLogoMobile')}
                                                <small>{lang}wcf.acp.style.globals.pageLogoMobile.description{/lang}</small>
                                        </dd>
                                </dl>
index b6a550bf9d31a99b4b7eb15878dbdb42b60f7fb1..89afd3d3f1206d3ae5509644eb2262984bf9d3c4 100644 (file)
@@ -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',
index f3da9407c348289f2a29a801dac06a73669e9346..f0f927bc18ca3f9a6a74fe2acab9bc13fc021f33 100644 (file)
@@ -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,
+                       ]);
+               }
        }
        
        /**
index 7dee8e47b0eab3ea989e73cdbb3b56977862936c..9280b444a1df5fbcf991663f37310ceb4de33b72 100644 (file)
@@ -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);