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