1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Ticket.php |
4
|
|
|
* |
5
|
|
|
* @author Bram de Leeuw |
6
|
|
|
* Date: 09/03/17 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Broarm\EventTickets; |
10
|
|
|
|
11
|
|
|
use CalendarEvent; |
12
|
|
|
use CalendarEvent_Controller; |
13
|
|
|
use DataObject; |
14
|
|
|
use Date; |
15
|
|
|
use DateField; |
16
|
|
|
use FieldList; |
17
|
|
|
use LiteralField; |
18
|
|
|
use NumericField; |
19
|
|
|
use Tab; |
20
|
|
|
use TabSet; |
21
|
|
|
use TextField; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Ticket |
25
|
|
|
* |
26
|
|
|
* @package Broarm\EventTickets |
27
|
|
|
* |
28
|
|
|
* @property string Title |
29
|
|
|
* @property float Price |
30
|
|
|
* @property string AvailableFromDate |
31
|
|
|
* @property string AvailableTillDate |
32
|
|
|
* @property NumericField AmountField the amount field is set on the TicketForm |
33
|
|
|
* |
34
|
|
|
* @method CalendarEvent|TicketExtension Event |
35
|
|
|
*/ |
36
|
|
|
class Ticket extends DataObject |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* The default sale start date |
40
|
|
|
* This defaults to the event start date '-1 week' |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private static $sale_start_threshold = '-1 week'; |
|
|
|
|
45
|
|
|
|
46
|
|
|
private static $db = array( |
|
|
|
|
47
|
|
|
'Title' => 'Varchar(255)', |
48
|
|
|
'Price' => 'Currency', |
49
|
|
|
'AvailableFromDate' => 'Date', |
50
|
|
|
'AvailableTillDate' => 'Date', |
51
|
|
|
'Sort' => 'Int' |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
private static $default_sort = 'Sort ASC, AvailableFromDate DESC'; |
|
|
|
|
55
|
|
|
|
56
|
|
|
private static $has_one = array( |
|
|
|
|
57
|
|
|
'Event' => 'CalendarEvent' |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
private static $summary_fields = array( |
|
|
|
|
61
|
|
|
'Title' => 'Title', |
62
|
|
|
'Price.NiceDecimalPoint' => 'Price', |
63
|
|
|
'AvailableFrom' => 'Available from', |
64
|
|
|
'AvailableTill' => 'Available till', |
65
|
|
|
'AvailableSummary' => 'Available' |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
private static $translate = array(); |
|
|
|
|
69
|
|
|
|
70
|
|
|
public function getCMSFields() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$fields = new FieldList(new TabSet('Root', $mainTab = new Tab('Main'))); |
73
|
|
|
|
74
|
|
|
$fields->addFieldsToTab('Root.Main', array( |
75
|
|
|
TextField::create('Title', _t('Ticket.TITLE_LABEL', 'Title for the ticket')), |
76
|
|
|
NumericField::create('Price', _t('Ticket.PRICE_LABEL', 'Ticket price')), |
77
|
|
|
$saleStart = DateField::create('AvailableFromDate', |
78
|
|
|
_t('Ticket.SALE_START_LABEL', 'Ticket sale starts from')), |
79
|
|
|
$saleEnd = DateField::create('AvailableTillDate', _t('Ticket.SALE_END_LABEL', 'Ticket sale ends on')) |
80
|
|
|
)); |
81
|
|
|
|
82
|
|
|
$saleStart->setConfig('showcalendar', true); |
83
|
|
|
$saleStart->setDescription(_t( |
84
|
|
|
'Ticket.SALE_START_DESCRIPTION', |
85
|
|
|
'If no date is given the following date will be used: {date}', null, |
86
|
|
|
array('date' => $this->getAvailableFrom()->Nice()) |
|
|
|
|
87
|
|
|
)); |
88
|
|
|
|
89
|
|
|
$saleEnd->setConfig('showcalendar', true); |
90
|
|
|
$saleEnd->setDescription(_t( |
91
|
|
|
'Ticket.SALE_END_DESCRIPTION', |
92
|
|
|
'If no date is given the event start date will be used: {date}', null, |
93
|
|
|
array('date' => $this->getAvailableTill()->Nice()) |
|
|
|
|
94
|
|
|
)); |
95
|
|
|
|
96
|
|
|
$this->extend('updateCMSFields', $fields); |
97
|
|
|
return $fields; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function singular_name() |
101
|
|
|
{ |
102
|
|
|
$name = explode('\\', parent::singular_name()); |
103
|
|
|
return trim(end($name)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the formatted price |
108
|
|
|
* @deprecated Use the "Price.NiceDecimalPoint" method instead |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
* / |
112
|
|
|
public function getPriceNice() |
113
|
|
|
{ |
114
|
|
|
return $this->dbObject('Price')->NiceDecimalPoint(); |
115
|
|
|
}*/ |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the available form date if it is set, |
119
|
|
|
* otherwise get it from the parent |
120
|
|
|
* |
121
|
|
|
* @return \SS_DateTime|Date|\DBField|null |
122
|
|
|
*/ |
123
|
|
|
public function getAvailableFrom() |
124
|
|
|
{ |
125
|
|
|
if ($this->AvailableFromDate) { |
126
|
|
|
return $this->dbObject('AvailableFromDate'); |
127
|
|
|
} elseif ($startDate = $this->getEventStartDate()) { |
128
|
|
|
$lastWeek = new Date(); |
129
|
|
|
$lastWeek->setValue(strtotime(self::config()->get('sale_start_threshold'), strtotime($startDate->value))); |
130
|
|
|
return $lastWeek; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return null; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the available till date if it is set, |
138
|
|
|
* otherwise get it from the parent |
139
|
|
|
* Use the event start date as last sale possibility |
140
|
|
|
* |
141
|
|
|
* @return \SS_DateTime|Date|\DBField|null |
142
|
|
|
*/ |
143
|
|
|
public function getAvailableTill() |
144
|
|
|
{ |
145
|
|
|
if ($this->AvailableTillDate) { |
146
|
|
|
return $this->dbObject('AvailableTillDate'); |
147
|
|
|
} elseif ($startDate = $this->getEventStartDate()) { |
148
|
|
|
return $startDate; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return null; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Validate if the start and end date are in the past and the future |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
private function validateDate() |
160
|
|
|
{ |
161
|
|
|
if ($this->getAvailableFrom() && $this->getAvailableTill()) { |
162
|
|
|
if ($this->getAvailableFrom()->InPast() && $this->getAvailableTill()->InFuture()) { |
163
|
|
|
return true; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Validate the available capacity |
172
|
|
|
* |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
|
|
private function validateAvailability() |
176
|
|
|
{ |
177
|
|
|
return $this->Event()->getAvailability() > 0; |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Return if the ticket is available or not |
182
|
|
|
* |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
|
|
public function getAvailable() |
186
|
|
|
{ |
187
|
|
|
if (!$this->getAvailableFrom() && !$this->getAvailableTill()) { |
188
|
|
|
return false; |
189
|
|
|
} elseif ($this->validateDate() && $this->validateAvailability()) { |
190
|
|
|
return true; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return false; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Return availability for use in grid fields |
198
|
|
|
* |
199
|
|
|
* @return LiteralField |
200
|
|
|
*/ |
201
|
|
|
public function getAvailableSummary() |
202
|
|
|
{ |
203
|
|
|
$available = $this->getAvailable() |
204
|
|
|
? '<span style="color: #3adb76;">' . _t('Ticket.AVAILABLE', 'Tickets available') . '</span>' |
205
|
|
|
: '<span style="color: #cc4b37;">' . _t('Ticket.UNAVAILABLE', 'Not for sale') . '</span>'; |
206
|
|
|
|
207
|
|
|
return new LiteralField('Available', $available); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get the event start date |
212
|
|
|
* |
213
|
|
|
* @return Date |
214
|
|
|
*/ |
215
|
|
|
private function getEventStartDate() |
216
|
|
|
{ |
217
|
|
|
if ($this->Event()->getController()->CurrentDate()->exists()) { |
|
|
|
|
218
|
|
|
return $this->Event()->getController()->CurrentDate()->obj('StartDate'); |
|
|
|
|
219
|
|
|
} else { |
220
|
|
|
return null; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function canView($member = null) |
|
|
|
|
225
|
|
|
{ |
226
|
|
|
return $this->Event()->canView($member); |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function canEdit($member = null) |
|
|
|
|
230
|
|
|
{ |
231
|
|
|
return $this->Event()->canEdit($member); |
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function canDelete($member = null) |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
return $this->Event()->canDelete($member); |
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function canCreate($member = null) |
|
|
|
|
240
|
|
|
{ |
241
|
|
|
return $this->Event()->canCreate($member); |
|
|
|
|
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.