Menu::build()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
1
<?php namespace EvolutionCMS\Support;
2
3
/**
4
 * menu->Build('id','parent','name','link','alt','onclick','permission','target','divider 1/0','menuindex')
5
 */
6
7
use EvolutionCMS\Interfaces\MenuInterface;
8
9
class Menu implements MenuInterface
10
{
11
    /**
12
     * @var array
13
     */
14
    public $defaults = array();
15
    /**
16
     * @var
17
     */
18
    public $menu;
19
    /**
20
     * @var
21
     */
22
    public $output;
23
24
    /**
25
     * @param $menu
26
     * @param array $setting
27
     * @param bool $show
28
     */
29
    public function build($menu, $setting = array(), $show = true)
30
    {
31
        $this->defaults['outerClass'] = 'nav';
32
        $this->defaults['parentClass'] = 'dropdown';
33
        $this->defaults['parentLinkClass'] = 'dropdown-toggle';
34
        $this->defaults['parentLinkAttr'] = 'data-toggle="dropdown"';
35
        $this->defaults['parentLinkIn'] = '<b class="caret"></b>';
36
        $this->defaults['innerClass'] = 'subnav';
37
38
        $this->defaults = $setting + $this->defaults;
39
        $this->structurise($menu);
40
        $this->output = $this->drawSub('main', 0);
41
        if ($show) {
42
            echo $this->output;
43
        } else {
44
            return $this->output;
45
        }
46
    }
47
48
    /**
49
     * @param array $menu
50
     */
51
    public function structurise($menu)
52
    {
53
        $new = array();
54
        foreach ($menu as $key => $row) {
55
            $data[$key] = $row[9];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = 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...
56
        }
57
58
        array_multisort($data, SORT_ASC, $menu);
59
60
        foreach ($menu as $key => $value) {
61
            $new[$value[1]][] = $value;
62
        }
63
64
        $this->menu = $new;
65
    }
66
67
    /**
68
     * @param int $parentid
69
     * @param int $level
70
     * @return string
71
     */
72
    public function drawSub($parentid, $level)
73
    {
74
        $modx = evolutionCMS();
75
76
        $output = '';
77
78
        if (isset($this->menu[$parentid])) {
79
80
            $ph = array();
81
            $countChild = 0;
82
            $itemTpl = '
83
			<li id="[+id+]" class="[+li_class+]"><a href="[+href+]" alt="[+alt+]" target="[+target+]" onclick="[+onclick+]"[+a_class+] [+LinkAttr+]>[+itemName+]</a>[+DrawSub+]</li>';
84
            $outerTpl = '<ul id="[+id+]" class="[+class+]">[+output+]</ul>';
85
            foreach ($this->menu[$parentid] as $key => $value) {
86
                if ($value[6] !== '') {
87
                    $permissions = explode(',', $value[6]);
88
                    foreach ($permissions as $val) {
89
                        if (!$modx->hasPermission($val)) {
90
                            continue;
91
                        }
92
                    }
93
                }
94
95
                $countChild++;
96
                $id = $value[0];
97
                $ph['id'] = $id;
98
                $ph['li_class'] = $this->getItemClass($id) . $value[10];
99
                $ph['href'] = $value[3];
100
                $ph['alt'] = $value[4];
101
                $ph['target'] = $value[7];
102
                $ph['onclick'] = $value[5];
103
                $ph['a_class'] = $this->getLinkClass($id);
104
                $ph['LinkAttr'] = $this->getLinkAttr($id);
105
                $ph['itemName'] = $value[2] . $this->getItemName($id);
106
107
                $ph['DrawSub'] = '';
108
109
                if (isset($this->menu[$id])) {
110
                    $level++;
111
                    $ph['DrawSub'] = $this->drawSub($id, $level);
112
                    $level--;
113
                    // Optional buttons
114
                } else {
115
                    if (isset($value[11]) && !empty($value[11])) {
116
                        $optionalButton = '';
117
                        if (is_array($value[11])) {
118
                            foreach ($value[11] as $opt) {
119
                                $optionalButton .= sprintf('<%s href="%s" class="%s" onclick="%s" title="%s">%s</%s>',
120
                                    $opt[0], $opt[1], $opt[2], $opt[3], $opt[4], $opt[5], $opt[0]);
121
                            }
122
                        } else {
123
                            $opt = $value[11];
124
                            $optionalButton = sprintf('<%s href="%s" class="%s" onclick="%s" title="%s">%s</%s>',
125
                                $opt[0], $opt[1], $opt[2], $opt[3], $opt[4], $opt[5], $opt[0]);
126
                        }
127
                        $ph['DrawSub'] = $optionalButton;
128
                    }
129
                }
130
131
                $output .= $modx->parseText($itemTpl, $ph);
132
            }
133
134
            $ph = array();
135
            if ($countChild > 0) {
136
                $ph['id'] = $level == 0 ? $this->defaults['outerClass'] : '';
137
                $ph['class'] = $level == 0 ? $this->defaults['outerClass'] : $this->defaults['innerClass'];
138
                $ph['output'] = $output;
139
                $output = $modx->parseText($outerTpl, $ph);
140
            }
141
        }
142
143
        return $output;
144
    }
145
146
    /**
147
     * @param int $id
148
     * @return string
149
     */
150
    public function getLinkClass($id)
151
    {
152
        if (isset($this->menu[$id])) {
153
            return ' class="' . $this->defaults['parentLinkClass'] . '"';
154
        } else {
155
            return '';
156
        }
157
    }
158
159
    /**
160
     * @param int $id
161
     * @return string
162
     */
163
    public function getLinkAttr($id)
164
    {
165
        if (isset($this->menu[$id])) {
166
            return $this->defaults['parentLinkAttr'];
167
        } else {
168
            return '';
169
        }
170
    }
171
172
    /**
173
     * @param int $id
174
     * @return string
175
     */
176
    public function getItemClass($id)
177
    {
178
        if (isset($this->menu[$id])) {
179
            return $this->defaults['parentClass'] . ' ';
180
        } else {
181
            return '';
182
        }
183
    }
184
185
    /**
186
     * @param int $id
187
     * @return string
188
     */
189
    public function getItemName($id)
190
    {
191
        if (isset($this->menu[$id])) {
192
            return $this->defaults['parentLinkIn'];
193
        } else {
194
            return '';
195
        }
196
    }
197
}
198