TemplateService.php 5.5 KB
Newer Older
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
1
2
<?php

3
namespace UnicaenRenderer\Service\Template;
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
4
5
6
7
8
9

use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\QueryBuilder;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Service\EntityManagerAwareTrait;
10
use UnicaenRenderer\Entity\Db\Template;
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
11
use UnicaenRenderer\Service\Macro\MacroServiceAwareTrait;
12
use UnicaenUtilisateur\Service\User\UserServiceAwareTrait;
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
13
14
use Zend\Mvc\Controller\AbstractActionController;

15
class TemplateService {
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
16
17
    use EntityManagerAwareTrait;
    use MacroServiceAwareTrait;
18
    use UserServiceAwareTrait;
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
19
20
21
22

    /** GESTION DES ENTITES *******************************************************************************************/

    /**
23
24
     * @param Template $template
     * @return Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
25
     */
26
    public function create(Template $template) : Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
27
28
    {
        try {
29
30
            $this->getEntityManager()->persist($template);
            $this->getEntityManager()->flush($template);
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
31
        } catch (ORMException $e) {
32
            throw new RuntimeException("Un problème est survenue lors de l'enregistrement en BD du Template.", $e);
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
33
        }
34
        return $template;
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
35
36
37
    }

    /**
38
39
     * @param Template $template
     * @return Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
40
     */
41
    public function update(Template $template) : Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
42
43
    {
        try {
44
            $this->getEntityManager()->flush($template);
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
45
        } catch (ORMException $e) {
46
            throw new RuntimeException("Un problème est survenue lors de l'enregistrement en BD du Template.", $e);
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
47
        }
48
        return $template;
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
49
50
51
    }

    /**
52
53
     * @param Template $template
     * @return Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
54
     */
55
    public function delete(Template $template) : Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
56
57
58
    {
        try
        {
59
60
            $this->getEntityManager()->remove($template);
            $this->getEntityManager()->flush($template);
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
61
        } catch (ORMException $e) {
62
            throw new RuntimeException("Un problème est survenue lors de l'enregistrement en BD du Template.", $e);
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
63
        }
64
        return $template;
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
65
66
67
68
69
70
71
72
73
    }

    /** REQUETAGE *****************************************************************************************************/

    /**
     * @return QueryBuilder
     */
    public function createQueryBuilder() : QueryBuilder
    {
74
        $qb = $this->getEntityManager()->getRepository(Template::class)->createQueryBuilder('template')
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
75
76
77
78
79
80
81
82
        ;

        return $qb;
    }

    /**
     * @param string $champ
     * @param string $ordre
83
     * @return Template[]
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
84
     */
85
    public function getTemplates(string $champ = 'code', string $ordre = 'ASC') : array
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
86
87
    {
        $qb = $this->createQueryBuilder()
88
            ->orderBy('template.' . $champ, $ordre)
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
89
90
91
92
93
94
95
96
        ;

        $result = $qb->getQuery()->getResult();
        return $result;
    }

    /**
     * @param int|null $id
97
     * @return Template|null
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
98
     */
99
    public function getTemplate(?int $id) : ?Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
100
101
    {
        $qb = $this->createQueryBuilder()
102
            ->andWhere('template.id = :id')
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
103
104
105
106
107
108
            ->setParameter('id', $id)
        ;

        try {
            $result = $qb->getQuery()->getOneOrNullResult();
        } catch (NonUniqueResultException $e) {
109
            throw new RuntimeException("Plusieurs Template partagent le même id [".$id."]");
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
110
111
112
113
114
115
        }
        return $result;
    }

    /**
     * @param string $code
116
     * @return Template|null
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
117
     */
118
    public function getTemplateByCode(string $code) : ?Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
119
120
    {
        $qb = $this->createQueryBuilder()
121
            ->andWhere('template.code = :code')
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
122
123
124
125
126
127
            ->setParameter('code', $code)
        ;

        try {
            $result = $qb->getQuery()->getOneOrNullResult();
        } catch (NonUniqueResultException $e) {
128
129
            throw new RuntimeException("Plusieurs Template partagent le même code [".$code."]");
        }
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
130
131
132
133
134
135
        return $result;
    }

    /**
     * @param AbstractActionController $controller
     * @param string $param
136
     * @return Template|null
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
137
     */
138
    public function getRequestedTemplate(AbstractActionController $controller, string $param='template') : ?Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
139
140
    {
        $id = $controller->params()->fromRoute($param);
141
        $result = $this->getTemplate($id);
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
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

        return $result;
    }

    /** TRAITEMENTS DES MACROS ****************************************************************************************/

    /**
     * @param string $texteInitial
     * @param array $variables
     * @return string
     */
    private function replaceMacros(string $texteInitial, array $variables) : string
    {
        $matches = [];
        preg_match_all('/VAR\[[a-zA-Z0-9_]*#[a-zA-Z0-9_]*\]/', $texteInitial, $matches);

        $patterns = array_unique($matches[0]);
        $replacements = [];
        foreach ($patterns as $pattern) {
            $replacements[] = $this->getMacroService()->getTexte($pattern, $variables);
        }
        $text = str_replace($patterns, $replacements, $texteInitial);

        return $text;
    }

    /**
169
     * @param Template $template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
170
171
172
     * @param array $variables
     * @return string
     */
173
    public function generateCors(Template $template, array $variables) : string
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
174
175
    {
        $texte = "<style>";
176
        $texte .= $template->getCss();
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
177
        $texte .= "</style>";
178
        $texte .= $this->replaceMacros($template->getCorps(), $variables);
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
179
180
181
182
        return $texte;
    }

    /**
183
     * @param Template $template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
184
185
186
     * @param array $variables
     * @return string
     */
187
    public function generateSujet(Template $template, array $variables) : string
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
188
    {
189
        return $this->replaceMacros($template->getSujet(), $variables);
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
190
191
192
    }

}