/
GlossaryController.php
415 lines (352 loc) · 14 KB
/
GlossaryController.php
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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/
namespace Spryker\Zed\Cms\Communication\Controller;
use Generated\Shared\Transfer\KeyTranslationTransfer;
use Generated\Shared\Transfer\LocaleTransfer;
use Generated\Shared\Transfer\PageKeyMappingTransfer;
use Generated\Shared\Transfer\PageTransfer;
use Orm\Zed\Cms\Persistence\SpyCmsPage;
use Spryker\Shared\Cms\CmsConstants;
use Spryker\Zed\Cms\Business\Exception\MissingPageException;
use Spryker\Zed\Cms\Communication\Form\CmsGlossaryForm;
use Spryker\Zed\Cms\Communication\Table\CmsGlossaryTable;
use Spryker\Zed\Cms\Communication\Table\CmsTableConstants;
use Spryker\Zed\Cms\Persistence\CmsQueryContainer;
use Spryker\Zed\Kernel\Communication\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
/**
* @method \Spryker\Zed\Cms\Communication\CmsCommunicationFactory getFactory()
* @method \Spryker\Zed\Cms\Persistence\CmsQueryContainerInterface getQueryContainer()
* @method \Spryker\Zed\Cms\Persistence\CmsRepositoryInterface getRepository()
* @method \Spryker\Zed\Cms\Business\CmsFacadeInterface getFacade()
*/
class GlossaryController extends AbstractController
{
protected const REDIRECT_ADDRESS = '/cms/glossary';
protected const SEARCH_LIMIT = 10;
protected const ID_FORM = 'id-form';
protected const TYPE = 'type';
/**
* @var string
*/
protected $glossaryKeyName = '';
/**
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return array|\Symfony\Component\HttpFoundation\JsonResponse
*/
public function indexAction(Request $request)
{
$idPage = $this->castId($request->get(CmsTableConstants::REQUEST_ID_PAGE));
$idForm = (int)$request->get(self::ID_FORM);
$type = CmsConstants::RESOURCE_TYPE_PAGE;
$cmsPageEntity = $this->findCmsPageById($idPage);
$localeTransfer = $this->getLocaleTransfer($cmsPageEntity);
$fkLocale = $this->getLocaleByCmsPage($cmsPageEntity);
$title = $cmsPageEntity->getUrl();
$placeholders = $this->findPagePlaceholders($cmsPageEntity);
$glossaryMappingArray = $this->extractGlossaryMapping($idPage, $localeTransfer);
$forms = [];
$formViews = [];
foreach ($placeholders as $place) {
$form = $this->createPlaceholderForm($request, $glossaryMappingArray, $place, $idPage, $fkLocale);
$forms[] = $form;
$formViews[] = $form->createView();
}
if ($idForm !== null && $request->isXmlHttpRequest()) {
return $this->handleAjaxRequest($forms, $idForm, $localeTransfer);
}
return [
'idPage' => $idPage,
'title' => $title,
'type' => $type,
'forms' => $formViews,
'localeTransfer' => $localeTransfer,
];
}
/**
* @param \Orm\Zed\Cms\Persistence\SpyCmsPage $cmsPage
*
* @return int|null
*/
protected function getLocaleByCmsPage(SpyCmsPage $cmsPage): ?int
{
$fkLocale = null;
$url = $cmsPage->getSpyUrls()->getFirst();
if ($url) {
$fkLocale = $url->getFkLocale();
}
return $fkLocale;
}
/**
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function deleteAction(Request $request): RedirectResponse
{
if (!$request->isMethod(Request::METHOD_DELETE)) {
throw new MethodNotAllowedHttpException([Request::METHOD_DELETE], 'This action requires a DELETE request.');
}
$idMapping = $this->castId($request->request->get(CmsGlossaryTable::REQUEST_ID_MAPPING));
$idPage = $this->castId($request->request->get(CmsTableConstants::REQUEST_ID_PAGE));
$mappingGlossary = $this->getQueryContainer()
->queryGlossaryKeyMappingById($idMapping)
->findOne();
$pageTransfer = (new PageTransfer())->setIdCmsPage($idPage);
$this->getFacade()
->deletePageKeyMapping($pageTransfer, $mappingGlossary->getPlaceholder());
$redirectUrl = static::REDIRECT_ADDRESS . '?' . http_build_query([CmsTableConstants::REQUEST_ID_PAGE => $idPage]);
return $this->redirectResponse($redirectUrl);
}
/**
* @param string $tempFile
*
* @return array
*/
protected function findTemplatePlaceholders(string $tempFile): array
{
$placeholderMap = [];
$placeholderPattern = $this->getFactory()->getConfig()->getPlaceholderPattern();
$placeholderValuePattern = $this->getFactory()->getConfig()->getPlaceholderValuePattern();
if (file_exists($tempFile)) {
$fileContent = file_get_contents($tempFile);
preg_match_all($placeholderPattern, $fileContent, $cmsPlaceholderLine);
preg_match_all($placeholderValuePattern, implode(' ', $cmsPlaceholderLine[0]), $placeholderMap);
return $placeholderMap[1];
}
return $placeholderMap;
}
/**
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function searchAction(Request $request): JsonResponse
{
$value = $request->query->get('value');
$key = $request->query->get('key');
$localeId = $this->castId($request->query->get('localeId'));
$searchedItems = $this->searchGlossaryKeysAndTranslations($value, $key, $localeId);
$result = [];
foreach ($searchedItems as $trans) {
$result[] = [
'key' => $trans->getLabel(),
'value' => $trans->getValue(),
];
}
return $this->jsonResponse($result);
}
/**
* @param string|null $value
* @param string|null $key
* @param int|null $localeId
*
* @return \Orm\Zed\Glossary\Persistence\SpyGlossaryKey[]|\Orm\Zed\Glossary\Persistence\SpyGlossaryTranslation[]
*/
protected function searchGlossaryKeysAndTranslations(?string $value, ?string $key, ?int $localeId)
{
$searchedItems = [];
if ($value !== null) {
$searchedItems = $this->getQueryContainer()
->queryTranslationWithKeyByValue($value)
->limit(self::SEARCH_LIMIT)
->find();
return $searchedItems;
}
if ($key !== null) {
$searchedItems = $this->getQueryContainer()
->queryKeyWithTranslationByKeyAndLocale($key, $localeId)
->limit(self::SEARCH_LIMIT)
->find();
}
return $searchedItems;
}
/**
* @param array $data
*
* @return \Generated\Shared\Transfer\PageKeyMappingTransfer
*/
protected function createKeyMappingTransfer(array $data): PageKeyMappingTransfer
{
$pageKeyMappingTransfer = (new PageKeyMappingTransfer())->fromArray($data, true);
$hasPageMapping = $this->getFacade()->hasPagePlaceholderMapping($data['fkPage'], $data['placeholder']);
if ($hasPageMapping) {
$pageKeyMappingFound = $this->getFacade()
->getPagePlaceholderMapping($data['fkPage'], $data['placeholder']);
$pageKeyMappingTransfer->setIdCmsGlossaryKeyMapping($pageKeyMappingFound->getIdCmsGlossaryKeyMapping());
}
$glossaryKey = $this->getQueryContainer()
->queryKey($this->glossaryKeyName)
->findOne();
$pageKeyMappingTransfer->setFkGlossaryKey($glossaryKey->getIdGlossaryKey());
return $pageKeyMappingTransfer;
}
/**
* @param array $data
* @param \Generated\Shared\Transfer\LocaleTransfer $localeTransfer
*
* @return void
*/
protected function saveGlossaryKeyPageMapping(array $data, LocaleTransfer $localeTransfer): void
{
$keyTranslationTransfer = $this->createKeyTranslationTransfer($data, $localeTransfer);
$this->getFactory()->getGlossaryFacade()
->saveGlossaryKeyTranslations($keyTranslationTransfer);
$pageKeyMappingTransfer = $this->createKeyMappingTransfer($data);
$this->getFacade()
->savePageKeyMappingAndTouch($pageKeyMappingTransfer, $localeTransfer);
}
/**
* @param \Orm\Zed\Cms\Persistence\SpyCmsPage $pageUrl
*
* @return array
*/
protected function findPagePlaceholders(SpyCmsPage $pageUrl): array
{
$pageUrlArray = $pageUrl->toArray();
$tempFiles = $this->getFactory()
->getTemplateRealPaths($pageUrlArray[CmsQueryContainer::TEMPLATE_PATH]);
/* Added for keeping BC */
if (!is_array($tempFiles)) {
$tempFiles = [$tempFiles];
}
$placeholders = [];
foreach ($tempFiles as $tempFile) {
if (!file_exists($tempFile)) {
continue;
}
$placeholders = $this->findTemplatePlaceholders($tempFile);
}
return $placeholders;
}
/**
* @param int|null $idPage
* @param \Generated\Shared\Transfer\LocaleTransfer $localeTransfer
*
* @return array
*/
protected function extractGlossaryMapping(?int $idPage, LocaleTransfer $localeTransfer): array
{
$glossaryQuery = $this->getQueryContainer()
->queryGlossaryKeyMappingsWithKeyByPageId($idPage, $localeTransfer->getIdLocale());
$glossaryMappingArray = [];
/** @var \Orm\Zed\Cms\Persistence\SpyCmsGlossaryKeyMapping[] $keyMappings */
$keyMappings = $glossaryQuery->find()
->getData();
foreach ($keyMappings as $keyMapping) {
$glossaryMappingArray[$keyMapping->getPlaceholder()] = $keyMapping->getIdCmsGlossaryKeyMapping();
}
return $glossaryMappingArray;
}
/**
* @param \Symfony\Component\Form\FormInterface[] $forms
* @param int $idForm
* @param \Generated\Shared\Transfer\LocaleTransfer $localeTransfer
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
protected function handleAjaxRequest(array $forms, int $idForm, LocaleTransfer $localeTransfer): JsonResponse
{
if ($forms[$idForm]->isSubmitted() && $forms[$idForm]->isValid()) {
$data = $forms[$idForm]->getData();
$this->saveGlossaryKeyPageMapping($data, $localeTransfer);
return $this->jsonResponse([
'success' => 'true',
'glossaryKeyName' => $this->glossaryKeyName,
'data' => $data,
]);
}
return $this->jsonResponse([
'success' => 'false',
'errorMessages' => $forms[$idForm]->getErrors()
->__toString(),
]);
}
/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @param array $glossaryMappingArray
* @param string|null $placeholder
* @param int|null $idPage
* @param int|null $fkLocale
*
* @return \Symfony\Component\Form\FormInterface
*/
protected function createPlaceholderForm(Request $request, array $glossaryMappingArray, ?string $placeholder, ?int $idPage, ?int $fkLocale): FormInterface
{
$idMapping = null;
if (isset($glossaryMappingArray[$placeholder])) {
$idMapping = $glossaryMappingArray[$placeholder];
}
$dataProvider = $this->getFactory()->createCmsGlossaryFormDataProvider();
$form = $this->getFactory()
->getCmsGlossaryForm(
$dataProvider->getData($idPage, $idMapping, $placeholder, $fkLocale)
)
->handleRequest($request);
return $form;
}
/**
* @param array $data
* @param \Generated\Shared\Transfer\LocaleTransfer $localeTransfer
*
* @return \Generated\Shared\Transfer\KeyTranslationTransfer
*/
protected function createKeyTranslationTransfer(array $data, LocaleTransfer $localeTransfer): KeyTranslationTransfer
{
$this->glossaryKeyName = $data[CmsGlossaryForm::FIELD_GLOSSARY_KEY];
if ($this->glossaryKeyName === null) {
$this->glossaryKeyName = $this->getFacade()
->generateGlossaryKeyName($data[CmsGlossaryForm::FIELD_TEMPLATE_NAME], $data[CmsGlossaryForm::FIELD_PLACEHOLDER]);
}
$keyTranslationTransfer = new KeyTranslationTransfer();
$keyTranslationTransfer->setGlossaryKey($this->glossaryKeyName);
$keyTranslationTransfer->setLocales([
$localeTransfer->getLocaleName() => $data[CmsGlossaryForm::FIELD_TRANSLATION],
]);
return $keyTranslationTransfer;
}
/**
* @param int $idPage
*
* @throws \Spryker\Zed\Cms\Business\Exception\MissingPageException
*
* @return \Orm\Zed\Cms\Persistence\SpyCmsPage|\Orm\Zed\Url\Persistence\SpyUrl
*/
protected function findCmsPageById(int $idPage)
{
$cmsPage = $this->getQueryContainer()
->queryPageWithTemplatesAndUrlByIdPage($idPage)
->findOne();
if ($cmsPage === null) {
throw new MissingPageException(
sprintf('Page with id %s not found', $idPage)
);
}
return $cmsPage;
}
/**
* @param \Orm\Zed\Cms\Persistence\SpyCmsPage $cmsPageEntity
*
* @return \Generated\Shared\Transfer\LocaleTransfer
*/
protected function getLocaleTransfer(SpyCmsPage $cmsPageEntity): LocaleTransfer
{
$localeTransfer = $this->getFactory()->getLocaleFacade()->getCurrentLocale();
$url = $cmsPageEntity->getSpyUrls()->getFirst();
if ($url) {
$localeTransfer = new LocaleTransfer();
$localeTransfer->fromArray($url->getSpyLocale()->toArray());
}
return $localeTransfer;
}
}