MacroService.php 6.29 KB
Newer Older
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

namespace UnicaenRenderer\Service\Macro;

use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\QueryBuilder;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Service\EntityManagerAwareTrait;
use UnicaenRenderer\Entity\Db\Macro;
use Zend\Mvc\Controller\AbstractActionController;

class MacroService {
    use EntityManagerAwareTrait;

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

    /**
     * @param Macro $macro
     * @return Macro
     */
    public function create(Macro $macro) : Macro
23
    {        
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
169
170
171
172
173
174
175
176
177
178
        try {
            $this->getEntityManager()->persist($macro);
            $this->getEntityManager()->flush($macro);
        } catch (ORMException $e) {
            throw new RuntimeException("Un problème est survenue lors de l'enregistrement en BD.", $e);
        }
        return $macro;
    }

    /**
     * @param Macro $macro
     * @return Macro
     */
    public function update(Macro $macro) : Macro
    {
        try {
            $this->getEntityManager()->flush($macro);
        } catch (ORMException $e) {
            throw new RuntimeException("Un problème est survenue lors de l'enregistrement en BD.", $e);
        }
        return $macro;
    }

    /**
     * @param Macro $macro
     * @return Macro
     */
    public function historise(Macro $macro) : Macro
    {
        try {
            $this->getEntityManager()->flush($macro);
        } catch (ORMException $e) {
            throw new RuntimeException("Un problème est survenue lors de l'enregistrement en BD.", $e);
        }
        return $macro;
    }

    /**
     * @param Macro $macro
     * @return Macro
     */
    public function restore(Macro $macro) : Macro
    {
        try {
            $this->getEntityManager()->flush($macro);
        } catch (ORMException $e) {
            throw new RuntimeException("Un problème est survenue lors de l'enregistrement en BD.", $e);
        }
        return $macro;
    }

    /**
     * @param Macro $macro
     * @return Macro
     */
    public function delete(Macro $macro) : Macro
    {
        try {
            $this->getEntityManager()->remove($macro);
            $this->getEntityManager()->flush($macro);
        } catch (ORMException $e) {
            throw new RuntimeException("Un problème est survenue lors de l'enregistrement en BD.", $e);
        }
        return $macro;
    }

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

    /**
     * @return QueryBuilder
     */
    public function createQueryBuilder() : QueryBuilder
    {
        $qb = $this->getEntityManager()->getRepository(Macro::class)->createQueryBuilder('macro')
        ;

        return $qb;
    }

    /**
     * @param string $champ
     * @param string $ordre
     * @return Macro[]
     */
    public function getMacros(string $champ = 'code', string $ordre = 'ASC') : array
    {
        $qb = $this->createQueryBuilder()
            ->orderBy('macro.' . $champ, $ordre)
        ;

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

    /**
     * @param integer $id
     * @return Macro
     */
    public function getMacro(int $id) : ?Macro
    {
        $qb = $this->createQueryBuilder()
            ->andWhere('macro.id = :id')
            ->setParameter('id', $id)
        ;

        try {
            $result = $qb->getQuery()->getOneOrNullResult();
        } catch (NonUniqueResultException $e) {
            throw new RuntimeException("Plusieurs Macro partagent le même id [".$id."]");
        }
        return $result;
    }

    /**
     * @param string $code
     * @return Macro
     */
    public function getMacroByCode(string $code) : ?Macro
    {
        $qb = $this->createQueryBuilder()
            ->andWhere('macro.code = :code')
            ->setParameter('code', $code)
        ;

        try {
            $result = $qb->getQuery()->getOneOrNullResult();
        } catch (NonUniqueResultException $e) {
            throw new RuntimeException("Plusieurs Macro partagent le même code [".$code."]");
        }
        return $result;
    }

    /**
     * @param AbstractActionController $controller
     * @param string $param
     * @return Macro
     */
    public function getRequestedMacro(AbstractActionController $controller, string $param = 'macro') : ?Macro
    {
        $id = $controller->params()->fromRoute($param);
        $result = $this->getMacro($id);
        return $result;
    }

    /** FONCTION MACRO ************************************************************************************************/

    /**
     * @param string $code
     * @param array $variables
     * @return string
     */
    public function getTexte(string $code, array $variables) : string
    {
        $code = str_replace('VAR[','', $code);
        $code = str_replace(']','', $code);
179
180
181
        $exploded = explode("|",$code);
        $parameter = ($exploded[1])??null;
        $code = ($exploded[0])??null;
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
182
183
184
185
186
        $macro = $this->getMacroByCode($code);

        if ($macro !== null) {
            if (isset($variables[$macro->getVariable()]) === true) {
                if (method_exists($variables[$macro->getVariable()], $macro->getMethode()) === true) {
187
188
189
190

                    if ($parameter !== null) $texte = $variables[$macro->getVariable()]->{$macro->getMethode()}($parameter);
                    else $texte = $variables[$macro->getVariable()]->{$macro->getMethode()}();

191
                    return ($texte)?:"";
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
                }
                return "<span style='color:darkred;'> Méthode [".$macro->getMethode()."] non trouvée </span>";
            }
            return "<span style='color:darkred;'> Variable [".$macro->getVariable()."] non trouvée </span>";
        }
        return "<span style='color:darkred;'> Macro [".$code."] non trouvée </span>";
    }

    /**
     * @return string
     */
    public function generateJSON() : string
    {
        $macros = $this->getMacros();

        $result = "let macros = [\n";
        foreach ($macros as $macro) {
209
            $result .= "    { title:'" . $macro->getCode() . "', description:'" . strip_tags(str_replace("'","\'",$macro->getDescription())) . "', content:'VAR[" . $macro->getCode() . "]' },\n";
Jean-Philippe Metivier's avatar
Jean-Philippe Metivier committed
210
211
212
213
214
215
216
        }
        $result .= "];\n";

        return $result;
    }

}