Passed
Push — master ( 857f8e...183174 )
by Alexey
03:16
created

DefaultController::actionGetDemoData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 3
nop 0
1
<?php
2
3
namespace modules\main\controllers\backend;
4
5
use Yii;
6
use yii\web\Controller;
7
use yii\filters\AccessControl;
8
use yii\web\NotFoundHttpException;
9
use yii\web\Response;
10
use modules\users\models\User;
11
use modules\rbac\models\Permission;
12
use modules\main\Module;
13
use backend\widgets\chart\flot\data\Demo;
14
15
/**
16
 * Class DefaultController
17
 * @package modules\main\controllers\backend
18
 */
19
class DefaultController extends Controller
20
{
21
    /**
22
     * @inheritdoc
23
     * @return array
24
     */
25
    public function behaviors()
26
    {
27
        return [
28
            'access' => [
29
                'class' => AccessControl::class,
30
                'rules' => [
31
                    [
32
                        'actions' => ['index', 'get-demo-data'],
33
                        'allow' => true,
34
                        'roles' => [Permission::PERMISSION_VIEW_ADMIN_PAGE]
35
                    ]
36
                ]
37
            ]
38
        ];
39
    }
40
41
    /**
42
     * Displays homepage.
43
     * @return mixed|Response
44
     */
45
    public function actionIndex()
46
    {
47
        /** @var yii\web\User $user */
48
        $user = Yii::$app->user;
49
        if (!$user->can(Permission::PERMISSION_VIEW_ADMIN_PAGE)) {
50
            /** @var yii\web\Session $session */
51
            $session = Yii::$app->session;
52
            $session->setFlash('error', Module::translate('module', 'You are not allowed access!'));
53
            return $this->goHome();
54
        }
55
        //Greeting in the admin panel :)
56
        /** @var yii\web\Session $session */
57
        $session = Yii::$app->session;
58
        $key = 'msgHello';
59
        if ($session->get($key) !== 1) {
60
            /** @var User $identity */
61
            $identity = Yii::$app->user->identity;
62
            $session->setFlash('info', Module::translate('module', 'Welcome, {:username}!', [
63
                ':username' => $identity->username
64
            ]));
65
            $session->set($key, 1);
66
        }
67
        return $this->render('index');
68
    }
69
70
    /**
71
     * Get Demo Data
72
     *
73
     * @return array
74
     * @throws NotFoundHttpException
75
     */
76
    public function actionGetDemoData()
77
    {
78
        if (Yii::$app->request->isAjax) {
79
            Yii::$app->response->format = Response::FORMAT_JSON;
80
            $post = Yii::$app->request->post();
81
            $data = (!empty($post['data'])) ? $post['data'] : [];
82
            return [
83
                'result' => Demo::getRandomData($data),
84
            ];
85
        }
86
        throw new NotFoundHttpException('The requested page does not exist.');
87
    }
88
}
89