1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Edit donor 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 donor page controller class |
18
|
|
|
* |
19
|
|
|
* @since 1.0 |
20
|
|
|
*/ |
21
|
|
|
class EditDonor extends Controller |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
* @since 1.6 |
26
|
|
|
*/ |
27
|
|
|
protected $donorId = 0; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \EBloodBank\Models\Donor|null |
31
|
|
|
* @since 1.0 |
32
|
|
|
*/ |
33
|
|
|
protected $donor; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @since 1.0 |
37
|
|
|
*/ |
38
|
|
|
public function __construct(ContainerInterface $container, $donorId) |
39
|
|
|
{ |
40
|
|
|
parent::__construct($container); |
41
|
|
|
if (EBB\isValidID($donorId)) { |
|
|
|
|
42
|
|
|
$this->donorId = (int) $donorId; |
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(), 'Donor', 'edit')) { |
53
|
|
|
$this->viewFactory->displayView('error-403'); |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($this->donorId) { |
58
|
|
|
$this->donor = $this->getDonorRepository()->find($this->donorId); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (! $this->donor) { |
62
|
|
|
$this->viewFactory->displayView('error-404'); |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$donor = $this->donor; |
67
|
|
|
|
68
|
|
|
if (! $this->getAcl()->canEditEntity($this->getAuthenticatedUser(), $donor)) { |
69
|
|
|
$this->viewFactory->displayView('error-403'); |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->doActions(); |
74
|
|
|
$this->addNotices(); |
75
|
|
|
$this->viewFactory->displayView('edit-donor', [ |
76
|
|
|
'donor' => $donor, |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return void |
82
|
|
|
* @since 1.0 |
83
|
|
|
*/ |
84
|
|
|
protected function doActions() |
85
|
|
|
{ |
86
|
|
|
switch (filter_input(INPUT_POST, 'action')) { |
87
|
|
|
case 'submit_donor': |
88
|
|
|
$this->doSubmitAction(); |
89
|
|
|
break; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return void |
95
|
|
|
* @since 1.0 |
96
|
|
|
*/ |
97
|
|
|
protected function addNotices() |
98
|
|
|
{ |
99
|
|
|
if (filter_has_var(INPUT_GET, 'flag-edited')) { |
100
|
|
|
Notices::addNotice('edited', __('Donor edited.'), 'success'); |
101
|
|
|
} |
102
|
|
|
if ($this->donor && $this->donor->isPending()) { |
103
|
|
|
Notices::addNotice('pending', __('This donor is pendng moderation.'), 'warning'); |
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
|
|
|
$donor = $this->donor; |
122
|
|
|
|
123
|
|
|
if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canEditEntity($this->getAuthenticatedUser(), $donor)) { |
|
|
|
|
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// Set the donor name. |
128
|
|
|
$donor->set('name', filter_input(INPUT_POST, 'donor_name'), true); |
|
|
|
|
129
|
|
|
|
130
|
|
|
// Set the donor gender. |
131
|
|
|
$donor->set('gender', filter_input(INPUT_POST, 'donor_gender'), true); |
132
|
|
|
|
133
|
|
|
// Set the donor birthdate. |
134
|
|
|
$donor->set('birthdate', filter_input(INPUT_POST, 'donor_birthdate'), true); |
135
|
|
|
|
136
|
|
|
// Set the donor blood group. |
137
|
|
|
$donor->set('blood_group', filter_input(INPUT_POST, 'donor_blood_group'), true); |
138
|
|
|
|
139
|
|
|
// Set the donor district ID. |
140
|
|
|
$donor->set('district', $this->getDistrictRepository()->find(filter_input(INPUT_POST, 'donor_district_id'))); |
141
|
|
|
|
142
|
|
|
// Set the donor weight. |
143
|
|
|
$donor->setMeta('weight', filter_input(INPUT_POST, 'donor_weight'), true); |
144
|
|
|
|
145
|
|
|
// Set the donor email address. |
146
|
|
|
$donor->setMeta('email', filter_input(INPUT_POST, 'donor_email'), true); |
147
|
|
|
|
148
|
|
|
// Set the donor email address visibility. |
149
|
|
|
$donor->setMeta('email_visibility', filter_input(INPUT_POST, 'donor_email_visibility'), true); |
150
|
|
|
|
151
|
|
|
// Set the donor phone number. |
152
|
|
|
$donor->setMeta('phone', filter_input(INPUT_POST, 'donor_phone'), true); |
153
|
|
|
|
154
|
|
|
// Set the donor phone number visibility. |
155
|
|
|
$donor->setMeta('phone_visibility', filter_input(INPUT_POST, 'donor_phone_visibility'), true); |
156
|
|
|
|
157
|
|
|
// Set the donor address. |
158
|
|
|
$donor->setMeta('address', filter_input(INPUT_POST, 'donor_address'), true); |
159
|
|
|
|
160
|
|
|
// Set the donor status. |
161
|
|
|
if ($donor->isApproved() && ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'Donor', 'approve')) { |
162
|
|
|
$donor->set('status', 'pending'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->getEntityManager()->flush($donor); |
166
|
|
|
|
167
|
|
|
EBB\redirect( |
|
|
|
|
168
|
|
|
EBB\addQueryArgs( |
|
|
|
|
169
|
|
|
EBB\getEditDonorURL($donor->get('id')), |
|
|
|
|
170
|
|
|
['flag-edited' => true] |
171
|
|
|
) |
172
|
|
|
); |
173
|
|
|
} catch (InvalidArgumentException $ex) { |
174
|
|
|
Notices::addNotice('invalid_donor_argument', $ex->getMessage()); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|