|
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 XOOPS Project (https://xoops.org) |
|
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
|
|
|
|
|
20
|
|
|
use Xmf\Request; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class MymenusBuilder |
|
24
|
|
|
*/ |
|
25
|
|
|
class MymenusBuilder |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
public $parents = array(); |
|
28
|
|
|
public $output = array(); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param $array |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct($array) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->addMenu($array); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param $array |
|
40
|
|
|
*/ |
|
41
|
|
|
public function addMenu($array) |
|
42
|
|
|
{ |
|
43
|
|
|
foreach ($array as $item) { |
|
44
|
|
|
$this->add($item); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param $item |
|
50
|
|
|
*/ |
|
51
|
|
|
public function add($item) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->parents[$item['pid']][] = $item; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param int $pid |
|
58
|
|
|
*/ |
|
59
|
|
|
public function buildMenus($pid = 0) |
|
60
|
|
|
{ |
|
61
|
|
|
static $idx = -1; |
|
62
|
|
|
static $level = -1; |
|
63
|
|
|
++$level; |
|
64
|
|
|
$first = true; |
|
65
|
|
|
|
|
66
|
|
|
foreach ($this->parents[$pid] as $item) { |
|
67
|
|
|
++$idx; |
|
68
|
|
|
|
|
69
|
|
|
$this->output[$idx]['oul'] = false; |
|
70
|
|
|
$this->output[$idx]['oli'] = false; |
|
71
|
|
|
$this->output[$idx]['close'] = ''; |
|
72
|
|
|
$this->output[$idx]['cul'] = false; |
|
73
|
|
|
$this->output[$idx]['cli'] = false; |
|
74
|
|
|
$this->output[$idx]['hassub'] = false; |
|
75
|
|
|
$this->output[$idx]['level'] = $level; |
|
76
|
|
|
|
|
77
|
|
|
if ($first) { |
|
78
|
|
|
$this->output[$idx]['oul'] = true; |
|
79
|
|
|
$first = false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$this->output[$idx]['oli'] = true; |
|
83
|
|
|
$this->output[$idx] = array_merge($item, $this->output[$idx]); |
|
84
|
|
|
|
|
85
|
|
|
if (isset($this->parents[$item['id']])) { |
|
86
|
|
|
$this->output[$idx]['hassub'] = true; |
|
87
|
|
|
$this->buildMenus($item['id']); |
|
88
|
|
|
} |
|
89
|
|
|
$this->output[$idx]['cli'] = true; |
|
90
|
|
|
$this->output[$idx]['close'] .= "</li>\n"; |
|
91
|
|
|
} |
|
92
|
|
|
$this->output[$idx]['cul'] = true; |
|
93
|
|
|
$this->output[$idx]['close'] .= "</ul>\n"; |
|
94
|
|
|
--$level; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param int $pid |
|
99
|
|
|
*/ |
|
100
|
|
|
public function buildUpDown($pid = 0) |
|
101
|
|
|
{ |
|
102
|
|
|
static $idx = -1; |
|
103
|
|
|
$prevWeight = null; |
|
104
|
|
|
$up = 0; |
|
105
|
|
|
$down = 1; |
|
106
|
|
|
$counter = 0; |
|
107
|
|
|
$count = count($this->parents[$pid]); |
|
108
|
|
|
|
|
109
|
|
|
foreach ($this->parents[$pid] as $item) { |
|
110
|
|
|
++$idx; |
|
111
|
|
|
$counter++; |
|
112
|
|
|
if ($counter == $count) { |
|
113
|
|
|
$down = 0; |
|
114
|
|
|
} // turn off down link for last entry |
|
115
|
|
|
|
|
116
|
|
|
if ($up) { |
|
117
|
|
|
$this->output[$idx]['up_weight'] = $prevWeight; |
|
118
|
|
|
} |
|
119
|
|
|
if ($down) { |
|
120
|
|
|
$this->output[$idx]['down_weight'] = $this->output[$idx]['weight'] + 2; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$prevWeight = $this->output[$idx]['weight']; |
|
124
|
|
|
$up = 1; // turn on up link for all entries after first one |
|
125
|
|
|
|
|
126
|
|
|
if (isset($this->parents[$item['id']])) { |
|
127
|
|
|
$this->buildUpDown($item['id']); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function buildSelected() |
|
133
|
|
|
{ |
|
134
|
|
|
//get the currentpage |
|
135
|
|
|
$sel = array(); |
|
136
|
|
|
// $queryString = $_SERVER['QUERY_STRING'] ? '?' . $_SERVER['QUERY_STRING'] : ''; |
|
|
|
|
|
|
137
|
|
|
$queryString = Request::getString('QUERY_STRING', '', 'SERVER') ? '?' . Request::getString('QUERY_STRING', '', 'SERVER') : ''; |
|
138
|
|
|
// $self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . $queryString; |
|
|
|
|
|
|
139
|
|
|
$self = 'http://' . Request::getString('HTTP_HOST', '', 'SERVER') . Request::getString('PHP_SELF', '', 'SERVER') . $queryString; |
|
140
|
|
|
|
|
141
|
|
|
//set a default page in case we don't get matches |
|
142
|
|
|
$default = XOOPS_URL . '/index.php'; |
|
143
|
|
|
|
|
144
|
|
|
//get all matching links |
|
145
|
|
|
foreach ($this->output as $idx => $menu) { |
|
146
|
|
|
$selected = 0; |
|
147
|
|
|
if ($menu['link']) { |
|
148
|
|
|
$selected = (false !== stristr($self, $menu['link'])) ? 1 : $selected; |
|
149
|
|
|
} |
|
150
|
|
|
$selected = ($menu['link'] == $self) ? 1 : $selected; |
|
151
|
|
|
$selected = ($menu['link'] == $default) ? 1 : $selected; |
|
152
|
|
|
if ($selected) { |
|
153
|
|
|
$sel[$idx] = $menu; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
//From those links get only the longer one |
|
158
|
|
|
$longlink = ''; |
|
159
|
|
|
$longidx = 0; |
|
160
|
|
|
foreach ($sel as $idx => $menu) { |
|
161
|
|
|
if (strlen($menu['link']) > strlen($longlink)) { |
|
162
|
|
|
$longidx = $idx; |
|
163
|
|
|
$longlink = $menu['link']; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/* |
|
168
|
|
|
* When visiting site.com when XOOPS_URL is set to www.site.com |
|
169
|
|
|
* longidx is not detected, this IF will prevent blank page |
|
170
|
|
|
*/ |
|
171
|
|
|
if (isset($this->output[$longidx])) { |
|
172
|
|
|
$this->output[$longidx]['selected'] = true; |
|
173
|
|
|
$this->output[$longidx]['topselected'] = true; |
|
174
|
|
|
|
|
175
|
|
|
//Now turn all this menu parents to selected |
|
176
|
|
|
$this->addSelectedParents($this->output[$longidx]['pid']); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param $pid |
|
182
|
|
|
*/ |
|
183
|
|
|
public function addSelectedParents($pid) |
|
184
|
|
|
{ |
|
185
|
|
|
foreach ($this->output as $idx => $menu) { |
|
186
|
|
|
if ($menu['id'] == $pid) { |
|
187
|
|
|
$this->output[$idx]['selected'] = true; |
|
188
|
|
|
$this->addSelectedParents($menu['pid']); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return array |
|
195
|
|
|
*/ |
|
196
|
|
|
public function render() |
|
197
|
|
|
{ |
|
198
|
|
|
$this->buildMenus(); |
|
199
|
|
|
$this->buildUpDown(); |
|
200
|
|
|
$this->buildSelected(); |
|
201
|
|
|
|
|
202
|
|
|
return $this->output; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.