|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\CMSBundle\Twig; |
|
4
|
|
|
|
|
5
|
|
|
use Cocur\Slugify\Slugify; |
|
6
|
|
|
use Doctrine\ORM\EntityManager; |
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use PiedWeb\CMSBundle\Entity\Media; |
|
9
|
|
|
use PiedWeb\CMSBundle\Entity\PageInterface as Page; |
|
10
|
|
|
use PiedWeb\CMSBundle\Service\PageCanonicalService; |
|
11
|
|
|
use PiedWeb\RenderAttributes\AttributesTrait; |
|
12
|
|
|
use Twig\Extension\AbstractExtension; |
|
13
|
|
|
use Twig\TwigFilter; |
|
14
|
|
|
use Twig\TwigFunction; |
|
15
|
|
|
use Twig_Environment; |
|
16
|
|
|
|
|
17
|
|
|
class AppExtension extends AbstractExtension |
|
18
|
|
|
{ |
|
19
|
|
|
use AttributesTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** @var PageCanonicalService */ |
|
22
|
|
|
protected $pageCanonical; |
|
23
|
|
|
|
|
24
|
|
|
/** @var EntityManagerInterface */ |
|
25
|
|
|
private $em; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
private $page_class; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(EntityManager $em, string $page_class, PageCanonicalService $pageCanonical) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->em = $em; |
|
33
|
|
|
$this->pageCanonical = $pageCanonical; |
|
34
|
|
|
$this->page_class = $page_class; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getFilters() |
|
38
|
|
|
{ |
|
39
|
|
|
return [ |
|
40
|
|
|
//new TwigFilter('markdown', [AppExtension::class, 'markdownToHtml'], ['is_safe' => ['all']]), |
|
41
|
|
|
new TwigFilter('html_entity_decode', 'html_entity_decode'), |
|
42
|
|
|
new TwigFilter( |
|
43
|
|
|
'punctuation_beautifer', |
|
44
|
|
|
[AppExtension::class, 'punctuationBeautifer'], |
|
45
|
|
|
['is_safe' => ['html']] |
|
46
|
|
|
), |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public static function convertMarkdownImage(string $body) |
|
51
|
|
|
{ |
|
52
|
|
|
return preg_replace( |
|
53
|
|
|
'/(?:!\[(.*?)\]\((.*?)\))/', |
|
54
|
|
|
'{%' |
|
55
|
|
|
.PHP_EOL.' include "@PiedWebCMS/component/_inline_image.html.twig" with {' |
|
56
|
|
|
.PHP_EOL.' "image_src" : "$2",' |
|
57
|
|
|
.PHP_EOL.' "image_alt" : "$1"' |
|
58
|
|
|
.PHP_EOL.' } only' |
|
59
|
|
|
.PHP_EOL.'%}'.PHP_EOL, |
|
60
|
|
|
$body |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getFunctions() |
|
65
|
|
|
{ |
|
66
|
|
|
return [ |
|
67
|
|
|
new TwigFunction('homepage', [$this->pageCanonical, 'generatePathForHomepage']), |
|
68
|
|
|
new TwigFunction('page', [$this->pageCanonical, 'generatePathForPage']), |
|
69
|
|
|
new TwigFunction('jslink', [AppExtension::class, 'renderJavascriptLink'], [ |
|
70
|
|
|
'is_safe' => ['html'], |
|
71
|
|
|
'needs_environment' => true, |
|
72
|
|
|
]), |
|
73
|
|
|
new TwigFunction('link', [AppExtension::class, 'renderJavascriptLink'], [ |
|
74
|
|
|
'is_safe' => ['html'], |
|
75
|
|
|
'needs_environment' => true, |
|
76
|
|
|
]), |
|
77
|
|
|
new TwigFunction('mail', [AppExtension::class, 'renderEncodedMail'], [ |
|
78
|
|
|
'is_safe' => ['html'], |
|
79
|
|
|
'needs_environment' => true, |
|
80
|
|
|
]), |
|
81
|
|
|
new TwigFunction( |
|
82
|
|
|
'bookmark', // = anchor |
|
83
|
|
|
[AppExtension::class, 'renderTxtAnchor'], |
|
84
|
|
|
['is_safe' => ['html'], 'needs_environment' => true] |
|
85
|
|
|
), |
|
86
|
|
|
new TwigFunction( |
|
87
|
|
|
'anchor', // = bookmark |
|
88
|
|
|
[AppExtension::class, 'renderTxtAnchor'], |
|
89
|
|
|
['is_safe' => ['html'], 'needs_environment' => true] |
|
90
|
|
|
), |
|
91
|
|
|
new TwigFunction( |
|
92
|
|
|
'isCurrentPage', |
|
93
|
|
|
[$this, 'isCurrentPage'], |
|
94
|
|
|
['is_safe' => ['html'], 'needs_environment' => false] |
|
95
|
|
|
), |
|
96
|
|
|
new TwigFunction( |
|
97
|
|
|
'gallery', |
|
98
|
|
|
[$this, 'renderGallery'], |
|
99
|
|
|
['is_safe' => ['html'], 'needs_environment' => true] |
|
100
|
|
|
), |
|
101
|
|
|
new TwigFunction( |
|
102
|
|
|
'list', |
|
103
|
|
|
[$this, 'renderPagesList'], |
|
104
|
|
|
['is_safe' => ['html'], 'needs_environment' => true] |
|
105
|
|
|
), |
|
106
|
|
|
new TwigFunction('isInternalImage', [AppExtension::class, 'isInternalImage']), |
|
107
|
|
|
new TwigFunction('getImageFrom', [AppExtension::class, 'transformInlineImageToMedia']), |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function renderPagesList( |
|
112
|
|
|
Twig_Environment $env, |
|
113
|
|
|
string $containing = '', |
|
114
|
|
|
int $number = 3, |
|
115
|
|
|
string $orderBy = 'createdAt', |
|
116
|
|
|
string $template = '@PiedWebCMS/page/_pages_list.html.twig' |
|
117
|
|
|
) { |
|
118
|
|
|
$qb = $this->em->getRepository($this->page_class)->getQueryToFindPublished('p'); |
|
|
|
|
|
|
119
|
|
|
$qb->andWhere('p.mainContent LIKE :containing')->setParameter('containing', '%'.$containing.'%'); |
|
120
|
|
|
$qb->orderBy('p.'.$orderBy, 'DESC'); |
|
121
|
|
|
$qb->setMaxResults($number); |
|
122
|
|
|
|
|
123
|
|
|
$pages = $qb->getQuery()->getResult(); |
|
124
|
|
|
|
|
125
|
|
|
return $env->render($template, ['pages' => $pages]); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public static function isInternalImage(string $media): bool |
|
129
|
|
|
{ |
|
130
|
|
|
return 0 === strpos($media, '/media/default/'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public static function transformInlineImageToMedia(string $src) |
|
134
|
|
|
{ |
|
135
|
|
|
if (self::isInternalImage($src)) { |
|
136
|
|
|
$src = substr($src, strlen('/media/default/')); |
|
137
|
|
|
|
|
138
|
|
|
$media = new Media(); |
|
139
|
|
|
$media->setRelativeDir('/media'); |
|
140
|
|
|
$media->setMedia($src); |
|
141
|
|
|
$media->setSlug(preg_replace('@(\.jpg|\.jpeg|\.png|\.gif)$@', '', $src), true); |
|
142
|
|
|
|
|
143
|
|
|
return $media; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$media = new Media(); |
|
147
|
|
|
$media->setRelativeDir($src); |
|
148
|
|
|
$media->setMedia(''); |
|
149
|
|
|
$media->setSlug('', true); |
|
150
|
|
|
|
|
151
|
|
|
return $media; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function renderGallery(Twig_Environment $env, Page $currentPage, $filterImageFrom = 1, $length = 1001) |
|
155
|
|
|
{ |
|
156
|
|
|
return $env->render('@PiedWebCMS/page/_gallery.html.twig', [ |
|
157
|
|
|
'page' => $currentPage, |
|
158
|
|
|
'galleryFilterFrom' => $filterImageFrom - 1, |
|
159
|
|
|
'length' => $length, |
|
160
|
|
|
]); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function isCurrentPage(string $uri, ?Page $currentPage) |
|
164
|
|
|
{ |
|
165
|
|
|
return |
|
166
|
|
|
null === $currentPage || $uri != $this->pageCanonical->generatePathForPage($currentPage->getRealSlug()) |
|
167
|
|
|
? false |
|
168
|
|
|
: true; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public static function renderTxtAnchor(Twig_Environment $env, $name) |
|
172
|
|
|
{ |
|
173
|
|
|
$slugify = new Slugify(); |
|
174
|
|
|
$name = $slugify->slugify($name); |
|
175
|
|
|
|
|
176
|
|
|
return $env->render('@PiedWebCMS/component/_txt_anchor.html.twig', ['name' => $name]); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public static function renderEncodedMail(Twig_Environment $env, $mail, $class = '') |
|
180
|
|
|
{ |
|
181
|
|
|
return $env->render('@PiedWebCMS/component/_encoded_mail.html.twig', [ |
|
182
|
|
|
'mail' => str_rot13($mail), |
|
183
|
|
|
'class' => $class, |
|
184
|
|
|
]); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public static function punctuationBeautifer($text) |
|
188
|
|
|
{ |
|
189
|
|
|
return str_replace( |
|
190
|
|
|
[' ;', ' :', ' ?', ' !', '« ', ' »', '« ', ' »'], |
|
191
|
|
|
[' ;', ' :', ' ?', ' !', '« ', ' »', '« ', ' »'], |
|
192
|
|
|
$text |
|
193
|
|
|
); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public static function renderJavascriptLink(Twig_Environment $env, $anchor, $path, $attr = []) |
|
197
|
|
|
{ |
|
198
|
|
|
if (0 === strpos($path, 'http://')) { |
|
199
|
|
|
$path = '-'.substr($path, 7); |
|
200
|
|
|
} elseif (0 === strpos($path, 'https://')) { |
|
201
|
|
|
$path = '_'.substr($path, 8); |
|
202
|
|
|
} elseif (0 === strpos($path, 'mailto:')) { |
|
203
|
|
|
$path = '@'.substr($path, 7); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return $env->render( |
|
207
|
|
|
'@PiedWebCMS/component/_javascript_link.html.twig', |
|
208
|
|
|
[ |
|
209
|
|
|
'attr' => self::mergeAndMapAttributes($attr, ['class' => 'a', 'data-rot' => str_rot13($path)]), |
|
210
|
|
|
'anchor' => $anchor, |
|
211
|
|
|
] |
|
212
|
|
|
); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|