Completed
Push — master ( e4197d...f733ed )
by Igor
11:05 queued 09:33
created

NewsController::actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 12
cts 12
cp 1
rs 9.0303
c 0
b 0
f 0
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\web\Response;
7
use yii\filters\VerbFilter;
8
use yii\helpers\Url;
9
use app\traits\ModelTrait;
10
use app\models\News;
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 4
                'modelClass' => 'app\models\News',
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 4
                'modelClass' => 'app\models\News',
50 4
                'attributes' => ['status' => News::STATUS_ACTIVE],
51
            ],
52
            'set-unpublish' => [
53 4
                'class' => 'app\modules\admin\controllers\common\UpdateAttributesAction',
54 4
                'modelClass' => 'app\models\News',
55 4
                'attributes' => ['status' => News::STATUS_BLOCKED],
56
            ],
57
            'delete' => [
58
                'class' => 'app\modules\admin\controllers\common\DeleteAction',
59
                'modelClass' => 'app\models\News',
60
            ],
61
            'text-upload' => [
62
                'class' => 'rkit\filemanager\actions\UploadAction',
63
                'modelClass' => 'app\models\News',
64
                'attribute' => 'text',
65
                'inputName' => 'file',
66
                'resultFieldPath' => 'filelink',
67
            ],
68
            'preview-upload' => [
69
                'class'     => 'rkit\filemanager\actions\UploadAction',
70
                'modelClass' => 'app\models\News',
71
                'attribute' => 'preview',
72
                'inputName' => 'file',
73
            ],
74
            'gallery-upload' => [
75
                'class'     => 'rkit\filemanager\actions\UploadAction',
76
                'modelClass' => 'app\models\News',
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
    public function actionEdit($id = null)
97
    {
98
        $model = new News();
99
100
        if ($id) {
101
            $model = $this->findModel($model, $id);
102
        }
103
104
        if (Yii::$app->request->isPost) {
105
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
106
                Yii::$app->session->setFlash('success', Yii::t('app.messages', 'Saved successfully'));
107
                $urlToModel = Url::toRoute(['edit', 'id' => $model->id]);
108
                if (Yii::$app->request->isAjax) {
109
                    return $this->asJson(['redirect' => $urlToModel]);
110
                }
111
                return $this->redirect($urlToModel);
112
            }
113
            if (Yii::$app->request->isAjax) {
114
                return $this->asJson($this->collectErrors($model));
115
            }
116
        }
117
118
        return $this->render('edit', [
119
            'model' => $model,
120
        ]);
121
    }
122
}
123