diff --git a/CHANGELOG b/CHANGELOG index 4eb7f53..eecf269 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,9 @@ ##Changelog +#### V 0.5.1 Sun Aug 12 +- first release for yii2 +- ported by philipp@frenzel.net + #### V 0.4 Mon Jul 2 16:25:53 COT 2012 - Added image preview assets - Added image processing assets diff --git a/composer.json b/composer.json index 96851f6..cfe2b6c 100644 --- a/composer.json +++ b/composer.json @@ -1,4 +1,26 @@ { - "name": "asgaroth/xupload", - "description": "jQuery file upload extension for Yii, allows your users to easily upload files to the server" + "name": "philippfrenzel/xupload", + "description": "jQuery file upload extension for Yii2, allows your users to easily upload files to the server", + "authors": [ + { + "name": "Asgaroth Belem", + "email": "asgaroth.belem@gmail.com", + "homepage": "https://github.com/Asgaroth/xupload", + "role": "Author Yii 1 Version" + }, + { + "name": "Philipp Frenzel", + "email": "philipp@frenzel.net", + "homepage": "http://www.frenzel.net/", + "role": "Port to Yii 2" + } + ], + "minimum-stability": "dev", + "require": { + "yiisoft/yii2": "*", + "yiisoft/yii2-jui": "*" + }, + "autoload": { + "psr-0": { "xupload\\": "/" } + } } \ No newline at end of file diff --git a/XUpload.php b/xupload/XUpload.php similarity index 55% rename from XUpload.php rename to xupload/XUpload.php index 24c9ed5..b99732f 100644 --- a/XUpload.php +++ b/xupload/XUpload.php @@ -1,18 +1,30 @@ + * @link http://blueimp.github.com/jQuery-File-Upload/ + * @link https://github.com/philippfrenzel/xupload + * @version 0.5 + * * @author AsgarothBelem * @link http://blueimp.github.com/jQuery-File-Upload/ * @link https://github.com/Asgaroth/xupload * @version 0.2 * */ -class XUpload extends CJuiInputWidget { + +namespace xupload; + +use \yii; +use \yii\base\View; +use \yii\helpers\Json; +use \yii\jui\InputWidget; + +class XUpload extends InputWidget { /** * the url to the upload handler @@ -80,11 +92,16 @@ class XUpload extends CJuiInputWidget { */ public $showForm = true; + public $options = array(); + /** * Publishes the required assets */ public function init() { parent::init(); + if (!isset($this->options['id'])) { + $this->options['id'] = $this->getId(); + } $this -> publishAssets(); } @@ -144,57 +161,42 @@ public function run() { * @throws CHttpException if the assets folder was not found */ public function publishAssets() { - $assets = dirname(__FILE__) . '/assets'; - $baseUrl = Yii::app() -> assetManager -> publish($assets); - if (is_dir($assets)) { - if($this->registerCSS){ - Yii::app() -> clientScript -> registerCssFile($baseUrl . '/css/jquery.fileupload-ui.css'); - } - //The Templates plugin is included to render the upload/download listings - Yii::app() -> clientScript -> registerScriptFile($baseUrl . '/js/tmpl.min.js', CClientScript::POS_END); - // The basic File Upload plugin - Yii::app() -> clientScript -> registerScriptFile($baseUrl . '/js/jquery.fileupload.js', CClientScript::POS_END); - if($this->previewImages || $this->imageProcessing){ - Yii::app() -> clientScript -> registerScriptFile($baseUrl . '/js/load-image.min.js', CClientScript::POS_END); - Yii::app() -> clientScript -> registerScriptFile($baseUrl . '/js/canvas-to-blob.min.js', CClientScript::POS_END); - } - //The Iframe Transport is required for browsers without support for XHR file uploads - Yii::app() -> clientScript -> registerScriptFile($baseUrl . '/js/jquery.iframe-transport.js', CClientScript::POS_END); - // The File Upload image processing plugin - if($this->imageProcessing){ - Yii::app() -> clientScript -> registerScriptFile($baseUrl . '/js/jquery.fileupload-ip.js', CClientScript::POS_END); - } - //The File Upload user interface plugin - Yii::app() -> clientScript -> registerScriptFile($baseUrl . '/js/jquery.fileupload-ui.js', CClientScript::POS_END); - - //The localization script - $messages = CJavaScript::encode(array( - 'fileupload' => array( - 'errors' => array( - "maxFileSize" => $this->t('File is too big'), - "minFileSize" => $this->t('File is too small'), - "acceptFileTypes" => $this->t('Filetype not allowed'), - "maxNumberOfFiles" => $this->t('Max number of files exceeded'), - "uploadedBytes" => $this->t('Uploaded bytes exceed file size'), - "emptyResult" => $this->t('Empty file upload result'), - ), - 'error' => $this->t('Error'), - 'start' => $this->t('Start'), - 'cancel' => $this->t('Cancel'), - 'destroy' => $this->t('Delete'), + $id = $this->options['id']; + //new syntax for asset registration + $view = $this->getView(); + $view->registerAssetBundle("xupload/core"); + + if($this->previewImages || $this->imageProcessing) + $view->registerAssetBundle("xupload/image"); + + if($this->imageProcessing) + $view->registerAssetBundle("xupload/imageprocessing"); + + //The localization script + $messages = Json::encode(array( + 'fileupload' => array( + 'errors' => array( + "maxFileSize" => $this->t('File is too big'), + "minFileSize" => $this->t('File is too small'), + "acceptFileTypes" => $this->t('Filetype not allowed'), + "maxNumberOfFiles" => $this->t('Max number of files exceeded'), + "uploadedBytes" => $this->t('Uploaded bytes exceed file size'), + "emptyResult" => $this->t('Empty file upload result'), ), - )); - $js = "window.locale = {$messages}"; - - Yii::app()->clientScript->registerScript('XuploadI18N', $js, CClientScript::POS_END); - /** - - - * - */ - } else { - throw new CHttpException(500, __CLASS__ . ' - Error: Couldn\'t find assets to publish.'); - } + 'error' => $this->t('Error'), + 'start' => $this->t('Start'), + 'cancel' => $this->t('Cancel'), + 'destroy' => $this->t('Delete'), + ), + )); + $js = "window.locale = {$messages}"; + + $view->registerJs(implode("\n", $js),View::POS_READY); + /** + + + * + */ } protected function t($message, $params=array ( )) diff --git a/actions/XUploadAction.php b/xupload/actions/XUploadAction.php similarity index 86% rename from actions/XUploadAction.php rename to xupload/actions/XUploadAction.php index ade5b12..399a800 100644 --- a/actions/XUploadAction.php +++ b/xupload/actions/XUploadAction.php @@ -22,8 +22,8 @@ * return array( * 'upload'=>array( * 'class'=>'xupload.actions.XUploadAction', - * 'path' =>Yii::app() -> getBasePath() . "/../uploads", - * 'publicPath' => Yii::app() -> getBaseUrl() . "/uploads", + * 'path' =>Yii::$app -> getBasePath() . "/../uploads", + * 'publicPath' => Yii::$app -> getBaseUrl() . "/uploads", * 'subfolderVar' => "parent_id", * ), * ); @@ -36,11 +36,21 @@ * * ###Resources * - [xupload](http://www.yiiframework.com/extension/xupload) + * @version 0.5 (yii2) + * @author Philipp (http://www.frenzel.net) * * @version 0.3 * @author Asgaroth (http://www.yiiframework.com/user/1883/) */ -class XUploadAction extends CAction { + +namespace xupload\models; + +use \Yii; +use \yii\helpers\Json; +use \yii\web\UploadedFile; +use \yii\base\Action; + +class XUploadAction extends Action { /** * XUploadForm (or subclass of it) to be used. Defaults to XUploadForm @@ -48,7 +58,7 @@ class XUploadAction extends CAction { * @var string * @since 0.5 */ - public $formClass = 'xupload.models.XUploadForm'; + public $formClass = '\xupload\models\XUploadForm'; /** * Name of the model attribute referring to the uploaded file. @@ -154,25 +164,25 @@ class XUploadAction extends CAction { public function init( ) { if( !isset( $this->path ) ) { - $this->path = realpath( Yii::app( )->getBasePath( )."/../uploads" ); + $this->path = realpath( Yii::$app->getBasePath( )."/../uploads" ); } if( !is_dir( $this->path ) ) { mkdir( $this->path, 0777, true ); chmod ( $this->path , 0777 ); - //throw new CHttpException(500, "{$this->path} does not exists."); + //throw new new \yii\web\HttpException(500, "{$this->path} does not exists."); } else if( !is_writable( $this->path ) ) { chmod( $this->path, 0777 ); - //throw new CHttpException(500, "{$this->path} is not writable."); + //throw new new \yii\web\HttpException(500, "{$this->path} is not writable."); } if( $this->subfolderVar !== null && $this->subfolderVar !== false ) { - $this->_subfolder = Yii::app( )->request->getQuery( $this->subfolderVar, date( "mdY" ) ); + $this->_subfolder = Yii::$app->request->getQuery( $this->subfolderVar, date( "mdY" ) ); } else if( $this->subfolderVar !== false ) { $this->_subfolder = date( "mdY" ); } if( !isset($this->_formModel)) { - $this->formModel = Yii::createComponent(array('class'=>$this->formClass)); + $this->formModel = new $this->formClass; } if($this->secureFileNames) { @@ -208,20 +218,20 @@ protected function handleDeleting() { if (isset($_GET["_method"]) && $_GET["_method"] == "delete") { $success = false; - if ($_GET["file"][0] !== '.' && Yii::app()->user->hasState($this->stateVariable)) { + if ($_GET["file"][0] !== '.' && Yii::$app->user->hasState($this->stateVariable)) { // pull our userFiles array out of state and only allow them to delete // files from within that array - $userFiles = Yii::app()->user->getState($this->stateVariable, array()); + $userFiles = Yii::$app->user->getState($this->stateVariable, array()); if ($this->fileExists($userFiles[$_GET["file"]])) { $success = $this->deleteFile($userFiles[$_GET["file"]]); if ($success) { unset($userFiles[$_GET["file"]]); // remove it from our session and save that info - Yii::app()->user->setState($this->stateVariable, $userFiles); + Yii::$app->user->setState($this->stateVariable, $userFiles); } } } - echo json_encode($success); + echo Json::encode($success); return true; } return false; @@ -236,7 +246,7 @@ protected function handleUploading() { $this->init(); $model = $this->formModel; - $model->{$this->fileAttribute} = CUploadedFile::getInstance($model, $this->fileAttribute); + $model->{$this->fileAttribute} = UploadedFile::getInstance($model, $this->fileAttribute); if ($model->{$this->fileAttribute} !== null) { $model->{$this->mimeTypeAttribute} = $model->{$this->fileAttribute}->getType(); $model->{$this->sizeAttribute} = $model->{$this->fileAttribute}->getSize(); @@ -257,7 +267,7 @@ protected function handleUploading() $returnValue = $this->beforeReturn(); if ($returnValue === true) { - echo json_encode(array(array( + echo Json::encode(array(array( "name" => $model->{$this->displayNameAttribute}, "type" => $model->{$this->mimeTypeAttribute}, "size" => $model->{$this->sizeAttribute}, @@ -270,14 +280,14 @@ protected function handleUploading() "delete_type" => "POST" ))); } else { - echo json_encode(array(array("error" => $returnValue,))); + echo Json::encode(array(array("error" => $returnValue,))); Yii::log("XUploadAction: " . $returnValue, CLogger::LEVEL_ERROR, "xupload.actions.XUploadAction"); } } else { $this->afterValidateError($model); } } else { - throw new CHttpException(500, "Could not upload file"); + throw new new \yii\web\HttpException(500, "Could not upload file"); } } @@ -292,7 +302,7 @@ protected function beforeReturn() { $path = $this->getPath(); // Now we need to save our file info to the user's session - $userFiles = Yii::app( )->user->getState( $this->stateVariable, array()); + $userFiles = Yii::$app->user->getState( $this->stateVariable, array()); $userFiles[$this->formModel->{$this->fileNameAttribute}] = array( "path" => $path.$this->formModel->{$this->fileNameAttribute}, @@ -303,7 +313,7 @@ protected function beforeReturn() { 'mime' => $this->formModel->{$this->mimeTypeAttribute}, 'name' => $this->formModel->{$this->displayNameAttribute}, ); - Yii::app( )->user->setState( $this->stateVariable, $userFiles ); + Yii::$app->user->setState( $this->stateVariable, $userFiles ); return true; } @@ -372,7 +382,7 @@ protected function fileExists($file) { * @param $model */ protected function afterValidateError($model) { - echo json_encode(array(array("error" => $model->getErrors($this->fileAttribute),))); - Yii::log("XUploadAction: " . CVarDumper::dumpAsString($model->getErrors()), CLogger::LEVEL_ERROR, "xupload.actions.XUploadAction"); + echo Json::encode(array(array("error" => $model->getErrors($this->fileAttribute),))); + Yii::log("XUploadAction: " . CVarDumper::dumpAsString($model->getErrors()), CLogger::LEVEL_ERROR, "\xupload\actions\XUploadAction"); } } diff --git a/xupload/assets.php b/xupload/assets.php new file mode 100644 index 0000000..0399b49 --- /dev/null +++ b/xupload/assets.php @@ -0,0 +1,32 @@ + array( + 'sourcePath' => __DIR__ . '/assets', + 'js' => array( + '/js/tmpl.min.js', + '/js/jquery.fileupload.js', + '/js/jquery.iframe-transport.js', //The Iframe Transport is required for browsers without support for XHR file uploads + '/js/jquery.fileupload-ui.js' + ), + 'css' => array( + '/css/jquery.fileupload-ui.css' + ), + ), + 'xupload/image' => array( + 'sourcePath' => __DIR__ . '/assets', + 'js' => array( + '/js/load-image.min.js', + '/js/canvas-to-blob.min.js' + ), + 'depends'=>array( + 'xupload/core' + ), + ), + 'xupload/imageprocessing'=>array( + 'sourcePath' => __DIR__ . '/assets', + 'js' => array( + '/js/jquery.fileupload-ip.js' + ), + ), +); diff --git a/assets/css/jquery.fileupload-ui.css b/xupload/assets/css/jquery.fileupload-ui.css similarity index 100% rename from assets/css/jquery.fileupload-ui.css rename to xupload/assets/css/jquery.fileupload-ui.css diff --git a/assets/img/loading.gif b/xupload/assets/img/loading.gif similarity index 100% rename from assets/img/loading.gif rename to xupload/assets/img/loading.gif diff --git a/assets/img/progressbar.gif b/xupload/assets/img/progressbar.gif similarity index 100% rename from assets/img/progressbar.gif rename to xupload/assets/img/progressbar.gif diff --git a/assets/js/canvas-to-blob.min.js b/xupload/assets/js/canvas-to-blob.min.js similarity index 100% rename from assets/js/canvas-to-blob.min.js rename to xupload/assets/js/canvas-to-blob.min.js diff --git a/assets/js/cors/jquery.postmessage-transport.js b/xupload/assets/js/cors/jquery.postmessage-transport.js similarity index 100% rename from assets/js/cors/jquery.postmessage-transport.js rename to xupload/assets/js/cors/jquery.postmessage-transport.js diff --git a/assets/js/cors/jquery.xdr-transport.js b/xupload/assets/js/cors/jquery.xdr-transport.js similarity index 100% rename from assets/js/cors/jquery.xdr-transport.js rename to xupload/assets/js/cors/jquery.xdr-transport.js diff --git a/assets/js/jquery.fileupload-fp.js b/xupload/assets/js/jquery.fileupload-fp.js similarity index 100% rename from assets/js/jquery.fileupload-fp.js rename to xupload/assets/js/jquery.fileupload-fp.js diff --git a/assets/js/jquery.fileupload-ip.js b/xupload/assets/js/jquery.fileupload-ip.js similarity index 100% rename from assets/js/jquery.fileupload-ip.js rename to xupload/assets/js/jquery.fileupload-ip.js diff --git a/assets/js/jquery.fileupload-ui.js b/xupload/assets/js/jquery.fileupload-ui.js similarity index 100% rename from assets/js/jquery.fileupload-ui.js rename to xupload/assets/js/jquery.fileupload-ui.js diff --git a/assets/js/jquery.fileupload.js b/xupload/assets/js/jquery.fileupload.js similarity index 100% rename from assets/js/jquery.fileupload.js rename to xupload/assets/js/jquery.fileupload.js diff --git a/assets/js/jquery.iframe-transport.js b/xupload/assets/js/jquery.iframe-transport.js similarity index 100% rename from assets/js/jquery.iframe-transport.js rename to xupload/assets/js/jquery.iframe-transport.js diff --git a/assets/js/load-image.min.js b/xupload/assets/js/load-image.min.js similarity index 100% rename from assets/js/load-image.min.js rename to xupload/assets/js/load-image.min.js diff --git a/assets/js/locale.js b/xupload/assets/js/locale.js similarity index 100% rename from assets/js/locale.js rename to xupload/assets/js/locale.js diff --git a/assets/js/tmpl.min.js b/xupload/assets/js/tmpl.min.js similarity index 100% rename from assets/js/tmpl.min.js rename to xupload/assets/js/tmpl.min.js diff --git a/assets/js/vendor/jquery.ui.widget.js b/xupload/assets/js/vendor/jquery.ui.widget.js similarity index 100% rename from assets/js/vendor/jquery.ui.widget.js rename to xupload/assets/js/vendor/jquery.ui.widget.js diff --git a/messages/ru/widget.php b/xupload/messages/ru/widget.php similarity index 100% rename from messages/ru/widget.php rename to xupload/messages/ru/widget.php diff --git a/models/XUploadForm.php b/xupload/models/XUploadForm.php similarity index 96% rename from models/XUploadForm.php rename to xupload/models/XUploadForm.php index a170b1c..82fba7a 100644 --- a/models/XUploadForm.php +++ b/xupload/models/XUploadForm.php @@ -1,5 +1,11 @@