1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoS\ResourceBundle\Controller; |
4
|
|
|
|
5
|
|
|
use FOS\RestBundle\View\View; |
6
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceController as BaseResourceController; |
7
|
|
|
use Sylius\Component\Resource\ResourceActions; |
8
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
12
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
13
|
|
|
|
14
|
|
|
class ResourceController extends BaseResourceController |
15
|
|
|
{ |
16
|
|
|
protected $activedPath = 'actived'; |
17
|
|
|
protected $enabledPath = 'enabled'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param $string |
21
|
|
|
* |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
|
|
protected function stringToBoolean($string) |
25
|
|
|
{ |
26
|
|
|
$string = is_string($string) ? strtolower($string) : $string; |
27
|
|
|
|
28
|
|
|
if (in_array($string, array(true, 1, '1', 'yes', 'true'), true)) { |
29
|
|
|
return true; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Request $request |
37
|
|
|
* @param $state |
38
|
|
|
* @param null $path |
39
|
|
|
* |
40
|
|
|
* @return RedirectResponse|Response |
41
|
|
|
*/ |
42
|
|
|
public function activeStateAction(Request $request, $state, $path = null) |
43
|
|
|
{ |
44
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
|
|
|
|
45
|
|
|
$path = $path ?: $this->activedPath; |
46
|
|
|
$resource = $this->findOr404($configuration); |
47
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
48
|
|
|
$accessor->setValue($resource, $path, $this->stringToBoolean($state)); |
49
|
|
|
|
50
|
|
|
if ($state) { |
51
|
|
|
// reset other to false |
52
|
|
|
$this->repository->bulkUpdate(array($path => false)); |
|
|
|
|
53
|
|
|
$this->manager->flush(); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!$configuration->isHtmlRequest()) { |
57
|
|
|
return $this->viewHandler->handle($configuration, View::create($resource, 204)); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->redirectHandler->redirectToReferer($configuration); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Request $request |
65
|
|
|
* @param $state |
66
|
|
|
* @param null $path |
67
|
|
|
* |
68
|
|
|
* @return RedirectResponse|Response |
69
|
|
|
*/ |
70
|
|
|
public function enableStateAction(Request $request, $state, $path = null) |
71
|
|
|
{ |
72
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$this->isGrantedOr403($configuration, ResourceActions::UPDATE); |
|
|
|
|
75
|
|
|
$resource = $this->findOr404($configuration); |
76
|
|
|
|
77
|
|
|
$path = $path ?: $this->enabledPath; |
78
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
79
|
|
|
$accessor->setValue($resource, $path, $this->stringToBoolean($state)); |
80
|
|
|
|
81
|
|
|
$resource = $this->findOr404($configuration); |
82
|
|
|
|
83
|
|
|
$this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource); |
|
|
|
|
84
|
|
|
$this->manager->flush(); |
|
|
|
|
85
|
|
|
$this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource); |
86
|
|
|
|
87
|
|
|
if (!$configuration->isHtmlRequest()) { |
88
|
|
|
return $this->viewHandler->handle($configuration, View::create($resource, 204)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->flashHelper->addSuccessFlash($configuration, $state ? $path . '_enabled' : $path . '_disabled', $resource); |
|
|
|
|
92
|
|
|
|
93
|
|
|
return $this->redirectHandler->redirectToIndex($configuration, $resource); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function batchDeleteAction(Request $request, array $ids = null) |
97
|
|
|
{ |
98
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
|
|
|
|
99
|
|
|
|
100
|
|
|
$this->isGrantedOr403($configuration, ResourceActions::DELETE); |
|
|
|
|
101
|
|
|
|
102
|
|
|
if (null == $ids) { |
103
|
|
|
$ids = $request->get('ids'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (is_string($ids)) { |
107
|
|
|
$ids = explode( ',', $ids); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$resources = $this->repository->findBy(array( |
111
|
|
|
'id' => $ids |
112
|
|
|
)); |
113
|
|
|
|
114
|
|
|
if (empty($resources)) { |
115
|
|
|
throw new NotFoundHttpException( |
116
|
|
|
sprintf( |
117
|
|
|
'Requested %s does not exist with these ids: %s.', |
118
|
|
|
$this->metadata->getPluralName(), |
119
|
|
|
json_encode($configuration->getCriteria($ids)) |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
foreach ($resources as $resource) { |
125
|
|
|
$this->manager->remove($resource); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->manager->flush(); |
|
|
|
|
129
|
|
|
|
130
|
|
|
if (!$configuration->isHtmlRequest()) { |
131
|
|
|
return $this->viewHandler->handle($configuration, View::create(null, 204)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->redirectHandler->redirectToIndex($configuration); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.