Add possibility to upload smiley file when adding new smiley
authorMatthias Schmidt <gravatronics@live.com>
Wed, 2 Apr 2014 18:09:09 +0000 (20:09 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Wed, 2 Apr 2014 18:09:09 +0000 (20:09 +0200)
wcfsetup/install/files/acp/templates/smileyAdd.tpl
wcfsetup/install/files/lib/acp/form/SmileyAddForm.class.php
wcfsetup/install/files/lib/acp/form/SmileyEditForm.class.php
wcfsetup/install/files/lib/data/smiley/SmileyAction.class.php
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

index ca3da52980352af2c0e6c1051e4998e76272bb25..114414c00c1c6a46c941127b16ccd9fc8661ff05 100644 (file)
@@ -23,7 +23,7 @@
        {/hascontent}
 </div>
 
-<form method="post" action="{if $action == 'add'}{link controller='SmileyAdd'}{/link}{else}{link controller='SmileyEdit' id=$smiley->smileyID}{/link}{/if}">
+<form method="post" action="{if $action == 'add'}{link controller='SmileyAdd'}{/link}{else}{link controller='SmileyEdit' id=$smiley->smileyID}{/link}{/if}" enctype="multipart/form-data">
        <div class="container containerPadding marginTop">
                <fieldset>
                        <legend>{lang}wcf.global.form.data{/lang}</legend>
                                </dd>
                        </dl>
                        
-                       <dl{if $errorField == 'smileyPath'} class="formError"{/if}>
-                               <dt><label for="smileyPath">{lang}wcf.acp.smiley.smileyPath{/lang}</label></dt>
+                       <dl{if $errorField == 'showOrder'} class="formError"{/if}>
+                               <dt><label for="showOrder">{lang}wcf.acp.smiley.showOrder{/lang}</label></dt>
                                <dd>
-                                       <input type="text" id="smileyPath" name="smileyPath" value="{$smileyPath}" required="required" class="long" />
+                                       <input type="number" id="showOrder" name="showOrder" value="{$showOrder}" min="0" class="short" />
                                        
-                                       {if $errorField == 'smileyPath'}
+                                       {if $errorField == 'showOrder'}
+                                               <small class="innerError">
+                                                       {lang}wcf.acp.smiley.showOrder.error.{@$errorType}{/lang}
+                                               </small>
+                                       {/if}
+                               </dd>
+                       </dl>
+                       
+                       {event name='dataFields'}
+               </fieldset>
+               
+               <fieldset>
+                       <legend>{lang}wcf.acp.smiley.smileyFile{/lang}</legend>
+                       
+                       <dl{if $errorField == 'fileUpload'} class="formError"{/if}>
+                               <dt><label for="fileUpload">{lang}wcf.acp.smiley.fileUpload{/lang}</label></dt>
+                               <dd>
+                                       {if $uploadedFilename}
+                                               <img src="{@$__wcf->getPath()}images/smilies/tmp/{$uploadedFilename}" alt="" />
+                                               <input type="hidden" name="uploadedFilename" value="{$uploadedFilename}" />
+                                       {/if}
+                                       <input type="file" id="fileUpload" name="fileUpload" value="" />
+                                       
+                                       {if $errorField == 'fileUpload'}
                                                <small class="innerError">
                                                        {if $errorType == 'empty'}
                                                                {lang}wcf.global.form.error.empty{/lang}
                                                        {else}
-                                                               {lang}wcf.acp.smiley.smileyPath.error.{@$errorType}{/lang}
+                                                               {lang}wcf.acp.smiley.fileUpload.error.{@$errorType}{/lang}
                                                        {/if}
                                                </small>
                                        {/if}
-                                       <small>{lang}wcf.acp.smiley.smileyPath.description{/lang}</small>
+                                       <small>{lang}wcf.acp.smiley.fileUpload.description{/lang}</small>
                                </dd>
                        </dl>
                        
-                       <dl{if $errorField == 'showOrder'} class="formError"{/if}>
-                               <dt><label for="showOrder">{lang}wcf.acp.smiley.showOrder{/lang}</label></dt>
+                       <dl{if $errorField == 'smileyPath'} class="formError"{/if}>
+                               <dt><label for="smileyPath">{lang}wcf.acp.smiley.smileyPath{/lang}</label></dt>
                                <dd>
-                                       <input type="number" id="showOrder" name="showOrder" value="{$showOrder}" min="0" class="short" />
+                                       <input type="text" id="smileyPath" name="smileyPath" value="{$smileyPath}" required="required" class="long" />
                                        
-                                       {if $errorField == 'showOrder'}
+                                       {if $errorField == 'smileyPath'}
                                                <small class="innerError">
-                                                       {lang}wcf.acp.smiley.showOrder.error.{@$errorType}{/lang}
+                                                       {if $errorType == 'empty'}
+                                                               {lang}wcf.global.form.error.empty{/lang}
+                                                       {else}
+                                                               {lang}wcf.acp.smiley.smileyPath.error.{@$errorType}{/lang}
+                                                       {/if}
                                                </small>
                                        {/if}
+                                       <small>{lang}wcf.acp.smiley.smileyPath.description{/lang}</small>
                                </dd>
                        </dl>
                        
-                       {event name='dataFields'}
+                       {event name='smileyFileFields'}
                </fieldset>
                
                {event name='fieldsets'}
index 50a55d138885f969959fffa4d70974945ecd4637..df08f3d6baadfb67e8da3336eed671418aa49686 100644 (file)
@@ -80,6 +80,18 @@ class SmileyAddForm extends AbstractForm {
         */
        public $categoryNodeTree = null;
        
+       /**
+        * data of the uploaded smiley file
+        * @var array()
+        */
+       public $fileUpload = array();
+       
+       /**
+        * temporary name of the uploaded smiley file
+        * @var string
+        */
+       public $uploadedFilename = '';
+       
        /**
         * @see \wcf\page\IPage::assignVariables()
         */
@@ -96,7 +108,8 @@ class SmileyAddForm extends AbstractForm {
                        'smileyCode' => $this->smileyCode,
                        'aliases' => $this->aliases,
                        'smileyPath' => $this->smileyPath,
-                       'categoryNodeList' => $this->categoryNodeTree->getIterator()
+                       'categoryNodeList' => $this->categoryNodeTree->getIterator(),
+                       'uploadedFilename' => $this->uploadedFilename
                ));
        }
        
@@ -133,6 +146,8 @@ class SmileyAddForm extends AbstractForm {
                if (isset($_POST['smileyCode'])) $this->smileyCode = StringUtil::trim($_POST['smileyCode']);
                if (isset($_POST['aliases'])) $this->aliases = StringUtil::unifyNewlines(StringUtil::trim($_POST['aliases']));
                if (isset($_POST['smileyPath'])) $this->smileyPath = FileUtil::removeLeadingSlash(StringUtil::trim($_POST['smileyPath']));
+               if (isset($_POST['uploadedFilename'])) $this->uploadedFilename = StringUtil::trim($_POST['uploadedFilename']);
+               if (isset($_FILES['fileUpload'])) $this->fileUpload = $_FILES['fileUpload'];
        }
        
        /**
@@ -150,7 +165,8 @@ class SmileyAddForm extends AbstractForm {
                                'showOrder' => $this->showOrder,
                                'categoryID' => $this->categoryID ?: null,
                                'packageID' => 1
-                       ))
+                       )),
+                       'fileLocation' => $this->uploadedFilename ? WCF_DIR.'images/smilies/tmp/'.$this->uploadedFilename : ''
                ));
                $this->objectAction->executeAction();
                $returnValues = $this->objectAction->getReturnValues();
@@ -172,6 +188,7 @@ class SmileyAddForm extends AbstractForm {
                $this->showOrder = 0;
                $this->smileyPath = '';
                $this->aliases = '';
+               $this->uploadedFilename = '';
                
                I18nHandler::getInstance()->reset();
                
@@ -187,6 +204,35 @@ class SmileyAddForm extends AbstractForm {
        public function validate() {
                parent::validate();
                
+               if ($this->uploadedFilename) {
+                       if (!file_exists(WCF_DIR.'images/smilies/tmp/'.$this->uploadedFilename)) {
+                               throw new UserInputException('fileUpload', 'uploadFailed');
+                       }
+               }
+               else if (!empty($this->fileUpload['name'])) {
+                       if (!getimagesize($this->fileUpload['tmp_name'])) {
+                               throw new UserInputException('fileUpload', 'noImage');
+                       }
+                       
+                       do {
+                               $this->uploadedFilename = StringUtil::getRandomID().'.'.mb_strtolower(mb_substr($this->fileUpload['name'], mb_strrpos($this->fileUpload['name'], '.') + 1));
+                       }
+                       while (file_exists(WCF_DIR.'images/smilies/tmp/'.$this->uploadedFilename));
+                       
+                       if (!@move_uploaded_file($this->fileUpload['tmp_name'], WCF_DIR.'images/smilies/tmp/'.$this->uploadedFilename)) {
+                               throw new UserInputException('fileUpload', 'uploadFailed');
+                       }
+               }
+               else {
+                       if (empty($this->smileyPath)) {
+                               throw new UserInputException('smileyPath');
+                       }
+                       
+                       if (!is_file(WCF_DIR.$this->smileyPath)) {
+                               throw new UserInputException('smileyPath', 'notFound');
+                       }
+               }
+               
                // validate title
                if (!I18nHandler::getInstance()->validateValue('smileyTitle')) {
                        if (I18nHandler::getInstance()->isPlainValue('smileyTitle')) {
@@ -208,14 +254,6 @@ class SmileyAddForm extends AbstractForm {
                        throw new UserInputException('smileyCode');
                }
                
-               if (empty($this->smileyPath)) {
-                       throw new UserInputException('smileyPath');
-               }
-               
-               if (!is_file(WCF_DIR.$this->smileyPath)) {
-                       throw new UserInputException('smileyPath', 'notFound');
-               }
-               
                // validate smiley code and aliases against existing smilies
                $conditionBuilder = new PreparedStatementConditionBuilder();
                if (isset($this->smiley)) {
index 79e1e7c40160cfd5dfd2ff0c100156f35b71b24d..b1c5e4fab477ccbd7c911d76471ea723e2b265a9 100644 (file)
@@ -69,16 +69,21 @@ class SmileyEditForm extends SmileyAddForm {
                }
                
                // update bbcode
-               $this->objectAction = new SmileyAction(array($this->smileyID), 'update', array('data' => array_merge($this->additionalFields, array(
-                       'smileyTitle' => $this->smileyTitle,
-                       'smileyCode' => $this->smileyCode,
-                       'aliases' => $this->aliases,
-                       'smileyPath' => $this->smileyPath,
-                       'showOrder' => $this->showOrder,
-                       'categoryID' => $this->categoryID ?: null
-               ))));
+               $this->objectAction = new SmileyAction(array($this->smileyID), 'update', array(
+                       'data' => array_merge($this->additionalFields, array(
+                               'smileyTitle' => $this->smileyTitle,
+                               'smileyCode' => $this->smileyCode,
+                               'aliases' => $this->aliases,
+                               'smileyPath' => $this->smileyPath,
+                               'showOrder' => $this->showOrder,
+                               'categoryID' => $this->categoryID ?: null
+                       )),
+                       'fileLocation' => $this->uploadedFilename ? WCF_DIR.'images/smilies/tmp/'.$this->uploadedFilename : ''
+               ));
                $this->objectAction->executeAction();
                
+               $this->uploadedFilename = '';
+               
                $this->saved();
                
                // show success
index 8c9f002d8a4b34932d64e9e24bf800b7179e9394..3fb1fb67f3125bbae855c23a292e36d5eeea475b 100644 (file)
@@ -37,6 +37,46 @@ class SmileyAction extends AbstractDatabaseObjectAction implements ISortableActi
         */
        protected $requireACP = array('delete', 'update', 'updatePosition');
        
+       /**
+        * @see \wcf\data\AbstractDatabaseObjectAction::create()
+        */
+       public function create() {
+               $smiley = parent::create();
+               
+               if (!empty($this->parameters['fileLocation'])) {
+                       $smileyFilename = 'smiley'.$smiley->smileyID.'.'.mb_strtolower(mb_substr($this->parameters['fileLocation'], mb_strrpos($this->parameters['fileLocation'], '.') + 1));
+                       @rename($this->parameters['fileLocation'], WCF_DIR.'images/smilies/'.$smileyFilename);
+                       
+                       $smileyEditor = new SmileyEditor($smiley);
+                       $smileyEditor->update(array(
+                               'smileyPath' => 'images/smilies/'.$smileyFilename
+                       ));
+                       
+                       $smiley = new Smiley($smiley->smileyID);
+               }
+               
+               return $smiley;
+       }
+       
+       /**
+        * @see \wcf\data\AbstractDatabaseObjectAction::update()
+        */
+       public function update() {
+               if (empty($this->objects)) {
+                       $this->readObjects();
+               }
+               
+               if (count($this->objects) == 1 && !empty($this->parameters['fileLocation'])) {
+                       $smiley = reset($this->objects);
+                       $smileyFilename = 'smiley'.$smiley->smileyID.'.'.mb_strtolower(mb_substr($this->parameters['fileLocation'], mb_strrpos($this->parameters['fileLocation'], '.') + 1));
+                       @rename($this->parameters['fileLocation'], WCF_DIR.'images/smilies/'.$smileyFilename);
+                       
+                       $this->parameters['data']['smileyPath'] = 'images/smilies/'.$smileyFilename;
+               }
+               
+               parent::update();
+       }
+       
        /**
         * @see \wcf\data\ISortableAction::validateUpdatePosition()
         */
@@ -68,7 +108,7 @@ class SmileyAction extends AbstractDatabaseObjectAction implements ISortableActi
                foreach ($this->parameters['data']['structure'][0] as $smileyID) {
                        $smiley = $smileyList->search($smileyID);
                        if ($smiley === null) continue;
-                               
+                       
                        $editor = new SmileyEditor($smiley);
                        $editor->update(array('showOrder' => $i++));
                }
index 116b40d9d8b661fa09093d07958dd2c455cb8b0d..f84b410dd15ee7a4e4879d6e54eca0d49b368875 100644 (file)
@@ -1092,6 +1092,11 @@ GmbH=Gesellschaft mit beschränkter Haftung]]></item>
                <item name="wcf.acp.smiley.smileyPath.description"><![CDATA[Der Smiley-Pfad wird relativ zu „{$__wcf->getPath()}“ interpretiert.]]></item>
                <item name="wcf.acp.smiley.smileyPath.error.notFound"><![CDATA[Es wurde keine Datei unter dem angegebenen Pfad gefunden]]></item>
                <item name="wcf.acp.smiley.showOrder"><![CDATA[Position]]></item>
+               <item name="wcf.acp.smiley.smileyFile"><![CDATA[Smiley-Datei]]></item>
+               <item name="wcf.acp.smiley.fileUpload"><![CDATA[Smiley hochladen]]></item>
+               <item name="wcf.acp.smiley.fileUpload.description"><![CDATA[Laden Sie hier die Bilddatei des Smileys hoch.]]></item>
+               <item name="wcf.acp.smiley.fileUpload.error.noImage"><![CDATA[Bei der hochgeladenen Datei handelt es sich um keine Bilddatei.]]></item>
+               <item name="wcf.acp.smiley.fileUpload.error.uploadFailed"><![CDATA[Beim Hochladen der Datei ist ein unbekannter Fehler aufgetreten.]]></item>
        </category>
        
        <category name="wcf.acp.style">
index 2f19456d8db37105e8564d1b13ea211d7a9a6cdd..f52f3cc2b2d18d322bcc6c6937a61da915521868 100644 (file)
@@ -1092,6 +1092,11 @@ GmbH=Gesellschaft mit beschränkter Haftung]]></item>
                <item name="wcf.acp.smiley.smileyPath.description"><![CDATA[Smiley path is relative to “{$__wcf->getPath()}”.]]></item>
                <item name="wcf.acp.smiley.smileyPath.error.notFound"><![CDATA[Unable to find a file on given path]]></item>
                <item name="wcf.acp.smiley.showOrder"><![CDATA[Show Order]]></item>
+               <item name="wcf.acp.smiley.smileyFile"><![CDATA[Smiley File]]></item>
+               <item name="wcf.acp.smiley.fileUpload"><![CDATA[Upload Smiley]]></item>
+               <item name="wcf.acp.smiley.fileUpload.description"><![CDATA[Upload the image file for the smiley.]]></item>
+               <item name="wcf.acp.smiley.fileUpload.error.noImage"><![CDATA[The uploaded file is no image file.]]></item>
+               <item name="wcf.acp.smiley.fileUpload.error.uploadFailed"><![CDATA[An unknown error occured during upload.]]></item>
        </category>
        
        <category name="wcf.acp.style">