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