Passed
Push — master ( 8526c1...04c591 )
by Dev
15:04
created

AppExtension::renderGallery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 4
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
3
namespace PiedWeb\CMSBundle\Twig;
4
5
use Cocur\Slugify\Slugify;
6
use PiedWeb\CMSBundle\Entity\PageInterface as Page;
7
use PiedWeb\CMSBundle\Service\PageCanonicalService;
8
use PiedWeb\RenderAttributes\AttributesTrait;
9
use Twig\Extension\AbstractExtension;
10
use Twig\TwigFilter;
11
use Twig\TwigFunction;
12
use Twig_Environment;
13
14
class AppExtension extends AbstractExtension
15
{
16
    use AttributesTrait;
17
18
    public function __construct(PageCanonicalService $pageCanonical)
19
    {
20
        $this->pageCanonical = $pageCanonical;
21
    }
22
23
    public function getFilters()
24
    {
25
        return [
26
            //new TwigFilter('markdown', [AppExtension::class, 'markdownToHtml'], ['is_safe' => ['all']]),
27
            new TwigFilter('html_entity_decode', 'html_entity_decode'),
28
            new TwigFilter(
29
                'punctuation_beautifer',
30
                [AppExtension::class, 'punctuationBeautifer'],
31
                ['is_safe' => ['html']]
32
            ),
33
        ];
34
    }
35
36
    public static function convertMarkdownImage(string $body)
37
    {
38
        return preg_replace(
39
            '/(?:!\[(.*?)\]\((.*?)\))/',
40
            '{%'
41
            .PHP_EOL.'    include "@PiedWebCMS/component/_inline_image.html.twig" with {'
42
            .PHP_EOL.'        "image_src" : "$2",'
43
            .PHP_EOL.'        "image_alt" : "$1"'
44
            .PHP_EOL.'    } only'
45
            .PHP_EOL.'%}'.PHP_EOL,
46
            $body
47
        );
48
    }
49
50
    public function getFunctions()
51
    {
52
        return [
53
            new TwigFunction('homepage', [$this->pageCanonical, 'generatePathForHomepage']),
54
            new TwigFunction('page', [$this->pageCanonical, 'generatePathForPage']),
55
            new TwigFunction('jslink', [AppExtension::class, 'renderJavascriptLink'], [
56
                'is_safe' => ['html'],
57
                'needs_environment' => true,
58
            ]),
59
            new TwigFunction('link', [AppExtension::class, 'renderJavascriptLink'], [
60
                'is_safe' => ['html'],
61
                'needs_environment' => true,
62
            ]),
63
            new TwigFunction('mail', [AppExtension::class, 'renderEncodedMail'], [
64
                'is_safe' => ['html'],
65
                'needs_environment' => true,
66
            ]),
67
            new TwigFunction(
68
                'bookmark', // = anchor
69
                [AppExtension::class, 'renderTxtAnchor'],
70
                ['is_safe' => ['html'], 'needs_environment' => true]
71
            ),
72
            new TwigFunction(
73
                'anchor', // = bookmark
74
                [AppExtension::class, 'renderTxtAnchor'],
75
                ['is_safe' => ['html'], 'needs_environment' => true]
76
            ),
77
            new TwigFunction(
78
                'mail', // = bookmark
79
                [AppExtension::class, 'renderMail'],
80
                ['is_safe' => ['html'], 'needs_environment' => true]
81
            ),
82
            new TwigFunction(
83
                'isCurrentPage', // = bookmark
84
                [$this, 'isCurrentPage'],
85
                ['is_safe' => ['html'], 'needs_environment' => false]
86
            ),
87
            new TwigFunction(
88
                'gallery',
89
                [$this, 'renderGallery'],
90
                ['is_safe' => ['html'], 'needs_environment' => true]
91
            ),
92
        ];
93
    }
94
95
    public function renderGallery(Twig_Environment $env, Page $currentPage, $filterImageFrom = 1, $filterImageTo = 1001)
96
    {
97
        return $env->render('@PiedWebCMS/page/_gallery.html.twig', [
98
            'page' => $currentPage,
99
            'galleryFilterFrom' => $filterImageFrom - 1,
100
            'galleryFilterTo' => $filterImageTo - 1,
101
        ]);
102
    }
103
104
    public function isCurrentPage(string $uri, ?Page $currentPage)
105
    {
106
        return
107
            null === $currentPage || $uri != $this->pageCanonical->generatePathForPage($currentPage->getRealSlug())
108
            ? false
109
            : true;
110
    }
111
112
    public static function renderTxtAnchor(Twig_Environment $env, $name)
113
    {
114
        $slugify = new Slugify();
115
        $name = $slugify->slugify($name);
116
117
        return $env->render('@PiedWebCMS/component/_txt_anchor.html.twig', ['name' => $name]);
118
    }
119
120
    public static function renderMail(Twig_Environment $env, $mail, $class = '')
121
    {
122
        $mail = str_rot13($mail);
123
124
        return $env->render('@PiedWebCMS/component/_mail.html.twig', ['mail' => $mail, 'class' => $class]);
125
    }
126
127
    public static function renderEncodedMail(Twig_Environment $env, $mail)
128
    {
129
        return $env->render('@PiedWebCMS/component/_encoded_mail.html.twig', ['mail' => str_rot13($mail)]);
130
    }
131
132
    public static function punctuationBeautifer($text)
133
    {
134
        return str_replace(
135
            [' ;', ' :', ' ?', ' !', '« ', ' »', '&laquo; ', ' &raquo;'],
136
            ['&nbsp;;', '&nbsp;:', '&nbsp;?', '&nbsp;!', '«&nbsp;', '&nbsp;»', '&laquo;&nbsp;', '&nbsp;&raquo;'],
137
            $text
138
        );
139
    }
140
141
    public static function renderJavascriptLink(Twig_Environment $env, $anchor, $path, $attr = [])
142
    {
143
        if (0 === strpos($path, 'http://')) {
144
            $path = '-'.substr($path, 7);
145
        } elseif (0 === strpos($path, 'https://')) {
146
            $path = '_'.substr($path, 8);
147
        } elseif (0 === strpos($path, 'mailto:')) {
148
            $path = '@'.substr($path, 7);
149
        }
150
151
        return $env->render(
152
            '@PiedWebCMS/component/_javascript_link.html.twig',
153
            [
154
            'attr' => self::mergeAndMapAttributes($attr, ['data-rot' => str_rot13($path)]),
155
            'anchor' => $anchor,
156
            ]
157
        );
158
    }
159
}
160