From 83d1d0e73e3052a592e1466ea82192983d68c31f Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Thu, 18 Sep 2014 13:25:21 +0200 Subject: [PATCH] Added upload for style page logo --- .../install/files/acp/js/WCF.ACP.Style.js | 139 ++++++++++++++++++ .../install/files/acp/templates/styleAdd.tpl | 15 ++ .../lib/data/style/StyleAction.class.php | 50 ++++++- 3 files changed, 202 insertions(+), 2 deletions(-) diff --git a/wcfsetup/install/files/acp/js/WCF.ACP.Style.js b/wcfsetup/install/files/acp/js/WCF.ACP.Style.js index 081804236f..51b49d25f2 100644 --- a/wcfsetup/install/files/acp/js/WCF.ACP.Style.js +++ b/wcfsetup/install/files/acp/js/WCF.ACP.Style.js @@ -161,6 +161,145 @@ WCF.ACP.Style.ImageUpload = WCF.Upload.extend({ } }); +/** + * Handles the logo upload. + * + * @param string tmpHash + */ +WCF.ACP.Style.LogoUpload = WCF.Upload.extend({ + /** + * upload button + * @var jQuery + */ + _button: null, + + /** + * image path + * @var jQuery + */ + _imagePath: null, + + /** + * logo + * @var jQuery + */ + _logo: null, + + /** + * page logo input field + * @var jQuery + */ + _pageLogo: null, + + /** + * tmp hash + * @var string + */ + _tmpHash: '', + + /** + * absolute path to WCF directory + * @var string + */ + _wcfPath: '', + + /** + * @see WCF.Upload.init() + */ + init: function(tmpHash, wcfPath) { + this._tmpHash = tmpHash; + this._wcfPath = wcfPath; + + this._button = $('#uploadLogo'); + this._image = $('#styleLogo'); + this._imagePath = $('#imagePath'); + this._pageLogo = $('#pageLogo'); + + this._super(this._button, undefined, 'wcf\\data\\style\\StyleAction', { action: 'uploadLogo' }); + + if (!this._image.attr('src').length) { + this._updateLogo(); + } + + this._pageLogo.blur($.proxy(this._updateLogo, this)); + }, + + /** + * Updates the logo preview. + */ + _updateLogo: function() { + var $src = this._pageLogo.val(); + if ($src.length) { + if (!$src.match(/^https?:\/\//)) { + var $path = this._pageLogo.val(); + if (!$path) { + $path = 'images/'; + } + + $path = this._wcfPath + $path; + } + } + else { + // no logo defined, fallback to application logo + $src = $('#logo > a > img').prop('src'); + } + + this._image.attr('src', $src); + }, + + /** + * @see WCF.Upload._initFile() + */ + _initFile: function(file) { + return this._image; + }, + + /** + * @see WCF.Upload._getParameters() + */ + _getParameters: function() { + return { + tmpHash: this._tmpHash + }; + }, + + /** + * @see WCF.Upload._success() + */ + _success: function(uploadID, data) { + if (data.returnValues.url) { + // show image + this._image.attr('src', data.returnValues.url); + this._pageLogo.val(data.returnValues.url); + + // hide error + this._button.next('.innerError').remove(); + + // show success message + var $notification = new WCF.System.Notification(WCF.Language.get('wcf.global.success')); + $notification.show(); + } + else if (data.returnValues.errorType) { + // show error + this._getInnerErrorElement().text(WCF.Language.get('wcf.acp.style.image.error.' + data.returnValues.errorType)); + } + }, + + /** + * Returns error display element. + * + * @return jQuery + */ + _getInnerErrorElement: function() { + var $span = this._button.next('.innerError'); + if (!$span.length) { + $span = $('').insertAfter(this._button); + } + + return $span; + } +}); + /** * Handles style list management buttons. */ diff --git a/wcfsetup/install/files/acp/templates/styleAdd.tpl b/wcfsetup/install/files/acp/templates/styleAdd.tpl index a05c3fe8f5..7bfc361a84 100644 --- a/wcfsetup/install/files/acp/templates/styleAdd.tpl +++ b/wcfsetup/install/files/acp/templates/styleAdd.tpl @@ -34,6 +34,7 @@ 'wcf.style.colorPicker.button.apply': '{lang}wcf.style.colorPicker.button.apply{/lang}' }); new WCF.ACP.Style.ImageUpload({if $action == 'add'}0{else}{@$style->styleID}{/if}, '{$tmpHash}'); + new WCF.ACP.Style.LogoUpload('{$tmpHash}', '{@$__wcf->getPath()}images/'); {if $action == 'edit'} new WCF.ACP.Style.CopyStyle({@$style->styleID}); @@ -350,6 +351,20 @@
+
+ + + {if $errorField == 'image'} + + {if $errorType == 'empty'} + {lang}wcf.global.form.error.empty{/lang} + {else} + {lang}wcf.acp.style.image.error.{$errorType}{/lang} + {/if} + + {/if} + {lang}wcf.acp.style.image.description{/lang} +
{lang}wcf.acp.style.globals.pageLogo.description{/lang} diff --git a/wcfsetup/install/files/lib/data/style/StyleAction.class.php b/wcfsetup/install/files/lib/data/style/StyleAction.class.php index c3120544f0..68ea12bbe7 100644 --- a/wcfsetup/install/files/lib/data/style/StyleAction.class.php +++ b/wcfsetup/install/files/lib/data/style/StyleAction.class.php @@ -48,7 +48,7 @@ class StyleAction extends AbstractDatabaseObjectAction implements IToggleAction /** * @see \wcf\data\AbstractDatabaseObjectAction::$requireACP */ - protected $requireACP = array('copy', 'delete', 'setAsDefault', 'toggle', 'update', 'upload'); + protected $requireACP = array('copy', 'delete', 'setAsDefault', 'toggle', 'update', 'upload', 'uploadLogo'); /** * style object @@ -279,7 +279,7 @@ class StyleAction extends AbstractDatabaseObjectAction implements IToggleAction try { if (!$file->getValidationErrorType()) { - // shrink avatar if necessary + // shrink preview image if necessary $fileLocation = $file->getLocation(); $imageData = getimagesize($fileLocation); if ($imageData[0] > Style::PREVIEW_IMAGE_MAX_WIDTH || $imageData[1] > Style::PREVIEW_IMAGE_MAX_HEIGHT) { @@ -328,6 +328,52 @@ class StyleAction extends AbstractDatabaseObjectAction implements IToggleAction return array('errorType' => $file->getValidationErrorType()); } + /** + * Validates parameters to update a logo. + */ + public function validateUploadLogo() { + $this->validateUpload(); + } + + /** + * Handles logo upload. + * + * @return array + */ + public function uploadLogo() { + // save files + $files = $this->parameters['__files']->getFiles(); + $file = $files[0]; + + try { + if (!$file->getValidationErrorType()) { + // shrink avatar if necessary + $fileLocation = $file->getLocation(); + + // move uploaded file + if (@copy($fileLocation, WCF_DIR.'images/styleLogo-'.$this->parameters['tmpHash'].'.'.$file->getFileExtension())) { + @unlink($fileLocation); + + // store extension within session variables + WCF::getSession()->register('styleLogo-'.$this->parameters['tmpHash'], $file->getFileExtension()); + + // return result + return array( + 'url' => WCF::getPath().'images/styleLogo-'.$this->parameters['tmpHash'].'.'.$file->getFileExtension() + ); + } + else { + throw new UserInputException('image', 'uploadFailed'); + } + } + } + catch (UserInputException $e) { + $file->setValidationErrorType($e->getType()); + } + + return array('errorType' => $file->getValidationErrorType()); + } + /** * Validates parameters to assign a new default style. */ -- 2.20.1