36d1807a
Yarik
Big commit.
|
1
|
<?php
|
36d1807a
Yarik
Big commit.
|
2
|
|
cc658b4c
Yarik
Big commit
|
3
|
namespace common\models;
|
36d1807a
Yarik
Big commit.
|
4
|
|
d55d2fe0
Yarik
Multilanguage
|
5
|
use common\modules\language\behaviors\LanguageBehavior;
|
cc658b4c
Yarik
Big commit
|
6
|
use common\modules\product\models\ProductVariant;
|
d55d2fe0
Yarik
Multilanguage
|
7
8
9
|
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\web\Request;
|
36d1807a
Yarik
Big commit.
|
10
11
|
/**
|
cc658b4c
Yarik
Big commit
|
12
13
14
15
16
17
18
19
20
21
22
|
* This is the model class for table "project".
* @property integer $project_id
* @property string $title
* @property string $link
* @property string $description
* @property integer $date_add
* @property ProjectImage[] $images
* @property ProjectImage $image
* @property array $imagesConfig
* @property ProductVariant[] $variants
* @property ProductToProject[] $productToProject
|
d55d2fe0
Yarik
Multilanguage
|
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
* * From language behavior *
* @property ProjectLang $lang
* @property ProjectLang[] $langs
* @property ProjectLang $object_lang
* @property string $ownerKey
* @property string $langKey
* @method string getOwnerKey()
* @method void setOwnerKey( string $value )
* @method string getLangKey()
* @method void setLangKey( string $value )
* @method ActiveQuery getLangs()
* @method ActiveQuery getLang( integer $language_id )
* @method ProjectLang[] generateLangs()
* @method void loadLangs( Request $request, ActiveRecord[] $model_langs )
* @method bool linkLangs( ActiveRecord[] $model_langs )
* @method bool saveLangs( ActiveRecord[] $model_langs )
* * End language behavior *
|
36d1807a
Yarik
Big commit.
|
40
|
*/
|
cc658b4c
Yarik
Big commit
|
41
|
class Project extends \yii\db\ActiveRecord
|
36d1807a
Yarik
Big commit.
|
42
|
{
|
cc658b4c
Yarik
Big commit
|
43
44
45
46
47
48
49
50
51
|
public $imagesUpload = [];
/**
* @inheritdoc
*/
public static function tableName()
{
return 'project';
|
36d1807a
Yarik
Big commit.
|
52
|
}
|
cc658b4c
Yarik
Big commit
|
53
54
55
56
57
58
59
|
/**
* @inheritdoc
*/
public function behaviors()
{
return [
|
d55d2fe0
Yarik
Multilanguage
|
60
|
'slug' => [
|
cc658b4c
Yarik
Big commit
|
61
62
63
64
65
|
'class' => 'common\behaviors\Slug',
'in_attribute' => 'title',
'out_attribute' => 'link',
'translit' => true,
],
|
d55d2fe0
Yarik
Multilanguage
|
66
67
68
|
'language' => [
'class' => LanguageBehavior::className(),
],
|
cc658b4c
Yarik
Big commit
|
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
|
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[
[ 'date_add' ],
'default',
'value' => function() {
return time();
},
],
[
[ 'title' ],
'required',
],
[
[ 'description' ],
'string',
],
[
[
'date_add',
'imagesUpload',
|
36d1807a
Yarik
Big commit.
|
97
|
],
|
cc658b4c
Yarik
Big commit
|
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
|
'safe',
],
[
[
'title',
'link',
],
'string',
'max' => 255,
],
[
[ 'date_add' ],
'filter',
'filter' => function($value) {
return strtotime($value) ? : time();
},
],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'project_id' => 'Project ID',
'title' => 'Title',
'link' => 'Link',
'description' => 'Description',
'date_add' => 'Date Add',
'images' => 'Images',
];
}
public function getImages()
{
return $this->hasMany(ProjectImage::className(), [ 'project_id' => 'project_id' ]);
}
public function getImage()
{
return $this->hasOne(ProjectImage::className(), [ 'project_id' => 'project_id' ]);
}
public function getImagesHTML()
{
$op = [];
if($this->images) {
foreach($this->images as $image) {
$op[] = \common\components\artboximage\ArtboxImageHelper::getImage($image->imageUrl, 'admin_thumb');
}
|
36d1807a
Yarik
Big commit.
|
150
|
}
|
cc658b4c
Yarik
Big commit
|
151
|
return $op;
|
36d1807a
Yarik
Big commit.
|
152
|
}
|
cc658b4c
Yarik
Big commit
|
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
public function getImagesConfig()
{
$op = [];
if($this->images) {
foreach($this->images as $image) {
$op[] = [
'caption' => $image->image,
'width' => '120px',
'url' => \yii\helpers\Url::to([
'/project/delimg',
'id' => $image->project_image_id,
]),
'key' => $image->project_image_id,
'extra' => [
'id' => $image->project_image_id,
],
];
|
36d1807a
Yarik
Big commit.
|
171
|
}
|
cc658b4c
Yarik
Big commit
|
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
}
return $op;
}
public function imagesUpload()
{
if($this->validate()) {
$images = [];
foreach($this->imagesUpload as $image) {
$imageName = $image->baseName . '.' . $image->extension;
$i = 0;
while(file_exists(\Yii::getAlias('@storage/projects/' . $imageName))) {
$i++;
$imageName = $image->baseName . '_' . $i . '.' . $image->extension;
}
$imgDir = \Yii::getAlias('@storage/projects/');
if(!is_dir($imgDir)) {
mkdir($imgDir, 0755, true);
}
$image->saveAs($imgDir . $imageName);
$images[] = $imageName;
|
36d1807a
Yarik
Big commit.
|
194
|
}
|
cc658b4c
Yarik
Big commit
|
195
196
197
|
return $images;
} else {
return false;
|
36d1807a
Yarik
Big commit.
|
198
|
}
|
cc658b4c
Yarik
Big commit
|
199
200
201
202
203
204
205
206
207
208
209
210
211
|
}
public function getProductToProject()
{
return $this->hasMany(ProductToProject::className(), [
'project_id' => 'project_id',
]);
}
public function getVariants()
{
return $this->hasMany(ProductVariant::className(), [ 'product_variant_id' => 'product_variant_id' ])
->viaTable('product_to_project', [ 'project_id' => 'project_id' ]);
|
36d1807a
Yarik
Big commit.
|
212
213
|
}
}
|