1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: 13: 14: 15: 16: 17: 18: 19:
20: class MenusBuilder
21: {
22: 23: 24:
25: protected $parents = array();
26:
27: 28: 29:
30: protected $output = array();
31:
32: 33: 34:
35: public function __construct($array)
36: {
37: $this->addMenu($array);
38: }
39:
40: 41: 42:
43: public function addMenu($array)
44: {
45: foreach ($array as $item) {
46: $this->add($item);
47: }
48: }
49:
50: 51: 52:
53: public function add($item)
54: {
55: $this->parents[$item['pid']][] = $item;
56: }
57:
58: 59: 60:
61: public function buildMenus($pid = 0)
62: {
63: static $idx = -1;
64: static $level = -1;
65: $level += 1;
66: $first = true;
67:
68: foreach ($this->parents[$pid] as $item) {
69: $idx += 1;
70:
71: $this->output[$idx]['oul'] = false;
72: $this->output[$idx]['oli'] = false;
73: $this->output[$idx]['close'] = '';
74: $this->output[$idx]['cul'] = false;
75: $this->output[$idx]['cli'] = false;
76: $this->output[$idx]['hassub'] = false;
77: $this->output[$idx]['level'] = $level;
78:
79: if ($first) {
80: $this->output[$idx]['oul'] = true;
81: $first = false;
82: }
83:
84: $this->output[$idx]['oli'] = true;
85: $this->output[$idx] = array_merge($item, $this->output[$idx]);
86:
87: if (isset($this->parents[$item['id']])) {
88: $this->output[$idx]['hassub'] = true;
89: $this->buildMenus($item['id']);
90: }
91: $this->output[$idx]['cli'] = true;
92: $this->output[$idx]['close'] .= '</li>';
93: }
94: $this->output[$idx]['cul'] = true;
95: $this->output[$idx]['close'] .= '</ul>';
96: $level -= 1;
97: }
98:
99: 100: 101:
102: public function buildUpDown($pid = 0)
103: {
104: static $idx = -1;
105: $prevWeight = null;
106: $up = 0;
107: $down = 1;
108: $counter = 0;
109: $count = count($this->parents[$pid]);
110:
111: foreach ($this->parents[$pid] as $item) {
112: $idx += 1;
113: ++$counter;
114: if ($counter == $count) { $down = 0; }
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;
125:
126: if (isset($this->parents[$item['id']])) {
127: $this->buildUpDown($item['id']);
128: }
129: }
130: }
131:
132: public function buildSelected()
133: {
134:
135: $sel = array();
136: $query_string = $_SERVER['QUERY_STRING'] ? '?' . $_SERVER['QUERY_STRING'] : '';
137: $self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . $query_string;
138:
139:
140: $default = \XoopsBaseConfig::get('url') . "/index.php";
141:
142:
143: foreach ($this->output as $idx => $menu) {
144: $selected = 0;
145: if (!empty($menu['link'])) {
146: $selected = (false !== stristr($self, $menu['link'])) ? 1 : $selected;
147: }
148: $selected = ($menu['link'] == $self) ? 1 : $selected;
149: $selected = ($menu['link'] == $default) ? 1 : $selected;
150: if ($selected) {
151: $sel[$idx] = $menu;
152: }
153: }
154:
155:
156: $longlink = "";
157: $longidx = 0;
158: foreach ($sel as $idx => $menu) {
159: if (strlen($menu['link']) > strlen($longlink)) {
160: $longidx = $idx;
161: $longlink = $menu['link'];
162: }
163: }
164:
165: 166: 167: 168:
169: if (isset($this->output[$longidx])) {
170: $this->output[$longidx]['selected'] = true;
171: $this->output[$longidx]['topselected'] = true;
172:
173:
174: $this->addSelectedParents($this->output[$longidx]['pid']);
175: }
176: }
177:
178: 179: 180:
181: public function addSelectedParents($pid)
182: {
183: foreach ($this->output as $idx => $menu) {
184: if ($menu['id'] == $pid) {
185: $this->output[$idx]['selected'] = true;
186: $this->addSelectedParents($menu['pid']);
187: }
188: }
189: }
190:
191: 192: 193:
194: public function render()
195: {
196: $this->buildMenus();
197: $this->buildUpDown();
198: $this->buildSelected();
199:
200: return $this->output;
201: }
202:
203: }
204: