1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Delete districts 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
|
|
|
use Psr\Container\ContainerInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Delete districts page controller class |
17
|
|
|
* |
18
|
|
|
* @since 1.1 |
19
|
|
|
*/ |
20
|
|
|
class DeleteDistricts extends Controller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \EBloodBank\Models\District[] |
24
|
|
|
* @since 1.1 |
25
|
|
|
*/ |
26
|
|
|
protected $districts = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return void |
30
|
|
|
* @since 1.1 |
31
|
|
|
*/ |
32
|
|
|
public function __invoke() |
33
|
|
|
{ |
34
|
|
|
if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'District', 'delete')) { |
35
|
|
|
$this->viewFactory->displayView('error-403'); |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (filter_has_var(INPUT_POST, 'districts')) { |
40
|
|
|
$districtsIDs = filter_input(INPUT_POST, 'districts', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY); |
41
|
|
|
if (! empty($districtsIDs) && is_array($districtsIDs)) { |
42
|
|
|
$this->districts = $this->getDistrictRepository()->findBy(['id' => $districtsIDs]); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->doActions(); |
47
|
|
|
$this->viewFactory->displayView( |
48
|
|
|
'delete-districts', |
49
|
|
|
[ |
50
|
|
|
'districts' => $this->districts, |
51
|
|
|
] |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return void |
57
|
|
|
* @since 1.1 |
58
|
|
|
*/ |
59
|
|
|
protected function doActions() |
60
|
|
|
{ |
61
|
|
|
switch (filter_input(INPUT_POST, 'action')) { |
62
|
|
|
case 'delete_districts': |
63
|
|
|
$this->doDeleteAction(); |
64
|
|
|
break; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return void |
70
|
|
|
* @since 1.1 |
71
|
|
|
*/ |
72
|
|
|
protected function doDeleteAction() |
73
|
|
|
{ |
74
|
|
|
if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'District', 'delete')) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$sessionToken = $this->getSession()->getCsrfToken(); |
79
|
|
|
$actionToken = filter_input(INPUT_POST, 'token'); |
80
|
|
|
|
81
|
|
|
if (! $actionToken || ! $sessionToken->isValid($actionToken)) { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$districts = $this->districts; |
86
|
|
|
|
87
|
|
|
if (! $districts || ! is_array($districts)) { |
|
|
|
|
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$deletedDistrictsCount = 0; |
92
|
|
|
|
93
|
|
|
foreach ($districts as $district) { |
94
|
|
|
if ($this->getAcl()->canDeleteEntity($this->getAuthenticatedUser(), $district)) { |
95
|
|
|
$donorsCount = $this->getDonorRepository()->countBy(['district' => $districts]); |
96
|
|
|
|
97
|
|
|
if ($donorsCount > 0) { |
98
|
|
|
Notices::addNotice('linked_donors_exists', sprintf(__('At first, delete any linked donors with district "%s".'), $district->get('id'))); |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->getEntityManager()->remove($district); |
103
|
|
|
$deletedDistrictsCount++; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->getEntityManager()->flush(); |
108
|
|
|
|
109
|
|
|
EBB\redirect( |
|
|
|
|
110
|
|
|
EBB\addQueryArgs( |
|
|
|
|
111
|
|
|
EBB\getEditDistrictsURL(), |
|
|
|
|
112
|
|
|
['flag-deleted' => $deletedDistrictsCount] |
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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.