|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Delete city page controller class file |
|
4
|
|
|
* |
|
5
|
|
|
* @package EBloodBank |
|
6
|
|
|
* @subpackage Controllers |
|
7
|
|
|
* @since 1.0 |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace EBloodBank\Controllers; |
|
10
|
|
|
|
|
11
|
|
|
use EBloodBank as EBB; |
|
12
|
|
|
use EBloodBank\Notices; |
|
13
|
|
|
use Psr\Container\ContainerInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Delete city page controller class |
|
17
|
|
|
* |
|
18
|
|
|
* @since 1.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class DeleteCity extends Controller |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var int |
|
24
|
|
|
* @since 1.6 |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $cityId = 0; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \EBloodBank\Models\City|null |
|
30
|
|
|
* @since 1.0 |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $city; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return void |
|
36
|
|
|
* @since 1.0 |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(ContainerInterface $container, $cityId) |
|
39
|
|
|
{ |
|
40
|
|
|
parent::__construct($container); |
|
41
|
|
|
if (EBB\isValidID($cityId)) { |
|
|
|
|
|
|
42
|
|
|
$this->cityId = $cityId; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return void |
|
48
|
|
|
* @since 1.0 |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __invoke() |
|
51
|
|
|
{ |
|
52
|
|
|
if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'City', 'delete')) { |
|
53
|
|
|
$this->viewFactory->displayView('error-403'); |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($this->cityId) { |
|
58
|
|
|
$this->city = $this->getCityRepository()->find($this->cityId); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (! $this->city) { |
|
62
|
|
|
$this->viewFactory->displayView('error-404'); |
|
63
|
|
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$city = $this->city; |
|
67
|
|
|
|
|
68
|
|
|
if (! $this->getAcl()->canDeleteEntity($this->getAuthenticatedUser(), $city)) { |
|
69
|
|
|
$this->viewFactory->displayView('error-403'); |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$this->doActions(); |
|
74
|
|
|
$this->viewFactory->displayView( |
|
75
|
|
|
'delete-city', |
|
76
|
|
|
[ |
|
77
|
|
|
'city' => $city, |
|
78
|
|
|
] |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return void |
|
84
|
|
|
* @since 1.0 |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function doActions() |
|
87
|
|
|
{ |
|
88
|
|
|
switch (filter_input(INPUT_POST, 'action')) { |
|
89
|
|
|
case 'delete_city': |
|
90
|
|
|
$this->doDeleteAction(); |
|
91
|
|
|
break; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return void |
|
97
|
|
|
* @since 1.0 |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function doDeleteAction() |
|
100
|
|
|
{ |
|
101
|
|
|
$sessionToken = $this->getSession()->getCsrfToken(); |
|
102
|
|
|
$actionToken = filter_input(INPUT_POST, 'token'); |
|
103
|
|
|
|
|
104
|
|
|
if (! $actionToken || ! $sessionToken->isValid($actionToken)) { |
|
105
|
|
|
return; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$city = $this->city; |
|
109
|
|
|
|
|
110
|
|
|
if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canDeleteEntity($this->getAuthenticatedUser(), $city)) { |
|
|
|
|
|
|
111
|
|
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$districtsCount = $this->getDistrictRepository()->countBy(['city' => $city]); |
|
115
|
|
|
|
|
116
|
|
|
if ($districtsCount > 0) { |
|
117
|
|
|
Notices::addNotice('linked_districts_exists', __('At first, delete any linked districts with this city.')); |
|
118
|
|
|
return; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$this->getEntityManager()->remove($city); |
|
122
|
|
|
$this->getEntityManager()->flush(); |
|
123
|
|
|
|
|
124
|
|
|
EBB\redirect( |
|
|
|
|
|
|
125
|
|
|
EBB\addQueryArgs( |
|
|
|
|
|
|
126
|
|
|
EBB\getEditCitiesURL(), |
|
|
|
|
|
|
127
|
|
|
['flag-deleted' => 1] |
|
128
|
|
|
) |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|