Completed
Push — master ( 01cdd8...cceadc )
by Mikołaj
02:40
created

AdminView::getMenuItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Rudolf\Framework\View;
4
5
use Rudolf\Component\Alerts\AlertsCollection;
6
use Rudolf\Component\Forms\AdminFields;
7
use Rudolf\Component\Helpers\Navigation\MenuItemCollection;
8
use Rudolf\Component\Html\Breadcrumbs;
9
use Rudolf\Component\Html\Navigation;
10
use Rudolf\Component\Modules\Module;
11
12
class AdminView extends BaseView
13
{
14
    /**
15
     * @var array
16
     */
17
    private static $userInfo;
18
19
    /**
20
     * @var MenuItemCollection
21
     */
22
    private static $menuItemsCollection;
23
24
    /**
25
     * @var string
26
     */
27
    protected static $request;
28
29
    /**
30
     * @var array
31
     */
32
    private $config;
33
34
    /**
35
     * @var AlertsCollection
36
     */
37
    private $alertsCollection;
38
39
    /**
40
     * @var AdminFields
41
     */
42
    protected $adminFields;
43
44
    /**
45
     * @var string
46
     */
47
    protected $templateType;
48
49
    public function init()
50
    {
51
        $this->config = (new Module('dashboard'))->getConfig();
52
        $this->domPlugins->admin();
53
        $this->alertsCollection = new AlertsCollection();
54
        $this->adminFields = new AdminFields();
55
    }
56
57
    /**
58
     * @param MenuItemCollection $collection
59
     * @param string $request
60
     */
61
    public static function setAdminData(MenuItemCollection $collection, $request)
62
    {
63
        self::$menuItemsCollection = $collection;
64
        self::$request = $request;
65
    }
66
67
    /**
68
     * Create page nav.
69
     *
70
     * @param string $type
71
     * @param int    $nesting
72
     * @param array  $classes
73
     * @param array  $before
74
     * @param array  $after
75
     * @param array  $config
76
     *
77
     * @return string
78
     */
79
    public function pageNav($type, $nesting, $classes, array $before = [], array $after = [], array $config = [])
80
    {
81
        $nav = new Navigation();
82
        $nav->setType($type);
83
        $nav->setItems($this->getMenuItems());
84
        $nav->setCurrent(self::$request);
85
        $nav->setClasses($classes);
86
        $nav->setNesting($nesting);
87
        $nav->setBefore($before);
88
        $nav->setAfter($after);
89
        $nav->setConfig($config);
90
91
        return $nav->create();
92
    }
93
94
    /**
95
     * @param int $nesting
96
     * @param array $classes
97
     * @return bool|string
98
     */
99
    public function breadcrumb($nesting = 0, array $classes = [])
100
    {
101
        $breadcrumbs = new Breadcrumbs();
102
        $breadcrumbs->setElements($this->getBreadcrumbElements());
103
        $breadcrumbs->setAddress(explode('/', trim(self::$request, '/')));
104
        $breadcrumbs->setClasses($classes);
105
        $breadcrumbs->setNesting($nesting);
106
107
        return $breadcrumbs->create($withStart = false);
108
    }
109
110
    /**
111
     * @return array
112
     */
113
    private function getBreadcrumbElements()
114
    {
115
        $array = [];
116
        foreach (self::$menuItemsCollection->getAll() as $key => $value) {
117
            $slug = explode('/', trim($value->getSlug(), '/'));
118
            $slug = end($slug);
119
            $parentId = $value->getParentId();
120
            $array[$slug][$parentId] = array(
121
                'id' => $value->getId(),
122
                'parent_id' => $parentId,
123
                'slug' => $slug,
124
                'title' => $value->getTitle(),
125
            );
126
        }
127
        $array['admin'][0] = array_merge(
128
            end($array['dashboard']),
129
            ['id' => 0, 'slug' => 'admin', 'parent_id' => 0, 'title' => 'Admin']
130
        );
131
        $overview = end($array['overview']);
132
        $array['overview'][0] = array_merge(
133
            $overview,
134
            ['parent_id' => 0]
135
        );
136
        $array['dashboard'][0] = [
137
            'title' => _('Dashboard'),
138
            'id' => 0,
139
            'parent_id' => 0,
140
            'slug' => 'dashboard',
141
        ];
142
143
        return $array;
144
    }
145
146
    protected function getMenuItems()
147
    {
148
        return self::$menuItemsCollection;
149
    }
150
151
    /**
152
     * @return string
153
     */
154
    protected function pageTitle()
155
    {
156
        return $this->pageTitle;
157
    }
158
159
    /**
160
     * @return string
161
     */
162
    public function adminDir()
163
    {
164
        return DIR.'/'.$this->config['admin_path'];
165
    }
166
167
    public static function setUserInfo($userInfo)
168
    {
169
        self::$userInfo = $userInfo;
170
    }
171
172
    /**
173
     * @return string
174
     */
175
    protected function getUserName()
176
    {
177
        return self::$userInfo['first_name'];
178
    }
179
180
    /**
181
     * @return string
182
     */
183
    protected function getUserFullName()
184
    {
185
        return self::$userInfo['first_name'].' '.self::$userInfo['surname'];
186
    }
187
188
    /**
189
     * @return string
190
     */
191
    protected function getUserEmail()
192
    {
193
        return self::$userInfo['email'];
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    protected function getUserNick()
200
    {
201
        return self::$userInfo['nick'];
202
    }
203
204
    /**
205
     * @return string
206
     */
207
    public function getUserRegisterDate()
208
    {
209
        return self::$userInfo['dt'];
210
    }
211
212
    /**
213
     * Get all alerts.
214
     *
215
     * @param array $classes
216
     *
217
     * @return string
218
     */
219
    protected function alerts(array $classes = [])
220
    {
221
        if (!$this->isAlerts()) {
222
            return false;
223
        }
224
225
        $classes = array_merge([
226
            'error' => 'danger',
227
            'warning' => 'warning',
228
            'success' => 'success',
229
            'info' => 'info',
230
        ], $classes);
231
232
        $a = $this->alertsCollection->getAll();
233
234
        $html = [];
235
236
        foreach ($a as $key => $alert) {
237
            $html[] = $this->alert($alert->getType(), $alert->getMessage(), $classes);
238
        }
239
240
        return implode("\n", $html);
241
    }
242
243
    /**
244
     * @return bool
245
     */
246
    protected function isAlerts()
247
    {
248
        if ($this->alertsCollection->isAlerts()) {
249
            return true;
250
        }
251
252
        return false;
253
    }
254
255
    /**
256
     * Get code for one alert.
257
     *
258
     * @param string $type
259
     * @param string $message
260
     * @param array  $classes
261
     *
262
     * @return string
263
     */
264
    protected function alert($type, $message, array $classes = [])
265
    {
266
        $html[] = sprintf('<div class="alert alert-%1$s %1$s">', $classes[$type]);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$html was never initialized. Although not strictly required by PHP, it is generally a good practice to add $html = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
267
        $html[] = '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
268
269
        switch ($type) {
270
            case 'warning':
271
                $title = _('Warning');
272
                break;
273
            case 'success':
274
                $title = _('Success');
275
                break;
276
            case 'info':
277
                $title = _('Info');
278
                break;
279
280
            case 'error':
281
            default:
282
                $title = _('Error');
283
                break;
284
        }
285
        $html[] = sprintf('<strong>%1$s!</strong> %2$s', $title, $message);
286
        $html[] = '</div>';
287
288
        return implode('', $html);
289
    }
290
291
    public function render($side = 'admin', $type = 'html')
292
    {
293
        parent::render($side, $type);
294
    }
295
}
296