FOSSology  4.4.0
Open Source License Compliance by Open Source Software
common-menu.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2012 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: LGPL-2.1-only
6 */
11 const MENU_PATH_SEPARATOR = "::";
12 const MENU_BREAK = "[BREAK]";
13 
19 class menu
20 {
21  var $Name = "";
22  var $URI = NULL;
23  var $HTML = NULL;
24  var $Order = 0;
25  var $Target = NULL;
26  var $MaxDepth = 0;
27  var $SubMenu = NULL;
28  public $FullName;
29 
36  public function getName($showFullName=false)
37  {
38  if ($showFullName) {
39  return $this->FullName . "(" . $this->Order . ")";
40  }
41  return $this->Name;
42  }
43 }
44 
45 /*********************************
46  Global array: don't touch!
47  *********************************/
48 $MenuList = array();
60 function MenuPage($Page, $TotalPage, $Uri = '')
61 {
62  $V = "<ul class='pagination pagination-sm justify-content-center'>";
63  if (empty($Uri)) {
64  $Uri = Traceback();
65  }
66  $Uri = preg_replace("/&page=[^&]*/", "", $Uri);
67  /* Create first page */
68  if ($Page > 0) {
69  $text = _("First");
70  $text1 = _("Prev");
71  $V.= "<li class='page-item'><a class='page-link' href='$Uri&page=0'>$text</a></li>";
72  $V.= "<li class='page-item'><a class='page-link' href='$Uri&page=" . ($Page - 1) . "'>$text1</a></li>";
73  if ($Page > 9) {
74  $V.= " ... ";
75  }
76  }
77  /* Create previous list page */
78  for ($i = $Page - 9;$i < $Page;$i++) {
79  if ($i >= 0) {
80  $V.= "<li class='page-item'><a class='page-link' href='$Uri&page=$i'>" . ($i + 1) . "</a></li>";
81  }
82  }
83  /* Show current page number */
84  $V.= "<li class='page-item active'><a class='page-link' href='#'>" . ($Page + 1) . "</a></li>";
85  /* Create next page */
86  for ($i = $Page + 1;($i <= $TotalPage) && ($i < $Page + 9);$i++) {
87  $V.= "<li class='page-item'><a class='page-link' href='$Uri&page=$i'>" . ($i + 1) . "</a></li>";
88  }
89  if ($Page < $TotalPage) {
90  if ($Page < $TotalPage - 9) {
91  $V.= " ...";
92  }
93  $text = _("Next");
94  $text1 = _("Last");
95  $V.= "<li class='page-item'><a class='page-link' href='$Uri&page=" . ($Page + 1) . "'>$text</a></li>";
96  $V.= "<li class='page-item'><a class='page-link' href='$Uri&page=" . ($TotalPage) . "'>$text1</a></li>";
97  }
98  $V.= "</ul>";
99  return ($V);
100 } // MenuPage
101 
111 function MenuEndlessPage($Page, $Next = 1, $Uri = '')
112 {
113  $V = "<center><ul class='pagination pagination-sm justify-content-center'>";
114  if (empty($Uri)) {
115  $Uri = Traceback();
116  }
117  $Uri = preg_replace("/&page=[^&]*/", "", $Uri);
118  /* Create first page */
119  if ($Page > 0) {
120  $text = _("First");
121  $text1 = _("Prev");
122  $V.= "<li class='page-item'><a class='page-link' href='$Uri&page=0'>$text</a></li>";
123  $V.= "<li class='page-item'><a class='page-link' href='$Uri&page=" . ($Page - 1) . "'>$text1</a></li>";
124  if ($Page > 9) {
125  $V.= " ... ";
126  }
127  }
128  /* Create previous list page */
129  for ($i = $Page - 9;$i < $Page;$i++) {
130  if ($i >= 0) {
131  $V.= "<li class='page-item'><a class='page-link' href='$Uri&page=$i'>" . ($i + 1) . "</a></li>";
132  }
133  }
134  /* Show current page number */
135  $V.= "<li class='page-item active'><a class='page-link' href='#'>" . ($Page + 1) . "</a></li>";
136  /* Create next page */
137  if ($Next) {
138  $text = _("Next");
139  $i = $Page + 1;
140  $V.= "<li class='page-item'><a class='page-link' href='$Uri&page=$i'>" . ($i + 1) . "</a></li>";
141  $V.= "<li class='page-item'><a class='page-link' href='$Uri&page=$i'>$text</a></li>";
142  }
143  $V.= "</ul></center>";
144  return ($V);
145 } // MenuEndlessPage()
146 
157 function menu_cmp($a, $b)
158 {
159  if ($a->Order > $b->Order) {
160  return (-1);
161  }
162  if ($a->Order < $b->Order) {
163  return (1);
164  }
165  $rc = strcmp($a->Name, $b->Name);
166  return (strcmp($a->Name, $b->Name));
167 } // menu_cmp()
168 
187 function menu_insert_r(&$menuItems, $path, $pathRemainder, $LastOrder, $Target, $URI, $HTML, &$Title)
188 {
189  $splitPath = explode(MENU_PATH_SEPARATOR, $pathRemainder, 2);
190  $pathElement = count($splitPath) > 0 ? $splitPath[0] : null;
191  $pathRemainder = count($splitPath) > 1 ? $splitPath[1] : null;
192  $hasPathComponent = $pathElement !== null && $pathElement !== "";
193 
194  if (!$hasPathComponent) {
195  return 0;
196  }
197 
198  $isLeaf = $pathRemainder === null;
199  $menuItemsExist = isset($menuItems) && is_array($menuItems);
200 
201  $currentMenuItem = NULL;
202  if ($menuItemsExist) {
203  foreach ($menuItems as &$menuItem) {
204  // need to escape the [ ] or the string will not match
205  if (!strcmp($menuItem->Name, $pathElement) && strcmp($menuItem->Name, MENU_BREAK)) {
206  $currentMenuItem = $menuItem;
207  break;
208  } else if (!strcmp($menuItem->Name, MENU_BREAK) && ($menuItem->Order == $LastOrder)) {
209  $currentMenuItem = $menuItem;
210  break;
211  }
212  }
213  }
214 
215  $path[] = $pathElement;
216  $FullName = str_replace(" ", "_", implode(MENU_PATH_SEPARATOR, $path));
217 
218  $sortItems = false;
219  $currentItemIsMissing = empty($currentMenuItem);
220  if ($currentItemIsMissing) {
221  $currentMenuItem = new menu;
222  $currentMenuItem->Name = $pathElement;
223  $currentMenuItem->FullName = $FullName;
224 
225  if (! $menuItemsExist) {
226  $menuItems = array();
227  }
228  $menuItems[] = $currentMenuItem;
229  $sortItems = true;
230  }
231 
232  /* $M is set! See if we need to traverse submenus */
233  if ($isLeaf) {
234  if ($LastOrder != 0) {
235  if ($currentMenuItem->Order != $LastOrder) {
236  $sortItems = true;
237  }
238  $currentMenuItem->Order = $LastOrder;
239  }
240  $currentMenuItem->Target = $Target;
241  $currentMenuItem->URI = $URI;
242  $currentMenuItem->HTML = $HTML;
243  $currentMenuItem->Title = $Title;
244  } else {
245  $Depth = menu_insert_r($currentMenuItem->SubMenu, $path, $pathRemainder, $LastOrder, $Target, $URI, $HTML, $Title);
246  $currentMenuItem->MaxDepth = max ($currentMenuItem->MaxDepth, $Depth + 1);
247  }
248 
249  if ($sortItems) {
250  usort($menuItems, 'menu_cmp');
251  }
252 
253  array_pop($path);
254  return ($currentMenuItem->MaxDepth);
255 } // menu_insert_r()
256 
257 
269 function menu_insert($Path, $LastOrder = 0, $URI = NULL, $Title = NULL, $Target = NULL, $HTML = NULL)
270 {
271  global $MenuList;
272  menu_insert_r($MenuList, array(), $Path, $LastOrder, $Target, $URI, $HTML, $Title);
273 } // menu_insert()
274 
275 
291 function menu_find($Name, &$MaxDepth, $Menu = NULL)
292 {
293  global $MenuList;
294  if (empty($Menu)) {
295  $Menu = $MenuList;
296  }
297  if (empty($Name)) {
298  return ($Menu);
299  }
300  $PathParts = explode('::', $Name, 2);
301  foreach ($Menu as $Val) {
302  if ($Val->Name == $PathParts[0]) {
303  if (empty($PathParts[1])) {
304  $MaxDepth = $Val->MaxDepth;
305  return ($Val->SubMenu);
306  } else {
307  return (menu_find($PathParts[1], $MaxDepth, $Val->SubMenu));
308  }
309  }
310  }
311  return (null);
312 } // menu_find()
313 
314 
329 function menu_to_1html($Menu, $ShowRefresh = 1, $ShowTraceback = 0, $ShowAll = 1)
330 {
331  $showFullName = isset($_SESSION) && array_key_exists('fullmenudebug', $_SESSION) && $_SESSION['fullmenudebug'] == 1;
332 
333  $V = "";
334  $Std = "";
335  global $menu_to_1html_counter;
336  if ($ShowTraceback) {
337  global $Plugins;
338  $Refresh = & $Plugins[plugin_find_id("refresh") ];
339  if (!empty($Refresh)) {
340  $text = _("Traceback");
341  $URL = Traceback_dir() . "?" . $Refresh->GetRefresh();
342  $Std.= "<a href='$URL' target='_top'>$text</a>";
343  }
344  }
345  if ($ShowRefresh) {
346  if (!empty($Std)) {
347  $Std.= " | ";
348  }
349  $text = _("Refresh");
350  $Std.= "<a href='" . Traceback() . "'>$text</a>";
351  }
352  $First = 1;
353  if (!empty($Menu)) {
354  foreach ($Menu as $Val) {
355  if ($Val->Name == MENU_BREAK) {
356  if (!$First) {
357  $V .= " &nbsp;&nbsp;&bull;";
358  if ($showFullName) {
359  $V .= getFullNameAddition($Val);
360  }
361  $V .= "&nbsp;&nbsp; ";
362  }
363  $First = 1;
364  } else if (!empty($Val->HTML)) {
365  $V.= $Val->HTML;
366  if ($showFullName) {
367  $V .= getFullNameAddition($Val);
368 
369  }
370  $First = 0;
371  } else if (!empty($Val->URI)) {
372  if (!$First) {
373  $V.= " | ";
374  }
375  $V.= "<a href='" . Traceback_uri() . "?mod=" . $Val->URI . "'";
376  if (!empty($Val->Title)) {
377  $V.= " title='" . htmlentities($Val->Title, ENT_QUOTES) . "'";
378  }
379  $V.= ">";
380  if ($showFullName) {
381  $V.= $Val->FullName . getFullNameAddition($Val);
382  } else {
383  $V.= $Val->Name;
384  }
385  $V.= "</a>";
386  $First = 0;
387  } else if ($ShowAll) {
388  if (!$First) {
389  $V.= " | ";
390  }
391  if ($showFullName) {
392  $V.= $Val->FullName . getFullNameAddition($Val);
393  } else {
394  $V.= $Val->Name;
395  }
396  $First = 0;
397  }
398  }
399  }
400  if (!empty($Std)) {
401  if (!$First) {
402  $V.= " &nbsp;&nbsp;&bull;&nbsp;&nbsp; ";
403  }
404  $V.= $Std;
405  $Std = null;
406  }
408  return ("<div id='menu1html-$menu_to_1html_counter' align='right' style='padding:0px 5px 0px 5px'><small>$V</small></div>");
409 }
410 
416 function getFullNameAddition(menu $menu)
417 {
418  return "(" . $menu->Order . ")";
419 } // menu_to_1html()
420 
421 
437 function menu_to_1list($Menu, &$Parm, $Pre = "", $Post = "", $ShowAll = 1, $upload_id = "")
438 {
439  if (empty($Menu)) {
440  return '';
441  }
442 
443  $showFullName = isset($_SESSION) && array_key_exists('fullmenudebug', $_SESSION) && $_SESSION['fullmenudebug'] == 1;
444  $V = "";
445 
446  foreach ($Menu as $Val) {
447  if (!empty($Val->HTML)) {
448  $entry = $Val->HTML;
449  } else if (!empty($Val->URI)) {
450  if (!empty($upload_id) && "tag" == $Val->URI) {
451  $tagstatus = TagStatus($upload_id);
452  if (0 == $tagstatus) {
453  break; // tagging on this upload is disabled
454  }
455  }
456 
457  $entry = "[<a href='" . Traceback_uri() . "?mod=" . $Val->URI . "&" . $Parm . "'";
458  if (!empty($Val->Title)) {
459  $entry .= " title='" . htmlentities($Val->Title, ENT_QUOTES) . "'";
460  }
461  $entry .= ">" ;
462  $entry .= $Val->getName($showFullName);
463  $entry .= "</a>]";
464  } else if ($ShowAll) {
465  $entry = "[" . $Val->getName($showFullName) . "]";
466  } else {
467  continue;
468  }
469  $V .= $Pre . $entry . $Post;
470  }
471  return $V;
472 }
473 
474 
483 function menu_print(&$Menu, $Indent)
484 {
485  if (!isset($Menu)) {
486  return;
487  }
488  foreach ($Menu as $Val) {
489  for ($i = 0;$i < $Indent;$i++) {
490  print " ";
491  }
492  print "$Val->Name ($Val->Order,$Val->URI)\n";
493  menu_print($Val->SubMenu, $Indent + 1);
494  }
495 } // menu_print()
496 
497 // DEBUG CODE
498 /**********
499  if (0)
500  {
501  menu_insert("abc::def::",0,"");
502  menu_insert("Applications::Accessories::Dictionary",0,"");
503  menu_insert("Applications::Accessories::Ark",0,"");
504  menu_insert("Places::Computer",3,"");
505  menu_insert("Places::CD/DVD Creator",3,"");
506  menu_insert("Places::Home Folder",4,"");
507  menu_insert("Places::Network Servers",2,"");
508  menu_insert("Places::Search for Files...",0,"");
509  menu_insert("Places::Desktop",4,"");
510  menu_insert("Places::Recent Documents",0,"");
511  menu_insert("Places::Connect to Server...",2,"");
512  menu_insert("Applications::Accessories::Calculator",0,"");
513  menu_print($MenuList,0);
514  print "Max depth: $MenuMaxDepth\n";
515  }
516  **********/
517 
518 
534 function menu_remove($Menu, $RmName)
535 {
536  $NewArray = array();
537  foreach ($Menu as $MenuObj) {
538  if ($MenuObj->Name != $RmName) {
539  $NewArray[] = $MenuObj;
540  }
541  }
542  return $NewArray;
543 }
Code for creating a menu list (2D linked list) from a set of plugins.
Definition: common-menu.php:20
$SubMenu
Sub menu to show.
Definition: common-menu.php:27
$MaxDepth
How deep is SubMenu?
Definition: common-menu.php:26
$Target
Recommended name of window for showing results.
Definition: common-menu.php:25
getName($showFullName=false)
Definition: common-menu.php:36
$HTML
HTML to include (if provided, used in place of all else)
Definition: common-menu.php:23
$Name
Name of the menu item.
Definition: common-menu.php:21
$Order
Used for ordering menu items.
Definition: common-menu.php:24
$FullName
List to submenu list.
Definition: common-menu.php:28
$URI
URI for the plugin (everything after the "?mod=")
Definition: common-menu.php:22
menu_print(&$Menu, $Indent)
Debugging code for printing the menu.
MenuEndlessPage($Page, $Next=1, $Uri='')
Create a "First Prev 1 2 ... Next" page links for paged output.
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item.
menu_insert_r(&$menuItems, $path, $pathRemainder, $LastOrder, $Target, $URI, $HTML, &$Title)
Given a Path, order level for the last item, and a plugin name, insert the menu item.
const MENU_PATH_SEPARATOR
Separator used between menu paths.
Definition: common-menu.php:11
getFullNameAddition(menu $menu)
menu_to_1list($Menu, &$Parm, $Pre="", $Post="", $ShowAll=1, $upload_id="")
Take a menu and render it as one HTML line with items in a "[name]" list.
const MENU_BREAK
Break menu at this.
Definition: common-menu.php:12
menu_remove($Menu, $RmName)
Remove a menu object (based on an object name) from a menu list.
MenuPage($Page, $TotalPage, $Uri='')
Create a "First Prev 1 2 ... Next Last" page links for paged output.
Definition: common-menu.php:60
$MenuMaxDepth
How deep is the tree (for UI display)
Definition: common-menu.php:49
$menu_to_1html_counter
Counter used by menu_to_1html()
$MenuList
Global menu list array.
Definition: common-menu.php:48
menu_cmp($a, $b)
Compare two menu items for sorting.
menu_find($Name, &$MaxDepth, $Menu=NULL)
Given a top-level menu name, find the list of sub-menus below it and max depth of menu.
menu_to_1html($Menu, $ShowRefresh=1, $ShowTraceback=0, $ShowAll=1)
Take a menu and render it as one HTML line.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
Traceback_dir()
Get the directory of the URI without query.
Traceback()
Get the URI + query to this location.
Definition: common-parm.php:89
FUNCTION int max(int permGroup, int permPublic)
Get the maximum group privilege.
Definition: libfossagent.c:295