1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TicketsField.php |
4
|
|
|
* |
5
|
|
|
* @author Bram de Leeuw |
6
|
|
|
* Date: 10/03/17 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Broarm\EventTickets; |
10
|
|
|
|
11
|
|
|
use ArrayList; |
12
|
|
|
use DataList; |
13
|
|
|
use DBField; |
14
|
|
|
use DropdownField; |
15
|
|
|
use FormField; |
16
|
|
|
use NumericField; |
17
|
|
|
use Validator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class TicketsField |
21
|
|
|
* |
22
|
|
|
* @package Broarm\EventTickets |
23
|
|
|
*/ |
24
|
|
|
class TicketsField extends FormField |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
protected $tickets; |
28
|
|
|
|
29
|
|
|
protected $template = 'TicketsField'; |
30
|
|
|
|
31
|
|
|
public function __construct($name, $title, DataList $tickets) |
32
|
|
|
{ |
33
|
|
|
$this->tickets = $tickets; |
34
|
|
|
parent::__construct($name, $title); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set the ticket list |
39
|
|
|
* |
40
|
|
|
* @param DataList $tickets |
41
|
|
|
*/ |
42
|
|
|
public function setTickets(DataList $tickets) |
43
|
|
|
{ |
44
|
|
|
$this->tickets = $tickets; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the ticket list |
49
|
|
|
* |
50
|
|
|
* @return DataList |
51
|
|
|
*/ |
52
|
|
|
public function getTickets() |
53
|
|
|
{ |
54
|
|
|
return $this->tickets; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get a list of editable tickets |
59
|
|
|
* These have an numeric input field |
60
|
|
|
* |
61
|
|
|
* @return ArrayList |
62
|
|
|
*/ |
63
|
|
|
private function getEditableTickets() |
64
|
|
|
{ |
65
|
|
|
$tickets = ArrayList::create(); |
66
|
|
|
foreach ($this->getTickets() as $ticket) { |
67
|
|
|
/** @var Ticket $ticket */ |
68
|
|
|
$fieldName = $this->name . "[{$ticket->ID}][Amount]"; |
69
|
|
|
$range = range($ticket->Event()->OrderMin, $ticket->Event()->OrderMax); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$ticket->AmountField = DropdownField::create( |
72
|
|
|
$fieldName, |
73
|
|
|
'Amount', |
74
|
|
|
array_combine($range, $range), |
75
|
|
|
$ticket->Event()->OrderMin |
|
|
|
|
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
$availability = $ticket->Event()->getAvailability(); |
|
|
|
|
79
|
|
|
if ($availability < $ticket->Event()->OrderMax) { |
|
|
|
|
80
|
|
|
$disabled = range($availability + 1, $ticket->Event()->OrderMax); |
|
|
|
|
81
|
|
|
$ticket->AmountField->setDisabledItems(array_combine($disabled, $disabled)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (!$ticket->getAvailable()) { |
85
|
|
|
$ticket->AmountField->setDisabled(true); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$tickets->push($ticket); |
89
|
|
|
} |
90
|
|
|
return $tickets; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the field customized with tickets and reservation |
95
|
|
|
* |
96
|
|
|
* @param array $properties |
97
|
|
|
* |
98
|
|
|
* @return \HTMLText|string |
99
|
|
|
*/ |
100
|
|
|
public function Field($properties = array()) |
101
|
|
|
{ |
102
|
|
|
$context = $this; |
103
|
|
|
$properties['Tickets'] = $this->getEditableTickets(); |
104
|
|
|
|
105
|
|
|
if (count($properties)) { |
106
|
|
|
$context = $context->customise($properties); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->extend('onBeforeRender', $this); |
110
|
|
|
|
111
|
|
|
$result = $context->renderWith($this->getTemplates()); |
112
|
|
|
|
113
|
|
View Code Duplication |
if (is_string($result)) { |
|
|
|
|
114
|
|
|
$result = trim($result); |
115
|
|
|
} else { |
116
|
|
|
if ($result instanceof DBField) { |
117
|
|
|
$result->setValue(trim($result->getValue())); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $result; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Make sure a ticket is selected and that the selected amount is available |
126
|
|
|
* |
127
|
|
|
* @param Validator $validator |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function validate($validator) |
132
|
|
|
{ |
133
|
|
|
// Throw an error when there are no tickets selected |
134
|
|
|
if (empty($this->value)) { |
135
|
|
|
$validator->validationError($this->name, _t( |
136
|
|
|
'TicketsField.VALIDATION_EMPTY', |
137
|
|
|
'Select at least one ticket' |
138
|
|
|
), 'validation'); |
139
|
|
|
|
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// Get the availability |
144
|
|
|
$available = $this->getForm()->event->getAvailability(); |
145
|
|
|
// get the sum of selected tickets |
146
|
|
|
$ticketCount = array_sum(array_map(function ($item) { |
147
|
|
|
return $item['Amount']; |
148
|
|
|
}, $this->value)); |
149
|
|
|
|
150
|
|
|
// If the sum of tickets is 0 trow the same error as empty |
151
|
|
|
if ($ticketCount === 0) { |
152
|
|
|
$validator->validationError($this->name, _t( |
153
|
|
|
'TicketsField.VALIDATION_EMPTY', |
154
|
|
|
'Select at least one ticket' |
155
|
|
|
), 'validation'); |
156
|
|
|
|
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// Throw an error when there are more tickets selected than available |
161
|
|
|
if ($ticketCount > $available) { |
162
|
|
|
$validator->validationError($this->name, _t( |
163
|
|
|
'TicketsField.VALIDATION_TO_MUCH', |
164
|
|
|
'There are {ticketCount} tickets left', |
165
|
|
|
null, |
166
|
|
|
array( |
|
|
|
|
167
|
|
|
'ticketCount' => $available |
168
|
|
|
) |
169
|
|
|
), 'validation'); |
170
|
|
|
|
171
|
|
|
return false; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return false; |
175
|
|
|
} |
176
|
|
|
} |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: