Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#69 Add event after attach files #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Installation
'mimeTypes' => 'image/png', // Only png images
'maxSize' => 1024 * 1024 // 1 MB
],
//'extForOpen' => 'pdf, png',//Extensions for open file instead of download
'tableName' => '{{%attachments}}' // Optional, default to 'attach_file'
]
...
Expand Down Expand Up @@ -132,6 +133,21 @@ Usage
}
```

Using Events
------------
You may add the following function to your model

```php
public function init(){
$this->on(\nemmo\attachments\behaviors\FileBehavior::EVENT_AFTER_ATTACH_FILES, function ($event) {
/** @var $files \nemmo\attachments\models\File[] */
$files = $event->files;
//your custom code
});
parent::init();
}
```

Change log
----------

Expand Down
8 changes: 8 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class Module extends \yii\base\Module

public $tempPath = '@app/uploads/temp';

/**
* Extensions for open file instead of download.
* Example: 'pdf, jpg'
*/
public $extForOpen = '';

public $rules = [];

public $tableName = 'attach_file';
Expand Down Expand Up @@ -142,6 +148,8 @@ public function attachFile($filePath, $owner)
$file->size = filesize($filePath);
$file->type = $fileType;
$file->mime = FileHelper::getMimeType($filePath);
$file->create_by = \Yii::$app->user->id;
$file->create_date = new \yii\db\Expression('NOW()');

if ($file->save()) {
unlink($filePath);
Expand Down
12 changes: 11 additions & 1 deletion src/behaviors/FileBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace nemmo\attachments\behaviors;

use nemmo\attachments\events\FileEvent;
use nemmo\attachments\models\File;
use nemmo\attachments\ModuleTrait;
use yii\base\Behavior;
Expand All @@ -21,6 +22,8 @@ class FileBehavior extends Behavior
{
use ModuleTrait;

const EVENT_AFTER_ATTACH_FILES = 'afterAttachFiles';

public function events()
{
return [
Expand All @@ -43,11 +46,18 @@ public function saveUploads($event)
}

$userTempDir = $this->getModule()->getUserDirPath();
$attachedFiles = [];
foreach (FileHelper::findFiles($userTempDir) as $file) {
if (!$this->getModule()->attachFile($file, $this->owner)) {
if (!($attachedFile = $this->getModule()->attachFile($file, $this->owner))) {
throw new \Exception(\Yii::t('yii', 'File upload failed.'));
}else{
$attachedFiles[] = $attachedFile;
}
}
if(count($attachedFiles)){
$event = \Yii::createObject(['class' => FileEvent::class, 'files' => $attachedFiles]);
$this->owner->trigger(self::EVENT_AFTER_ATTACH_FILES, $event);
}
rmdir($userTempDir);
}

Expand Down
6 changes: 5 additions & 1 deletion src/controllers/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public function actionDownload($id)
$file = File::findOne(['id' => $id]);
$filePath = $this->getModule()->getFilesDirPath($file->hash) . DIRECTORY_SEPARATOR . $file->hash . '.' . $file->type;

return Yii::$app->response->sendFile($filePath, "$file->name.$file->type");
$inline = false;
$extForOpen = array_map('trim', explode(',', $this->getModule()->extForOpen));
if(@in_array($file->type, $extForOpen)) $inline = true;

return Yii::$app->response->sendFile($filePath, "$file->name.$file->type", ['inline' => $inline]);
}

public function actionDelete($id)
Expand Down
29 changes: 29 additions & 0 deletions src/events/FileEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace nemmo\attachments\events;

use yii\base\Event;

class FileEvent extends Event
{
/**
* @var nemmo\attachments\models\File[]
*/
private $_files;

/**
* @return nemmo\attachments\models\File[]
*/
public function getFiles()
{
return $this->_files;
}

/**
* @param nemmo\attachments\models\File[] $files
*/
public function setFiles($files)
{
$this->_files = $files;
}
}
32 changes: 32 additions & 0 deletions src/migrations/m180327_050508_add_fields_create_date_etc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace nemmo\attachments\migrations;

use yii\db\Migration;

/**
* Class m180327_050508_add_fields_create_date_etc
*/
class m180327_050508_add_fields_create_date_etc extends Migration
{
use \nemmo\attachments\ModuleTrait;

/**
* @inheritdoc
*/
public function safeUp()
{
$this->addColumn($this->getModule()->tableName, 'create_by', $this->integer());
$this->addColumn($this->getModule()->tableName, 'create_date', $this->timestamp()->defaultValue(null));

$this->createIndex('file_create_by', $this->getModule()->tableName, 'create_by');
}

/**
* @inheritdoc
*/
public function safeDown()
{
$this->dropColumn($this->getModule()->tableName, 'create_by');
$this->dropColumn($this->getModule()->tableName, 'create_date');
}
}
6 changes: 5 additions & 1 deletion src/models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @property integer $size
* @property string $type
* @property string $mime
* @property integer $create_by
* @property string $create_date
*/
class File extends ActiveRecord
{
Expand Down Expand Up @@ -66,7 +68,9 @@ public function attributeLabels()
'hash' => 'Hash',
'size' => 'Size',
'type' => 'Type',
'mime' => 'Mime'
'mime' => 'Mime',
'create_by' => 'Create by',
'create_date' => 'Create date',
];
}

Expand Down