Added upload for style page logo
authorAlexander Ebert <ebert@woltlab.com>
Thu, 18 Sep 2014 11:25:21 +0000 (13:25 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 18 Sep 2014 11:25:21 +0000 (13:25 +0200)
wcfsetup/install/files/acp/js/WCF.ACP.Style.js
wcfsetup/install/files/acp/templates/styleAdd.tpl
wcfsetup/install/files/lib/data/style/StyleAction.class.php

index 081804236ffbc3d1daa9cb82a831a279befa9e6e..51b49d25f22575d1d148a2148493556ef9c34a30 100644 (file)
@@ -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 = $('<small class="innerError" />').insertAfter(this._button);
+               }
+               
+               return $span;
+       }
+});
+
 /**
  * Handles style list management buttons.
  */
index a05c3fe8f5c3031660ff8c879fa447a8d79963fe..7bfc361a847ab3edf50a1e157ca1c7ba6b141dcc 100644 (file)
@@ -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});
                                
                                <dl>
                                        <dt><label for="pageLogo">{lang}wcf.acp.style.globals.pageLogo{/lang}</label></dt>
+                                       <dd class="framed">
+                                               <img src="" alt="" id="styleLogo" />
+                                               <div id="uploadLogo"></div>
+                                               {if $errorField == 'image'}
+                                                       <small class="innerError">
+                                                               {if $errorType == 'empty'}
+                                                                       {lang}wcf.global.form.error.empty{/lang}
+                                                               {else}
+                                                                       {lang}wcf.acp.style.image.error.{$errorType}{/lang}
+                                                               {/if}
+                                                       </small>
+                                               {/if}
+                                               <small>{lang}wcf.acp.style.image.description{/lang}</small>
+                                       </dd>
                                        <dd>
                                                <input type="text" name="pageLogo" id="pageLogo" value="{$variables[pageLogo]}" class="long" />
                                                <small>{lang}wcf.acp.style.globals.pageLogo.description{/lang}</small>
index c3120544f03607c5bc842127fe646061855da532..68ea12bbe734305e265f43958595a3216020785b 100644 (file)
@@ -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<string>
+        */
+       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.
         */