XOOPS  2.6.0
builder.php
Go to the documentation of this file.
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 
21 {
25  protected $parents = array();
26 
30  protected $output = array();
31 
35  public function __construct($array)
36  {
37  $this->addMenu($array);
38  }
39 
43  public function addMenu($array)
44  {
45  foreach ($array as $item) {
46  $this->add($item);
47  }
48  }
49 
53  public function add($item)
54  {
55  $this->parents[$item['pid']][] = $item;
56  }
57 
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 
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; } // 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  $query_string = $_SERVER['QUERY_STRING'] ? '?' . $_SERVER['QUERY_STRING'] : '';
137  $self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . $query_string;
138 
139  //set a default page in case we don't get matches
140  $default = \XoopsBaseConfig::get('url') . "/index.php";
141 
142  //get all matching links
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  //From those links get only the longer one
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  * When visiting site.com when \XoopsBaseConfig::get('url') is set to www.site.com
167  * longidx is not detected, this IF will prevent blank page
168  */
169  if (isset($this->output[$longidx])) {
170  $this->output[$longidx]['selected'] = true;
171  $this->output[$longidx]['topselected'] = true;
172 
173  //Now turn all this menu parents to selected
174  $this->addSelectedParents($this->output[$longidx]['pid']);
175  }
176  }
177 
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 
194  public function render()
195  {
196  $this->buildMenus();
197  $this->buildUpDown();
198  $this->buildSelected();
199 
200  return $this->output;
201  }
202 
203 }
__construct($array)
Definition: builder.php:35
buildUpDown($pid=0)
Definition: builder.php:102
$sel
Definition: main.php:48
$_SERVER['REQUEST_URI']
$pid
Definition: admin_menu.php:37
add($item)
Definition: builder.php:53
buildMenus($pid=0)
Definition: builder.php:61
$menu
addSelectedParents($pid)
Definition: builder.php:181
static get($name)
addMenu($array)
Definition: builder.php:43