-
Notifications
You must be signed in to change notification settings - Fork 34
/
UploadBehavior.php
221 lines (201 loc) · 6.04 KB
/
UploadBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
namespace mdm\upload;
use Yii;
use yii\web\UploadedFile;
use yii\db\BaseActiveRecord;
/**
* UploadBehavior save uploaded file into [[$uploadPath]] and store information in database.
*
* Usage at [[\yii\base\Model::behaviors()]] add the following code
*
* ~~~
* return [
* ...
* [
* 'class' => 'mdm\upload\UploadBehavior',
* 'uploadPath' => '@common/upload', // default to '@runtime/upload'
* 'attribute' => 'file', // attribute use to receive from FileField
* 'savedAttribute' => 'file_id', // attribute use to receive id of file
* ],
* ];
* ~~~
*
* @property \yii\base\Model $owner
* @property FileModel $savedFile
*
* @author Misbahul D Munir <[email protected]>
* @since 1.0
*/
class UploadBehavior extends \yii\base\Behavior
{
/**
* @var string the directory to store uploaded files. You may use path alias here.
* If not set, it will use the "upload" subdirectory under the application runtime path.
*/
public $uploadPath = '@runtime/upload';
/**
* @var string the attribute that will receive the uploaded file
*/
public $attribute = 'file';
/**
* @var string the attribute that will receive the file id
*/
public $savedAttribute;
/**
* @var integer the level of sub-directories to store uploaded files. Defaults to 1.
* If the system has huge number of uploaded files (e.g. one million), you may use a bigger value
* (usually no bigger than 3). Using sub-directories is mainly to ensure the file system
* is not over burdened with a single directory having too many files.
*/
public $directoryLevel = 1;
/**
* @var boolean when true `saveUploadedFile()` will be called on event 'beforeSave'
*/
public $autoSave = true;
/**
* @var boolean when true then related file will be deleted on event 'beforeDelete'
*/
public $autoDelete = false;
/**
* @var boolean
*/
public $deleteOldFile = false;
/**
* @var \Closure|string
*/
public $saveCallback;
/**
* @var UploadedFile
*/
private $_file;
/**
* @inheritdoc
*/
public function init()
{
$this->uploadPath = Yii::getAlias($this->uploadPath);
}
/**
* @inheritdoc
*/
public function events()
{
$event = [];
if ($this->autoSave) {
$event[BaseActiveRecord::EVENT_BEFORE_INSERT] = 'beforeSave';
$event[BaseActiveRecord::EVENT_BEFORE_UPDATE] = 'beforeSave';
}
if ($this->autoDelete && $this->savedAttribute !== null) {
$event[BaseActiveRecord::EVENT_BEFORE_DELETE] = 'beforeDelete';
}
return $event;
}
/**
* Get saved file
* @return FileModel
*/
public function getSavedFile()
{
if($this->savedAttribute && $this->owner->{$this->savedAttribute}){
return FileModel::findOne($this->owner->{$this->savedAttribute});
}
}
/**
* @inheritdoc
*/
public function __get($name)
{
if ($name === $this->attribute) {
if ($this->_file === null) {
$this->_file = UploadedFile::getInstance($this->owner, $this->attribute);
}
return $this->_file;
} else {
return parent::__get($name);
}
}
/**
* @inheritdoc
*/
public function __set($name, $value)
{
if ($name === $this->attribute) {
if ($value instanceof UploadedFile || $value === null) {
$this->_file = $value;
}
} else {
parent::__set($name, $value);
}
}
/**
* @inheritdoc
*/
public function canSetProperty($name, $checkVars = true)
{
return $name === $this->attribute || parent::canSetProperty($name, $checkVars);
}
/**
* @inheritdoc
*/
public function canGetProperty($name, $checkVars = true)
{
return $name === $this->attribute || parent::canGetProperty($name, $checkVars);
}
/**
* Save uploaded file into [[$uploadPath]]
* @param boolean $deleteOldFile If true and file exists, file will be deleted.
* @return boolean|null if success return true, fault return false.
* Return null mean no uploaded file.
*/
public function saveUploadedFile($deleteOldFile = null)
{
/* @var $file UploadedFile */
$file = $this->{$this->attribute};
if ($file !== null) {
$callback = $this->saveCallback;
if ($callback !== null && is_string($callback)) {
$callback = [$this->owner, $callback];
}
$model = FileModel::saveAs($file, [
'uploadPath' => $this->uploadPath,
'directoryLevel' => $this->directoryLevel,
'saveCallback' => $callback,
]);
if ($model) {
if ($this->savedAttribute !== null) {
if ($deleteOldFile === null) {
$deleteOldFile = $this->deleteOldFile;
}
$oldId = $this->owner->{$this->savedAttribute};
$this->owner->{$this->savedAttribute} = $model->id;
if ($deleteOldFile && ($oldModel = FileModel::findOne($oldId)) !== null) {
return $oldModel->delete();
}
}
return true;
}
return false;
}
}
/**
* Event handler for beforeSave
* @param \yii\base\ModelEvent $event
*/
public function beforeSave($event)
{
if ($this->saveUploadedFile() === false) {
$event->isValid = false;
}
}
/**
* Event handler for beforeDelete
* @param \yii\base\ModelEvent $event
*/
public function beforeDelete($event)
{
$oldId = $this->owner->{$this->savedAttribute};
if (($oldModel = FileModel::findOne($oldId)) !== null) {
$event->isValid = $event->isValid && $oldModel->delete();
}
}
}