forked from thelia-modules/Diaporamas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiaporamaImageController.php
158 lines (135 loc) · 5.78 KB
/
DiaporamaImageController.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
<?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\DiaporamaImageController as BaseDiaporamaImageController;
use Diaporamas\Event\DiaporamaEvent;
use Diaporamas\Event\DiaporamaImageEvent;
use Diaporamas\Model\DiaporamaImage;
use Diaporamas\Model\DiaporamaImageQuery;
use Propel\Runtime\Exception\PropelException;
use Thelia\Core\Event\File\FileCreateOrUpdateEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\HttpFoundation\Response;
use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Log\Tlog;
use Thelia\Tools\URL;
/**
* Class DiaporamaImageController
* @package Diaporamas\Controller
*/
class DiaporamaImageController extends BaseDiaporamaImageController
{
/**
* Load a object for modification, and display the edit template.
*
* @return \Thelia\Core\HttpFoundation\Response the response
*/
public function updateAction()
{
// Check current user authorization
if (null !== $response = $this->checkAuth($this->resourceCode, $this->getModuleCode(), AccessManager::UPDATE)) {
return $response;
}
$image = $this->getExistingObject();
if (is_null($image)) {
return $this->pageNotFound();
}
$redirectUrl = $image->getRedirectionUrl();
return $this->render('diaporama-image-edit', array(
'diaporama_image_id' => $image->getId(),
'redirectUrl' => $redirectUrl,
'breadcrumb' => $image->getBreadcrumb(
$this->getRouter($this->getCurrentRouter()),
$this->container,
'images',
$this->getCurrentEditionLocale()
)
));
}
/**
* Put in this method post object update processing if required.
*
* @param DiaporamaImageEvent $updateEvent the update event
* @return Response a response, or null to continue normal processing
*/
protected function performAdditionalUpdateAction($updateEvent)
{
$this->updateImageFile($updateEvent->getDiaporamaImage());
return null;
}
/**
* Updating the image. Inspired by FileController::updateFileAction().
*
* @param DiaporamaImage $file Diaporama Image ID.
* @param string $eventName the event type.
*
* @return DiaporamaImage
*/
protected function updateImageFile(DiaporamaImage $file)
{
$fileUpdateForm = $this->createForm($file->getUpdateFormId());
try {
if (null === $file) {
throw new \InvalidArgumentException(sprintf('%d image id does not exist', $file->getId()));
}
$event = new FileCreateOrUpdateEvent(null);
$event->setModel($file);
$event->setOldModel($file);
$fileForm = $this->getRequest()->files->get($fileUpdateForm->getName());
if (isset($fileForm['file'])) {
$event->setUploadedFile($fileForm['file']);
}
$this->dispatch(TheliaEvents::IMAGE_SAVE, $event);
$fileUpdated = $event->getModel();
$this->adminLogAppend(
AdminResources::MODULE,
AccessManager::UPDATE,
sprintf('Image with Ref %s (ID %d) modified', $fileUpdated->getTitle(), $fileUpdated->getId())
);
if ($this->getRequest()->get('save_mode') == 'close') {
return $this->generateRedirect(
URL::getInstance()->absoluteUrl($file->getRedirectionUrl(), ['current_tab' => 'images'])
);
} else {
return $this->generateSuccessRedirect($fileUpdateForm);
}
} catch (PropelException $e) {
$message = $e->getMessage();
} catch (\Exception $e) {
$message = sprintf('Sorry, an error occurred: %s', $e->getMessage().' '.$e->getFile());
}
if (isset($message)) {
Tlog::getInstance()->error(sprintf('Error during image editing : %s.', $message));
$fileUpdateForm->setErrorMessage($message);
$this->getParserContext()->addForm($fileUpdateForm)->setGeneralError($message);
}
return $file;
}
public function deleteImageAction($imageId)
{
// Check current user authorization
if (null !== $response = $this->checkAuth($this->resourceCode, $this->getModuleCode(), AccessManager::DELETE)) {
return $response;
}
try {
$diaporama_image = DiaporamaImageQuery::create()->findPk($imageId);
$event = new DiaporamaImageEvent($imageId);
$event->setId($imageId);
$this->dispatch($this->deleteEventIdentifier, $event);
$this->performAdditionalDeleteAction($event);
return $this->generateRedirect($diaporama_image->getRedirectionUrl());
} catch (\Exception $e) {
return $this->renderAfterDeleteError($e);
}
}
}