Completed
Push — master ( 079139...59379f )
by Mikołaj
02:42
created

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