b519af22
Karnovsky A
Base-product func...
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<?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
* @property integer $product_id
|
e2128676
Karnovsky A
Karnovsky 0505201...
|
13
|
* @property integer $product_variant_id
|
b519af22
Karnovsky A
Base-product func...
|
14
15
16
|
* @property string $image
* @property string $alt
* @property string $title
|
b519af22
Karnovsky A
Base-product func...
|
17
|
* @property Product $product
|
e2128676
Karnovsky A
Karnovsky 0505201...
|
18
|
* @property ProductVariant $productVariant
|
b519af22
Karnovsky A
Base-product func...
|
19
20
21
|
*/
class ProductImage extends \yii\db\ActiveRecord
{
|
e2128676
Karnovsky A
Karnovsky 0505201...
|
22
|
public $imageUpload;
|
b519af22
Karnovsky A
Base-product func...
|
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 [
|
e2128676
Karnovsky A
Karnovsky 0505201...
|
37
38
39
|
[['product_id'], 'required'],
[['product_image_id', 'product_id', 'product_variant_id'], 'integer'],
[['alt', 'title', 'image'], 'string', 'max' => 255],
|
b519af22
Karnovsky A
Base-product func...
|
40
|
[['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']],
|
e2128676
Karnovsky A
Karnovsky 0505201...
|
41
42
43
|
[['product_variant_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProductVariant::className(), 'targetAttribute' => ['product_variant_id' => 'product_variant_id']],
[['imageUpload'], 'safe'],
[['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'],
|
b519af22
Karnovsky A
Base-product func...
|
44
45
46
47
48
49
50
51
52
53
54
|
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'product_image_id' => Yii::t('product', 'Product Image ID'),
'product_id' => Yii::t('product', 'Product ID'),
|
e2128676
Karnovsky A
Karnovsky 0505201...
|
55
56
57
58
|
'product_variant_id' => Yii::t('product', 'Product Variant ID'),
'product' => Yii::t('product', 'Product'),
'product' => Yii::t('product', 'Product'),
'product_variant' => Yii::t('product', 'Product Variant'),
|
b519af22
Karnovsky A
Base-product func...
|
59
60
61
62
63
64
65
66
67
68
69
|
'image' => Yii::t('product', 'Image'),
'alt' => Yii::t('product', 'Alt'),
'title' => Yii::t('product', 'Title'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProduct()
{
|
e2128676
Karnovsky A
Karnovsky 0505201...
|
70
71
72
73
74
75
76
77
78
79
80
81
82
|
$return = $this->hasOne(Product::className(), ['product_id' => 'product_id']);
if (empty($return)) {
$return = $this->productVariant->product_id;
}
return $return;
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductVariant()
{
return $this->hasOne(Product::className(), ['product_variant_id' => 'product_variant_id']);
|
b519af22
Karnovsky A
Base-product func...
|
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
}
/**
* @inheritdoc
* @return ProductImageQuery the active query used by this AR class.
*/
public static function find()
{
return new ProductImageQuery(get_called_class());
}
/**
* fetch stored image file name with complete path
* @return string
*/
public function getImageFile()
{
|
cf07505d
Karnovsky A
Karnovsky 0605201...
|
100
|
return isset($this->image) ? '/images/products/' . $this->image : null;
|
b519af22
Karnovsky A
Base-product func...
|
101
102
103
104
105
106
107
108
109
|
}
/**
* fetch stored image url
* @return string
*/
public function getImageUrl()
{
// return a default image placeholder if your source image is not found
|
cf07505d
Karnovsky A
Karnovsky 0605201...
|
110
|
return isset($this->image) ? '/images/products/'. $this->image : 'default.jpg';
|
b519af22
Karnovsky A
Base-product func...
|
111
112
113
114
115
116
117
118
119
120
121
|
}
/**
* 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)
|
e2128676
Karnovsky A
Karnovsky 0505201...
|
122
|
$image = UploadedFile::getInstance($this, 'imageUpload');
|
b519af22
Karnovsky A
Base-product func...
|
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
|
// if no image was uploaded abort the upload
if (empty($image)) {
return false;
}
// store the source file name
$this->filename = $image->name;
$ext = end((explode(".", $image->name)));
// 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;
}
}
|