-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUploadBehavior.php
More file actions
137 lines (124 loc) · 4.51 KB
/
Copy pathFileUploadBehavior.php
File metadata and controls
137 lines (124 loc) · 4.51 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
<?php
/**
* Created by PhpStorm.
* User: Sergiy
* Company: http://web-logic.biz/
* Email: sirogabox@gmail.com
* Date: 19.08.14
* Time: 12:12
*
* if you want work with images need connect image component in main.php and add extension CImageComponent like this
* http://www.yiiframework.com/extension/image
'image'=>array(
'class' => 'application.extensions.image.CImageComponent',
'driver'=>'GD',
),
* don't forget add to your form 'htmlOptions'=>array('enctype'=>'multipart/form-data'),
* example of use
* пример использования
public function behaviors()
{
return array(
'Upload'=>array(
'class'=>'application.components.FileUploadBehavior',
'attribute'=>'image', // model attribute
'fileAlias'=>'webroot.upload.good', //directory path for saving files
'returnAlias'=>'/upload/good/', // return path
'types'=>'jpg, jpeg, png', // type files
'defaultName'=>'no_image.jpg' // default name if model save without file
)
);
}
*/
class FileUploadBehavior extends CActiveRecordBehavior
{
public $defaultName = null;
public $returnAlias = null;
public $deleteOriginal = true;
public $fileAlias = 'webroot.upload';
public $attribute = 'file';
public $types = 'jpg, jpeg, png';
public function beforeSave($event)
{
$file = CUploadedFile::getInstance($this->owner, $this->attribute);
if ($file) {
$tempName = uniqid().'.'.$file->getExtensionName();
$dir = Yii::getPathOfAlias($this->fileAlias).DIRECTORY_SEPARATOR;
if(!is_dir($dir)){
mkdir($dir);
}
$file->saveAs($dir.$tempName);
$this->owner->setAttribute($this->attribute, $tempName);
} else
$this->owner->setAttribute($this->attribute, $this->_old_file);
if(!$this->owner->isNewRecord){
if($this->owner->{$this->attribute}!==$this->_old_file){
$this->deleteOldFile($this->_old_file);
}
}
$event->isValid = true;
}
protected $_old_file;
public function afterFind($event)
{
$this->_old_file = $this->owner->{$this->attribute};
}
public function afterDelete($event)
{
$file = $this->owner->{$this->attribute};
$this->deleteOldFile($file);
}
public function attach($owner) {
parent::attach($owner);
$validators = $this->owner->getValidatorList();
$validator = CValidator::createValidator('file', $this->owner, $this->attribute, array('types'=>$this->types, 'allowEmpty'=>true));
$validators->add($validator);
}
public function deleteOldFile($file){
if($file!==$this->defaultName){
$dir = Yii::getPathOfAlias($this->fileAlias).DIRECTORY_SEPARATOR;
$files = glob($dir.'*'.$file);
$originalFile = $dir.$file;
foreach ($files as $file) {
if ($this->deleteOriginal==true){
@unlink($file);
}else{
if( $file != $originalFile){
@unlink($file);
}
}
}
}
}
//if file is image you can get this image(see in the head and connect extension)
public function getImage($width,$height,$crop=true)
{
$image = $this->owner->{$this->attribute};
if (empty($image))
return false;
$dir = Yii::getPathOfAlias($this->fileAlias).DIRECTORY_SEPARATOR;
if(!is_file($dir.$image)){
return Yii::app()->baseUrl.$this->returnAlias.$this->defaultName;
}
$name = $width.'_'.$height.'_'.$image;
if (!is_file($dir.$name)) {
self::resizeImage($width, $height, $dir.$image, $dir.$name,$crop);
}
return Yii::app()->baseUrl.$this->returnAlias.$name;
}
public static function resizeImage($width,$height,$scr,$dest,$crop=true)
{
$image = Yii::app()->image->load($scr);
if($image->width > $width || $image->height > $height)
{
if (($image->width/$width) < ($image->height/$height))
$image->resize($width, null, Image::AUTO);
else
$image->resize(null, $height, Image::AUTO);
}
if($crop){
$image->crop($width, $height, 'center', 'center');
}
$image->save($dest);
}
}