-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathEFileUploadAction.php
More file actions
271 lines (267 loc) · 6.46 KB
/
EFileUploadAction.php
File metadata and controls
271 lines (267 loc) · 6.46 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/**
* EFileUploadAction class file.
*
* @author Veaceslav Medvedev <slavcopost@gmail.com>
* @link http://code.google.com/p/yiiext/
* @license http://www.opensource.org/licenses/bsd-license.php
*/
/**
* EFileUploadAction file upload action.
*
* @author Veaceslav Medvedev <slavcopost@gmail.com>
* @version 0.3
* @package yiiext.actions.fileUpload
*/
class EFileUploadAction extends CAction
{
/**
* @var CModel|string|null the data model which contains file attribute with validation rules.
*/
public $model;
/**
* @var string the model attribute.
*/
public $attribute;
/**
* @var string the input field name. This must be resolve from {@link model} and {@link attribute}.
* This property is required if not set {@link model}.
*/
public $name;
/**
* @var string the directory where save files.
* Defaults to webroot directory.
*/
public $path;
/**
* @var boolean try create directory if not exists. Defaults to false.
* @see http://php.net/manual/en/function.mkdir.php
*/
public $createDirectory=false;
/**
* @var int which means the widest possible access. Defaults to 0644.
* @see http://php.net/manual/en/function.mkdir.php
*/
public $createDirectoryMode=0644;
/**
* @var int whether create directories recursive. Defaults to false.
* @see http://php.net/manual/en/function.mkdir.php
*/
public $createDirectoryRecursive=false;
/**
* @var string the filename.
* Defaults to original filename.
* @link CUploadedFile::getName()
*/
public $filename;
/**
* @var string the rule for generate filename.
*/
public $filenameRule;
/**
* @var CUploadedFile|null the uploaded file.
*/
protected $_file;
protected $_errors=array();
/**
* @return CUploadedFile|null the uploaded file.
*/
public function getFile()
{
return $this->_file;
}
/**
* Returns a value indicating whether there is any error.
* @return boolean whether there is any error.
*/
public function hasErrors()
{
return $this->_errors!==array();
}
/**
* Returns the process errors.
* @return array errors. Empty array is returned if no error.
*/
public function getErrors()
{
return $this->_errors;
}
/**
* Adds a new error.
* @param string $error new error message.
*/
public function addError($error)
{
$this->_errors[]=strval($error);
}
public function run()
{
// If model set, check attribute and generate input filed name.
if(is_string($this->model))
{
$this->model=new $this->model;
}
if($this->hasModel())
{
$this->name=CHtml::activeName($this->model,$this->attribute);
}
// Check input field name.
if(empty($this->name))
{
throw new CException(Yii::t('yiiext','Input field name required.'));
}
// Upload files.
$this->upload();
// Show errors.
if($this->hasErrors())
throw new CException(implode("\n",$this->getErrors()));
}
/**
* @throws CException
* @return void
*/
protected function upload()
{
$this->beforeUpload();
// Check file exists.
if(($this->_file=CUploadedFile::getInstanceByName($this->name))!==null)
{
// If model set, validate it.
if($this->hasModel())
{
$this->model->{$this->attribute}=$this->_file;
if(!$this->model->validate())
{
array_walk_recursive($this->model->getErrors(), array($this,'addError'));
}
}
// Prepare directory.
if($this->path===null)
{
$this->path=Yii::getPathOfAlias('webroot');
}
else if(!is_dir($this->path))
{
if($this->createDirectory===true)
{
if(!mkdir($this->path,$this->createDirectoryMode,$this->createDirectoryRecursive))
{
$this->addError(Yii::t('yiiext','Cannot create directory "{dir}".',array('{dir}'=>$this->path)));
}
Yii::trace('Create directory "{dir}"',array('{dir}'=>$this->path));
}
else
{
$this->addError(Yii::t('yiiext','Invalid path "{path}".',array('{path}'=>$this->path)));
}
}
if($this->filename===null)
{
// If set filename rule, evaluate it.
if($this->filenameRule!==null)
$this->filename=$this->evaluateExpression($this->filenameRule,array('file'=>$this->getFile()));
// If filename not set, get the original uploaded filename.
if($this->filename===null)
$this->filename=$this->getFile()->getName();
}
// Run beforeSave events.
if(!$this->hasErrors() && $this->beforeSave())
{
// Save file.
$filepath=rtrim($this->path,'/').'/'.$this->filename;
if(!$this->_file->saveAs($filepath))
{
$this->addError(Yii::t('yiiext','Cannot save file "{filepath}".',array('{filepath}'=>$filepath)));
}
else
{
Yii::trace(Yii::t('yiiext','File "{filepath}" success saved.',array('{filepath}'=>$filepath)));
// Run afterSave events.
$this->afterSave();
}
}
$this->afterUpload();
}
else
{
$this->addError(Yii::t('yiiext','File not sent.'));
}
}
/**
* @return boolean whether this action is associated with a data model.
*/
protected function hasModel()
{
return $this->model instanceof CModel&&$this->attribute!==null;
}
/**
* @param CEvent $event
* @return void
*/
public function onBeforeSave($event)
{
$this->raiseEvent('onBeforeSave',$event);
}
/**
* @param CEvent $event
* @return void
*/
public function onAfterSave($event)
{
$this->raiseEvent('onAfterSave',$event);
}
/**
* @param CEvent $event
* @return void
*/
public function onBeforeUpload($event)
{
$this->raiseEvent('onBeforeUpload',$event);
}
/**
* @param CEvent $event
* @return void
*/
public function onAfterUpload($event)
{
$this->raiseEvent('onAfterUpload',$event);
}
/**
* @return boolean
*/
protected function beforeSave()
{
if($this->hasEventHandler('onBeforeSave'))
{
$event=new CModelEvent($this);
$this->onBeforeSave($event);
return $event->isValid;
}
else
return true;
}
/**
* @return void
*/
protected function afterSave()
{
if($this->hasEventHandler('onAfterSave'))
$this->onAfterSave(new CEvent($this));
}
/**
* @return void
*/
protected function beforeUpload()
{
if($this->hasEventHandler('onBeforeUpload'))
$this->onBeforeUpload(new CEvent($this));
}
/**
* @return void
*/
protected function afterUpload()
{
if($this->hasEventHandler('onAfterUpload'))
$this->onAfterUpload(new CEvent($this));
}
}