FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
Menu.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2008-2011 Hewlett-Packard Development Company, L.P.
4  * Copyright (C) 2014-2015 Siemens AG
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 
21 
24 
25 class Menu
26 {
27  const FULL_MENU_DEBUG = 'fullmenudebug';
28  var $MenuTarget = "treenav";
29  protected $renderer;
30 
31  public function __construct(Twig_Environment $renderer)
32  {
33  // Add default menus (with no actions linked to plugins)
34  menu_insert("Main::Upload", 70);
35  menu_insert("Main::Jobs", 60);
36  menu_insert("Main::Organize", 50);
37  menu_insert("Main::Help", -1);
38  menu_insert("Main::Help::Documentation", 0, NULL, NULL, NULL, "<a href='http://www.fossology.org/projects/fossology/wiki/User_Documentation'>Documentation</a>");
39  $this->renderer = $renderer;
40  }
41 
45  function menu_html(&$menu, $indent)
46  {
47  if (empty($menu)) {
48  return;
49  }
50  $output = "";
51  $output .= "<!--[if lt IE 7]><table><tr><td><![endif]-->\n";
52  $output .= "<ul id='menu-$indent'>\n";
53 
54  foreach ($menu as $menuEntry) {
55  $output .= '<li>';
56 
57  if (!empty($menuEntry->HTML)) {
58  $output .= $menuEntry->HTML;
59  } else { /* create HTML */
60  $output .= $this->createHtmlFromMenuEntry($menuEntry, $indent);
61  }
62 
63  if (!empty($menuEntry->SubMenu)) {
64  $output .= $this->menu_html($menuEntry->SubMenu, $indent + 1);
65  }
66  }
67  $output .= "</ul>\n";
68  $output .= "<!--[if lt IE 7]></td></tr></table></a><![endif]-->\n";
69  return preg_replace("|<li><a href=\"#\"><font color(.*)*?$|m", '', $output);
70  }
71 
72  function createHtmlFromMenuEntry(\menu $menuEntry, $indent)
73  {
74  $isFullMenuDebug = array_key_exists(self::FULL_MENU_DEBUG, $_SESSION) && $_SESSION[self::FULL_MENU_DEBUG] == 1;
75  $output = "";
76  if (!empty($menuEntry->URI)) {
77  $output .= '<a id="'. htmlentities($menuEntry->FullName) .'" href="' . Traceback_uri() . "?mod=" . $menuEntry->URI;
78  if (empty($menuEntry->Target) || ($menuEntry->Target == "")) {
79  $output .= '">';
80  } else {
81  $output .= '" target="' . $menuEntry->Target . '">';
82  }
83  if ($isFullMenuDebug) {
84  $output .= $menuEntry->FullName . "(" . $menuEntry->Order . ")";
85  } else {
86  $output .= $menuEntry->Name;
87  }
88  } else {
89  $output .= '<a id="'. htmlentities($menuEntry->FullName) .'" href="#">';
90  if (empty($menuEntry->SubMenu)) {
91  $output .= "<font color='#C0C0C0'>";
92  if ($isFullMenuDebug) {
93  $output .= $menuEntry->FullName . "(" . $menuEntry->Order . ")";
94  } else {
95  $output .= '';
96  }
97  $output .= "</font>";
98  } else {
99  if ($isFullMenuDebug) {
100  $output .= $menuEntry->FullName . "(" . $menuEntry->Order . ")";
101  } else {
102  $output .= $menuEntry->Name;
103  }
104  }
105  }
106 
107  if (!empty($menuEntry->SubMenu) && ($indent > 0)) {
108  $output .= " <span>&raquo;</span>";
109  }
110  $output .= "</a>\n";
111  return $output;
112  }
113 
117  function OutputCSS()
118  {
119  $output = "";
120 
121  $output .= "<style type=\"text/css\">\n";
122  /* Depth 0 is special: position is relative, colors are blue */
123  $depth = 0;
124  $label = "";
125  $Menu = menu_find("Main", $MenuDepth);
126  $cssBorder = "border-color:#CCC #CCC #CCC #CCC; border-width:1px 1px 1px 1px;";
127  $cssPadding = "padding:4px 0px 4px 4px;";
128  $FOSScolor1 = "#c50830";
129  $FOSScolor2 = "#808080";
130  $FOSSbg = "white";
131 
132  $FOSSfg1 = "black";
133  $FOSSbg1 = "white";
134  $FOSSfg1h = $FOSScolor1; // highlight colors
135  $FOSSbg1h = "beige";
136 
137  $FOSSfg2 = "black";
138  $FOSSbg2 = "beige";
139  $FOSSfg2h = $FOSScolor1; // highlight colors
140  $FOSSbg2h = "beige";
141 
142  $FOSSfg3 = "black";
143  $FOSSbg3 = "beige";
144  $FOSSfg3h = $FOSScolor1; // highlight colors
145  $FOSSbg3h = "beige";
146 
147  if ($depth < $MenuDepth) {
149  $output .= "\n/* CSS for Depth $depth */\n";
150  $label = "ul#menu-" . $depth;
151  $output .= $label . "\n";
152  $output .= " { z-index:0; margin:0; padding:0px; list-style:none; background:$FOSSbg1; width:100%; height:24px; font:normal 14px verdana, arial, helvetica; font-weight: bold; }\n";
153  $label .= " li";
154  $output .= $label . "\n";
155  $output .= " { float:left; margin:0; padding:0px; display:block; position:relative; width:auto; border:0px solid #000; }\n";
156  $output .= $label . " a:link,\n";
157  $output .= $label . " a:visited\n";
158  $output .= " { float:left; padding:4px 10px; text-decoration:none; color:$FOSSfg1; background:$FOSSbg1; width:auto; display:block; }\n";
159  $output .= $label . ":hover a,\n";
160  $output .= $label . " a:hover,\n";
161  $output .= $label . " a:active\n";
162  $output .= " { float:left; padding:4px 10px; color:$FOSSfg1h; background:$FOSSbg1h; $cssBorder width:auto; display:block; }\n";
163  $output .= $label . " a span\n";
164  $output .= " { float:left; position:absolute; top:0; left:135px; font-size:12pt; }\n";
165  $depth++;
166  }
167 
168  /* Depth 1 is special: position is absolute. Left is 0, top is 24 */
169  if ($depth < $MenuDepth) {
170  $output .= "\n/* CSS for Depth $depth */\n";
171  $output .= $label . " ul#menu-" . $depth . "\n";
172  $output .= " { margin:0; padding:0px 0; list-style:none; display:none; visibility:hidden; left:0px; width:150px; position:absolute; top:24px; font-weight: bold; }\n";
173  $output .= $label . ":hover ul#menu-" . $depth . "\n";
174  $output .= " { float:left; display:block; visibility:visible; }\n";
175  $label .= " ul#menu-" . $depth . " li";
176  $output .= $label . "\n";
177  $output .= " { z-index:$depth; margin:0; padding:0; display:block; visibility:visible; position:relative; width:150px; }\n";
178  $output .= $label . " a:link,\n";
179  $output .= $label . " a:visited\n";
180  $output .= " { z-index:$depth; $cssPadding color:$FOSSfg2; background:$FOSSbg2; border:1px solid #000; $cssBorder width:150px; display:block; visibility:visible; }\n";
181  $output .= $label . ":hover a,\n";
182  $output .= $label . " a:active,\n";
183  $output .= $label . " a:hover\n";
184  $output .= " { z-index:$depth; $cssPadding color:$FOSSfg2h; background:$FOSSbg2h; width:150px; display:block; visibility:visible; }\n";
185  $output .= $label . " a span\n";
186  $output .= " { text-align:left; }\n";
187  $depth++;
188  }
189 
190  /* Depth 2+ is recursive: position is absolute. Left is 150*(Depth-1), top is 0 */
191  for (; $depth < $MenuDepth; $depth++) {
192  $output .= "\n/* CSS for Depth $depth */\n";
193  $output .= $label . " ul#menu-" . $depth . "\n";
194  $output .= " { margin:0; padding:1px 0; list-style:none; display:none; visibility:hidden; left:156px; width:150px; position:absolute; top:-1px; font-weight: bold; }\n";
195  $output .= $label . ":hover ul#menu-" . $depth . "\n";
196  $output .= " { float:left; display:block; visibility:visible; }\n";
197  $label .= " ul#menu-" . $depth . " li";
198  $output .= $label . "\n";
199  $output .= " { z-index:$depth; margin:0; padding:0; display:block; visibility:visible; position:relative; width:150px; }\n";
200  $output .= $label . " a:link,\n";
201  $output .= $label . " a:visited\n";
202  $output .= " { z-index:$depth; $cssPadding color:$FOSSfg3; background:$FOSSbg2h; border:1px solid #000; $cssBorder width:150px; display:block; }\n";
203  $output .= $label . ":hover a,\n";
204  $output .= $label . " a:active,\n";
205  $output .= $label . " a:hover\n";
206  $output .= " { z-index:$depth; $cssPadding color:$FOSSfg3h; background:$FOSSbg3h; width:150px; display:block; visibility:visible; }\n";
207  $output .= $label . " a span\n";
208  $output .= " { text-align:left; }\n";
209  }
210  $output .= "</style>\n";
211 
212  /* For IE's screwed up CSS: this defines "hover". */
213  $output .= "<!--[if lt IE 8]>\n";
214  $output .= "<style type='text/css' media='screen'>\n";
216  $output .= "body { behavior:url(csshover.htc); }\n";
218  for ($i = 1; $i < $MenuDepth; $i++) {
219  $output .= "#menu-$i table {height:0px; border-collapse:collapse; margin:0; padding:0; }\n";
220  $output .= "#menu-$i td {height:0px; border:none; margin:0; padding:0; }\n";
221  }
222  $output .= "</style>\n";
223  $output .= "<![endif]-->\n";
224 
225  $this->_CSSdone = 1;
226  return ($output);
227  } // OutputCSS()
228 
232  function Output($title = NULL)
233  {
234  global $SysConf;
235  $sysConfig = $SysConf['SYSCONFIG'];
236 
237  $vars = array();
238  $vars['title'] = empty($title) ? _("Welcome to FOSSology") : $title;
239  $vars['bannerMsg'] = @$sysConfig['BannerMsg'];
240  $vars['logoLink'] = $sysConfig['LogoLink']?: 'http://fossology.org';
241  $vars['logoImg'] = $sysConfig['LogoImage']?: 'images/fossology-logo.gif';
242 
243  if ( array_key_exists('SupportEmailLabel',$sysConfig) && !empty($sysConfig['SupportEmailLabel'])
244  && array_key_exists('SupportEmailAddr',$sysConfig) && !empty($sysConfig['SupportEmailAddr'])) {
245  $menuItem = '<a href="mailto:'.$sysConfig['SupportEmailAddr'].'?subject='.@$sysConfig['SupportEmailSubject'].'">'.$sysConfig['SupportEmailLabel'].'</a>';
246  menu_insert("Main::Help::".$sysConfig['SupportEmailLabel'], 0, NULL, NULL, NULL, $menuItem);
247  }
248 
249  $menu = menu_find("Main", $MenuDepth);
250  $vars['mainMenu'] = $this->menu_html($menu, 0);
251  $vars['uri'] = Traceback_uri();
252 
253  /* Handle login information */
254  $vars['isLoggedOut'] = ((empty($_SESSION[Auth::USER_NAME])) or ($_SESSION[Auth::USER_NAME] == "Default User"));
255  $vars['isLoginPage'] = GetParm("mod", PARM_STRING)=='auth';
256 
257  global $SysConf;
258  if (array_key_exists('BUILD', $SysConf)) {
259  $vars['versionInfo'] = array(
260  'version' => $SysConf['BUILD']['VERSION'],
261  'buildDate' => $SysConf['BUILD']['BUILD_DATE'],
262  'commitHash' => $SysConf['BUILD']['COMMIT_HASH'],
263  'commitDate' => $SysConf['BUILD']['COMMIT_DATE'],
264  'branchName' => $SysConf['BUILD']['BRANCH']
265  );
266  }
267 
268  if (!$vars['isLoggedOut']) {
269  $this->mergeUserLoginVars($vars);
270  }
271 
272  $out = $this->renderer->loadTemplate('components/menu.html.twig')->render($vars);
273  return $out;
274  }
275 
276  private function mergeUserLoginVars(&$vars)
277  {
278  global $container;
279  $dbManager = $container->get("db.manager");
280 
281  $vars['logOutUrl'] = Traceback_uri() . '?mod=' . ((plugin_find_id('auth')>=0) ? 'auth' : 'smauth');
282  $vars['userName'] = $_SESSION[Auth::USER_NAME];
283 
284  $sql = 'SELECT group_pk, group_name FROM group_user_member LEFT JOIN groups ON group_fk=group_pk WHERE user_fk=$1';
285  $stmt = __METHOD__ . '.availableGroups';
286  $dbManager->prepare($stmt, $sql);
287  $res = $dbManager->execute($stmt, array($_SESSION['UserId']));
288  $allAssignedGroups = array();
289  while ($row = $dbManager->fetchArray($res)) {
290  $allAssignedGroups[$row['group_pk']] = $row['group_name'];
291  }
292  $dbManager->freeResult($res);
293  if (count($allAssignedGroups) > 1) {
294  $vars['backtraceUri'] = Traceback_uri() . "?mod=" . Traceback_parm();
295  $vars['groupId'] = $_SESSION[Auth::GROUP_ID];
296  $vars['allAssignedGroups'] = $allAssignedGroups;
297  } else {
298  $vars['singleGroup'] = @$_SESSION['GroupName'];
299  }
300  }
301 }
Traceback_uri()
Get the URI without query to this location.
menu_html(&$menu, $indent)
Recursively generate the menu in HTML.
Definition: Menu.php:45
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...
Traceback_parm($ShowMod=1)
Get the URI query to this location.
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
const PARM_STRING
Definition: common-parm.php:29
OutputCSS()
Create the output CSS.
Definition: Menu.php:117
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...
Code for creating a menu list (2D linked list) from a set of plugins.
Definition: common-menu.php:30
Output($title=NULL)
Create the output.
Definition: Menu.php:232