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.
Passed
Push — master ( 922fc8...47a99e )
by
unknown
10:35
created

PropertiesController::checkDoubledSlugs()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 49
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 49
rs 8.5906
cc 6
eloc 34
nc 5
nop 1
1
<?php
2
3
namespace app\backend\controllers;
4
5
use app\components\Helper;
6
use app\models\Form;
7
use app\models\Object;
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(),
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(),
97
            ],
98
            'addImage' => [
99
                'class' => AddImageAction::className(),
100
            ],
101
        ];
102
    }
103
104 View Code Duplication
    public function actionIndex()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Coding Style introduced by
actionIndex uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Coding Style introduced by
actionGroup uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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 'TINYTEXT';
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)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Coding Style introduced by
actionEditProperty uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
188
    {
189
        if ($id === null) {
190
            $model = new Property();
191
            $model->handler_additional_params = '[]';
192
        } else {
193
            $model = Property::findById($id);
194
        }
195
        $object = Object::getForClass(Property::className());
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);
200
            if (!$propertyHandler->changePropertyType($model)) {
201
                if ($model->is_column_type_stored) {
202
                    if ($model->isNewRecord) {
203
                        $object = Object::findById($model->group->object_id);
204
                        Yii::$app->db->createCommand()
205
                            ->addColumn($object->column_properties_table_name, $model->key, "TINYTEXT")
206
                            ->execute();
207
                        if ($object->object_class == Form::className()) {
208
                            $submissionObject = Object::getForClass(Submission::className());
209
                            $col_type = $this->getColumnType($model->value_type);
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 = Object::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()) {
224
                                $submissionObject = Object::getForClass(Submission::className());
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 = Object::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()) {
243
                                $submissionObject = Object::getForClass(Submission::className());
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)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
313
    {
314
        if ($id === null) {
315
            $model = new PropertyStaticValues();
316
        } else {
317
            $model = PropertyStaticValues::findOne($id);
318
        }
319
        $object = Object::getForClass(PropertyStaticValues::className());
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)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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()),
387
                    ActiveRecordHelper::getObjectTag(Property::className(), $property->id),
388
                    ActiveRecordHelper::getCommonTag(PropertyGroup::className()),
389
                    ActiveRecordHelper::getObjectTag(PropertyGroup::className(), $property->property_group_id),
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(Object::findById($objectId)->object_class);
413
                    $tags[] = ActiveRecordHelper::getObjectTag(
414
                        Object::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)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
520
    {
521
        return $this->render('handlers');
522
    }
523
}
524