Completed
Push — master ( 8f6f38...60a482 )
by Michael
04:32 queued 02:16
created

MymenusBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
14
 * @license         http://www.gnu.org/licenses/gpl-2.0.html GNU Public License
15
 * @package         Mymenus
16
 * @since           1.0
17
 * @author          trabis <[email protected]>
18
 */
19
class MymenusBuilder
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
20
{
21
    public $parents = array();
22
    public $output  = array();
23
24
    /**
25
     * @param $array
26
     */
27
    public function __construct($array)
28
    {
29
        $this->addMenu($array);
30
    }
31
32
    /**
33
     * @param $array
34
     */
35
    public function addMenu($array)
36
    {
37
        foreach ($array as $item) {
38
            $this->add($item);
39
        }
40
    }
41
42
    /**
43
     * @param $item
44
     */
45
    public function add($item)
46
    {
47
        $this->parents[$item['pid']][] = $item;
48
    }
49
50
    /**
51
     * @param int $pid
52
     */
53
    public function buildMenus($pid = 0)
54
    {
55
        static $idx = -1;
56
        static $level = -1;
57
        ++$level;
58
        $first = true;
59
60
        foreach ($this->parents[$pid] as $item) {
61
            ++$idx;
62
63
            $this->output[$idx]['oul']    = false;
64
            $this->output[$idx]['oli']    = false;
65
            $this->output[$idx]['close']  = '';
66
            $this->output[$idx]['cul']    = false;
67
            $this->output[$idx]['cli']    = false;
68
            $this->output[$idx]['hassub'] = false;
69
            $this->output[$idx]['level']  = $level;
70
71
            if ($first) {
72
                $this->output[$idx]['oul'] = true;
73
                $first                     = false;
74
            }
75
76
            $this->output[$idx]['oli'] = true;
77
            $this->output[$idx]        = array_merge($item, $this->output[$idx]);
78
79
            if (isset($this->parents[$item['id']])) {
80
                $this->output[$idx]['hassub'] = true;
81
                $this->buildMenus($item['id']);
82
            }
83
            $this->output[$idx]['cli'] = true;
84
            $this->output[$idx]['close'] .= "</li>\n";
85
        }
86
        $this->output[$idx]['cul'] = true;
87
        $this->output[$idx]['close'] .= "</ul>\n";
88
        --$level;
89
    }
90
91
    /**
92
     * @param int $pid
93
     */
94
    public function buildUpDown($pid = 0)
95
    {
96
        static $idx = -1;
97
        $prevWeight = null;
98
        $up         = 0;
99
        $down       = 1;
100
        $counter    = 0;
101
        $count      = count($this->parents[$pid]);
102
103
        foreach ($this->parents[$pid] as $item) {
104
            ++$idx;
105
            $counter++;
106
            if ($counter == $count) {
107
                $down = 0;
108
            } // turn off down link for last entry
109
110
            if ($up) {
111
                $this->output[$idx]['up_weight'] = $prevWeight;
112
            }
113
            if ($down) {
114
                $this->output[$idx]['down_weight'] = $this->output[$idx]['weight'] + 2;
115
            }
116
117
            $prevWeight = $this->output[$idx]['weight'];
118
            $up         = 1; // turn on up link for all entries after first one
119
120
            if (isset($this->parents[$item['id']])) {
121
                $this->buildUpDown($item['id']);
122
            }
123
        }
124
    }
125
126
    public function buildSelected()
127
    {
128
        //get the currentpage
129
        $sel = array();
130
        //        $queryString = $_SERVER['QUERY_STRING'] ? '?' . $_SERVER['QUERY_STRING'] : '';
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
131
        $queryString = XoopsRequest::getString('QUERY_STRING', '', 'SERVER') ? '?'
132
                                                                               . XoopsRequest::getString('QUERY_STRING',
133
                                                                                                         '',
134
                                                                                                         'SERVER') : '';
135
        //        $self         = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . $queryString;
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
136
        $self = 'http://' . XoopsRequest::getString('HTTP_HOST', '', 'SERVER') . XoopsRequest::getString('PHP_SELF', '',
137
                                                                                                         'SERVER')
138
                . $queryString;
139
140
        //set a default page in case we don't get matches
141
        $default = XOOPS_URL . '/index.php';
142
143
        //get all matching links
144
        foreach ($this->output as $idx => $menu) {
145
            $selected = 0;
146
            if ($menu['link']) {
147
                $selected = (false !== stristr($self, $menu['link'])) ? 1 : $selected;
148
            }
149
            $selected = ($menu['link'] == $self) ? 1 : $selected;
150
            $selected = ($menu['link'] == $default) ? 1 : $selected;
151
            if ($selected) {
152
                $sel[$idx] = $menu;
153
            }
154
        }
155
156
        //From those links get only the longer one
157
        $longlink = '';
158
        $longidx  = 0;
159
        foreach ($sel as $idx => $menu) {
160
            if (strlen($menu['link']) > strlen($longlink)) {
161
                $longidx  = $idx;
162
                $longlink = $menu['link'];
163
            }
164
        }
165
166
        /*
167
         * When visiting site.com when XOOPS_URL is set to www.site.com
168
         * longidx is not detected, this IF will prevent blank page
169
         */
170
        if (isset($this->output[$longidx])) {
171
            $this->output[$longidx]['selected']    = true;
172
            $this->output[$longidx]['topselected'] = true;
173
174
            //Now turn all this menu parents to selected
175
            $this->addSelectedParents($this->output[$longidx]['pid']);
176
        }
177
    }
178
179
    /**
180
     * @param $pid
181
     */
182
    public function addSelectedParents($pid)
183
    {
184
        foreach ($this->output as $idx => $menu) {
185
            if ($menu['id'] == $pid) {
186
                $this->output[$idx]['selected'] = true;
187
                $this->addSelectedParents($menu['pid']);
188
            }
189
        }
190
    }
191
192
    /**
193
     * @return array
194
     */
195
    public function render()
196
    {
197
        $this->buildMenus();
198
        $this->buildUpDown();
199
        $this->buildSelected();
200
201
        return $this->output;
202
    }
203
}
204