MacroController.php 3.84 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
23
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
<?php

namespace UnicaenRenderer\Controller;

use Application\Service\Agent\AgentServiceAwareTrait;
use UnicaenRenderer\Entity\Db\Macro;
use UnicaenRenderer\Form\Macro\MacroFormAwareTrait;
use UnicaenRenderer\Service\Macro\MacroServiceAwareTrait;
use Zend\Http\Request;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class MacroController extends AbstractActionController {
    use MacroServiceAwareTrait;
    use MacroFormAwareTrait;

    public function indexAction() {
        $macrosAll = $this->getMacroService()->getMacros();
        $variable = $this->params()->fromQuery('variable');

        $macros = [];
        $variables = [];
        foreach ($macrosAll as $macro) {
            $variables[$macro->getVariable()] = $macro->getVariable();
            if ($variable === null OR $variable ==='' OR $macro->getVariable() === $variable) {
                $macros[] = $macro;
            }
        }

        return new ViewModel([
            'macros' => $macros,
            'variables' => $variables,
            'variable' => $variable,
        ]);
    }

    public function ajouterAction() {
        $macro = new Macro();
        $form = $this->getMacroForm();
        $form->setAttribute('action', $this->url()->fromRoute('contenu/macro/ajouter', [], [], true));
        $form->bind($macro);

        /** @var Request $request */
        $request = $this->getRequest();
        if ($request->isPost()) {
            $data = $request->getPost();
            $form->setData($data);
            if ($form->isValid()) {
                $this->getMacroService()->create($macro);
            }
        }

        $vm = new ViewModel();
        $vm->setTemplate('unicaen-renderer/default/default-form');
        $vm->setVariables([
            'title' => "Ajout d'une nouvelle macro",
            'form' => $form,
        ]);
        return $vm;
    }

    public function modifierAction() {
        $macro = $this->getMacroService()->getRequestedMacro($this);
        $form = $this->getMacroForm();
        $form->setAttribute('action', $this->url()->fromRoute('contenu/macro/modifier', ['macro' => $macro->getId()], [], true));
        $form->bind($macro);
        $form->setOldCode($macro->getCode());

        /** @var Request $request */
        $request = $this->getRequest();
        if ($request->isPost()) {
            $data = $request->getPost();
            $form->setData($data);
            if ($form->isValid()) {
                $this->getMacroService()->update($macro);
            }
        }

        $vm = new ViewModel();
        $vm->setTemplate('unicaen-renderer/default/default-form');
        $vm->setVariables([
            'title' => "Modification d'une macro",
            'form' => $form,
        ]);
        return $vm;
    }

    public function supprimerAction()
    {
        $macro = $this->getMacroService()->getRequestedMacro($this);

        /** @var Request $request */
        $request = $this->getRequest();
        if ($request->isPost()) {
            $data = $request->getPost();
            if ($data["reponse"] === "oui") $this->getMacroService()->delete($macro);
            exit();
        }

        $vm = new ViewModel();
        if ($macro !== null) {
            $vm->setTemplate('unicaen-renderer/default/confirmation');
            $vm->setVariables([
                'title' => "Suppression de la macro " . $macro->getCode(),
                'text' => "La suppression est définitive êtes-vous sûr&middot;e de vouloir continuer ?",
                'action' => $this->url()->fromRoute('contenu/macro/supprimer', ["macro" => $macro->getId()], [], true),
            ]);
        }
        return $vm;
    }

    public function genererJsonAction()
    {
        $json = $this->getMacroService()->generateJSON();

        return new ViewModel([
            'title' => 'JSON pour tinyMCE',
            'json' => $json,
        ]);
    }

}