TemplateService.php 6.65 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
29
30
31
    {
        try {
            $this->getEntityManager()->persist($content);
            $this->getEntityManager()->flush($content);
        } 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 historise(Template $template) : Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
56
57
    {
        try {
58
59
            $template->historiser();
            $this->getEntityManager()->flush($template);
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
60
        } catch (ORMException $e) {
61
            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
62
        }
63
        return $template;
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
64
65
66
    }

    /**
67
68
     * @param Template $template
     * @return Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
69
     */
70
    public function restore(Template $template) : Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
71
72
    {
        try {
73
74
            $template->dehistoriser();
            $this->getEntityManager()->flush($template);
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
75
        } catch (ORMException $e) {
76
            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
77
        }
78
        return $template;
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
79
80
81
    }

    /**
82
83
     * @param Template $template
     * @return Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
84
     */
85
    public function delete(Template $template) : Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
86
87
88
    {
        try
        {
89
90
            $this->getEntityManager()->remove($template);
            $this->getEntityManager()->flush($template);
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
91
        } catch (ORMException $e) {
92
            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
93
        }
94
        return $template;
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
95
96
97
98
99
100
101
102
103
    }

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

    /**
     * @return QueryBuilder
     */
    public function createQueryBuilder() : QueryBuilder
    {
104
        $qb = $this->getEntityManager()->getRepository(Template::class)->createQueryBuilder('template')
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
105
106
107
108
109
110
111
112
        ;

        return $qb;
    }

    /**
     * @param string $champ
     * @param string $ordre
113
     * @return Template[]
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
114
     */
115
    public function getTemplates(string $champ = 'code', string $ordre = 'ASC') : array
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
116
117
    {
        $qb = $this->createQueryBuilder()
118
            ->orderBy('template.' . $champ, $ordre)
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
119
120
121
122
123
124
125
126
        ;

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

    /**
     * @param int|null $id
127
     * @return Template|null
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
128
     */
129
    public function getTemplate(?int $id) : ?Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
130
131
    {
        $qb = $this->createQueryBuilder()
132
            ->andWhere('template.id = :id')
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
133
134
135
136
137
138
            ->setParameter('id', $id)
        ;

        try {
            $result = $qb->getQuery()->getOneOrNullResult();
        } catch (NonUniqueResultException $e) {
139
            throw new RuntimeException("Plusieurs Template partagent le même id [".$id."]");
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
140
141
142
143
144
145
        }
        return $result;
    }

    /**
     * @param string $code
146
     * @return Template|null
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
147
     */
148
    public function getTemplateByCode(string $code) : ?Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
149
150
    {
        $qb = $this->createQueryBuilder()
151
            ->andWhere('template.code = :code')
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
152
153
154
155
156
157
            ->setParameter('code', $code)
        ;

        try {
            $result = $qb->getQuery()->getOneOrNullResult();
        } catch (NonUniqueResultException $e) {
158
159
            throw new RuntimeException("Plusieurs Template partagent le même code [".$code."]");
        }
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
160
161
162
163
164
165
        return $result;
    }

    /**
     * @param AbstractActionController $controller
     * @param string $param
166
     * @return Template|null
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
167
     */
168
    public function getRequestedTemplate(AbstractActionController $controller, string $param='template') : ?Template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
169
170
    {
        $id = $controller->params()->fromRoute($param);
171
        $result = $this->getTemplate($id);
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
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

        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;
    }

    /**
199
     * @param Template $template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
200
201
202
     * @param array $variables
     * @return string
     */
203
    public function generateComplement(Template $template, array $variables) : string
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
204
    {
205
        return $this->replaceMacros($template->getComplement(), $variables);
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
206
207
208
    }

    /**
209
     * @param Template $template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
210
211
212
     * @param array $variables
     * @return string
     */
213
    public function generateContenu(Template $template, array $variables) : string
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
214
215
    {
        $texte = "<style>";
216
        $texte .= $template->getCss();
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
217
        $texte .= "</style>";
218
        $texte .= $this->replaceMacros($template->getContent(), $variables);
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
219
220
221
222
        return $texte;
    }

    /**
223
     * @param Template $template
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
224
225
226
     * @param array $variables
     * @return string
     */
227
    public function generateTitre(Template $template, array $variables) : string
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
228
    {
229
        return $this->replaceMacros($template->getComplement(), $variables);
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
230
231
232
    }

}