-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPageInfo.php
240 lines (208 loc) · 6.42 KB
/
PageInfo.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
namespace Xanweb\PageInfo;
use Concrete\Core\Entity\File\File;
use Concrete\Core\Localization\Service\Date;
use Concrete\Core\Page\Page;
use Concrete\Core\Support\Facade\UserInfo;
use Concrete\Core\Url\Resolver\PageUrlResolver;
use Concrete\Core\Utility\Service\Text;
use League\URL\URLInterface;
class PageInfo
{
/**
* @var Page
*/
protected $page;
/**
* @var PageUrlResolver
*/
protected $urlResolver;
/**
* @var Text
*/
protected $th;
/**
* @var Date
*/
protected $dh;
/**
* @var Config
*/
protected $config;
/**
* @var URLInterface
*/
private $url;
/**
* @var \Concrete\Core\User\UserInfo
*/
private $pageAuthor;
/**
* @var \Concrete\Core\User\UserInfo
*/
private $lastEditor;
public function __construct(Page $page, PageUrlResolver $urlResolver, Text $th, Date $dh, Config $config)
{
$this->page = $page;
$this->th = $th;
$this->dh = $dh;
$this->urlResolver = $urlResolver;
$this->config = $config;
}
/**
* Get Page Name after applying htmlentities().
*
* @param int|null $truncateChars
* @param string $tail
*
* @return string
*/
public function fetchPageName(?int $truncateChars = null, string $tail = '…'): string
{
$pageName = '';
foreach ($this->config->getPageNameFetchers() as $fetcher) {
$pageName = (string) $fetcher->fetch($this->page);
if (!empty($pageName)) {
$pageName = $this->th->entities($pageName);
if ($truncateChars) {
$pageName = $this->th->shortenTextWord($pageName, $truncateChars, $tail);
}
break;
}
}
return $pageName;
}
/**
* Get Page Description.
*
* @param int|null $truncateChars
* @param string $tail
*
* @return string
*/
public function fetchPageDescription(?int $truncateChars = null, string $tail = '…'): string
{
$description = '';
foreach ($this->config->getPageDescriptionFetchers() as $fetcher) {
$description = (string) $fetcher->fetch($this->page);
if (!empty($description)) {
break;
}
}
return $truncateChars ? $this->th->shortenTextWord($description, $truncateChars, $tail) : $description;
}
/**
* Get Page Author.
*
* @return \Concrete\Core\User\UserInfo|null
*/
public function getAuthor(): ?\Concrete\Core\User\UserInfo
{
return $this->pageAuthor ?? $this->pageAuthor = UserInfo::getByID((int) $this->page->getCollectionUserID());
}
/**
* Get Latest Version Author.
*
* @return \Concrete\Core\User\UserInfo|null
*/
public function getLastEditor(): ?\Concrete\Core\User\UserInfo
{
if (!isset($this->lastEditor)) {
$cvAuthorID = (int) $this->page->getVersionObject()->getVersionAuthorUserID();
$this->lastEditor = UserInfo::getByID($cvAuthorID);
}
return $this->lastEditor;
}
/**
* Get Latest Version Author Name.
*
* @return string
*/
public function getLastEditorUserName(): string
{
$ui = $this->getLastEditor();
return $ui !== null ? $ui->getUserDisplayName() : '';
}
/**
* Get Page URL.
*
* @return URLInterface
*/
public function getURL(): ?URLInterface
{
return $this->url ?? $this->url = $this->urlResolver->resolve([$this->page]);
}
/**
* Get Navigation Target.
*
* @return string
*/
public function getTarget(): string
{
$akNavTarget = $this->config->getNavTargetAttributeKey();
$target = ($this->page->getCollectionPointerExternalLink() !== '' && $this->page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank' : $this->page->getAttribute($akNavTarget);
return empty($target) ? '_self' : $target;
}
/**
* Get Publish Date.
*
* @param string|null $format can be 'full', 'long', 'medium' or 'short' (see \Localization\Service\Date::formatDate()#$format)
* or a custom format (see http://www.php.net/manual/en/function.date.php for applicable formats)
*
* @return string
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/
public function getPublishDate(?string $format = null): string
{
$datePublic = $this->page->getCollectionDatePublic();
if ($format === null || in_array($format, ['full', 'long', 'medium', 'short'])) {
return $this->dh->formatDate($datePublic, $format ?? 'short');
}
return $this->dh->formatCustom($format, $datePublic);
}
/**
* Get Publish DateTime.
*
* @param bool $longDate Set to true for the long date format (eg 'December 31, 2000 at ...'), false (default) for the short format (eg '12/31/2000 at ...')
* @param bool $withSeconds Set to true to include seconds (eg '... at 11:59:59 PM'), false (default) otherwise (eg '... at 11:59 PM');
*
* @return string
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/
public function getPublishDateTime(bool $longDate = false, bool $withSeconds = false): string
{
$datePublic = $this->page->getCollectionDatePublic();
return $this->dh->formatDateTime($datePublic, $longDate, $withSeconds);
}
/**
* Get Page Tags.
*
* @return \Concrete\Core\Entity\Attribute\Value\Value\SelectValueOption[]
*/
public function getTags(): array
{
/** @var \Concrete\Core\Entity\Attribute\Value\Value\SelectValue $tags */
$tags = $this->page->getAttribute($this->config->getTagsAttributeKey());
return $tags !== null ? $tags->getSelectedOptions()->toArray() : [];
}
/**
* Get Main Page Thumbnail.
*
* @param File|null $fallbackThumbnail
*
* @return File|null
*/
public function fetchThumbnail(?File $fallbackThumbnail = null): ?File
{
$thumbnail = null;
foreach ($this->config->getThumbnailFetchers() as $fetcher) {
$thumbnail = $fetcher->fetch($this->page);
if ($thumbnail !== null) {
break;
}
}
return $thumbnail ?? $fallbackThumbnail;
}
}