FOSSology  4.4.0
Open Source License Compliance by Open Source Software
MicroMenu.php
1 <?php
2 # SPDX-FileCopyrightText: © Fossology contributors
3 
4 # SPDX-License-Identifier: GPL-2.0-only
5 
6 namespace Fossology\Lib\UI\Component;
7 
8 class MicroMenu
9 {
10  const VIEW = 'View';
11  const VIEW_META = 'View-Meta';
12 
13  const FORMAT_HEX = 'hex';
14  const FORMAT_TEXT = 'text';
15  const FORMAT_FLOW = 'flow';
16 
17  const TARGET_DEFAULT = 'default';
18  const TARGET_VIEW = 'view';
19 
20  private $formatOptions = array(self::FORMAT_FLOW, self::FORMAT_TEXT, self::FORMAT_HEX);
21 
22  private $textFormats = array(self::FORMAT_HEX, self::FORMAT_TEXT, self::FORMAT_FLOW);
23 
24  private $targets = array(
25  self::TARGET_DEFAULT => array(MicroMenu::VIEW_META, MicroMenu::VIEW),
26  self::TARGET_VIEW => array(MicroMenu::VIEW)
27  );
28 
29  public function insert($groups, $name, $position, $module, $uri, $tooltip)
30  {
31  if (!is_array($groups)) {
32  $groups = $this->targets[$groups];
33  }
34 
35  $showLink = GetParm("mod", PARM_STRING) !== $module;
36 
37  foreach ($groups as $group) {
38  $menuKey = $group . MENU_PATH_SEPARATOR . $name;
39  if ($showLink) {
40  menu_insert($menuKey, $position, $uri, $tooltip);
41  } else {
42  menu_insert($menuKey, $position);
43  }
44  }
45  }
46 
51  public function getFormatParameter($itemId = null)
52  {
53  $selectedFormat = GetParm("format", PARM_STRING);
54 
55  if (in_array($selectedFormat, $this->formatOptions)) {
56  return $selectedFormat;
57  }
58  if (empty($itemId)) {
59  return self::FORMAT_FLOW;
60  } else {
61  $mimeType = GetMimeType($itemId);
62  list($type, $dummy) = explode("/", $mimeType, 2);
63  return $type == 'text' ? self::FORMAT_TEXT : self::FORMAT_FLOW;
64  }
65  }
66 
74  public function addFormatMenuEntries($selectedFormat, $pageNumber, $menuKey = self::VIEW, $hexFactor = 10)
75  {
76  $uri = Traceback_parm();
77  $uri = preg_replace("/&format=[a-zA-Z0-9]*/", "", $uri);
78  $uri = preg_replace("/&page=[0-9]*/", "", $uri);
79 
80  $pageNumberHex = null;
81  $pageNumberText = null;
82 
83  $tooltipTexts = array(
84  self::FORMAT_HEX => _("View as a hex dump"),
85  self::FORMAT_TEXT => _("View as unformatted text"),
86  self::FORMAT_FLOW => _("View as formatted text")
87  );
88 
89  $menuTexts = array(
90  self::FORMAT_HEX => "Hex",
91  self::FORMAT_TEXT => "Text",
92  self::FORMAT_FLOW => "Formatted"
93  );
94 
95  $menuPosition = -9;
96  menu_insert("$menuKey::[BREAK]", $menuPosition--);
97 
98  foreach ($this->textFormats as $currentFormat) {
99  $menuName = $menuKey . MENU_PATH_SEPARATOR . $menuTexts[$currentFormat];
100  if ($currentFormat == $selectedFormat) {
101  menu_insert($menuName, $menuPosition--);
102  } else {
103  $targetPageNumber = $currentFormat == self::FORMAT_HEX ? $hexFactor * $pageNumberHex : $pageNumber;
104  menu_insert($menuName, $menuPosition--, "$uri&format=$currentFormat&pageNumber=$targetPageNumber", $tooltipTexts[$currentFormat]);
105  }
106  }
107  menu_insert("$menuKey::[BREAK]", $menuPosition);
108  }
109 }
addFormatMenuEntries($selectedFormat, $pageNumber, $menuKey=self::VIEW, $hexFactor=10)
Definition: MicroMenu.php:74
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.
const MENU_PATH_SEPARATOR
Separator used between menu paths.
Definition: common-menu.php:11
Traceback_parm($ShowMod=1)
Get the URI query to this location.
const PARM_STRING
Definition: common-parm.php:18
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
GetMimeType($Item)
Given an uploadtree_pk, return a string that describes the mime type.
Definition: common-repo.php:25
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308