-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAlbumController.php
146 lines (119 loc) · 5.25 KB
/
AlbumController.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
<?php
namespace FOQ\AlbumBundle\Controller;
use FOQ\AlbumBundle\Model\AlbumInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOQ\AlbumBundle\Validator\Exception\InvalidImageException;
class AlbumController extends ContainerAware
{
public function indexAction()
{
return $this->getTemplating()->renderResponse('FOQAlbumBundle:Album:index.html.twig', array(
'albums' => $this->getProvider()->getAlbums(),
'sortBy' => $this->container->get('foq_album.sorter.album')->getRequestSortField()
));
}
public function listByUserAction($username)
{
return $this->getTemplating()->renderResponse('FOQAlbumBundle:Album:byUser.html.twig', array(
'albums' => $this->getProvider()->getUserAlbums($username),
'user' => $this->container->get('fos_user.user_manager')->findUserByUsername($username)
));
}
public function showAction($username, $slug)
{
return $this->getTemplating()->renderResponse('FOQAlbumBundle:Album:show.html.twig', array(
'album' => $this->getProvider()->getAlbum($username, $slug, true),
'sortBy' => $this->container->get('foq_album.sorter.album')->getRequestSortField()
));
}
public function newAction()
{
$form = $this->container->get('form.factory')->create($this->container->get('foq_album.form_type.album'));
$handler = $this->container->get('foq_album.form_handler.album');
if ($handler->process($form)) {
$this->container->get('foq_album.object_manager')->flush();
return new RedirectResponse($this->getAlbumUrl($form->getData()));
}
return $this->container->get('templating')->renderResponse('FOQAlbumBundle:Album:new.html.twig', array('form' => $form->createView()));
}
public function editAction($username, $slug)
{
$album = $this->getProvider()->getAlbum($username, $slug);
$form = $this->container->get('form.factory')->create($this->container->get('foq_album.form_type.album'));
$handler = $this->container->get('foq_album.form_handler.album');
if ($handler->process($form, $album)) {
$this->container->get('foq_album.object_manager')->flush();
return new RedirectResponse($this->getAlbumUrl($album));
}
return $this->container->get('templating')->renderResponse('FOQAlbumBundle:Album:new.html.twig', array('form' => $form->createView()));
}
public function deleteAction($username, $slug)
{
$album = $this->getProvider()->getAlbum($username, $slug);
$this->checkAlbumOwning($album);
$this->container->get('foq_album.deleter.album')->delete($album);
$this->container->get('foq_album.object_manager')->flush();
return new RedirectResponse($this->container->get('router')->generate('foq_album_index'));
}
public function publishAction($username, $slug)
{
$album = $this->getProvider()->getAlbum($username, $slug);
$this->checkAlbumOwning($album);
$this->container->get('foq_album.publisher.album')->publish($album);
$this->container->get('foq_album.object_manager')->flush();
return new RedirectResponse($this->getAlbumUrl($album));
}
public function uploadAction($username, $slug)
{
$album = $this->getProvider()->getAlbum($username, $slug);
$this->checkAlbumOwning($album);
return $this->container->get('templating')->renderResponse('FOQAlbumBundle:Album:upload.html.twig', array(
'album' => $album
));
}
public function uploadCallbackAction($username, $slug)
{
$album = $this->getProvider()->getAlbum($username, $slug);
$this->checkAlbumOwning($album);
$file = $this->container->get('request')->files->get('file');
try {
$this->container->get('foq_album.uploader')->upload($album, $file);
} catch (InvalidImageException $e) {
return new Response($e->getMessage());
}
$this->container->get('foq_album.object_manager')->flush();
return new Response('ok');
}
protected function setFlash($action, $value)
{
$this->container->get('session')->getFlashBag->add($action, $value);
}
protected function getTemplating()
{
return $this->container->get('templating');
}
protected function getProvider()
{
return $this->container->get('foq_album.provider.album');
}
protected function getAlbumUrl(AlbumInterface $album)
{
return $this->container->get('foq_album.url_generator')->getAlbumUrl('foq_album_album_show', $album);
}
/**
* @param AlbumInterface $album
* @throws AccessDeniedException
* @return void
*/
protected function checkAlbumOwning(AlbumInterface $album)
{
if (!$this->container->get('foq_album.security_helper') ||
$album->getUser()->getId() !== $this->container->get('foq_album.security_helper')->getUser()->getId()) {
throw new AccessDeniedException();
}
}
}