Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 24 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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\\": "/" }
}
}
106 changes: 54 additions & 52 deletions XUpload.php → xupload/XUpload.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
<?php
Yii::import('zii.widgets.jui.CJuiInputWidget');
/**
* XUpload extension for Yii.
*
* jQuery file upload extension for Yii, allows your users to easily upload files to your server using jquery
* Its a wrapper of http://blueimp.github.com/jQuery-File-Upload/
*
* @author Philipp Frenzel <philipp@frenzel.net>
* @link http://blueimp.github.com/jQuery-File-Upload/
* @link https://github.com/philippfrenzel/xupload
* @version 0.5
*
* @author AsgarothBelem <asgaroth.belem@gmail.com>
* @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
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
/**
<!-- The XDomainRequest Transport is included for cross-domain file deletion for IE8+ -->
<!--[if gte IE 8]><script src="<?php echo Yii::app()->baseUrl; ?>/js/cors/jquery.xdr-transport.js"></script><![endif]-->
*
*/
} 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);
/**
<!-- The XDomainRequest Transport is included for cross-domain file deletion for IE8+ -->
<!--[if gte IE 8]><script src="<?php echo Yii::app()->baseUrl; ?>/js/cors/jquery.xdr-transport.js"></script><![endif]-->
*
*/
}

protected function t($message, $params=array ( ))
Expand Down
52 changes: 31 additions & 21 deletions actions/XUploadAction.php → xupload/actions/XUploadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
* ),
* );
Expand All @@ -36,19 +36,29 @@
*
* ###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
* @see XUploadAction::init()
* @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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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},
Expand All @@ -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");
}
}

Expand All @@ -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},
Expand All @@ -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;
}
Expand Down Expand Up @@ -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");
}
}
32 changes: 32 additions & 0 deletions xupload/assets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

return array(
'xupload/core' => 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'
),
),
);
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 7 additions & 1 deletion models/XUploadForm.php → xupload/models/XUploadForm.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?php
class XUploadForm extends CFormModel

namespace xupload\models;

use \Yii;
use \yii\base\Model;

class XUploadForm extends Model
{
public $file;
public $mime_type;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.