-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathSortableAdminController.php
More file actions
executable file
·80 lines (68 loc) · 2.38 KB
/
SortableAdminController.php
File metadata and controls
executable file
·80 lines (68 loc) · 2.38 KB
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
<?php
/*
* This file is part of the pixSortableBehaviorBundle.
*
* (c) Nicolas Ricci <nicolas.ricci@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Pix\SortableBehaviorBundle\Controller;
use Doctrine\Common\Util\ClassUtils;
use Pix\SortableBehaviorBundle\Services\PositionHandler;
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PropertyAccess\PropertyAccess;
/**
* @package Pix\SortableBehaviorBundle
*/
class SortableAdminController extends CRUDController
{
/**
* Move element
*
* @param string $position
*
* @return RedirectResponse|Response
*/
public function moveAction(
$position,
Request $request,
PositionHandler $positionHandler
): Response
{
$translator = $this->get('translator');
if (!$this->admin->isGranted('EDIT')) {
$this->addFlash(
'sonata_flash_error',
$translator->trans('flash_error_no_rights_update_position')
);
return new RedirectResponse($this->admin->generateUrl(
'list',
array('filter' => $this->admin->getFilterParameters())
));
}
$object = $this->admin->getSubject();
$lastPositionNumber = $positionHandler->getLastPosition($object);
$newPositionNumber = $positionHandler->getPosition($object, $position, $lastPositionNumber);
$accessor = PropertyAccess::createPropertyAccessor();
$accessor->setValue($object, $positionHandler->getPositionFieldByEntity($object), $newPositionNumber);
$this->admin->update($object);
if ($this->isXmlHttpRequest($request)) {
return $this->renderJson(array(
'result' => 'ok',
'objectId' => $this->admin->getNormalizedIdentifier($object)
));
}
$this->addFlash(
'sonata_flash_success',
$translator->trans('flash_success_position_updated')
);
return new RedirectResponse($this->admin->generateUrl(
'list',
array('filter' => $this->admin->getFilterParameters())
));
}
}