Completed
Push — master ( e1c233...726cec )
by Igor
07:28
created

CountriesController::actionIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
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\components\BaseController;
9
use app\models\Country;
10
use app\modules\admin\models\search\CountrySearch;
11
12
class CountriesController extends BaseController
13
{
14
    public function behaviors()
15
    {
16
        return [
17
            'verbs' => [
18
                'class' => VerbFilter::className(),
19
                'actions' => [
20
                    'delete' => ['post'],
21
                    'operations' => ['post'],
22
                    'autocomplete' => ['post'],
23
                ],
24
            ],
25
        ];
26
    }
27
28
    public function actions()
29
    {
30
        return [
31
            'operations' => [
32
                'class' => 'app\modules\admin\controllers\common\OperationsAction',
33
                'modelName' => 'app\models\Country',
34
                'operations' => [
35
                    'delete' => [],
36
                ]
37
            ],
38
            'delete' => [
39
                'class' => 'app\modules\admin\controllers\common\DeleteAction',
40
                'modelName' => 'app\models\Country',
41
            ],
42
        ];
43
    }
44
45
    public function actionIndex()
46
    {
47
        $countrySearch = new CountrySearch();
48
        $dataProvider = $countrySearch->search(Yii::$app->request->get());
49
50
        return $this->render('index', [
51
            'countrySearch' => $countrySearch,
52
            'dataProvider' => $dataProvider,
53
        ]);
54
    }
55
56
    public function actionEdit($id = null)
57
    {
58
        $model = new Country();
59
60
        if ($id) {
61
            $model = $this->loadModel($model, $id);
62
        }
63
64
        if (Yii::$app->request->isPost) {
65
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
66
                Yii::$app->session->setFlash('success', Yii::t('app', 'Saved successfully'));
67
                if (Yii::$app->request->isAjax) {
68
                    return $this->response(['redirect' => Url::toRoute(['edit', 'id' => $model->country_id])]);
69
                }
70
            } else {
71
                if (Yii::$app->request->isAjax) {
72
                    return $this->response(\app\helpers\Util::getValidationErrors($model));
0 ignored issues
show
Bug introduced by
The method getValidationErrors() does not seem to exist on object<app\helpers\Util>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
                }
74
            }
75
        }
76
77
        return $this->render('edit', ['model' => $model]);
78
    }
79
80
    public function actionAutocomplete()
81
    {
82
        $result = [];
83
        if (($term = Yii::$app->request->post('term')) !== null) {
84
            $data = Country::find()
85
                ->like($term, 'title')
86
                ->asArray()
87
                ->limit(10)
88
                ->all();
89
90
            foreach ($data as $item) {
91
                $result[] = [
92
                    'text' => $item['title'],
93
                    'id' => $item['country_id']
94
                ];
95
            }
96
        }
97
        return $this->response($result);
98
    }
99
}
100