Newer
Older
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
<?php
/**
* @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
* @license GPL2 or any later version
*/
class LinkElement extends WidgetElement implements ArrayAccess
{
/**
* Create link by parsing a html chunk.
*
* @param String $html HTML chunk to parse
* @param Icon $icon Optional icon
* @return LinkElement Link element from parsed html
* @throws Exception if html can not be parsed
*/
public static function fromHTML($html, \Icon $icon = null)
{
$matched = preg_match('~(<a(?P<attributes>(?:\s+\w+=".*?")+)>\s*(?P<label>.*?)\s*</a>)~s', $html, $match);
if (!$matched) {
throw new Exception('Could not parse html');
}
$attributes = self::parseAttributes($match['attributes']);
$url = $attributes['href'] ?: '#';
unset($attributes['href']);
return new self(html_entity_decode($match['label']), html_entity_decode($url), $icon, $attributes);
}
/**
* Parse a string of html attributes into an associative array.
*
* @param String $text String of html attributes
* @return Array parsed attributes as key => value pairs
* @see https://gist.github.com/rodneyrehm/3070128
*/
protected static function parseAttributes($text)
{
$attributes = [];
$pattern = '#(?(DEFINE)
(?<name>[a-zA-Z][a-zA-Z0-9-:]*)
(?<value_double>"[^"]+")
(?<value_single>\'[^\']+\')
(?<value_none>[^\s>]+)
(?<value>((?&value_double)|(?&value_single)|(?&value_none)))
)
(?<n>(?&name))(=(?<v>(?&value)))?#xs';
if (preg_match_all($pattern, $text, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$attributes[$match['n']] = isset($match['v'])
? html_entity_decode(trim($match['v'], '\'"'))
: null;
}
}
return $attributes;
}
public $url;
public $label;
public $icon;
public $active = false;
public $attributes = [];
public $as_button = false;
/**
* create a link for a widget
*
* @param String $label Label/content of the link
* @param String $url URL/Location of the link (raw url, no entities)
* @param Icon $icon Icon for the link
* @param array $attributes HTML-attributes for the a-tag in an associative array.
*/
public function __construct($label, $url, \Icon $icon = null, $attributes = [])
{
parent::__construct();
// TODO: Remove this some versions after 5.0
$url = html_entity_decode($url);
$this->label = $label;
$this->url = $url;
$this->attributes = $attributes;
$this->icon = $icon;
}
/**
* Sets the active state of the element.
*
* @param bool $active Active state (optional, defaults to true)
* @return LinkElement instance to allow chaining
*/
public function setActive($active = true)
{
$this->active = $active;
return $this;
}
/**
* Sets the dialog options for the element. Passing false as $state will
* reset the dialog options to "none".
*
* @param mixed $active Dialog options (optional, defaults to blank/standard
* dialog)
* @return LinkElement instance to allow chaining
*/
public function asDialog($state = '')
{
if ($state !== false) {
$this->attributes['data-dialog'] = $state;
} else {
unset($this->attributes['data-dialog']);
}
return $this;
}
/**
* Defines whether the link should be rendered as a button/form with POST
* method.
*
* @param bool $active State (optional, defaults to true)
* @return LinkElement instance to allow chaining
*/
public function asButton($state = true)
{
$this->as_button = $state;
return $this;
}
/**
* Sets the target attribute of the element.
*
* @param string $target Target attribute
* @return LinkElement instance to allow chaining
*/
public function setTarget($target)
{
if ($target) {
$this->attributes['target'] = $target;
} else {
unset($this->attributes['target']);
}
return $this;
}
/**
* Adds a css class to the rendered element.
*
* @param string $clas CSS class to add
* @return LinkElement instance to allow chaining
*/
public function addClass($class)
{
$this->attributes['class'] = isset($this->attributes['class'])
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
211
212
213
? $this->attributes['class'] . " " . $class
: $class;
return $this;
}
/**
* Set disabled state
*
* @param boolean $state Disabled state
* @return LinkElement instance to allow chaining
*/
public function setDisabled($state = true)
{
if ($state) {
$this->attributes['disabled'] = true;
} else {
unset($this->attributes['disabled']);
}
return $this;
}
/**
* Returns whether the element is disabled.
*
* @return bool
*/
public function isDisabled()
{
return isset($this->attributes['disabled']) && $this->attributes['disabled'] !== false;
}
/**
* Renders the element.
*
* @return string
*/
public function render()
{
$disabled = $this->isDisabled();
if ($this->as_button && !$disabled) {
return $this->renderButton();
}
if ($this->active) {
$this->addClass('active');
}
$attributes = (array) $this->attributes;
if ($disabled) {
$tag = 'span';
} else {
$tag = 'a';
$attributes['href'] = $this->url;
}
return sprintf(
$tag,
arrayToHtmlAttributes($attributes),
$this->icon ? $this->icon->asImg(null, ['class' => 'text-bottom']) : '',
htmlReady($this->label)
);
}
/**
* Renders the element as a button/form.
*
* @return string
*/
protected function renderButton()
{
return sprintf(
'<form action="%1$s" method="post" %2$s>%3$s<button type="submit">%4$s</button></form>',
htmlReady($this->url),
arrayToHtmlAttributes((array) $this->attributes),
CSRFProtection::tokenTag(),
htmlReady($this->label)
);
}
/**
* Returns whether the given url is valid.
*
* @param string $url URL to test
* @return bool
*/
protected function isURL($url)
{
return filter_var($url, FILTER_VALIDATE_URL) !== false;
}
// Array access for attributes
/**
* @todo Add bool return type when Stud.IP requires PHP8 minimal
*/
#[ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->attributes[$offset]);
}
/**
* @param $offset
* @return mixed
*
* @todo Add mixed return type when Stud.IP requires PHP8 minimal
*/
#[ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->attributes[$offset];
}
/**
* @todo Add void return type when Stud.IP requires PHP8 minimal
*/
#[ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
$this->attributes[$offset] = $value;
}
/**
* @todo Add void return type when Stud.IP requires PHP8 minimal
*/
#[ReturnTypeWillChange]
public function offsetUnset($offset)
{
unset($this->attributes[$offset]);
}
}