Completed
Push — master ( c064eb...85769a )
by Igor
03:38
created

NewsController::actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 47
ccs 9
cts 9
cp 1
rs 9.0303
cc 1
eloc 36
nc 1
nop 0
crap 1
1
<?php
2
3
namespace app\modules\admin\controllers;
4
5
use Yii;
6
use yii\filters\VerbFilter;
7
use yii\helpers\Url;
8
use app\traits\ModelTrait;
9
use app\models\News;
10
use app\modules\admin\models\forms\NewsForm;
11
use app\modules\admin\models\search\NewsSearch;
12
13
class NewsController extends \yii\web\Controller
14
{
15
    use ModelTrait;
16
17 4
    public function behaviors()
18
    {
19
        return [
20 4
            'verbs' => [
21
                'class' => VerbFilter::class,
22
                'actions' => [
23
                    'publish' => ['post'],
24
                    'unpublish' => ['post'],
25
                    'delete' => ['post'],
26
                    'operations' => ['post'],
27
                    'text-upload' => ['post'],
28
                    'preview-upload' => ['post'],
29
                    'gallery-upload' => ['post'],
30
                ],
31
            ],
32
        ];
33
    }
34
35 4
    public function actions()
36
    {
37
        return [
38 4
            'operations' => [
39 4
                'class' => 'app\modules\admin\controllers\common\OperationsAction',
40
                'modelClass' => News::class,
41
                'operations' => [
42
                    'delete' => [],
43 4
                    'set-publish' => ['status' => News::STATUS_ACTIVE],
44 4
                    'set-unpublish' => ['status' => News::STATUS_BLOCKED]
45
                ]
46
            ],
47
            'set-publish' => [
48 4
                'class' => 'app\modules\admin\controllers\common\UpdateAttributesAction',
49
                'modelClass' => News::class,
50 4
                'attributes' => ['status' => News::STATUS_ACTIVE],
51
            ],
52
            'set-unpublish' => [
53 4
                'class' => 'app\modules\admin\controllers\common\UpdateAttributesAction',
54
                'modelClass' => News::class,
55 4
                'attributes' => ['status' => News::STATUS_BLOCKED],
56
            ],
57
            'delete' => [
58
                'class' => 'app\modules\admin\controllers\common\DeleteAction',
59
                'modelClass' => News::class,
60
            ],
61
            'text-upload' => [
62
                'class' => 'rkit\filemanager\actions\UploadAction',
63
                'modelClass' => News::class,
64
                'attribute' => 'text',
65
                'inputName' => 'file',
66
                'resultFieldPath' => 'filelink',
67
            ],
68
            'preview-upload' => [
69
                'class' => 'rkit\filemanager\actions\UploadAction',
70
                'modelClass' => News::class,
71
                'attribute' => 'preview',
72
                'inputName' => 'file',
73
            ],
74
            'gallery-upload' => [
75
                'class' => 'rkit\filemanager\actions\UploadAction',
76
                'modelClass' => News::class,
77
                'attribute' => 'gallery',
78
                'inputName' => 'file',
79
            ],
80
        ];
81
    }
82
83 4
    public function actionIndex()
84
    {
85 4
        $newsSearch = new NewsSearch();
86 4
        $dataProvider = $newsSearch->search(Yii::$app->request->get());
87 4
        $statuses = News::getStatuses();
88
89 4
        return $this->render('index', [
90 4
            'newsSearch' => $newsSearch,
91 4
            'dataProvider' => $dataProvider,
92 4
            'statuses' => $statuses,
93
        ]);
94
    }
95
96 3
    public function actionEdit($id = null)
97
    {
98 3
        $model = new NewsForm();
99
100 3
        if ($id) {
101 2
            $model->setModel($this->findModel(new News, $id));
0 ignored issues
show
Compatibility introduced by
$this->findModel(new \app\models\News(), $id) of type object<yii\db\ActiveRecord> is not a sub-type of object<app\models\News>. It seems like you assume a child class of the class yii\db\ActiveRecord to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
102
        }
103
104 2
        if (Yii::$app->request->isPost) {
105
            $data = Yii::$app->request->post();
106
            $data['NewsForm'] = $data['NewsForm'] + $data['News'];
107
108
            if ($model->load($data) && $model->validate()) {
109
                try {
110
                    $model->save();
111
112
                    Yii::$app->session->setFlash('success', Yii::t('app.msg', 'Saved successfully'));
113
                    return $this->asJson(['redirect' => Url::toRoute(['edit', 'id' => $model->id])]);
114
                } catch (\Exception $e) {
115
                    Yii::error($e);
116
                    return $this->asJson(false);
117
                }
118
            }
119
            return $this->asJson($this->collectErrors($model));
120
        }
121
122 2
        return $this->render('edit', [
123 2
            'model' => $model,
124
        ]);
125
    }
126
}
127