1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File holding the abstract Menu class |
4
|
|
|
* |
5
|
|
|
* This file is part of the MediaWiki skin Chameleon. |
6
|
|
|
* |
7
|
|
|
* @copyright 2013 - 2014, Stephan Gambke |
8
|
|
|
* @license GNU General Public License, version 3 (or any later version) |
9
|
|
|
* |
10
|
|
|
* The Chameleon skin is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU General Public License as published by the Free |
12
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any |
13
|
|
|
* later version. |
14
|
|
|
* |
15
|
|
|
* The Chameleon skin is distributed in the hope that it will be useful, but |
16
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
18
|
|
|
* details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU General Public License along |
21
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
* @file |
24
|
|
|
* @ingroup Skins |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Skins\Chameleon\Menu; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class Menu |
31
|
|
|
* |
32
|
|
|
* @author Stephan Gambke |
33
|
|
|
* @since 1.0 |
34
|
|
|
* @ingroup Skins |
35
|
|
|
*/ |
36
|
|
|
abstract class Menu { |
37
|
|
|
|
38
|
|
|
private $menuItemFormatter = null; |
39
|
|
|
private $itemListFormatter = null; |
40
|
|
|
|
41
|
|
|
abstract public function getHtml(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $href |
45
|
|
|
* @param string $text |
46
|
|
|
* @param int $depth |
47
|
|
|
* @param string $subitems |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
1 |
|
protected function getHtmlForMenuItem( $href, $text, $depth, $subitems ) { |
52
|
1 |
|
return call_user_func( $this->getMenuItemFormatter(), $href, $text, $depth, $subitems ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return callable |
57
|
|
|
*/ |
58
|
|
|
public function getMenuItemFormatter() { |
59
|
|
|
|
60
|
|
|
if ( $this->menuItemFormatter === null ) { |
61
|
|
|
|
62
|
|
|
$this->setMenuItemFormatter( function ( $href, $text, $depth, $subitems ) { |
63
|
|
|
$href = \Sanitizer::cleanUrl( $href ); |
64
|
|
|
$text = htmlspecialchars( $text ); |
65
|
|
|
$indent = str_repeat( "\t", 2 * $depth ); |
66
|
|
|
|
67
|
|
|
if ( $subitems !== '' ) { |
68
|
|
|
return "$indent<li>\n$indent\t<a href=\"$href\">$text</a>\n$subitems$indent</li>\n"; |
69
|
|
|
} else { |
70
|
|
|
return "$indent<li><a href=\"$href\">$text</a></li>\n"; |
71
|
|
|
} |
72
|
|
|
} ); |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->menuItemFormatter; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param callable $menuItemFormatter |
81
|
|
|
*/ |
82
|
|
|
public function setMenuItemFormatter( $menuItemFormatter ) { |
83
|
|
|
$this->menuItemFormatter = $menuItemFormatter; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $rawItemsHtml |
88
|
|
|
* @param int $depth |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
1 |
|
protected function getHtmlForMenuItemList( $rawItemsHtml, $depth ) { |
93
|
1 |
|
return call_user_func( $this->getItemListFormatter(), $rawItemsHtml, $depth ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return callable |
98
|
|
|
*/ |
99
|
|
|
public function getItemListFormatter() { |
100
|
|
|
|
101
|
|
|
if ( $this->itemListFormatter === null ) { |
102
|
|
|
$this->setItemListFormatter( function ( $rawItemsHtml, $depth ) { |
103
|
|
|
$indent = str_repeat( "\t", 2 * $depth + 1 ); |
104
|
|
|
return "$indent<ul>\n$rawItemsHtml$indent</ul>\n"; |
105
|
|
|
} ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->itemListFormatter; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param callable $itemListFormatter |
113
|
|
|
*/ |
114
|
|
|
public function setItemListFormatter( $itemListFormatter ) { |
115
|
|
|
$this->itemListFormatter = $itemListFormatter; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|