1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
<?php
/**
***********************************************************************************************
* Class manages display of menus
*
* @copyright 2004-2016 The Admidio Team
* @see http://www.admidio.org/
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
***********************************************************************************************
*/
/**
* @class Menu
* Create, modify and display menus. Each menu item is defined by
*
* - $id : identifier of the menu item
* - $link : URL, relative to the admidio root directory, starting with a /
* or full URL with http or https protocol
* - $text : menu text
* - $icon : URL, relative to the theme plugin, starting with a /
* : or full URL with http or https protocol
* - $desc : (optional) long description of the menu item
*/
class Menu
{
protected $id;
protected $title;
protected $items;
protected $root_path;
/**
* constructor
* @param string $id
* @param string $title
*/
public function __construct($id, $title)
{
global $g_root_path;
$this->id = $id;
$this->title = $title;
$this->items = array();
$this->root_path = $g_root_path;
}
/**
* @param string $id
* @param string $link
* @param string $text
* @param string $icon
* @param string $desc
* @return array<string,string|array>
*/
private function mkItem($id, $link, $text, $icon, $desc = '')
{
// add root path to link unless the full URL is given
if (preg_match('/^http(s?):\/\//', $link) === 0)
{
$link = $this->root_path . $link;
}
// add THEME_PATH to images unless the full URL is given
if (preg_match('/^http(s?):\/\//', $icon) === 0)
{
$icon = THEME_PATH . $icon;
}
return array(
'id' => $id,
'link' => $link,
'text' => $text,
'icon' => $icon,
'desc' => $desc,
'subitems' => array()
);
}
/**
* @param string $id
* @param string $link
* @param string $text
* @param string $icon
* @param string $desc
*/
public function addItem($id, $link, $text, $icon, $desc = '')
{
$this->items[$id] = $this->mkItem($id, $link, $text, $icon, $desc);
}
/**
* @param string $parentId
* @param string $id
* @param string $link
* @param string $text
*/
public function addSubItem($parentId, $id, $link, $text)
{
// add root path to link unless the full URL is given
if (preg_match('/^http(s?):\/\//', $link) === 0)
{
$link = $this->root_path . $link;
}
$this->items[$parentId]['subitems'][$id] = array('link' => $link, 'text' => $text);
}
/**
* gets the position of a given ID in the menu
* @param string $id
* @return int|false
*/
public function getPosition($id)
{
$keys = array_keys($this->items);
return array_search($id, $keys, true);
}
/**
* inserts a new menu entry before the named position
* @param int $position
* @param string $id
* @param string $link
* @param string $text
* @param string $icon
* @param string $desc
*/
public function insertItem($position, $id, $link, $text, $icon, $desc = '')
{
$item = $this->mkItem($id, $link, $text, $icon, $desc);
$insert = array($id => $item);
$this->items = array_splice($this->items, $position, 0, $insert);
}
/**
* Create the html menu from the internal array that must be filled before.
* You have the option to create a simple menu with icon and link or
* a more complex menu with submenu and description text.
* @param bool $complex Create a @b simple menu as default. If you set the param to @b true
* then you will create a menu with submenus and description
* @return string Return the html code of the form.
*/
public function show($complex = false)
{
$html = '';
if ($complex)
{
$html .= '<h2 id="head_'.$this->id.'">'.$this->title.'</h2>';
$html .= '<menu id="menu_'.$this->id.'" class="list-unstyled admidio-media-menu">'; // or class="media-list"
}
else
{
$html .= '<h3 id="head_'.$this->id.'">'.$this->title.'</h3>';
$html .= '<menu id="menu_'.$this->id.'" class="list-unstyled admidio-menu btn-group-vertical">';
}
// now create each menu item
foreach($this->items as $item)
{
if ($complex)
{
$html .= '
<li class="media">
<div class="media-left">
<a id="menu_'.$this->id.'_'.$item['id'].'" href="'.$item['link'].'">
<img class="media-object" src="'.$item['icon'].'" alt="'.strip_tags($item['text']).'" />
</a>
</div>
<div class="media-body">
<h4 class="media-heading">
<a id="lmenu_'.$this->id.'_'.$item['id'].'" href="'.$item['link'].'">'.$item['text'].'</a>
</h4>';
// adding submenus if any
if ($item['subitems'])
{
$html .= '<menu id="lsubmenu_'.$this->id.'_'.$item['id'].'" class="list-inline admidio-media-submenu">';
foreach($item['subitems'] as $subitem)
{
$html .= '<li><a href="'.$subitem['link'].'">'.$subitem['text'].'</a></li>';
}
$html .= '</menu>'; // closes sub-menu "menu.admidio-media-submenu"
}
$html .= '<p>'.$item['desc'].'</p>';
$html .= '</div></li>'; // closes "div.media-body" and "li.media"
}
else
{
$html .= '
<li>
<a id="lmenu_'.$this->id.'_'.$item['id'].'" class="btn" href="'.$item['link'].'">
<img src="'.$item['icon'].'" alt="'.strip_tags($item['text']).'" />'.$item['text'].'
</a>
</li>';
}
}
$html .= '</menu>'; // closes main-menu "menu.list-unstyled"
if (count($this->items) > 0)
{
return $html;
}
return '';
}
}