Module::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 11
loc 11
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
/**
3
 * @link      http://www.writesdown.com/
4
 * @copyright Copyright (c) 2015 WritesDown
5
 * @license   http://www.writesdown.com/license/
6
 */
7
8
namespace modules\toolbar\frontend;
9
10
use common\models\PostType;
11
use common\models\search\Option;
12
use common\models\Taxonomy;
13
use Yii;
14
use yii\base\Application;
15
use yii\base\BootstrapInterface;
16
use yii\bootstrap\Nav;
17
use yii\bootstrap\NavBar;
18
use yii\helpers\ArrayHelper;
19
use yii\helpers\Html;
20
use yii\web\View;
21
22
/**
23
 * Class Module
24
 *
25
 * @author  Agiel K. Saputra <[email protected]>
26
 * @since   0.2.0
27
 */
28
class Module extends \yii\base\Module implements BootstrapInterface
29
{
30
    /**
31
     * @var string
32
     */
33
    public $controllerNamespace = false;
34
35
    /**
36
     * @inheritdoc
37
     */
38 View Code Duplication
    public function init()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        parent::init();
41
        if (!isset(Yii::$app->i18n->translations['toolbar'])) {
42
            Yii::$app->i18n->translations['toolbar'] = [
43
                'class'          => 'yii\i18n\PhpMessageSource',
44
                'sourceLanguage' => Yii::$app->language,
45
                'basePath'       => __DIR__ . '/../messages',
46
            ];
47
        }
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function bootstrap($app)
54
    {
55
        if (!$app->user->isGuest) {
56
            $app->on(Application::EVENT_BEFORE_REQUEST, function () use ($app) {
57
                $app->getView()->on(View::EVENT_END_BODY, [$this, 'renderToolbar']);
58
            });
59
        }
60
    }
61
62
    /**
63
     * Renders mini-toolbar at the end of page body.
64
     *
65
     * @param \yii\base\Event $event
66
     */
67
    public function renderToolbar($event)
68
    {
69
        /* @var $view View */
70
        /* @var $urlBack \yii\web\UrlManager */
71
72
        $urlBack = Yii::$app->urlManagerBack;
73
        $view = $event->sender;
74
        $view->registerCss($view->renderPhpFile(__DIR__ . '/assets/toolbar.min.css'));
75
        NavBar::begin([
76
            'id'                    => 'wd-frontend-toolbar',
77
            'brandLabel'            => Html::img('@web/img/logo-mini.png'),
78
            'brandUrl'              => $urlBack->baseUrl,
79
            'innerContainerOptions' => ['class' => 'container-fluid'],
80
            'options'               => ['class' => 'navbar navbar-inverse navbar-fixed-top'],
81
        ]);
82
        echo Nav::widget([
83
            'encodeLabels' => false,
84
            'options'      => ['class' => 'navbar-nav'],
85
            'items'        => [
86
                [
87
                    'label' => '<span aria-hidden="true" class="glyphicon glyphicon-dashboard"></span> '
88
                        . Option::get('sitetitle'),
89
                    'items' => [
90
                        ['label' => Yii::t('toolbar', 'Dashboard'), 'url' => $urlBack->baseUrl],
91
                        [
92
                            'label'   => Yii::t('toolbar', 'Themes'),
93
                            'url'     => $urlBack->createUrl(['/theme']),
94
                            'visible' => Yii::$app->user->can('administrator'),
95
                        ],
96
                        [
97
                            'label'   => Yii::t('toolbar', 'Menus'),
98
                            'url'     => $urlBack->createUrl(['/menu']),
99
                            'visible' => Yii::$app->user->can('administrator'),
100
                        ],
101
                        [
102
                            'label'   => Yii::t('toolbar', 'Modules'),
103
                            'url'     => $urlBack->createUrl(['/module']),
104
                            'visible' => Yii::$app->user->can('administrator'),
105
                        ],
106
                        [
107
                            'label'   => Yii::t('toolbar', 'Widgets'),
108
                            'url'     => $urlBack->createUrl(['/widget']),
109
                            'visible' => Yii::$app->user->can('administrator'),
110
                        ],
111
                    ],
112
                ],
113
                [
114
                    'label' => '<span aria-hidden="true" class="glyphicon glyphicon-plus"></span> '
115
                        . Yii::t('toolbar', 'New'),
116
                    'items' => $this->getAddNewMenu() ? $this->getAddNewMenu() : null,
117
                ],
118
            ],
119
        ]);
120
        echo Nav::widget([
121
            'encodeLabels' => false,
122
            'options'      => ['class' => 'navbar-nav navbar-right'],
123
            'items'        => [
124
                [
125
                    'label' => '<span aria-hidden="true" class="glyphicon glyphicon-user"></span> '
126
                        . Yii::$app->user->identity->username,
127
                    'items' => [
128
                        ['label' => 'Profile', 'url' => $urlBack->createUrl(['/user/profile'])],
129
                        [
130
                            'label'       => 'Logout',
131
                            'url'         => ['/site/logout'],
132
                            'linkOptions' => ['data-method' => 'post'],
133
                        ],
134
                    ],
135
                ],
136
            ],
137
        ]);
138
        NavBar::end();
139
    }
140
141
    /**
142
     * Get menu dropdown for post type.
143
     *
144
     * @return array
145
     */
146 View Code Duplication
    protected function getPostTypeMenu()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
    {
148
        /* @var $urlBack \yii\web\UrlManager */
149
        /* @var $postTypes \common\models\PostType[] */
150
151
        $urlBack = Yii::$app->urlManagerBack;
152
        $items = [];
153
        $postTypes = PostType::find()->select(['id', 'singular_name', 'permission'])->all();
154
155
        foreach ($postTypes as $postType) {
156
            $items[] = [
157
                'label'   => $postType->singular_name,
158
                'url'     => $urlBack->createUrl(['/post/create', 'type' => $postType->id]),
159
                'visible' => Yii::$app->user->can($postType->permission),
160
            ];
161
        }
162
163
        return $items;
164
    }
165
166
    /**
167
     * Get dropdown menu for taxonomy.
168
     *
169
     * @return array
170
     */
171 View Code Duplication
    protected function getTaxonomyMenu()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172
    {
173
        /* @var $urlBack \yii\web\UrlManager */
174
        /* @var $taxonomies \common\models\Taxonomy[] */
175
176
        $urlBack = Yii::$app->urlManagerBack;
177
        $items = [];
178
        $taxonomies = Taxonomy::find()->select(['id', 'singular_name'])->all();
179
180
        foreach ($taxonomies as $taxonomy) {
181
            $items[] = [
182
                'label'   => $taxonomy->singular_name,
183
                'url'     => $urlBack->createUrl(['/taxonomy/view', 'id' => $taxonomy->id]),
184
                'visible' => Yii::$app->user->can('editor'),
185
            ];
186
        }
187
188
        return $items;
189
    }
190
191
    /**
192
     * Get dropdown menu for add new.
193
     *
194
     * @return array
195
     */
196
    protected function getAddNewMenu()
197
    {
198
        /* @var $urlBack \yii\web\UrlManager */
199
200
        $urlBack = Yii::$app->urlManagerBack;
201
        $postTypeMenu = $this->getPostTypeMenu();
202
        $taxonomyMenu = $this->getTaxonomyMenu();
203
        $items = ArrayHelper::merge(
204
            $postTypeMenu,
205
            ['<li class="divider"></li>'],
206
            $taxonomyMenu,
207
            ['<li class="divider"></li>'],
208
            [
209
                [
210
                    'label'   => Yii::t('toolbar', 'Taxonomy'),
211
                    'url'     => $urlBack->createUrl('/taxonomy/create'),
212
                    'visible' => Yii::$app->user->can('administrator'),
213
                ],
214
                [
215
                    'label'   => Yii::t('toolbar', 'Post Type'),
216
                    'url'     => $urlBack->createUrl('/post-type/create'),
217
                    'visible' => Yii::$app->user->can('administrator'),
218
                ],
219
            ]
220
        );
221
222
        return $items;
223
    }
224
}
225