Completed
Push — master ( cb854d...e1c233 )
by Igor
04:58
created

BaseController::loadModel()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 14
rs 8.8571
cc 5
eloc 8
nc 4
nop 3
1
<?php
2
3
namespace app\components;
4
5
use Yii;
6
use yii\web\Controller;
7
use yii\helpers\ArrayHelper;
8
9
/**
10
 * Global base controller
11
 */
12
class BaseController extends Controller
13
{
14
    /**
15
     * @var string Path to js bundle
16
     */
17
    public $jsBundle = 'front.js';
18
    /**
19
     * @var string Path to css bundle
20
     */
21
    public $cssBundle = 'front.css';
22
23
    /**
24
     * @return string Return CSS bundle
25
     */
26
    public function getCssBundle()
27
    {
28
        return '/assets/' . ArrayHelper::getValue(Yii::$app->params['assets'], $this->cssBundle);
29
    }
30
31
    /**
32
     * @return string Return JS bundle
33
     */
34
    public function getJsBundle()
35
    {
36
        return '/assets/' . ArrayHelper::getValue(Yii::$app->params['assets'], $this->jsBundle);
37
    }
38
39
    /**
40
     * Load the model based on its primary key value or WHERE condition.
41
     * If the model is not found or access denied, a 404 HTTP exception will be thrown.
42
     *
43
     * @param ActiveRecord $model
44
     * @param int|array $id primary key or WHERE condition
45
     * @param bool $ownerCheck
46
     * @return ActiveRecord
47
     * @throws HttpException
48
     */
49
    public function loadModel($model, $id, $ownerCheck = false)
50
    {
51
        if (is_array($id)) {
52
            $model = $model::find()->where($id)->one();
53
        } else {
54
            $model = $model::findOne($id);
55
        }
56
57
        if ($model === null || ($ownerCheck && !$model->isOwner())) {
58
            return $this->pageNotFound();
59
        }
60
61
        return $model;
62
    }
63
64
    /**
65
     * Show alert message
66
     *
67
     * @param string $type success|error
68
     * @param string $message
69
     * @param string $url If null go Home
70
     */
71
    public function alert($type, $message, $url = null)
72
    {
73
        Yii::$app->getSession()->setFlash($type, $message);
74
        return $url ? $this->redirect($url) : $this->goHome();
75
    }
76
77
    /**
78
     * Response
79
     *
80
     * @param mixed $data
81
     * @param string $format The response format
82
     * @see http://www.yiiframework.com/doc-2.0/yii-web-response.html
83
     */
84
    public function response($data, $format = \yii\web\Response::FORMAT_JSON)
85
    {
86
        \Yii::$app->response->format = $format;
87
        return $data;
88
    }
89
90
    /**
91
     * Triggers a 404 (Page Not Found) error
92
     *
93
     * @param string $msg
94
     * @throws HttpException when invoked
95
     */
96
    public function pageNotFound($msg = null)
97
    {
98
        throw new \yii\web\HttpException(404, $msg ? $msg : Yii::t('app', 'Page not found'));
99
    }
100
101
    /**
102
     * Triggers a 403 (Access Denied) error
103
     *
104
     * @param string $msg
105
     * @throws HttpException when invoked
106
     */
107
    public function accessDenied($msg = null)
108
    {
109
        throw new \yii\web\HttpException(403, $msg ? $msg : Yii::t('app', 'Access Denied'));
110
    }
111
112
    /**
113
     * Triggers a 400 (Bad Request) error
114
     *
115
     * @param string $msg
116
     * @throws HttpException when invoked
117
     */
118
    public function badRequest($msg = null)
119
    {
120
        throw new \yii\web\HttpException(400, $msg ? $msg : Yii::t('app', 'Bad request'));
121
    }
122
}
123