1 | <?php |
||
57 | class Reservation extends DataObject |
||
58 | { |
||
59 | /** |
||
60 | * Time to wait before deleting the discarded cart |
||
61 | * Give a string that is parsable by strtotime |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | private static $delete_after = '+1 week'; |
||
1 ignored issue
–
show
|
|||
66 | |||
67 | private static $db = array( |
||
2 ignored issues
–
show
|
|||
68 | 'Status' => 'Enum("CART,PENDING,PAID,CANCELED","CART")', |
||
69 | 'Title' => 'Varchar(255)', |
||
70 | 'Total' => 'Currency', |
||
71 | 'Email' => 'Varchar(255)', |
||
72 | 'Gateway' => 'Varchar(255)', |
||
73 | 'Comments' => 'Text', |
||
74 | 'ReservationCode' => 'Varchar(255)' |
||
75 | ); |
||
76 | |||
77 | private static $has_one = array( |
||
78 | 'Event' => 'CalendarEvent', |
||
79 | 'TicketFile' => 'File', |
||
80 | 'MainContact' => 'Broarm\EventTickets\Attendee' |
||
81 | ); |
||
82 | |||
83 | private static $has_many = array( |
||
1 ignored issue
–
show
|
|||
84 | 'Payments' => 'Payment', |
||
85 | 'Attendees' => 'Broarm\EventTickets\Attendee.Reservation' |
||
86 | ); |
||
87 | |||
88 | private static $belongs_many_many = array( |
||
89 | 'PriceModifiers' => 'Broarm\EventTickets\PriceModifier' |
||
90 | ); |
||
91 | |||
92 | private static $indexes = array( |
||
2 ignored issues
–
show
|
|||
93 | 'ReservationCode' => 'unique("ReservationCode")' |
||
94 | ); |
||
95 | |||
96 | private static $summary_fields = array( |
||
1 ignored issue
–
show
|
|||
97 | 'Title' => 'Customer', |
||
98 | 'Total.Nice' => 'Total', |
||
99 | 'State' => 'Status' |
||
100 | ); |
||
101 | |||
102 | public function getCMSFields() |
||
103 | { |
||
104 | $fields = new FieldList(new TabSet('Root', $mainTab = new Tab('Main'))); |
||
105 | $gridFieldConfig = GridFieldConfig_RecordViewer::create(); |
||
106 | $fields->addFieldsToTab('Root.Main', array( |
||
107 | ReadonlyField::create('ReservationCode', _t('Reservation.Code', 'Code')), |
||
108 | ReadonlyField::create('Title', _t('Reservation.MainContact', 'Main contact')), |
||
109 | ReadonlyField::create('Gateway', _t('Reservation.Gateway', 'Gateway')), |
||
110 | ReadonlyField::create('Total', _t('Reservation.Total', 'Total')), |
||
111 | ReadonlyField::create('Comments', _t('Reservation.Comments', 'Comments')), |
||
112 | $reservationFileField = ReadonlyField::create( |
||
113 | 'ReservationFile', |
||
114 | _t('Attendee.Reservation', 'Reservation'), |
||
115 | "<a class='readonly' href='{$this->TicketFile()->Link()}' target='_blank'>Download reservation PDF</a>" |
||
116 | ), |
||
117 | GridField::create('Attendees', 'Attendees', $this->Attendees(), $gridFieldConfig), |
||
118 | GridField::create('Payments', 'Payments', $this->Payments(), $gridFieldConfig) |
||
119 | )); |
||
120 | $reservationFileField->dontEscape = true; |
||
121 | $fields->addFieldsToTab('Root.Main', array()); |
||
122 | $this->extend('updateCMSFields', $fields); |
||
123 | return $fields; |
||
124 | } |
||
125 | |||
126 | public function onBeforeWrite() |
||
127 | { |
||
128 | // Set the title to the name of the reservation holder |
||
129 | $this->Title = $this->getName(); |
||
130 | |||
131 | // Create a validation code to be used for confirmation and in the barcode |
||
132 | if ($this->exists() && empty($this->ReservationCode)) { |
||
133 | $this->ReservationCode = $this->createReservationCode(); |
||
134 | } |
||
135 | |||
136 | parent::onBeforeWrite(); |
||
137 | } |
||
138 | |||
139 | public function onBeforeDelete() |
||
140 | { |
||
141 | // If a reservation is deleted remove the names from the guest list |
||
142 | foreach ($this->Attendees() as $attendee) { |
||
143 | /** @var Attendee $attendee */ |
||
144 | if ($attendee->exists()) { |
||
145 | $attendee->delete(); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | // Make sure the ticket file is not downloadable |
||
150 | if ($this->TicketFile()->exists()) { |
||
151 | $this->TicketFile()->delete(); |
||
152 | } |
||
153 | |||
154 | // Remove the folder |
||
155 | if ($this->fileFolder()->exists()) { |
||
156 | $this->fileFolder()->delete(); |
||
157 | } |
||
158 | |||
159 | parent::onBeforeDelete(); |
||
160 | } |
||
161 | |||
162 | public function singular_name() |
||
163 | { |
||
164 | $name = explode('\\', parent::singular_name()); |
||
165 | return trim(end($name)); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Check if the cart is still in cart state and the delete_after time period has been exceeded |
||
170 | * |
||
171 | * @return bool |
||
172 | */ |
||
173 | public function isDiscarded() |
||
174 | { |
||
175 | $deleteAfter = strtotime(self::config()->get('delete_after'), strtotime($this->Created)); |
||
176 | return ($this->Status === 'CART') && (time() > $deleteAfter); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Get the full name |
||
181 | * |
||
182 | * @return string |
||
183 | */ |
||
184 | public function getName() |
||
185 | { |
||
186 | /** @var Attendee $attendee */ |
||
187 | if ($this->MainContact()->exists()) { |
||
188 | return $this->MainContact()->getName(); |
||
189 | } else { |
||
190 | return 'new reservation'; |
||
191 | } |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Return the translated state |
||
196 | * @return string |
||
197 | */ |
||
198 | public function getState() |
||
199 | { |
||
200 | return _t("Reservation.{$this->Status}", $this->Status); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Get the total by querying the sum of attendee ticket prices |
||
205 | * |
||
206 | * @return float |
||
207 | */ |
||
208 | public function calculateTotal() |
||
209 | { |
||
210 | $total = $this->Attendees()->leftJoin( |
||
211 | 'Broarm\EventTickets\Ticket', |
||
212 | '`Broarm\EventTickets\Attendee`.`TicketID` = `Broarm\EventTickets\Ticket`.`ID`' |
||
213 | )->sum('Price'); |
||
214 | |||
215 | // Calculate any price modifications if added |
||
216 | if ($this->PriceModifiers()->exists()) { |
||
217 | foreach ($this->PriceModifiers() as $priceModifier) { |
||
218 | $priceModifier->updateTotal($total); |
||
219 | } |
||
220 | } |
||
221 | |||
222 | return $this->Total = $total; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Safely change to a state |
||
227 | * |
||
228 | * @param $state |
||
229 | */ |
||
230 | public function changeState($state) |
||
231 | { |
||
232 | $availableStates = $this->dbObject('Status')->enumValues(); |
||
233 | if (in_array($state, $availableStates)) { |
||
234 | $this->Status = $state; |
||
235 | } else { |
||
236 | user_error(_t('Reservation.STATE_CHANGE_ERROR', 'Selected state is not available')); |
||
237 | } |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Set the main contact id |
||
242 | * |
||
243 | * @param $id |
||
244 | */ |
||
245 | public function setMainContact($id) { |
||
246 | $this->MainContactID = $id; |
||
247 | $this->write(); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Create a reservation code |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | public function createReservationCode() |
||
256 | { |
||
257 | return uniqid($this->ID); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Create the folder for the qr code and ticket file |
||
262 | * |
||
263 | * @return Folder|DataObject|null |
||
264 | */ |
||
265 | public function fileFolder() |
||
266 | { |
||
267 | return Folder::find_or_make("/event-tickets/{$this->ReservationCode}/"); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Generate the qr codes and downloadable pdf |
||
272 | */ |
||
273 | public function createFiles() |
||
274 | { |
||
275 | $folder = $folder = $this->fileFolder(); |
||
276 | /** @var Attendee $attendee */ |
||
277 | foreach ($this->Attendees() as $attendee) { |
||
278 | $attendee->createQRCode($folder); |
||
279 | } |
||
280 | |||
281 | // Create a pdf with the newly create QR codes |
||
282 | $this->createTicketFile($folder); |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Create a Printable ticket file |
||
287 | * |
||
288 | * @param Folder $folder |
||
289 | * |
||
290 | * @return File |
||
291 | */ |
||
292 | private function createTicketFile(Folder $folder) |
||
293 | { |
||
294 | // Find or make a folder |
||
295 | $relativeFilePath = "/{$folder->Filename}{$this->ReservationCode}.pdf"; |
||
296 | $absoluteFilePath = Director::baseFolder() . $relativeFilePath; |
||
297 | |||
298 | if (!$file = File::get()->find('Filename', $relativeFilePath)) { |
||
299 | $file = File::create(); |
||
300 | $file->ParentID = $folder->ID; |
||
301 | $file->OwnerID = (Member::currentUser()) ? Member::currentUser()->ID : 0; |
||
302 | $file->Title = $this->ReservationCode; |
||
303 | $file->setFilename($relativeFilePath); |
||
304 | $file->write(); |
||
305 | } |
||
306 | |||
307 | // Set the template and parse the data |
||
308 | $template = new SSViewer('PrintableTicket'); |
||
309 | $html = $template->process($this->getViewableData()); |
||
310 | |||
311 | // Create a DomPDF instance |
||
312 | $domPDF = new Dompdf(); |
||
313 | $domPDF->loadHtml($html); |
||
314 | $domPDF->setPaper('A4'); |
||
315 | $domPDF->getOptions()->setDpi(250); |
||
316 | $domPDF->render(); |
||
317 | |||
318 | // Save the pdf stream as a file |
||
319 | file_put_contents($absoluteFilePath, $domPDF->output()); |
||
320 | |||
321 | // Attach the ticket file to the Attendee |
||
322 | $this->TicketFileID = $file->ID; |
||
323 | $this->write(); |
||
324 | |||
325 | return $file; |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Get a viewable data object for this reservation |
||
330 | * For use in the Email and print template |
||
331 | * |
||
332 | * @return ViewableData |
||
333 | */ |
||
334 | public function getViewableData() |
||
335 | { |
||
336 | $config = SiteConfig::current_site_config(); |
||
337 | $data = $this->Me(); |
||
338 | $data->CurrentDate = $this->Event()->getController()->CurrentDate(); |
||
339 | //$calendarController = new CalendarEvent_Controller($this->Event()); |
||
340 | //$data->CurrentDate = $calendarController->CurrentDate(); |
||
341 | $data->Logo = $config->TicketLogo(); |
||
342 | $this->extend('updateViewableData', $data); |
||
343 | return $data; |
||
344 | } |
||
345 | |||
346 | public function canView($member = null) |
||
350 | |||
351 | public function canEdit($member = null) |
||
355 | |||
356 | public function canDelete($member = null) |
||
360 | |||
361 | public function canCreate($member = null) |
||
365 | } |
||
366 |
This check marks private properties in classes that are never used. Those properties can be removed.