|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\CMSBundle\Extension\TemplateEditor; |
|
4
|
|
|
|
|
5
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
7
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
8
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextareaType; |
|
9
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
|
12
|
|
|
use Symfony\Component\Process\Process; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @IsGranted("ROLE_PIEDWEB_ADMIN_THEME") |
|
16
|
|
|
*/ |
|
17
|
|
|
class ElementAdmin extends AbstractController |
|
18
|
|
|
{ |
|
19
|
|
|
protected function getElements() |
|
20
|
|
|
{ |
|
21
|
|
|
return new ElementRepository($this->get('kernel')->getProjectDir().'/templates'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function listElement() |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->render('@pwcTemplateEditor/list.html.twig', ['elements' => $this->getElements()->getAll()]); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function getElement($encodedPath) |
|
30
|
|
|
{ |
|
31
|
|
|
if (null !== $encodedPath) { |
|
32
|
|
|
$element = $this->getElements()->getOneByEncodedPath($encodedPath); |
|
33
|
|
|
if (!$element) { |
|
34
|
|
|
throw $this->createNotFoundException('This element does not exist...'); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $element ?? new Element($this->get('kernel')->getProjectDir().'/templates'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
protected function clearTwigCache() |
|
42
|
|
|
{ |
|
43
|
|
|
$twigCacheFolder = $this->get('twig')->getCache(true); |
|
44
|
|
|
|
|
45
|
|
|
$process = new Process(['rm', '-rf', $twigCacheFolder]); |
|
46
|
|
|
$process->run(); |
|
47
|
|
|
|
|
48
|
|
|
if (!$process->isSuccessful()) { |
|
49
|
|
|
throw new ProcessFailedException($process); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function editElement($encodedPath = null, Request $request = null) |
|
54
|
|
|
{ |
|
55
|
|
|
$element = $this->getElement($encodedPath); |
|
56
|
|
|
|
|
57
|
|
|
$form = $this->editElementForm($element); |
|
58
|
|
|
|
|
59
|
|
|
$form->handleRequest($request); |
|
60
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
61
|
|
|
$element = $form->getData(); |
|
62
|
|
|
$element->storeElement(); |
|
63
|
|
|
|
|
64
|
|
|
$this->clearTwigCache(); |
|
65
|
|
|
|
|
66
|
|
|
return $this->redirectToRoute( |
|
67
|
|
|
'piedweb_cms_template_editor_edit', |
|
68
|
|
|
[ |
|
69
|
|
|
'encodedPath' => $element->getEncodedPath(), |
|
70
|
|
|
] |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->render( |
|
75
|
|
|
'@pwcTemplateEditor/edit.html.twig', |
|
76
|
|
|
[ |
|
77
|
|
|
'element' => $element, |
|
78
|
|
|
'form' => $form->createView(), |
|
79
|
|
|
] |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function editElementForm(Element $element) |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->createFormBuilder($element) |
|
86
|
|
|
->add('path', TextType::class) |
|
87
|
|
|
->add('code', TextareaType::class, [ |
|
88
|
|
|
'attr' => [ |
|
89
|
|
|
'style' => 'min-height: 90vh;font-size:125%;', |
|
90
|
|
|
'data-editor' => 'twig', |
|
91
|
|
|
'data-gutter' => 0, |
|
92
|
|
|
], |
|
93
|
|
|
'required' => false, |
|
94
|
|
|
]) |
|
95
|
|
|
|
|
96
|
|
|
->getForm(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function deleteElement($encodedPath, Request $request) |
|
100
|
|
|
{ |
|
101
|
|
|
$element = $this->getElement($encodedPath); |
|
102
|
|
|
|
|
103
|
|
|
$form = $this->createFormBuilder() |
|
104
|
|
|
->add('delete', SubmitType::class, ['label' => 'Supprimer', 'attr' => ['class' => 'btn-danger']]) |
|
105
|
|
|
->getForm(); |
|
106
|
|
|
|
|
107
|
|
|
$form->handleRequest($request); |
|
108
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
109
|
|
|
$element->deleteElement(); |
|
110
|
|
|
|
|
111
|
|
|
$this->addFlash('error', 'Element supprimé.'); |
|
112
|
|
|
|
|
113
|
|
|
return $this->redirectToRoute('piedweb_cms_template_editor_list'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $this->render( |
|
117
|
|
|
'@pwcTemplateEditor/delete.html.twig', |
|
118
|
|
|
[ |
|
119
|
|
|
'form' => $form->createView(), |
|
120
|
|
|
'element' => $element, |
|
121
|
|
|
] |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|