|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* HiPanel core package |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://hipanel.com/ |
|
7
|
|
|
* @package hipanel-core |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Created by PhpStorm. |
|
14
|
|
|
* User: tofid |
|
15
|
|
|
* Date: 11.02.15 |
|
16
|
|
|
* Time: 17:59. |
|
17
|
|
|
*/ |
|
18
|
|
|
namespace hipanel\behaviors; |
|
19
|
|
|
|
|
20
|
|
|
use hipanel\base\Model; |
|
21
|
|
|
use hipanel\components\FileStorage; |
|
22
|
|
|
use Yii; |
|
23
|
|
|
use yii\base\Behavior; |
|
24
|
|
|
use yii\db\BaseActiveRecord; |
|
25
|
|
|
use yii\web\UploadedFile; |
|
26
|
|
|
|
|
27
|
|
|
class File extends Behavior |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string the attribute which holds the attachment. |
|
31
|
|
|
*/ |
|
32
|
|
|
public $attribute = 'file'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array the scenarios in which the behavior will be triggered |
|
36
|
|
|
*/ |
|
37
|
|
|
public $scenarios = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string the attribute that will receive the file id |
|
41
|
|
|
*/ |
|
42
|
|
|
public $targetAttribute; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function events() |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
|
|
BaseActiveRecord::EVENT_BEFORE_INSERT => 'processFiles', |
|
51
|
|
|
BaseActiveRecord::EVENT_BEFORE_UPDATE => 'processFiles', |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Event handler for beforeInsert and beforeUpdate actions |
|
57
|
|
|
* |
|
58
|
|
|
* @param \yii\base\ModelEvent $event |
|
59
|
|
|
*/ |
|
60
|
|
|
public function processFiles($event) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
/** @var Model $model */ |
|
63
|
|
|
$model = $this->owner; |
|
64
|
|
|
$ids = []; |
|
65
|
|
|
|
|
66
|
|
|
if (in_array($model->scenario, $this->scenarios, true)) { |
|
67
|
|
|
$files = UploadedFile::getInstances($model, $this->attribute); |
|
68
|
|
|
|
|
69
|
|
|
foreach ($files as $file) { |
|
70
|
|
|
$model = $this->uploadFile($file); |
|
71
|
|
|
$ids[] = $model->id; |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (!empty($ids)) { |
|
75
|
|
|
$this->owner->{$this->targetAttribute} = implode(',', $ids); |
|
76
|
|
|
} else { |
|
77
|
|
|
// Protect attribute |
|
78
|
|
|
unset($model->{$this->attribute}); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Uploads file to the API server |
|
85
|
|
|
* |
|
86
|
|
|
* @param UploadedFile $file |
|
87
|
|
|
* @return \hipanel\models\File |
|
88
|
|
|
*/ |
|
89
|
|
|
private function uploadFile(UploadedFile $file) |
|
90
|
|
|
{ |
|
91
|
|
|
/** @var FileStorage $fileStorage */ |
|
92
|
|
|
$fileStorage = Yii::$app->get('fileStorage'); |
|
93
|
|
|
|
|
94
|
|
|
$filename = $fileStorage->saveUploadedFile($file); |
|
95
|
|
|
return $fileStorage->put($filename, $file->name); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.