GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

PropertiesController::actionIndex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace app\backend\controllers;
4
5
use app\components\Helper;
6
use app\models\Form;
7
use app\models\BaseObject;
8
use app\models\ObjectStaticValues;
9
use app\models\Property;
10
use app\models\PropertyGroup;
11
use app\models\PropertyStaticValues;
12
use app\models\Submission;
13
use app\modules\image\widgets\views\AddImageAction;
14
use app\properties\PropertyHandlers;
15
use app\modules\image\widgets\SaveInfoAction;
16
use devgroup\TagDependencyHelper\ActiveRecordHelper;
17
use Yii;
18
use yii\caching\TagDependency;
19
use yii\filters\AccessControl;
20
use yii\helpers\Html;
21
use yii\helpers\Url;
22
use yii\web\Controller;
23
use yii\web\NotFoundHttpException;
24
25
class PropertiesController extends Controller
26
{
27
    protected function checkDoubledSlugs($slug)
28
    {
29
        if (empty($slug) === false) {
30
            $propertyStaticValues = PropertyStaticValues::find()
31
                ->where(['slug' => $slug])
32
                ->all();
33
            if (count($propertyStaticValues) > 1) {
34
                $result = Html::tag('h4', Yii::t('app', 'You have doubled slugs. Fix it please.'));
35
                $result .= Html::beginTag('ul');
36
                foreach ($propertyStaticValues as $propertyStaticValue) {
37
                    $property = Property::findById($propertyStaticValue->property_id);
38
                    if ($property !== null) {
39
                        $propertyGroup = PropertyGroup::findById($property->property_group_id);
40
                        $result .= Html::tag(
41
                            'li',
42
                            ($propertyGroup !== null ? Html::a(
43
                                $propertyGroup->name,
44
                                [
45
                                    '/backend/properties/group',
46
                                    'id' => $property->property_group_id,
47
                                ]
48
                            ) : '')
49
                            . ' > '
50
                            . Html::a(
51
                                $property->name,
52
                                [
53
                                    '/backend/properties/edit-property',
54
                                    'id' => $propertyStaticValue->property_id,
55
                                    'property_group_id' => $property->property_group_id,
56
                                ]
57
                            )
58
                            . ' > '
59
                            . Html::a(
60
                                $propertyStaticValue->name,
61
                                [
62
                                    '/backend/properties/edit-static-value',
63
                                    'id' => $propertyStaticValue->id,
64
                                    'property_id' => $propertyStaticValue->property_id,
65
                                    'property_group_id' => $property->property_group_id,
66
                                ]
67
                            )
68
                        );
69
                    }
70
                }
71
                $result .= Html::endTag('ul');
72
                Yii::$app->session->setFlash('warning', $result);
73
            }
74
        }
75
    }
76
77 View Code Duplication
    public function behaviors()
78
    {
79
        return [
80
            'access' => [
81
                'class' => AccessControl::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
82
                'rules' => [
83
                    [
84
                        'allow' => true,
85
                        'roles' => ['property manage'],
86
                    ],
87
                ],
88
            ],
89
        ];
90
    }
91
92
    public function actions()
93
    {
94
        return [
95
            'save-info' => [
96
                'class' => SaveInfoAction::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
97
            ],
98
            'addImage' => [
99
                'class' => AddImageAction::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
100
            ],
101
        ];
102
    }
103
104 View Code Duplication
    public function actionIndex()
105
    {
106
        $searchModel = new PropertyGroup();
107
        $dataProvider = $searchModel->search($_GET);
108
109
        return $this->render(
110
            'index',
111
            [
112
                'dataProvider' => $dataProvider,
113
                'searchModel' => $searchModel,
114
            ]
115
        );
116
    }
117
118
    public function actionGroup($id = null)
119
    {
120
        if ($id === null) {
121
            $model = new PropertyGroup();
122
        } else {
123
            $model = PropertyGroup::findById($id);
124
        }
125
126
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
127
            $save_result = $model->save();
128
            if ($save_result) {
129
                Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved'));
130
                $returnUrl = Yii::$app->request->get('returnUrl', ['/backend/properties/index']);
131
                switch (Yii::$app->request->post('action', 'save')) {
132
                    case 'next':
133
                        return $this->redirect(
134
                            [
135
                                '/backend/properties/group',
136
                                'returnUrl' => $returnUrl,
137
                            ]
138
                        );
139
                    case 'back':
140
                        return $this->redirect($returnUrl);
141
                    default:
142
                        return $this->redirect(
143
                            [
144
                                '/backend/properties/group',
145
                                'id' => $model->id,
146
                                'returnUrl' => $returnUrl,
147
                            ]
148
                        );
149
                }
150
            } else {
151
                Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot save data'));
152
            }
153
        }
154
155
        $searchModel = new Property();
156
        $searchModel->property_group_id = $model->id;
157
        $dataProvider = $searchModel->search($_GET);
158
159
160
        return $this->render(
161
            'group',
162
            [
163
                'model' => $model,
164
                'dataProvider' => $dataProvider,
165
                'searchModel' => $searchModel,
166
            ]
167
        );
168
    }
169
170
    /**
171
     * @param $value_type
172
     * @return string
173
     * @throws \Exception
174
     */
175
    private function getColumnType($value_type)
176
    {
177
        switch ($value_type) {
178
            case 'STRING':
179
                return 'TEXT';
180
            case 'NUMBER':
181
                return 'FLOAT';
182
            default:
183
                throw new \Exception('Unknown value type');
184
        }
185
    }
186
187
    public function actionEditProperty($property_group_id, $id = null)
188
    {
189
        if ($id === null) {
190
            $model = new Property();
191
            $model->handler_additional_params = '[]';
192
        } else {
193
            $model = Property::findById($id);
194
        }
195
        $object = BaseObject::getForClass(Property::className());
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
196
        $model->property_group_id = $property_group_id;
197
198
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
199
            $propertyHandler = PropertyHandlers::createHandler($model->handler);
0 ignored issues
show
Bug introduced by
The property handler does not seem to exist. Did you mean handlerAdditionalParams?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
200
            if (!$propertyHandler->changePropertyType($model)) {
201
                if ($model->is_column_type_stored) {
202
                    if ($model->isNewRecord) {
203
                        $col_type = $this->getColumnType($model->value_type);
204
                        $object = BaseObject::findById($model->group->object_id);
205
                        Yii::$app->db->createCommand()
206
                            ->addColumn($object->column_properties_table_name, $model->key, $col_type)
207
                            ->execute();
208
                        if ($object->object_class == Form::className()) {
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
209
                            $submissionObject = BaseObject::getForClass(Submission::className());
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
210
                            Yii::$app->db->createCommand()
211
                                ->addColumn($submissionObject->column_properties_table_name, $model->key, $col_type)
212
                                ->execute();
213
                        }
214
                    } else {
215 View Code Duplication
                        if ($model->key != $model->getOldAttribute('key')) {
216
                            $object = BaseObject::findById($model->group->object_id);
217
                            Yii::$app->db->createCommand()
218
                                ->renameColumn(
219
                                    $object->column_properties_table_name,
220
                                    $model->getOldAttribute('key'),
221
                                    $model->key
222
                                )->execute();
223
                            if ($object->object_class == Form::className()) {
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
224
                                $submissionObject = BaseObject::getForClass(Submission::className());
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
225
                                Yii::$app->db->createCommand()
226
                                    ->renameColumn(
227
                                        $submissionObject->column_properties_table_name,
228
                                        $model->getOldAttribute('key'),
229
                                        $model->key
230
                                    )->execute();
231
                            }
232
                        }
233 View Code Duplication
                        if ($model->value_type != $model->getOldAttribute('value_type')) {
234
                            $object = BaseObject::findById($model->group->object_id);
235
                            $new_type = $this->getColumnType($model->value_type);
236
                            Yii::$app->db->createCommand()
237
                                ->alterColumn(
238
                                    $object->column_properties_table_name,
239
                                    $model->getOldAttribute('key'),
240
                                    $new_type
241
                                )->execute();
242
                            if ($object->object_class == Form::className()) {
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
243
                                $submissionObject = BaseObject::getForClass(Submission::className());
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
244
                                Yii::$app->db->createCommand()
245
                                    ->renameColumn(
246
                                        $submissionObject->column_properties_table_name,
247
                                        $model->getOldAttribute('key'),
248
                                        $new_type
249
                                    )->execute();
250
                            }
251
                        }
252
                    }
253
                }
254
            }
255
256
            $save_result = $model->save();
257 View Code Duplication
            if ($save_result) {
258
                $this->runAction('save-info');
259
                Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved'));
260
                $returnUrl = Yii::$app->request->get(
261
                    'returnUrl',
262
                    [
263
                        '/backend/properties/group',
264
                        'id' => $property_group_id,
265
                    ]
266
                );
267
                switch (Yii::$app->request->post('action', 'save')) {
268
                    case 'next':
269
                        return $this->redirect(
270
                            [
271
                                '/backend/properties/edit-property',
272
                                'property_group_id' => $property_group_id,
273
                                'returnUrl' => $returnUrl,
274
                            ]
275
                        );
276
                    case 'back':
277
                        return $this->redirect($returnUrl);
278
                    default:
279
                        return $this->redirect(
280
                            Url::toRoute(
281
                                [
282
                                    '/backend/properties/edit-property',
283
                                    'id' => $model->id,
284
                                    'property_group_id' => $model->property_group_id,
285
                                    'returnUrl' => $returnUrl,
286
                                ]
287
                            )
288
                        );
289
                }
290
            } else {
291
                Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot save data'));
292
            }
293
        }
294
295
        $searchModel = new PropertyStaticValues();
296
297
        $searchModel->property_id = $model->id;
298
        $dataProvider = $searchModel->search($_GET);
299
300
        return $this->render(
301
            'edit-property',
302
            [
303
                'model' => $model,
304
                'dataProvider' => $dataProvider,
305
                'searchModel' => $searchModel,
306
                'fieldinterpretParentId' => 0,
307
                'object' => $object,
308
            ]
309
        );
310
    }
311
312
    public function actionEditStaticValue($property_id, $id = null)
313
    {
314
        if ($id === null) {
315
            $model = new PropertyStaticValues();
316
        } else {
317
            $model = PropertyStaticValues::findOne($id);
318
        }
319
        $object = BaseObject::getForClass(PropertyStaticValues::className());
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
320
        $model->property_id = $property_id;
321
        $post = \Yii::$app->request->post();
322
        if ($model->load($post) && $model->validate()) {
323
            $save_result = $model->save();
324
            if ($save_result) {
325
                $this->checkDoubledSlugs($model->slug);
326
                $this->runAction('save-info');
327
                Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved'));
328
                $returnUrl = Yii::$app->request->get(
329
                    'returnUrl',
330
                    [
331
                        '/backend/properties/edit-property',
332
                        'id' => $model->property_id,
333
                        'property_group_id' => $model->property->property_group_id,
334
                    ]
335
                );
336
                switch (Yii::$app->request->post('action', 'save')) {
337
                    case 'next':
338
                        return $this->redirect(
339
                            [
340
                                '/backend/properties/edit-static-value',
341
                                'property_id' => $model->property_id,
342
                                'returnUrl' => $returnUrl,
343
                            ]
344
                        );
345
                    case 'back':
346
                        return $this->redirect($returnUrl);
347
                    default:
348
                        return $this->redirect(
349
                            Url::toRoute(
350
                                [
351
                                    '/backend/properties/edit-static-value',
352
                                    'id' => $model->id,
353
                                    'property_id' => $model->property_id,
354
                                    'returnUrl' => $returnUrl,
355
                                ]
356
                            )
357
                        );
358
                }
359
            } else {
360
                Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot save data'));
361
            }
362
        }
363
        return $this->render(
364
            'edit-static-value',
365
            [
366
                'model' => $model,
367
                'object' => $object,
368
            ]
369
        );
370
    }
371
372
373
    public function actionAddStaticValue($key, $value, $returnUrl, $objectId = null, $objectModelId = null)
374
    {
375
        $model = new PropertyStaticValues();
376
        /** @var Property $property */
377
        $property = Property::findOne(['key'=>$key]);
378
        if (is_null($property)) {
379
            throw new NotFoundHttpException;
380
        }
381
        $model->property_id = $property->id;
382
        if (Yii::$app->request->isPost) {
383
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
384
                $this->checkDoubledSlugs($model->slug);
385
                $tags = [
386
                    ActiveRecordHelper::getCommonTag(Property::className()),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
387
                    ActiveRecordHelper::getObjectTag(Property::className(), $property->id),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
388
                    ActiveRecordHelper::getCommonTag(PropertyGroup::className()),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
389
                    ActiveRecordHelper::getObjectTag(PropertyGroup::className(), $property->property_group_id),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
390
                ];
391
                if (!is_null($objectId) && !is_null($objectModelId)) {
392
                    if ($property->multiple == 0) {
393
                        $propertyStaticValueIds = PropertyStaticValues::find()
394
                            ->select('id')
395
                            ->where(['property_id' => $property->id])
396
                            ->column();
397
                        ObjectStaticValues::deleteAll(
398
                            [
399
                                'object_id' => $objectId,
400
                                'object_model_id' => $objectModelId,
401
                                'property_static_value_id' => $propertyStaticValueIds,
402
                            ]
403
                        );
404
                    }
405
                    $objectStaticValues = new ObjectStaticValues;
406
                    $objectStaticValues->attributes = [
407
                        'object_id' => $objectId,
408
                        'object_model_id' => $objectModelId,
409
                        'property_static_value_id' => $model->id,
410
                    ];
411
                    $objectStaticValues->save();
412
                    $tags[] = ActiveRecordHelper::getCommonTag(BaseObject::findById($objectId)->object_class);
413
                    $tags[] = ActiveRecordHelper::getObjectTag(
414
                        BaseObject::findById($objectId)->object_class,
415
                        $objectModelId
416
                    );
417
                }
418
                TagDependency::invalidate(Yii::$app->cache, $tags);
419
                return $this->redirect($returnUrl);
420
            }
421
        } elseif ($value !== "") {
422
            $model->name = $value;
423
            $model->value = $value;
424
            $model->slug = Helper::createSlug($value);
425
            $model->sort_order = 0;
426
        }
427
        return $this->renderAjax('ajax-static-value', ['model' => $model]);
428
    }
429
430
431
    public function actionDeleteStaticValue($id, $property_id, $property_group_id)
432
    {
433
        /** @var PropertyStaticValues $model */
434
        $model = PropertyStaticValues::findOne($id);
435
        if (is_null($model)) {
436
            throw new NotFoundHttpException;
437
        }
438
        $model->delete();
439
        Yii::$app->session->setFlash('danger', Yii::t('app', 'Object removed'));
440
        return $this->redirect(
441
            Url::to(
442
                [
443
                    '/backend/properties/edit-property',
444
                    'id'=>$property_id,
445
                    'property_group_id'=>$property_group_id
446
                ]
447
            )
448
        );
449
    }
450
451 View Code Duplication
    public function actionDeleteProperty($id, $property_group_id)
452
    {
453
        /** @var Property $model */
454
        $model = Property::findOne($id);
455
        if (is_null($model)) {
456
            throw new NotFoundHttpException;
457
        }
458
        $model->delete();
459
        Yii::$app->session->setFlash('danger', Yii::t('app', 'Object removed'));
460
        return $this->redirect(
461
            Url::to(
462
                [
463
                    '/backend/properties/group',
464
                    'id'=>$property_group_id,
465
                ]
466
            )
467
        );
468
    }
469
470
    public function actionRemoveAllProperties($group_id)
471
    {
472
        $items = Yii::$app->request->post('items', []);
473
        if (!empty($items)) {
474
            $items = Property::find()->where(['in', 'id', $items])->all();
475
            foreach ($items as $item) {
476
                $item->delete();
477
            }
478
        }
479
        return $this->redirect(['group', 'id' => $group_id]);
480
    }
481
482 View Code Duplication
    public function actionDeleteGroup($id)
483
    {
484
        /** @var PropertyGroup $model */
485
        $model = PropertyGroup::findOne($id);
486
        if (is_null($model)) {
487
            throw new NotFoundHttpException;
488
        }
489
        $model->delete();
490
        Yii::$app->session->setFlash('danger', Yii::t('app', 'Object removed'));
491
        return $this->redirect(
492
            Url::to(
493
                [
494
                    '/backend/properties/index',
495
                ]
496
            )
497
        );
498
    }
499
500
    public function actionRemoveAllGroups()
501
    {
502
        $items = Yii::$app->request->post('items', []);
503
        if (!empty($items)) {
504
            $items = PropertyGroup::find()->where(['in', 'id', $items])->all();
505
            foreach ($items as $item) {
506
                $properties = Property::find()
507
                    ->where(['property_group_id' => $item->id])
508
                    ->all();
509
                foreach ($properties as $prop) {
510
                    $prop->delete();
511
                }
512
                $item->delete();
513
            }
514
        }
515
516
        return $this->redirect(['index']);
517
    }
518
519
    public function actionHandlers()
520
    {
521
        return $this->render('handlers');
522
    }
523
}
524