forked from boomcms/boom-core
/
Link.php
84 lines (67 loc) · 2.1 KB
/
Link.php
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
<?php
namespace BoomCMS\Core\Chunk;
use BoomCMS\Foundation\Chunk\AcceptsHtmlString;
/**
* Link chunk - this is essentially a linkset which only has one link associated with it.
*/
class Link extends Linkset
{
use AcceptsHtmlString;
protected $defaultHtml = "<a href='{url}'>{text}</a>";
protected $links;
public function addContentToHtml($url, $text)
{
$html = $this->html ?: $this->defaultHtml;
return str_replace(['{url}', '{text}'], [$url, $text], $html);
}
public function attributes()
{
return [
$this->attributePrefix.'text' => $this->getText(),
$this->attributePrefix.'url' => (string) $this->getUrl(),
$this->attributePrefix.'target_page_id' => $this->getTargetPageId(),
];
}
public function show()
{
return $this->addContentToHtml($this->getUrl(), $this->getText());
}
public function showDefault()
{
return $this->addContentToHtml('#', $this->getPlaceholderText());
}
public function getLink()
{
if ($this->links === null) {
if (isset($this->attrs['links'])) {
$this->links = $this->attrs['links'];
if (!$this->editable) {
foreach ($this->links as $i => $link) {
if ($link->isInternal() && !$link->getLink()->getPage()->isVisible()) {
unset($this->links[$i]);
}
}
}
} else {
$this->links = [];
}
}
return isset($this->links[0]) ? $this->links[0] : null;
}
public function getTargetPageId()
{
return $this->hasContent() ? $this->getLink()->getTargetPageId() : 0;
}
public function getText()
{
return $this->hasContent() ? $this->getLink()->getTitle() : '';
}
public function getUrl()
{
return $this->hasContent() ? $this->getLink()->getTarget()->url() : '';
}
public function hasContent()
{
return $this->getLink() !== null;
}
}