1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains a form creator for Bans |
4
|
|
|
* |
5
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace BZIon\Form\Creator; |
9
|
|
|
|
10
|
|
|
use BZIon\Form\Type\AdvancedModelType; |
11
|
|
|
use BZIon\Form\Type\IpType; |
12
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
13
|
|
|
use Symfony\Component\Form\Extension\Core\Type\DateType; |
14
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextareaType; |
16
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
17
|
|
|
use Symfony\Component\Form\Form; |
18
|
|
|
use Symfony\Component\Validator\Constraints\Length; |
19
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Form creator for bans |
23
|
|
|
* |
24
|
|
|
* @property \Ban $editing |
25
|
|
|
*/ |
26
|
|
|
class BanFormCreator extends ModelFormCreator |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
protected function build($builder) |
32
|
|
|
{ |
33
|
|
|
$builder |
34
|
|
|
->add('player', new AdvancedModelType('player'), [ |
35
|
|
|
'constraints' => [ |
36
|
|
|
new NotBlank() |
37
|
|
|
], |
38
|
|
|
'disabled' => $this->isEdit(), |
39
|
|
|
'required' => true, |
40
|
|
|
]) |
41
|
|
|
->add('expiration', DateType::class, [ |
42
|
|
|
'data' => \TimeDate::now(), |
43
|
|
|
'label' => 'Expiration Date', |
44
|
|
|
'required' => false, |
45
|
|
|
]) |
46
|
|
|
->add('is_permanent', CheckboxType::class, [ |
47
|
|
|
'label' => 'Permanent Ban', |
48
|
|
|
'required' => false, |
49
|
|
|
'attr' => [ |
50
|
|
|
'data-help-message' => 'When checked, no expiration date needs to be set and will be ignored', |
51
|
|
|
], |
52
|
|
|
]) |
53
|
|
|
->add('reason', TextareaType::class, [ |
54
|
|
|
'constraints' => [ |
55
|
|
|
new NotBlank() |
56
|
|
|
], |
57
|
|
|
'required' => true, |
58
|
|
|
]) |
59
|
|
|
->add('is_soft_ban', CheckboxType::class, [ |
60
|
|
|
'label' => 'Soft Ban', |
61
|
|
|
'required' => false, |
62
|
|
|
'attr' => [ |
63
|
|
|
'data-help-message' => "A soft ban will not affect a player's permissions on this site; e.g. a mute", |
64
|
|
|
], |
65
|
|
|
]) |
66
|
|
|
->add('server_message', TextType::class, [ |
67
|
|
|
'required' => false, |
68
|
|
|
'constraints' => [ |
69
|
|
|
new Length([ |
70
|
|
|
'max' => 150, |
71
|
|
|
]), |
72
|
|
|
], |
73
|
|
|
'attr' => [ |
74
|
|
|
'data-help-message' => 'The ban message that will appear on match servers', |
75
|
|
|
], |
76
|
|
|
]) |
77
|
|
|
->add('ip_addresses', new IpType(), [ |
78
|
|
|
'label' => 'IP Addresses', |
79
|
|
|
'required' => false, |
80
|
|
|
'attr' => [ |
81
|
|
|
'data-help-message' => 'The IP addresses that will be banned on match servers. Use commas to separate multiple IPs.', |
82
|
|
|
], |
83
|
|
|
]) |
84
|
|
|
; |
85
|
|
|
|
86
|
|
|
return $builder->add('submit', SubmitType::class, [ |
87
|
|
|
'label' => 'Enter Ban', |
88
|
|
|
]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
* |
94
|
|
|
* @param \Ban $ban |
95
|
|
|
*/ |
96
|
|
|
public function fill($form, $ban) |
97
|
|
|
{ |
98
|
|
|
$form->get('player')->setData($ban->getVictim()); |
99
|
|
|
$form->get('is_permanent')->setData($ban->isPermanent()); |
100
|
|
|
$form->get('reason')->setData($ban->getReason()); |
101
|
|
|
$form->get('is_soft_ban')->setData($ban->isSoftBan()); |
102
|
|
|
$form->get('server_message')->setData($ban->getServerMessage()); |
103
|
|
|
$form->get('ip_addresses')->setData($ban->getIpAddresses()); |
104
|
|
|
|
105
|
|
|
if (!$ban->isPermanent()) { |
106
|
|
|
$form->get('expiration')->setData($ban->getExpiration()); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
* |
113
|
|
|
* @param \Ban $ban |
114
|
|
|
*/ |
115
|
|
|
public function update($form, $ban) |
116
|
|
|
{ |
117
|
|
|
$ban |
118
|
|
|
->setIPs($form->get('ip_addresses')->getData()) |
119
|
|
|
->setExpiration($this->getExpiration($form)) |
|
|
|
|
120
|
|
|
->setReason($form->get('reason')->getData()) |
121
|
|
|
->setServerMessage($form->get('server_message')->getData()) |
122
|
|
|
->setSoftBan($form->get('is_soft_ban')->getData()) |
123
|
|
|
; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function enter($form) |
130
|
|
|
{ |
131
|
|
|
return |
132
|
|
|
\Ban::addBan( |
133
|
|
|
$form->get('player')->getData()->getId(), |
134
|
|
|
$this->me->getId(), |
135
|
|
|
$this->getExpiration($form), |
136
|
|
|
$form->get('reason')->getData(), |
137
|
|
|
$form->get('server_message')->getData(), |
138
|
|
|
$form->get('ip_addresses')->getData(), |
139
|
|
|
$form->get('is_soft_ban')->getData() |
140
|
|
|
) |
141
|
|
|
; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the expiration time of the ban based on the fields of the form |
146
|
|
|
* |
147
|
|
|
* @param Form $form The form |
148
|
|
|
* |
149
|
|
|
* @return \TimeDate|null |
150
|
|
|
*/ |
151
|
|
|
private function getExpiration($form) |
152
|
|
|
{ |
153
|
|
|
if ($form->get('is_permanent')->getData()) { |
154
|
|
|
return null; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $form->get('expiration')->getData(); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: