1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Delete cities page controller class file |
4
|
|
|
* |
5
|
|
|
* @package EBloodBank |
6
|
|
|
* @subpackage Controllers |
7
|
|
|
* @since 1.1 |
8
|
|
|
*/ |
9
|
|
|
namespace EBloodBank\Controllers; |
10
|
|
|
|
11
|
|
|
use EBloodBank as EBB; |
12
|
|
|
use EBloodBank\Notices; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Delete cities page controller class |
16
|
|
|
* |
17
|
|
|
* @since 1.1 |
18
|
|
|
*/ |
19
|
|
|
class DeleteCities extends Controller |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var \EBloodBank\Models\City[] |
23
|
|
|
* @since 1.1 |
24
|
|
|
*/ |
25
|
|
|
protected $cities = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return void |
29
|
|
|
* @since 1.1 |
30
|
|
|
*/ |
31
|
|
|
public function __invoke() |
32
|
|
|
{ |
33
|
|
|
if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'City', 'delete')) { |
34
|
|
|
$this->viewFactory->displayView('error-403'); |
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (filter_has_var(INPUT_POST, 'cities')) { |
39
|
|
|
$citiesIDs = filter_input(INPUT_POST, 'cities', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY); |
40
|
|
|
if (! empty($citiesIDs) && is_array($citiesIDs)) { |
41
|
|
|
$this->cities = $this->getCityRepository()->findBy(['id' => $citiesIDs]); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->doActions(); |
46
|
|
|
$this->viewFactory->displayView( |
47
|
|
|
'delete-cities', |
48
|
|
|
[ |
49
|
|
|
'cities' => $this->cities, |
50
|
|
|
] |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return void |
56
|
|
|
* @since 1.1 |
57
|
|
|
*/ |
58
|
|
|
protected function doActions() |
59
|
|
|
{ |
60
|
|
|
switch (filter_input(INPUT_POST, 'action')) { |
61
|
|
|
case 'delete_cities': |
62
|
|
|
$this->doDeleteAction(); |
63
|
|
|
break; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return void |
69
|
|
|
* @since 1.1 |
70
|
|
|
*/ |
71
|
|
|
protected function doDeleteAction() |
72
|
|
|
{ |
73
|
|
|
if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'City', 'delete')) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$sessionToken = $this->getSession()->getCsrfToken(); |
78
|
|
|
$actionToken = filter_input(INPUT_POST, 'token'); |
79
|
|
|
|
80
|
|
|
if (! $actionToken || ! $sessionToken->isValid($actionToken)) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$cities = $this->cities; |
85
|
|
|
|
86
|
|
|
if (! $cities || ! is_array($cities)) { |
|
|
|
|
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$deletedCitiesCount = 0; |
91
|
|
|
|
92
|
|
|
foreach ($cities as $city) { |
93
|
|
|
if ($this->getAcl()->canDeleteEntity($this->getAuthenticatedUser(), $city)) { |
94
|
|
|
$districtsCount = $this->getDistrictRepository()->countBy(['city' => $city]); |
95
|
|
|
|
96
|
|
|
if ($districtsCount > 0) { |
97
|
|
|
Notices::addNotice('linked_districts_exists', sprintf(__('At first, delete any linked districts with city "%s".'), $city->get('name'))); |
98
|
|
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->getEntityManager()->remove($city); |
102
|
|
|
$deletedCitiesCount++; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->getEntityManager()->flush(); |
107
|
|
|
|
108
|
|
|
EBB\redirect( |
|
|
|
|
109
|
|
|
EBB\addQueryArgs( |
|
|
|
|
110
|
|
|
EBB\getEditCitiesURL(), |
|
|
|
|
111
|
|
|
['flag-deleted' => $deletedCitiesCount] |
112
|
|
|
) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.