<?php
class IPxeMenu
{
protected $menuid;
public $timeoutMs;
public $title;
public $defaultEntryId;
/**
* @var MenuEntry[]
*/
public $items = [];
/**
* @param int $menuId
*/
public static function get($menuId, $emptyFallback = false)
{
$menu = Database::queryFirst("SELECT menuid, timeoutms, title, defaultentryid FROM serversetup_menu
WHERE menuid = :menuid LIMIT 1", ['menuid' => $menuId]);
if ($menu !== false)
return new IPxeMenu($menu);
if (!$emptyFallback)
return null;
return new EmptyIPxeMenu();
}
/**
* IPxeMenu constructor.
*
* @param array $menu array for according menu row
*/
public function __construct($menu)
{
$this->menuid = (int)$menu['menuid'];
$this->timeoutMs = (int)$menu['timeoutms'];
$this->title = $menu['title'];
$this->defaultEntryId = $menu['defaultentryid'];
$res = Database::simpleQuery("SELECT e.menuentryid, e.entryid, e.refmenuid, e.hotkey, e.title,
e.hidden, e.sortval, e.md5pass, b.module, b.data AS bootentry, b.title AS betitle
FROM serversetup_menuentry e
LEFT JOIN serversetup_bootentry b USING (entryid)
WHERE e.menuid = :menuid
ORDER BY e.sortval ASC, e.title ASC", ['menuid' => $menu['menuid']]);
while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
$this->items[] = new MenuEntry($row);
}
// Make sure we have a default entry if the menu isn't empty
if ($this->defaultEntryId === null && !empty($this->items)) {
$this->defaultEntryId = $this->items[0]->menuEntryId();
}
}
public function title()
{
return $this->title;
}
public function timeoutMs()
{
return $this->timeoutMs;
}
/**
* @return int Number of items in this menu
*/
public function itemCount()
{
return count($this->items);
}
/**
* @return string|null Return script label of default entry, null if not set
*/
public function getDefaultEntryId()
{
return $this->defaultEntryId;
}
/**
* @return MenuEntry|null Return preselected menu entry
*/
public function defaultEntry()
{
foreach ($this->items as $item) {
if ($item->menuEntryId() == $this->defaultEntryId)
return $item;
}
return null;
}
/*
*
*/
public static function forLocation($locationId) : IPxeMenu
{
$chain = null;
if (Module::isAvailable('locations')) {
$chain = Location::getLocationRootChain($locationId);
}
if (!empty($chain)) {
$res = Database::simpleQuery("SELECT m.menuid, m.timeoutms, m.title,
IFNULL(ml.defaultentryid, m.defaultentryid) AS defaultentryid, ml.locationid
FROM serversetup_menu m
INNER JOIN serversetup_menu_location ml USING (menuid)
WHERE ml.locationid IN (:chain)", ['chain' => $chain]);
if ($res->rowCount() > 0) {
// Make the location id key, preserving order (closest location is first)
$chain = array_flip($chain);
while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
// Overwrite the value (numeric ascending values, useless) with menu array of according location
$chain[(int)$row['locationid']] = $row;
}
// Use first one that was found
foreach ($chain as $menu) {
if (is_array($menu)) {
return new IPxeMenu($menu);
}
}
// Should never end up here, but we'd just fall through and use the default
}
}
// We're here, no specific menu, use default
$menu = Database::queryFirst("SELECT menuid, timeoutms, title, defaultentryid
FROM serversetup_menu
ORDER BY isdefault DESC LIMIT 1");
if ($menu === false) {
return new EmptyIPxeMenu;
}
return new IPxeMenu($menu);
}
public static function forClient($ip, $uuid) : IPxeMenu
{
$locationId = 0;
if (Module::isAvailable('locations')) {
$locationId = Location::getFromIpAndUuid($ip, $uuid);
}
return self::forLocation($locationId);
}
}
class EmptyIPxeMenu extends IPxeMenu
{
/** @noinspection PhpMissingParentConstructorInspection */
public function __construct()
{
$this->title = 'No menu defined';
$this->menuid = -1;
$this->items[] = new MenuEntry([
'title' => 'Please create a menu in Server-Setup first'
]);
$this->items[] = new MenuEntry([
'title' => 'Bitte erstellen Sie zunächst ein Menü'
]);
}
}