18ac4398
Yarik
Добавлен блог (ча...
|
1
2
3
4
5
6
7
|
<?php
namespace common\modules\blog\controllers;
use common\models\Language;
use common\modules\blog\models\Article;
use common\modules\blog\models\ArticleCategory;
use common\modules\blog\models\ArticleCategoryLang;
|
4921cad4
Yarik
Добален блог v 0....
|
8
|
use common\modules\blog\models\ArticleCategoryMedia;
|
18ac4398
Yarik
Добавлен блог (ча...
|
9
10
|
use common\modules\blog\models\ArticleLang;
use yii\data\ActiveDataProvider;
|
4921cad4
Yarik
Добален блог v 0....
|
11
|
use yii\filters\VerbFilter;
|
18ac4398
Yarik
Добавлен блог (ча...
|
12
|
use yii\web\Controller;
|
4921cad4
Yarik
Добален блог v 0....
|
13
14
|
use yii\web\NotFoundHttpException;
use yii\web\UploadedFile;
|
18ac4398
Yarik
Добавлен блог (ча...
|
15
16
17
|
class CategoryController extends Controller
{
|
4921cad4
Yarik
Добален блог v 0....
|
18
19
20
21
22
23
24
25
26
27
28
29
|
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post']
]
]
];
}
|
18ac4398
Yarik
Добавлен блог (ча...
|
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => ArticleCategory::find(),
'pagination' => [
'pageSize' => 1,
],
]);
return $this->render('index', ['dataProvider' => $dataProvider]);
}
public function actionCreate()
{
$category_langs = array();
$category = new ArticleCategory();
|
4921cad4
Yarik
Добален блог v 0....
|
45
46
47
48
49
|
$images = array();
$images['full'] = new ArticleCategoryMedia(['scenario' => ArticleCategoryMedia::SCENARIO_FULL]);
$images['preview'] = new ArticleCategoryMedia(['scenario' => ArticleCategoryMedia::SCENARIO_PREVIEW]);
$images['additional'] = new ArticleCategoryMedia(['scenario' => ArticleCategoryMedia::SCENARIO_ADDITIONAL]);
$category->loadDefaultValues();
|
18ac4398
Yarik
Добавлен блог (ча...
|
50
51
52
|
$langs = Language::getActiveLanguages();
$default_lang = Language::getDefaultLang();
$isValid = false;
|
4921cad4
Yarik
Добален блог v 0....
|
53
54
55
56
57
58
59
60
61
|
foreach($images as $index => $value) {
$images[$index]->type = $index;
if($index == 'additional') {
$images[$index]->imageFile = UploadedFile::getInstances($images[$index], "[{$index}]imageFile");
} else {
$images[$index]->imageFile = UploadedFile::getInstance($images[$index], "[{$index}]imageFile");
}
$isValid = $images[$index]->validate(['imageFile']) && $isValid;
}
|
18ac4398
Yarik
Добавлен блог (ча...
|
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
if(!empty(\Yii::$app->request->post())) {
$isValid = true;
$category->load(\Yii::$app->request->post());
$isValid = $category->validate();
if(empty(\Yii::$app->request->post()['ArticleCategoryLang'])) {
$category_langs[$default_lang->language_id] = new ArticleCategoryLang();
$isValid = ArticleCategoryLang::validateMultiple($category_langs) && $isValid;
} else {
foreach(\Yii::$app->request->post()['ArticleCategoryLang'] as $index => $category_lang) {
$category_langs[$index] = new ArticleCategoryLang();
}
ArticleCategoryLang::loadMultiple($category_langs, \Yii::$app->request->post());
$isValid = ArticleCategoryLang::validateMultiple($category_langs) && $isValid;
}
} else {
$category_langs[$default_lang->language_id] = new ArticleCategoryLang();
}
if($isValid) {
$category->save(false);
|
4921cad4
Yarik
Добален блог v 0....
|
81
82
83
84
|
$first = 1;
foreach($images as $index => $image) {
$images[$index]->upload($category->id);
}
|
18ac4398
Yarik
Добавлен блог (ча...
|
85
|
foreach($category_langs as $category_lang) {
|
4921cad4
Yarik
Добален блог v 0....
|
86
87
88
89
90
91
|
if($first) {
$category_lang_clone = clone $category_lang;
$category_lang_clone->lang_id = 0;
$category_lang_clone->link('category', $category);
unset($category_lang_clone);
}
|
18ac4398
Yarik
Добавлен блог (ча...
|
92
|
$category_lang->link('category', $category);
|
4921cad4
Yarik
Добален блог v 0....
|
93
|
$first = 0;
|
18ac4398
Yarik
Добавлен блог (ча...
|
94
95
|
}
echo "ok";
|
4921cad4
Yarik
Добален блог v 0....
|
96
|
//$this->redirect('index');
|
18ac4398
Yarik
Добавлен блог (ча...
|
97
98
99
100
|
} else {
return $this->render('create', [
'category_langs' => $category_langs,
'category' => $category,
|
4921cad4
Yarik
Добален блог v 0....
|
101
102
|
'langs' => $langs,
'images' => $images
|
18ac4398
Yarik
Добавлен блог (ча...
|
103
104
105
|
]);
}
}
|
4921cad4
Yarik
Добален блог v 0....
|
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
public function actionUpdate($id)
{
$category = ArticleCategory::findOne($id);
$images = $category->getArticleCategoryMedia()->indexBy('type')->all();
if(!array_key_exists('full', $images)) {
$images['full'] = new ArticleCategoryMedia();
}
if(!array_key_exists('preview', $images)) {
$images['preview'] = new ArticleCategoryMedia();
}
foreach($images as $index => $image) {
$images[$index]->scenario = $index;
}
$images['additional'] = array(
0 => new ArticleCategoryMedia(['scenario' => ArticleCategoryMedia::SCENARIO_ADDITIONAL])
);
$images['additional'] = array_merge($images['additional'], $category->getArticleCategoryMedia()->andWhere(['type' => 'additional'])->indexBy('id')->all());
foreach($images['additional'] as $index => $image) {
$images['additional'][$index]->scenario = 'additional';
}
$category_langs = $category->getArticleCategoryLangs()->where(['>=', 'lang_id', '1'])->indexBy('lang_id')->all();
$langs = Language::getActiveLanguages();
$default_lang = Language::getDefaultLang();
$isValid = false;
if(!empty(\Yii::$app->request->post())) {
$isValid = true;
$category->load(\Yii::$app->request->post());
$isValid = $category->validate();
foreach($images as $index => $value) {
if($index == 'additional') {
$images[$index][0]->type = $index;
$images[$index][0]->imageFile = UploadedFile::getInstances($images[$index][0], "[{$index}]imageFile");
$isValid = $images[$index][0]->validate(['imageFile']) && $isValid;
} else {
$images[$index]->type = $index;
$images[$index]->imageFile = UploadedFile::getInstance($images[$index], "[{$index}]imageFile");
$isValid = $images[$index]->validate(['imageFile']) && $isValid;
}
}
if(empty(\Yii::$app->request->post()['ArticleCategoryLang'])) {
$isValid = ArticleCategoryLang::validateMultiple($category_langs) && $isValid;
} else {
foreach(\Yii::$app->request->post()['ArticleCategoryLang'] as $index => $category_lang) {
if(!array_key_exists($index, $category_langs)) {
$category_langs[$index] = new ArticleCategoryLang();
$category_langs[$index]->category_id = $category->id;
}
}
ArticleCategoryLang::loadMultiple($category_langs, \Yii::$app->request->post());
$isValid = ArticleCategoryLang::validateMultiple($category_langs) && $isValid;
}
}
if($isValid) {
$category->save(false);
foreach($images as $index => $image) {
if($index == 'additional') {
$images[$index][0]->upload($category->id);
} else {
if(!empty($images[$index]->imageFile)) {
$images[$index]->replace($category->id, true);
}
}
}
foreach($category_langs as $category_lang) {
$category_lang->save(false);
}
echo "ok";
//$this->redirect('index');
} else {
return $this->render('update', [
'category_langs' => $category_langs,
'category' => $category,
'langs' => $langs,
'images' => $images
]);
}
}
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
protected function findModel($id)
{
if (($model = ArticleCategory::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
|
18ac4398
Yarik
Добавлен блог (ча...
|
199
|
}
|