1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Edit city page controller class file |
4
|
|
|
* |
5
|
|
|
* @package EBloodBank |
6
|
|
|
* @subpackage Controllers |
7
|
|
|
* @since 1.0 |
8
|
|
|
*/ |
9
|
|
|
namespace EBloodBank\Controllers; |
10
|
|
|
|
11
|
|
|
use InvalidArgumentException; |
12
|
|
|
use EBloodBank as EBB; |
13
|
|
|
use EBloodBank\Notices; |
14
|
|
|
use Psr\Container\ContainerInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Edit city page controller class |
18
|
|
|
* |
19
|
|
|
* @since 1.0 |
20
|
|
|
*/ |
21
|
|
|
class EditCity extends Controller |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
* @since 1.6 |
26
|
|
|
*/ |
27
|
|
|
protected $cityId = 0; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \EBloodBank\Models\City|null |
31
|
|
|
* @since 1.0 |
32
|
|
|
*/ |
33
|
|
|
protected $city; |
34
|
|
|
|
35
|
|
|
/** |
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', 'edit')) { |
53
|
|
|
$this->viewFactory->displayView('error-403'); |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (EBB\isValidID($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()->canEditEntity($this->getAuthenticatedUser(), $city)) { |
69
|
|
|
$this->viewFactory->displayView('error-403'); |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->doActions(); |
74
|
|
|
$this->addNotices(); |
75
|
|
|
$this->viewFactory->displayView( |
76
|
|
|
'edit-city', |
77
|
|
|
[ |
78
|
|
|
'city' => $city, |
79
|
|
|
] |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return void |
85
|
|
|
* @since 1.0 |
86
|
|
|
*/ |
87
|
|
|
protected function doActions() |
88
|
|
|
{ |
89
|
|
|
switch (filter_input(INPUT_POST, 'action')) { |
90
|
|
|
case 'submit_city': |
91
|
|
|
$this->doSubmitAction(); |
92
|
|
|
break; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return void |
98
|
|
|
* @since 1.0 |
99
|
|
|
*/ |
100
|
|
|
protected function addNotices() |
101
|
|
|
{ |
102
|
|
|
if (filter_has_var(INPUT_GET, 'flag-edited')) { |
103
|
|
|
Notices::addNotice('edited', __('City edited.'), 'success'); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return void |
109
|
|
|
* @since 1.0 |
110
|
|
|
*/ |
111
|
|
|
protected function doSubmitAction() |
112
|
|
|
{ |
113
|
|
|
try { |
114
|
|
|
$sessionToken = $this->getSession()->getCsrfToken(); |
115
|
|
|
$actionToken = filter_input(INPUT_POST, 'token'); |
116
|
|
|
|
117
|
|
|
if (! $actionToken || ! $sessionToken->isValid($actionToken)) { |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$city = $this->city; |
122
|
|
|
|
123
|
|
|
if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canEditEntity($this->getAuthenticatedUser(), $city)) { |
|
|
|
|
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// Set the city name. |
128
|
|
|
$city->set('name', filter_input(INPUT_POST, 'city_name'), true); |
|
|
|
|
129
|
|
|
|
130
|
|
|
$duplicateCity = $this->getCityRepository()->findOneBy(['name' => $city->get('name')]); |
131
|
|
|
|
132
|
|
|
if (! empty($duplicateCity) && $duplicateCity->get('id') != $city->get('id')) { |
133
|
|
|
throw new InvalidArgumentException(__('Please enter a unique city name.')); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->getEntityManager()->flush($city); |
137
|
|
|
|
138
|
|
|
EBB\redirect( |
|
|
|
|
139
|
|
|
EBB\addQueryArgs( |
|
|
|
|
140
|
|
|
EBB\getEditCityURL($city->get('id')), |
|
|
|
|
141
|
|
|
['flag-edited' => true] |
142
|
|
|
) |
143
|
|
|
); |
144
|
|
|
} catch (InvalidArgumentException $ex) { |
145
|
|
|
Notices::addNotice('invalid_city_argument', $ex->getMessage()); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|