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 string SuccessMessage |
30
|
|
|
* @property string SuccessMessageMail |
31
|
|
|
* |
32
|
|
|
* @method \HasManyList Tickets |
33
|
|
|
* @method \HasManyList Reservations |
34
|
|
|
* @method \HasManyList Attendees |
35
|
|
|
*/ |
36
|
|
|
class TicketExtension extends DataExtension |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var CalendarEvent_Controller |
40
|
|
|
*/ |
41
|
|
|
protected $controller; |
42
|
|
|
|
43
|
|
|
private static $db = array( |
|
|
|
|
44
|
|
|
'Capacity' => 'Int', |
45
|
|
|
'SuccessMessage' => 'HTMLText', |
46
|
|
|
'SuccessMessageMail' => 'HTMLText' |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
private static $has_many = array( |
|
|
|
|
50
|
|
|
'Tickets' => 'Broarm\EventTickets\Ticket.Event', |
51
|
|
|
'Reservations' => 'Broarm\EventTickets\Reservation.Event', |
52
|
|
|
'Attendees' => 'Broarm\EventTickets\Attendee.Event' |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
public function updateCMSFields(FieldList $fields) |
56
|
|
|
{ |
57
|
|
|
$gridFieldConfig = GridFieldConfig_RecordEditor::create(); |
58
|
|
|
|
59
|
|
|
// If the event dates are in the past remove ability to create new tickets, reservations and attendees. |
60
|
|
|
// This is done here instead of in the canCreate method because of the lack of context there. |
61
|
|
|
if (!$this->canCreateTickets()) { |
62
|
|
|
$gridFieldConfig->removeComponentsByType(new GridFieldAddNewButton()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$ticketLabel = _t('TicketExtension.Tickets', 'Tickets'); |
66
|
|
|
$fields->addFieldsToTab( |
67
|
|
|
"Root.$ticketLabel", array( |
68
|
|
|
GridField::create('Tickets', $ticketLabel, $this->owner->Tickets(), $gridFieldConfig), |
69
|
|
|
NumericField::create('Capacity', _t('TicketExtension.Capacity', 'Capacity')), |
70
|
|
|
HtmlEditorField::create('SuccessMessage', 'Success message')->setRows(4), |
71
|
|
|
HtmlEditorField::create('SuccessMessageMail', 'Mail message')->setRows(4) |
72
|
|
|
)); |
73
|
|
|
|
74
|
|
View Code Duplication |
if ($this->owner->Reservations()->exists()) { |
|
|
|
|
75
|
|
|
$reservationLabel = _t('TicketExtension.Reservations', 'Reservations'); |
76
|
|
|
$fields->addFieldToTab( |
77
|
|
|
"Root.$reservationLabel", |
78
|
|
|
GridField::create('Reservations', $reservationLabel, $this->owner->Reservations(), $gridFieldConfig) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
if ($this->owner->Attendees()->exists()) { |
|
|
|
|
83
|
|
|
$guestListLabel = _t('TicketExtension.GuestList', 'GuestList'); |
84
|
|
|
$fields->addFieldToTab( |
85
|
|
|
"Root.$guestListLabel", |
86
|
|
|
GridField::create('Attendees', $guestListLabel, $this->owner->Attendees(), $gridFieldConfig) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $fields; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Extend the page actions with an start check in action |
95
|
|
|
* |
96
|
|
|
* @param FieldList $actions |
97
|
|
|
*/ |
98
|
|
|
public function updateCMSActions(FieldList $actions) |
99
|
|
|
{ |
100
|
|
|
$checkInButton = new LiteralField('StartCheckIn', |
101
|
|
|
"<a class='action ss-ui-button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only' |
102
|
|
|
id='Edit_StartCheckIn' |
103
|
|
|
role='button' |
104
|
|
|
href='{$this->owner->Link('checkin')}' |
105
|
|
|
target='_blank'> |
106
|
|
|
Start check in |
107
|
|
|
</a>" |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
if ($this->owner->Attendees()->exists()) { |
111
|
|
|
$actions->push($checkInButton); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the leftover capacity |
117
|
|
|
* |
118
|
|
|
* @return int |
|
|
|
|
119
|
|
|
*/ |
120
|
|
|
public function getAvailability() |
121
|
|
|
{ |
122
|
|
|
return $this->owner->Capacity - $this->owner->Attendees()->count(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the success message |
127
|
|
|
* |
128
|
|
|
* @return mixed|string |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
public function getSuccessContent() |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
if (!empty($this->owner->SuccessMessage)) { |
133
|
|
|
return $this->owner->dbObject('SuccessMessage'); |
134
|
|
|
} else { |
135
|
|
|
return SiteConfig::current_site_config()->dbObject('SuccessMessage'); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get the mail message |
141
|
|
|
* |
142
|
|
|
* @return mixed|string |
143
|
|
|
*/ |
144
|
|
View Code Duplication |
public function getMailContent() |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
if (!empty($this->owner->SuccessMessageMail)) { |
147
|
|
|
return $this->owner->dbObject('SuccessMessageMail'); |
148
|
|
|
} else { |
149
|
|
|
return SiteConfig::current_site_config()->dbObject('SuccessMessageMail'); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Check if the current event can have tickets |
155
|
|
|
* |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
public function canCreateTickets() |
159
|
|
|
{ |
160
|
|
|
$currentDate = $this->owner->getController()->CurrentDate(); |
|
|
|
|
161
|
|
|
if ($currentDate->exists()) { |
162
|
|
|
return $currentDate->dbObject('StartDate')->InFuture(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return false; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get the calendar controller |
170
|
|
|
* |
171
|
|
|
* @return CalendarEvent_Controller |
172
|
|
|
*/ |
173
|
|
|
public function getController() |
174
|
|
|
{ |
175
|
|
|
return $this->controller |
176
|
|
|
? $this->controller |
177
|
|
|
: $this->controller = CalendarEvent_Controller::create($this->owner); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.