4
4
5
5
use DateTimeInterface ;
6
6
use Exception ;
7
+ use Kreatif \Model ;
8
+ use Kreatif \Utils ;
7
9
use RexGraphQL \Type \Media \Media ;
8
10
use rex ;
9
11
use rex_article ;
15
17
use TheCodingMachine \GraphQLite \Exceptions \GraphQLException ;
16
18
use Url \UrlManager ;
17
19
20
+
18
21
#[Type]
19
22
class Metadata
20
23
{
@@ -24,19 +27,26 @@ class Metadata
24
27
private ?string $ canonical ;
25
28
private ?string $ image ;
26
29
27
- private ?int $ createdAt ;
28
- private ?int $ updatedAt ;
30
+ private ?int $ createdAt ;
31
+ private ?int $ updatedAt ;
29
32
public ?string $ type = 'article ' ;
30
33
31
- public function __construct (string $ title = null , string $ description = null , string $ robots = null , string $ canonical = null , string $ image = null , ?int $ createdAt = null , ?int $ updatedAt = null )
32
- {
33
- $ this ->title = $ title ;
34
+ public function __construct (
35
+ string $ title = null ,
36
+ string $ description = null ,
37
+ string $ robots = null ,
38
+ string $ canonical = null ,
39
+ string $ image = null ,
40
+ ?int $ createdAt = null ,
41
+ ?int $ updatedAt = null
42
+ ) {
43
+ $ this ->title = $ title ;
34
44
$ this ->description = $ description ;
35
- $ this ->robots = $ robots ;
36
- $ this ->canonical = $ canonical ;
37
- $ this ->image = $ image ;
38
- $ this ->createdAt = $ createdAt ;
39
- $ this ->updatedAt = $ updatedAt ;
45
+ $ this ->robots = $ robots ;
46
+ $ this ->canonical = $ canonical ;
47
+ $ this ->image = $ image ;
48
+ $ this ->createdAt = $ createdAt ;
49
+ $ this ->updatedAt = $ updatedAt ;
40
50
}
41
51
42
52
#[Field]
@@ -48,7 +58,15 @@ public function getTitle(): string
48
58
#[Field]
49
59
public function getDescription (): string
50
60
{
51
- return $ this ->description ;
61
+ // shorten the description to 160 characters
62
+ return Utils::cropText ($ this ->description , 160 );
63
+ }
64
+
65
+ #[Field]
66
+ public function getPreviewText (int $ wordCount = 200 , string $ suffix = '... ' , string $ allowedTags = '<p><strong> ' ): string
67
+ {
68
+ // crop description to $wordCount words and close open tags
69
+ return Utils::cropText ($ this ->description , $ wordCount , $ suffix , $ allowedTags );
52
70
}
53
71
54
72
#[Field]
@@ -113,11 +131,9 @@ public static function getByArticleId(int $elementId, int $clangId): self
113
131
if (!$ article ) {
114
132
throw new GraphQLException ('Article not found ' );
115
133
}
116
- $ seo = new rex_yrewrite_seo ($ article ->getId (), $ clangId );
134
+ $ seo = new rex_yrewrite_seo ($ article ->getId (), $ clangId );
117
135
$ robots = 'noindex, nofollow ' ;
118
- $ index = $ article ->getValue (
119
- rex_yrewrite_seo::$ meta_index_field ,
120
- ) ?? rex_yrewrite_seo::$ index_setting_default ;
136
+ $ index = $ article ->getValue (rex_yrewrite_seo::$ meta_index_field ,) ?? rex_yrewrite_seo::$ index_setting_default ;
121
137
if (1 == $ index || (0 == $ index && $ article ->isOnline ())) {
122
138
$ robots = 'index, follow ' ;
123
139
} elseif (2 == $ index ) {
@@ -139,35 +155,31 @@ public static function getByArticleId(int $elementId, int $clangId): self
139
155
/**
140
156
* @throws GraphQLException|rex_exception if the url object is not found
141
157
*/
142
- public static function getByUrlObject (): self
158
+ public static function getByUrlObject ($ modelObject = null ): ? self
143
159
{
144
- /** @var UrlManager $urlObject */
145
- $ urlObject = rex::getProperty ('url_object ' );
160
+ if ($ modelObject && $ modelObject instanceof Model) {
161
+ $ urlObject = $ modelObject ->getUrlObject ();
162
+ } else {
163
+ /** @var UrlManager $urlObject */
164
+ $ urlObject = rex::getProperty ('url_object ' );
165
+ }
146
166
if (!$ urlObject ) {
147
- throw new GraphQLException ( ' Url object not found ' ) ;
167
+ return null ;
148
168
}
149
169
$ createdAt = strtotime ($ urlObject ->getValue ('createdate ' ));
150
170
$ updatedAt = strtotime ($ urlObject ->getValue ('updatedate ' ));
151
- $ title = htmlspecialchars_decode (trim (rex_yrewrite::getCurrentDomain ()->getTitle ()));
171
+ $ title = htmlspecialchars_decode (trim (rex_yrewrite::getCurrentDomain ()->getTitle ()));
152
172
if ('' == $ title ) {
153
173
$ title = rex_yrewrite_seo::$ title_scheme_default ;
154
174
}
155
175
$ title = str_replace ('%T ' , $ urlObject ->getSeoTitle (), $ title );
156
176
$ title = str_replace ('%SN ' , rex::getServerName (), $ title );
157
177
158
178
$ image = $ urlObject ->getSeoImage ();
159
- if ($ image ) {
179
+ if ($ image ) {
160
180
$ image = explode (', ' , $ image )[0 ];
161
181
}
162
- $ item = new self (
163
- $ title ,
164
- $ urlObject ->getSeoDescription (),
165
- 'index, follow ' ,
166
- '' ,
167
- $ image ,
168
- $ createdAt ,
169
- $ updatedAt ,
170
- );
182
+ $ item = new self ($ title , $ urlObject ->getSeoDescription (), 'index, follow ' , '' , $ image , $ createdAt , $ updatedAt ,);
171
183
$ item ->type = $ urlObject ->getProfile ()->getNamespace ();
172
184
return $ item ;
173
185
}
0 commit comments