forked from thelia-modules/Diaporamas
/
DiaporamaController.php
133 lines (122 loc) · 5.57 KB
/
DiaporamaController.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
<?php
/*************************************************************************************/
/* This file is part of the "Diaporamas" Thelia 2 module. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Diaporamas\Controller;
use Diaporamas\Controller\Base\DiaporamaController as BaseDiaporamaController;
use Diaporamas\Diaporamas;
use Diaporamas\Event\DiaporamaEvent;
use Diaporamas\Event\DiaporamaEvents;
use Diaporamas\Event\DiaporamaHtmlEvent;
use Diaporamas\Loop\DiaporamaImage as DiaporamaImageLoop;
use Diaporamas\Model\Diaporama;
use Diaporamas\Model\DiaporamaImage;
use Diaporamas\Model\DiaporamaImageQuery;
use Thelia\Core\HttpFoundation\JsonResponse;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\HttpFoundation\Response;
use Thelia\Core\Security\AccessManager;
use Thelia\Core\Template\Element\LoopResult;
use Thelia\Core\Template\Element\LoopResultRow;
use Thelia\Form\Exception\FormValidationException;
/**
* Class DiaporamaController
* @package Diaporamas\Controller
*/
class DiaporamaController extends BaseDiaporamaController
{
public function deleteAction()
{
// Check current user authorization
if (null !== $response = $this->checkAuth($this->resourceCode, $this->getModuleCode(), AccessManager::DELETE)) {
return $response;
}
try {
$form = $this->createForm('diaporama.delete');
$valform = $this->validateForm($form);
$event = new DiaporamaEvent();
$event->setId($valform->getData()['diaporama_id']);
$this->dispatch($this->deleteEventIdentifier, $event);
$this->performAdditionalDeleteAction($event);
return $this->generateSuccessRedirect($form);
} catch (FormValidationException $e) {
return $this->renderAfterDeleteError($e);
} catch (\Exception $e) {
return $this->renderAfterDeleteError($e);
}
}
public function getDiaporamaHtmlAction($shortcode)
{
$width = $this->getRequest()->query->get('width');
$height = $this->getRequest()->query->get('height');
$event = new DiaporamaHtmlEvent();
$event->setShortcode($shortcode);
$event->setImageWidth($width);
$event->setImageHeight($height);
$this->dispatch(DiaporamaEvents::DIAPORAMA_HTML, $event);
return new Response($event->getDiaporamaHtml());
}
public function replaceShortcodesAction()
{
$description = $this->getRequest()->request->get('description');
$event = new DiaporamaHtmlEvent();
$event->setEntityDescription($description);
$this->dispatch(DiaporamaEvents::DIAPORAMA_PARSE, $event);
return new Response($event->getEntityDescription());
}
public function getDiaporamaDataAction($shortcode)
{
$diaporama = Diaporama::getByShortcode($shortcode);
if (is_null($diaporama)) {
$result = array(
'error' => $this->translator->trans(
'diaporama.read.no_shortcode %shortcode',
array('shortcode' => $shortcode),
Diaporamas::BO_MESSAGE_DOMAIN
)
);
} else {
$result = array(
'id' => $diaporama->getId(),
'title' => $diaporama->getTitle(),
'shortcode' => $diaporama->getShortcode(),
'created_at' => $diaporama->getCreatedAt(),
'updated_at' => $diaporama->getUpdatedAt(),
'locale' => $diaporama->getLocale(),
'images' => array(),
);
// Data for images
$loop = new DiaporamaImageLoop($this->getContainer());
$loop->initializeArgs(array(
'source_id' => $diaporama->getId(),
'order' => 'manual',
));
/** @var DiaporamaImageQuery $query */
$query = $loop->buildModelCriteria();
$res= $query->find();
$diaporamaImagesRows = $loop->parseResults(new LoopResult($res));
/** @var LoopResultRow $row */
foreach ($diaporamaImagesRows as $row) {
$result['images'][] = array(
'id' => $row->get('ID'),
'position' => $row->get('POSITION'),
'visible' => boolval($row->get('VISIBLE')),
'title' => is_null($row->get('TITLE')) ? '' : $row->get('TITLE'),
'chapo' => is_null($row->get('CHAPO')) ? '' : $row->get('CHAPO'),
'description' => is_null($row->get('DESCRIPTION')) ? '' : $row->get('DESCRIPTION'),
'postscriptum' =>is_null($row->get('POSTSCRIPTUM')) ? '' : $row->get('POSTSCRIPTUM'),
'image_url' => $row->get('IMAGE_URL'),
'processing_error' => boolval($row->get('PROCESSING_ERROR')),
);
}
}
return new JsonResponse($result);
}
}