1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiedWeb\CMSBundle\Entity; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use PiedWeb\CMSBundle\Service\PageMainContentManager\MainContentManagerInterface; |
7
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
8
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
9
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
11
|
|
|
|
12
|
|
|
trait PageExtendedMainContentTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @ORM\Column(type="integer", nullable=true) |
16
|
|
|
*/ |
17
|
|
|
protected $mainContentType; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @ORM\Column(type="text", nullable=true) |
21
|
|
|
*/ |
22
|
|
|
protected $otherProperties; |
23
|
|
|
|
24
|
|
|
protected $otherPropertiesParsed; |
25
|
|
|
|
26
|
|
|
protected $twig; |
27
|
|
|
protected $mainContentManager; |
28
|
|
|
|
29
|
|
|
abstract public function getMainContent(): ?string; |
30
|
|
|
|
31
|
|
|
public function getReadableContent() |
32
|
|
|
{ |
33
|
|
|
throw new Exception('You should use getContent.content'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getChapeau() |
37
|
|
|
{ |
38
|
|
|
throw new Exception('You should use getContent'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setTwig($twig) |
42
|
|
|
{ |
43
|
|
|
$this->twig = $twig; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function mustParseTwig(): bool |
47
|
|
|
{ |
48
|
|
|
return (bool) (null !== $this->twig ? $this->twig : $this->getOtherProperty('twig')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function mainContentIsMarkdown(): ?bool |
52
|
|
|
{ |
53
|
|
|
return PageMainContentType::MARKDOWN === $this->mainContentType; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getTemplate(): ?string |
57
|
|
|
{ |
58
|
|
|
return $this->getOtherProperty('template'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getOtherProperties() |
62
|
|
|
{ |
63
|
|
|
return $this->otherProperties; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function setOtherProperties($otherProperties) |
67
|
|
|
{ |
68
|
|
|
$this->otherProperties = $otherProperties; |
69
|
|
|
$this->otherPropertiesParsed = null; |
70
|
|
|
|
71
|
|
|
return $otherProperties; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @Assert\Callback |
76
|
|
|
*/ |
77
|
|
|
public function validateOtherProperties(ExecutionContextInterface $context) |
78
|
|
|
{ |
79
|
|
|
if (! empty($this->otherProperties)) { |
80
|
|
|
// ou utiliser yaml_parse |
81
|
|
|
try { |
82
|
|
|
Yaml::parse($this->otherProperties); |
83
|
|
|
} catch (ParseException $exception) { |
84
|
|
|
$context->buildViolation('page.otherProperties.malformed') //'$exception->getMessage()) |
85
|
|
|
->atPath('otherProperties') |
86
|
|
|
->addViolation(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getOtherProperty($name) |
92
|
|
|
{ |
93
|
|
|
if (null === $this->otherPropertiesParsed) { |
94
|
|
|
$this->otherPropertiesParsed = $this->otherProperties ? Yaml::parse($this->otherProperties) : []; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->otherPropertiesParsed[$name] ?? null; |
98
|
|
|
/* |
99
|
|
|
if (!isset($this->otherPropertiesParsed[$name])) { |
100
|
|
|
throw new \Exception('"'.$name.'" is not defined.'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->otherPropertiesParsed[$name]; |
104
|
|
|
/**/ |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Magic getter for otherProperties. |
109
|
|
|
* |
110
|
|
|
* @param string $method |
111
|
|
|
* @param array $arguments |
112
|
|
|
* |
113
|
|
|
* @return mixed |
114
|
|
|
*/ |
115
|
|
|
public function __call($method, $arguments) |
116
|
|
|
{ |
117
|
|
|
if ('_action' == $method) { |
118
|
|
|
return; // avoid error with sonata |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (preg_match('/^get/', $method)) { |
122
|
|
|
$property = lcfirst(preg_replace('/^get/', '', $method)); |
123
|
|
|
if (! property_exists(static::class, $property)) { |
124
|
|
|
return $this->getOtherProperty($property) ?? $this->getEmc($property); |
125
|
|
|
// todo remove the else next release |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->$property; |
129
|
|
|
} else { |
130
|
|
|
$vars = array_keys(get_object_vars($this)); |
131
|
|
|
if (\in_array($method, $vars)) { |
132
|
|
|
return \call_user_func_array([$this, 'get'.ucfirst($method)], $arguments); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $this->getOtherProperty(lcfirst($method)) ?? $this->getEmc($method); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// To remove next release |
140
|
|
|
public function getEmc($name) |
141
|
|
|
{ |
142
|
|
|
if (preg_match('/<!--"'.$name.'"--(.*)--\/-->/sU', $this->getMainContent(), $match)) { |
143
|
|
|
return $match[1]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the value of mainContentType. |
149
|
|
|
*/ |
150
|
|
|
public function getMainContentType() |
151
|
|
|
{ |
152
|
|
|
return $this->mainContentType; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set the value of mainContentType. |
157
|
|
|
* |
158
|
|
|
* @return self |
159
|
|
|
*/ |
160
|
|
|
public function setMainContentType($mainContentType) |
161
|
|
|
{ |
162
|
|
|
$this->mainContentType = (int) $mainContentType; |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the value of mainContentManager. |
169
|
|
|
*/ |
170
|
|
|
public function getContent() |
171
|
|
|
{ |
172
|
|
|
return $this->mainContentManager; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Set the value of mainContentManager. |
177
|
|
|
* |
178
|
|
|
* @return self |
179
|
|
|
*/ |
180
|
|
|
public function setContent(MainContentManagerInterface $mainContentManager) |
181
|
|
|
{ |
182
|
|
|
$this->mainContentManager = $mainContentManager; |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|