3f2bc3d0
Administrator
first commit
|
1
2
3
4
5
6
7
8
9
10
11
|
<?php
namespace common\modules\product\models;
use Yii;
use yii\web\UploadedFile;
/**
* This is the model class for table "product_image".
*
* @property integer $product_image_id
|
ccc7a9d3
Karnovsky A
Karnovsky 12052016
|
12
|
* @property integer $product_id
|
3f2bc3d0
Administrator
first commit
|
13
14
15
16
|
* @property integer $product_variant_id
* @property string $image
* @property string $alt
* @property string $title
|
3f2bc3d0
Administrator
first commit
|
17
|
* @property Product $product
|
ccc7a9d3
Karnovsky A
Karnovsky 12052016
|
18
|
* @property ProductVariant $productVariant
|
3f2bc3d0
Administrator
first commit
|
19
20
21
|
*/
class ProductImage extends \yii\db\ActiveRecord
{
|
ccc7a9d3
Karnovsky A
Karnovsky 12052016
|
22
|
public $imageUpload;
|
3f2bc3d0
Administrator
first commit
|
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
/**
* @inheritdoc
*/
public static function tableName()
{
return 'product_image';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
|
ccc7a9d3
Karnovsky A
Karnovsky 12052016
|
37
38
39
40
|
[['product_id'], 'required'],
[['product_image_id', 'product_id', 'product_variant_id'], 'integer'],
[['alt', 'title', 'image'], 'string', 'max' => 255],
[['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']],
|
3f2bc3d0
Administrator
first commit
|
41
|
[['product_variant_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProductVariant::className(), 'targetAttribute' => ['product_variant_id' => 'product_variant_id']],
|
ccc7a9d3
Karnovsky A
Karnovsky 12052016
|
42
43
|
[['imageUpload'], 'safe'],
[['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'],
|
3f2bc3d0
Administrator
first commit
|
44
45
46
47
48
49
50
51
52
53
|
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'product_image_id' => Yii::t('product', 'Product Image ID'),
|
ccc7a9d3
Karnovsky A
Karnovsky 12052016
|
54
|
'product_id' => Yii::t('product', 'Product ID'),
|
3f2bc3d0
Administrator
first commit
|
55
|
'product_variant_id' => Yii::t('product', 'Product Variant ID'),
|
ccc7a9d3
Karnovsky A
Karnovsky 12052016
|
56
|
'product' => Yii::t('product', 'Product'),
|
ccc7a9d3
Karnovsky A
Karnovsky 12052016
|
57
|
'product_variant' => Yii::t('product', 'Product Variant'),
|
3f2bc3d0
Administrator
first commit
|
58
59
60
61
62
63
64
65
66
|
'image' => Yii::t('product', 'Image'),
'alt' => Yii::t('product', 'Alt'),
'title' => Yii::t('product', 'Title'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
|
ccc7a9d3
Karnovsky A
Karnovsky 12052016
|
67
68
69
70
71
72
73
74
75
76
77
78
|
public function getProduct()
{
$return = $this->hasOne(Product::className(), ['product_id' => 'product_id']);
if (empty($return)) {
$return = $this->productVariant->product_id;
}
return $return;
}
/**
* @return \yii\db\ActiveQuery
*/
|
3f2bc3d0
Administrator
first commit
|
79
80
|
public function getProductVariant()
{
|
ccc7a9d3
Karnovsky A
Karnovsky 12052016
|
81
|
return $this->hasOne(Product::className(), ['product_variant_id' => 'product_variant_id']);
|
3f2bc3d0
Administrator
first commit
|
82
83
|
}
|
1b898c16
Administrator
20.07.16
|
84
85
86
87
88
89
90
91
|
// /**
// * @inheritdoc
// * @return ProductImageQuery the active query used by this AR class.
// */
// public static function find()
// {
// return new ProductImageQuery(get_called_class());
// }
|
3f2bc3d0
Administrator
first commit
|
92
93
94
95
96
97
98
|
/**
* fetch stored image file name with complete path
* @return string
*/
public function getImageFile()
{
|
30ea8a35
Administrator
29.06.16
|
99
|
return isset($this->image) ? '/storage/products/' . $this->image : null;
|
3f2bc3d0
Administrator
first commit
|
100
101
102
103
104
105
106
107
108
|
}
/**
* fetch stored image url
* @return string
*/
public function getImageUrl()
{
// return a default image placeholder if your source image is not found
|
30ea8a35
Administrator
29.06.16
|
109
|
return isset($this->image) ? '/storage/products/'. $this->image : '/storage/no_photo.png';
|
3f2bc3d0
Administrator
first commit
|
110
111
112
113
114
115
116
117
118
119
120
|
}
/**
* Process upload of image
*
* @return mixed the uploaded image instance
*/
public function uploadImage() {
// get the uploaded file instance. for multiple file uploads
// the following data will return an array (you may need to use
// getInstances method)
|
ccc7a9d3
Karnovsky A
Karnovsky 12052016
|
121
|
$image = UploadedFile::getInstance($this, 'imageUpload');
|
3f2bc3d0
Administrator
first commit
|
122
123
124
125
126
127
128
129
|
// if no image was uploaded abort the upload
if (empty($image)) {
return false;
}
// store the source file name
$this->filename = $image->name;
|
ccc7a9d3
Karnovsky A
Karnovsky 12052016
|
130
|
$ext = end((explode(".", $image->name)));
|
3f2bc3d0
Administrator
first commit
|
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
|
// generate a unique file name
$this->image = Yii::$app->security->generateRandomString().".{$ext}";
// the uploaded image instance
return $image;
}
/**
* Process deletion of image
*
* @return boolean the status of deletion
*/
public function deleteImage() {
$file = $this->getImageFile();
// check if file exists on server
if (empty($file) || !file_exists($file)) {
return false;
}
// check if uploaded file can be deleted on server
if (!unlink($file)) {
return false;
}
// if deletion successful, reset your file attributes
$this->image = null;
$this->filename = null;
return true;
}
}
|