|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Helper\HTML; |
|
4
|
|
|
|
|
5
|
|
|
use Ffcms\Core\Helper\HTML\System\Dom; |
|
6
|
|
|
use Ffcms\Core\Helper\HTML\System\NativeGenerator; |
|
7
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
|
8
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
|
9
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Listing - build html listing carcase |
|
13
|
|
|
* @package Ffcms\Core\Helper\HTML |
|
14
|
|
|
*/ |
|
15
|
|
|
class Listing extends NativeGenerator |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Construct listing elements,property's and future's |
|
20
|
|
|
* @param array $elements |
|
21
|
|
|
* @return string |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function display($elements) |
|
24
|
|
|
{ |
|
25
|
|
|
// check input elements |
|
26
|
|
|
if (!Arr::in($elements['type'], ['ul', 'ol']) || count($elements['items']) < 1) { |
|
27
|
|
|
return null; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
// initialize new DOM model |
|
31
|
|
|
$dom = new Dom(); |
|
32
|
|
|
// return DOM-HTML, build based on closures! |
|
33
|
|
|
return $dom->{$elements['type']}(function () use ($dom, $elements) { |
|
34
|
|
|
// prepare output avg variable |
|
35
|
|
|
$itemHTML = null; |
|
36
|
|
|
// get active order level |
|
37
|
|
|
$orderActiveLink = false; |
|
38
|
|
|
if (isset($elements['activeOrder'])) { |
|
39
|
|
|
$orderActiveLink = $elements['activeOrder']; |
|
40
|
|
|
unset($elements['activeOrder']); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
foreach ($elements['items'] as $item) { |
|
44
|
|
|
// sounds like dropdown array |
|
45
|
|
|
if (isset($item['dropdown']) && isset($item['items'])) { |
|
46
|
|
|
$itemHTML .= self::buildDropdown($dom, $item); |
|
47
|
|
|
} elseif (isset($item['link'])) { // looks like link item |
|
48
|
|
|
$itemHTML .= self::buildLink($dom, $item, $orderActiveLink); |
|
49
|
|
|
} else { // just text item |
|
50
|
|
|
$itemHTML .= self::buildText($dom, $item); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
return $itemHTML; |
|
54
|
|
|
}, $elements['property']); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Build dropdown dom element |
|
59
|
|
|
* @param Dom $dom |
|
60
|
|
|
* @param array $item |
|
61
|
|
|
*/ |
|
62
|
|
|
private static function buildDropdown($dom, $item) |
|
63
|
|
|
{ |
|
64
|
|
|
if (!Obj::isArray($item['items'])) { |
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $dom->li(function() use($dom, $item){ |
|
69
|
|
|
$dropdownLink = $dom->a(function() use ($dom, $item){ |
|
70
|
|
|
return self::applyEscape($item['text'], $item['html'], $item['!secure']) . ' ' . $dom->span(function(){}, ['class' => 'caret']); |
|
71
|
|
|
}, $item['dropdown']); |
|
72
|
|
|
|
|
73
|
|
|
$dropdownElements = $dom->ul(function() use ($dom, $item){ |
|
74
|
|
|
$resp = null; |
|
75
|
|
|
foreach ($item['items'] as $obj) { |
|
76
|
|
|
$resp .= self::buildLink($dom, $obj, false); |
|
77
|
|
|
} |
|
78
|
|
|
return $resp; |
|
79
|
|
|
}, ['class' => 'dropdown-menu']); |
|
80
|
|
|
|
|
81
|
|
|
return $dropdownLink . $dropdownElements; |
|
82
|
|
|
}, $item['property']); |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Build text item |
|
88
|
|
|
* @param unknown $dom |
|
89
|
|
|
* @param unknown $item |
|
90
|
|
|
*/ |
|
91
|
|
|
private static function buildText($dom, $item) |
|
92
|
|
|
{ |
|
93
|
|
|
$text = self::applyEscape($item['text'], $item['html'], $item['!secure']); |
|
94
|
|
|
return $dom->li(function() use ($text){ |
|
95
|
|
|
return $text; |
|
96
|
|
|
}, $item['property']); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Build link with active definition in listing |
|
101
|
|
|
* @param Dom $dom |
|
102
|
|
|
* @param array $item |
|
103
|
|
|
* @param string $orderActiveLink |
|
104
|
|
|
*/ |
|
105
|
|
|
private static function buildLink($dom, $item, $orderActiveLink = false) |
|
106
|
|
|
{ |
|
107
|
|
|
// set default link data - text and properties |
|
108
|
|
|
$text = self::applyEscape($item['text'], $item['html'], $item['!secure']); |
|
109
|
|
|
$properties = $item['property']; |
|
110
|
|
|
|
|
111
|
|
|
// try to parse link format for controller/action definition (must be array: 'main/index' to ['main/index']) |
|
112
|
|
|
if (!Obj::isArray($item['link']) && !Str::startsWith('http', $item['link']) && !Str::startsWith('#', $item['link'])) { |
|
113
|
|
|
$item['link'] = [$item['link']]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// if its a controller/action definition try to work with active class |
|
117
|
|
|
if (Obj::isArray($item['link'])) { |
|
118
|
|
|
// check if css class for active item is defined |
|
119
|
|
|
if (!isset($item['activeClass'])) { |
|
120
|
|
|
$item['activeClass'] = 'active'; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
// check if element is active on current URI |
|
124
|
|
|
if (self::isCurrentLink($item['link'], $item['activeOn'], $orderActiveLink) === true) { |
|
|
|
|
|
|
125
|
|
|
$properties['class'] = Str::concat(' ', $item['activeClass'], $properties['class']); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// set href source for link |
|
130
|
|
|
$item['linkProperty']['href'] = self::convertLink($item['link']); |
|
131
|
|
|
|
|
132
|
|
|
// build output <li@params><a @params>@text</li> |
|
133
|
|
|
return $dom->li(function () use ($dom, $text, $item) { |
|
134
|
|
|
return $dom->a(function() use ($text) { |
|
135
|
|
|
return $text; |
|
136
|
|
|
}, $item['linkProperty']); |
|
137
|
|
|
}, $properties); |
|
138
|
|
|
} |
|
139
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: