ProductImage.php
4.66 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
<?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
* @property integer $product_variant_id
* @property string $image
* @property string $alt
* @property string $title
* @property Product $product
* @property ProductVariant $productVariant
*/
class ProductImage extends \yii\db\ActiveRecord
{
public $imageUpload;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'product_image';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['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']],
[['product_variant_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProductVariant::className(), 'targetAttribute' => ['product_variant_id' => 'product_variant_id']],
[['imageUpload'], 'safe'],
[['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'product_image_id' => Yii::t('product', 'Product Image ID'),
'product_id' => Yii::t('product', 'Product ID'),
'product_variant_id' => Yii::t('product', 'Product Variant ID'),
'product' => Yii::t('product', 'Product'),
'product_variant' => Yii::t('product', 'Product Variant'),
'image' => Yii::t('product', 'Image'),
'alt' => Yii::t('product', 'Alt'),
'title' => Yii::t('product', 'Title'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
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
*/
public function getProductVariant()
{
return $this->hasOne(Product::className(), ['product_variant_id' => 'product_variant_id']);
}
// /**
// * @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()
{
return isset($this->image) ? '/storage/products/' . $this->image : null;
}
/**
* fetch stored image url
* @return string
*/
public function getImageUrl()
{
// return a default image placeholder if your source image is not found
return isset($this->image) ? '/storage/products/'. $this->image : '/storage/no_photo.png';
}
/**
* 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)
$image = UploadedFile::getInstance($this, 'imageUpload');
// 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;
}
}