82f961f9
Administrator
big commti
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?php
namespace common\behaviors;
use common\modules\comment\models\CommentModel;
use yii\base\Behavior;
use yii\db\ActiveRecord;
use yii\web\UploadedFile;
/**
* Class RatingBehavior
* @property CommentModel $owner
* @package common\behaviors
*/
class SaveImgBehavior extends Behavior
{
|
612a4ea1
Administrator
big commti
|
17
18
|
public $fields;
//public $directory;
|
82f961f9
Administrator
big commti
|
19
20
21
22
23
24
25
26
27
28
29
|
public function events()
{
return [
ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeUpdate',
ActiveRecord::EVENT_BEFORE_INSERT => 'beforeInsert',
];
}
public function beforeUpdate($event)
{
|
612a4ea1
Administrator
big commti
|
30
31
|
foreach($this->fields as $field){
if ( ($image = UploadedFile::getInstance($this->owner, $field['name'])) ) {
|
37415bee
Administrator
big commti
|
32
|
$this->owner->{$field['name']} = $image->name;
|
612a4ea1
Administrator
big commti
|
33
|
}
|
82f961f9
Administrator
big commti
|
34
|
|
37415bee
Administrator
big commti
|
35
36
|
if(!$this->owner->{$field['name']}){
$this->owner->{$field['name']} = $this->owner->getOldAttribute($field['name']);
|
612a4ea1
Administrator
big commti
|
37
|
}
|
82f961f9
Administrator
big commti
|
38
|
|
82f961f9
Administrator
big commti
|
39
|
|
612a4ea1
Administrator
big commti
|
40
41
|
if ($image) {
$imgDir = \Yii::getAlias('@storage/'.$field['directory'].'/');
|
82f961f9
Administrator
big commti
|
42
|
|
612a4ea1
Administrator
big commti
|
43
44
45
|
if(!is_dir($imgDir)) {
mkdir($imgDir, 0755, true);
}
|
82f961f9
Administrator
big commti
|
46
|
|
612a4ea1
Administrator
big commti
|
47
|
$image->saveAs(\Yii::getAlias('@storage/'.$field['directory'].'/' . $image->name));
|
82f961f9
Administrator
big commti
|
48
|
}
|
82f961f9
Administrator
big commti
|
49
|
}
|
612a4ea1
Administrator
big commti
|
50
51
52
|
|
82f961f9
Administrator
big commti
|
53
54
|
}
|
66285eca
Administrator
big commti
|
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
public function getImagesConfig($image = 'image') {
$op = [];
if ($this->owner->$image) {
$op[] = [
'caption' => $this->owner->$image,
'url' => \yii\helpers\Url::to(['delimg', 'id' => $this->owner->primaryKey, 'field' =>$image ]),
'key' => $this->owner->primaryKey,
'extra' => [
'id' => $this->owner->primaryKey,
],
];
}
return $op;
}
|
82f961f9
Administrator
big commti
|
72
73
74
|
public function beforeInsert($event)
{
|
612a4ea1
Administrator
big commti
|
75
|
foreach($this->fields as $field){
|
82f961f9
Administrator
big commti
|
76
|
|
612a4ea1
Administrator
big commti
|
77
|
if ( ($image = UploadedFile::getInstance($this->owner, $field['name'])) ) {
|
37415bee
Administrator
big commti
|
78
|
$this->owner->{$field['name']} = $image->name;
|
612a4ea1
Administrator
big commti
|
79
|
}
|
82f961f9
Administrator
big commti
|
80
|
|
82f961f9
Administrator
big commti
|
81
82
|
|
612a4ea1
Administrator
big commti
|
83
84
|
if ($image) {
$imgDir = \Yii::getAlias('@storage/'.$field['directory'].'/');
|
82f961f9
Administrator
big commti
|
85
|
|
612a4ea1
Administrator
big commti
|
86
87
88
|
if(!is_dir($imgDir)) {
mkdir($imgDir, 0755, true);
}
|
82f961f9
Administrator
big commti
|
89
|
|
612a4ea1
Administrator
big commti
|
90
|
$image->saveAs(\Yii::getAlias('@storage/'.$field['directory'].'/' . $image->name));
|
82f961f9
Administrator
big commti
|
91
|
}
|
82f961f9
Administrator
big commti
|
92
93
94
|
}
}
|
612a4ea1
Administrator
big commti
|
95
96
|
public function getImageFile($image = 'image') {
return empty($this->owner->$image) ? null : '/storage/'.$this->fields[0]['directory'].'/'. $this->owner->$image;
|
82f961f9
Administrator
big commti
|
97
98
|
}
|
612a4ea1
Administrator
big commti
|
99
100
|
public function getImageUrl($image = 'image') {
return empty($this->owner->$image) ? null : '/storage/'.$this->fields[0]['directory'].'/'. $this->owner->$image;
|
82f961f9
Administrator
big commti
|
101
|
}
|
612a4ea1
Administrator
big commti
|
102
103
104
|
|
82f961f9
Administrator
big commti
|
105
|
}
|