|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* TicketExtension.php |
|
4
|
|
|
* |
|
5
|
|
|
* @author Bram de Leeuw |
|
6
|
|
|
* Date: 09/03/17 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Broarm\EventTickets; |
|
10
|
|
|
|
|
11
|
|
|
use CalendarEvent_Controller; |
|
12
|
|
|
use DataExtension; |
|
13
|
|
|
use FieldList; |
|
14
|
|
|
use GridField; |
|
15
|
|
|
use GridFieldConfig_RecordEditor; |
|
16
|
|
|
use HasManyList; |
|
17
|
|
|
use HtmlEditorField; |
|
18
|
|
|
use LiteralField; |
|
19
|
|
|
use NumericField; |
|
20
|
|
|
use SiteConfig; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class TicketExtension |
|
24
|
|
|
* |
|
25
|
|
|
* @package Broarm\EventTickets |
|
26
|
|
|
* |
|
27
|
|
|
* @property TicketExtension|\CalendarEvent $owner |
|
28
|
|
|
* @property int Capacity |
|
29
|
|
|
* @property int OrderMin |
|
30
|
|
|
* @property int OrderMax |
|
31
|
|
|
* @property string SuccessMessage |
|
32
|
|
|
* @property string SuccessMessageMail |
|
33
|
|
|
* |
|
34
|
|
|
* @method \HasManyList Tickets() |
|
35
|
|
|
* @method \HasManyList Reservations() |
|
36
|
|
|
* @method \HasManyList Attendees() |
|
37
|
|
|
* @method \HasManyList WaitingList() |
|
38
|
|
|
* @method \HasManyList Fields() |
|
39
|
|
|
*/ |
|
40
|
|
|
class TicketExtension extends DataExtension |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* @var CalendarEvent_Controller |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $controller; |
|
46
|
|
|
|
|
47
|
|
|
private static $db = array( |
|
|
|
|
|
|
48
|
|
|
'Capacity' => 'Int', |
|
49
|
|
|
'OrderMin' => 'Int', |
|
50
|
|
|
'OrderMax' => 'Int', |
|
51
|
|
|
'SuccessMessage' => 'HTMLText', |
|
52
|
|
|
'SuccessMessageMail' => 'HTMLText' |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
private static $has_many = array( |
|
|
|
|
|
|
56
|
|
|
'Tickets' => 'Broarm\EventTickets\Ticket.Event', |
|
57
|
|
|
'Reservations' => 'Broarm\EventTickets\Reservation.Event', |
|
58
|
|
|
'Attendees' => 'Broarm\EventTickets\Attendee.Event', |
|
59
|
|
|
'WaitingList' => 'Broarm\EventTickets\WaitingListRegistration.Event', |
|
60
|
|
|
'Fields' => 'Broarm\EventTickets\UserField.Event' |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
private static $defaults = array( |
|
|
|
|
|
|
64
|
|
|
'Capacity' => 50 |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
private static $translate = array( |
|
|
|
|
|
|
68
|
|
|
'SuccessMessage', |
|
69
|
|
|
'SuccessMessageMail' |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
public function updateCMSFields(FieldList $fields) |
|
73
|
|
|
{ |
|
74
|
|
|
$ticketLabel = _t('TicketExtension.Tickets', 'Tickets'); |
|
75
|
|
|
$fields->addFieldsToTab( |
|
76
|
|
|
"Root.$ticketLabel", array( |
|
77
|
|
|
GridField::create('Tickets', $ticketLabel, $this->owner->Tickets(), TicketsGridFieldConfig::create($this->canCreateTickets())), |
|
|
|
|
|
|
78
|
|
|
NumericField::create('Capacity', _t('TicketExtension.Capacity', 'Capacity')), |
|
79
|
|
|
HtmlEditorField::create('SuccessMessage', _t('TicketExtension.SuccessMessage', 'Success message'))->setRows(4), |
|
80
|
|
|
HtmlEditorField::create('SuccessMessageMail', _t('TicketExtension.MailMessage', 'Mail message'))->setRows(4) |
|
81
|
|
|
)); |
|
82
|
|
|
|
|
83
|
|
|
// Create Reservations tab |
|
84
|
|
View Code Duplication |
if ($this->owner->Reservations()->exists()) { |
|
|
|
|
|
|
85
|
|
|
$reservationLabel = _t('TicketExtension.Reservations', 'Reservations'); |
|
86
|
|
|
$fields->addFieldToTab( |
|
87
|
|
|
"Root.$reservationLabel", |
|
88
|
|
|
GridField::create('Reservations', $reservationLabel, $this->owner->Reservations(), GridFieldConfig_RecordEditor::create()) |
|
|
|
|
|
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Create Attendees tab |
|
93
|
|
View Code Duplication |
if ($this->owner->Attendees()->exists()) { |
|
|
|
|
|
|
94
|
|
|
$guestListLabel = _t('TicketExtension.GuestList', 'GuestList'); |
|
95
|
|
|
$fields->addFieldToTab( |
|
96
|
|
|
"Root.$guestListLabel", |
|
97
|
|
|
GridField::create('Attendees', $guestListLabel, $this->owner->Attendees(), GuestListGridFieldConfig::create($this->owner)) |
|
|
|
|
|
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// Create WaitingList tab |
|
102
|
|
View Code Duplication |
if ($this->owner->WaitingList()->exists()) { |
|
|
|
|
|
|
103
|
|
|
$waitingListLabel = _t('TicketExtension.WaitingList', 'WaitingList'); |
|
104
|
|
|
$fields->addFieldToTab( |
|
105
|
|
|
"Root.$waitingListLabel", |
|
106
|
|
|
GridField::create('WaitingList', $waitingListLabel, $this->owner->WaitingList(), GridFieldConfig_RecordEditor::create()) |
|
|
|
|
|
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// Create Fields tab |
|
111
|
|
|
$extraFieldsLabel = _t('TicketExtension.ExtraFields', 'Attendee fields'); |
|
112
|
|
|
$fields->addFieldToTab( |
|
113
|
|
|
"Root.$extraFieldsLabel", |
|
114
|
|
|
GridField::create('ExtraFields', $extraFieldsLabel, $this->owner->Fields(), UserFieldsGridFieldConfig::create()) |
|
|
|
|
|
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
$this->owner->extend('updateTicketExtensionFields', $fields); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Trigger actions after write |
|
122
|
|
|
*/ |
|
123
|
|
|
public function onAfterWrite() |
|
124
|
|
|
{ |
|
125
|
|
|
$this->createDefaultFields(); |
|
126
|
|
|
parent::onAfterWrite(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Creates and sets up the default fields |
|
131
|
|
|
*/ |
|
132
|
|
|
public function createDefaultFields() |
|
133
|
|
|
{ |
|
134
|
|
|
$fields = Attendee::config()->get('default_fields'); |
|
135
|
|
|
if (!$this->owner->Fields()->exists()) { |
|
136
|
|
|
foreach ($fields as $fieldName => $config) { |
|
137
|
|
|
$field = UserField::createDefaultField($fieldName, $config); |
|
138
|
|
|
$this->owner->Fields()->add($field); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Extend the page actions with an start check in action |
|
145
|
|
|
* |
|
146
|
|
|
* @param FieldList $actions |
|
147
|
|
|
*/ |
|
148
|
|
|
public function updateCMSActions(FieldList $actions) |
|
149
|
|
|
{ |
|
150
|
|
|
$checkInButton = new LiteralField('StartCheckIn', |
|
151
|
|
|
"<a class='action ss-ui-button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only' |
|
152
|
|
|
id='Edit_StartCheckIn' |
|
153
|
|
|
role='button' |
|
154
|
|
|
href='{$this->owner->Link('checkin')}' |
|
155
|
|
|
target='_blank'> |
|
156
|
|
|
Start check in |
|
157
|
|
|
</a>" |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
if ($this->owner->Attendees()->exists()) { |
|
|
|
|
|
|
161
|
|
|
$actions->push($checkInButton); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Get the leftover capacity |
|
167
|
|
|
* |
|
168
|
|
|
* @return int |
|
|
|
|
|
|
169
|
|
|
*/ |
|
170
|
|
|
public function getAvailability() |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->owner->Capacity - $this->owner->getGuestList()->count(); |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Check if the event is expired, either by unavailable tickets or because the date has passed |
|
177
|
|
|
* |
|
178
|
|
|
* @return bool |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getEventExpired() |
|
181
|
|
|
{ |
|
182
|
|
|
$expired = false; |
|
183
|
|
|
if (($tickets = $this->owner->Tickets()) && $expired = $tickets->exists()) { |
|
184
|
|
|
/** @var Ticket $ticket */ |
|
185
|
|
|
foreach ($this->owner->Tickets() as $ticket) { |
|
|
|
|
|
|
186
|
|
|
$expired = (!$ticket->validateDate() && $expired); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $expired; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Get only the attendees who are certain to attend |
|
195
|
|
|
* Fixme: Using callback filter instead of join, because join gives ambiguous error on 'EventID' |
|
196
|
|
|
* |
|
197
|
|
|
* @return \ArrayList |
|
198
|
|
|
*/ |
|
199
|
|
|
public function getGuestList() |
|
200
|
|
|
{ |
|
201
|
|
|
return $this->owner->Attendees()->filterByCallback(function(Attendee $attendee, HasManyList $list) { |
|
|
|
|
|
|
202
|
|
|
return $attendee->Reservation()->Status === 'PAID'; |
|
203
|
|
|
}); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Get the success message |
|
208
|
|
|
* |
|
209
|
|
|
* @return mixed|string |
|
210
|
|
|
*/ |
|
211
|
|
View Code Duplication |
public function getSuccessContent() |
|
|
|
|
|
|
212
|
|
|
{ |
|
213
|
|
|
if (!empty($this->owner->SuccessMessage)) { |
|
214
|
|
|
return $this->owner->dbObject('SuccessMessage'); |
|
215
|
|
|
} else { |
|
216
|
|
|
return SiteConfig::current_site_config()->dbObject('SuccessMessage'); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Get the mail message |
|
222
|
|
|
* |
|
223
|
|
|
* @return mixed|string |
|
224
|
|
|
*/ |
|
225
|
|
View Code Duplication |
public function getMailContent() |
|
|
|
|
|
|
226
|
|
|
{ |
|
227
|
|
|
if (!empty($this->owner->SuccessMessageMail)) { |
|
228
|
|
|
return $this->owner->dbObject('SuccessMessageMail'); |
|
229
|
|
|
} else { |
|
230
|
|
|
return SiteConfig::current_site_config()->dbObject('SuccessMessageMail'); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Get the Ticket logo |
|
236
|
|
|
* |
|
237
|
|
|
* @return \Image |
|
238
|
|
|
*/ |
|
239
|
|
|
public function getMailLogo() |
|
240
|
|
|
{ |
|
241
|
|
|
return SiteConfig::current_site_config()->TicketLogo(); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Check if the current event can have tickets |
|
246
|
|
|
* |
|
247
|
|
|
* @return bool |
|
248
|
|
|
*/ |
|
249
|
|
|
public function canCreateTickets() |
|
250
|
|
|
{ |
|
251
|
|
|
$currentDate = $this->owner->getController()->CurrentDate(); |
|
|
|
|
|
|
252
|
|
|
if ($currentDate && $currentDate->exists()) { |
|
253
|
|
|
return $currentDate->dbObject('StartDate')->InFuture(); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
return false; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Get the calendar controller |
|
261
|
|
|
* |
|
262
|
|
|
* @return CalendarEvent_Controller |
|
263
|
|
|
*/ |
|
264
|
|
|
public function getController() |
|
265
|
|
|
{ |
|
266
|
|
|
return $this->controller |
|
267
|
|
|
? $this->controller |
|
268
|
|
|
: $this->controller = CalendarEvent_Controller::create($this->owner); |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.