Completed
Push — master ( cceadc...3df73a )
by Mikołaj
04:41
created

AdminView::getUserId()   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
    protected function getUserId()
173
    {
174
        return (int) self::$userInfo['id'];
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    protected function getUserName()
181
    {
182
        return self::$userInfo['first_name'];
183
    }
184
185
    /**
186
     * @return string
187
     */
188
    protected function getUserFullName()
189
    {
190
        return self::$userInfo['first_name'].' '.self::$userInfo['surname'];
191
    }
192
193
    /**
194
     * @return string
195
     */
196
    protected function getUserEmail()
197
    {
198
        return self::$userInfo['email'];
199
    }
200
201
    /**
202
     * @return string
203
     */
204
    protected function getUserNick()
205
    {
206
        return self::$userInfo['nick'];
207
    }
208
209
    /**
210
     * @return string
211
     */
212
    public function getUserRegisterDate()
213
    {
214
        return self::$userInfo['dt'];
215
    }
216
217
    /**
218
     * Get all alerts.
219
     *
220
     * @param array $classes
221
     *
222
     * @return string
223
     */
224
    protected function alerts(array $classes = [])
225
    {
226
        if (!$this->isAlerts()) {
227
            return false;
228
        }
229
230
        $classes = array_merge([
231
            'error' => 'danger',
232
            'warning' => 'warning',
233
            'success' => 'success',
234
            'info' => 'info',
235
        ], $classes);
236
237
        $a = $this->alertsCollection->getAll();
238
239
        $html = [];
240
241
        foreach ($a as $key => $alert) {
242
            $html[] = $this->alert($alert->getType(), $alert->getMessage(), $classes);
243
        }
244
245
        return implode("\n", $html);
246
    }
247
248
    /**
249
     * @return bool
250
     */
251
    protected function isAlerts()
252
    {
253
        if ($this->alertsCollection->isAlerts()) {
254
            return true;
255
        }
256
257
        return false;
258
    }
259
260
    /**
261
     * Get code for one alert.
262
     *
263
     * @param string $type
264
     * @param string $message
265
     * @param array  $classes
266
     *
267
     * @return string
268
     */
269
    protected function alert($type, $message, array $classes = [])
270
    {
271
        $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...
272
        $html[] = '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
273
274
        switch ($type) {
275
            case 'warning':
276
                $title = _('Warning');
277
                break;
278
            case 'success':
279
                $title = _('Success');
280
                break;
281
            case 'info':
282
                $title = _('Info');
283
                break;
284
285
            case 'error':
286
            default:
287
                $title = _('Error');
288
                break;
289
        }
290
        $html[] = sprintf('<strong>%1$s!</strong> %2$s', $title, $message);
291
        $html[] = '</div>';
292
293
        return implode('', $html);
294
    }
295
296
    public function render($side = 'admin', $type = 'html')
297
    {
298
        parent::render($side, $type);
299
    }
300
}
301