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