1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ReservationForm.php |
4
|
|
|
* |
5
|
|
|
* @author Bram de Leeuw |
6
|
|
|
* Date: 13/03/17 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Broarm\EventTickets; |
10
|
|
|
|
11
|
|
|
use FieldList; |
12
|
|
|
use FormAction; |
13
|
|
|
use RequiredFields; |
14
|
|
|
use SS_HTTPResponse; |
15
|
|
|
use ValidationException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ReservationForm |
19
|
|
|
* |
20
|
|
|
* @package Broarm\EventTickets |
21
|
|
|
*/ |
22
|
|
|
class ReservationForm extends FormStep |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Set this to true if you want to require credentials for all attendees |
26
|
|
|
* |
27
|
|
|
* @config |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
private static $require_all_attendees = false; |
31
|
|
|
|
32
|
|
|
public function __construct($controller, $name, Reservation $reservation = null) |
33
|
|
|
{ |
34
|
|
|
$requiredFields = array(); |
35
|
|
|
$fields = FieldList::create(); |
36
|
|
|
if ($this->reservation = $reservation) { |
37
|
|
|
// Ask details about created attendees |
38
|
|
|
foreach ($reservation->Attendees() as $index => $attendee) { |
39
|
|
|
// The main reservation information is the first field |
40
|
|
|
$main = $index === 0; |
41
|
|
|
// Set required to true for all attendees or for the first only |
42
|
|
|
$required = self::config()->get('require_all_attendees') |
43
|
|
|
? self::config()->get('require_all_attendees') |
44
|
|
|
: $main; |
45
|
|
|
|
46
|
|
|
$fields->add($field = AttendeeField::create($attendee, $main, $required)); |
47
|
|
|
$requiredFields = array_merge($requiredFields, $field->getRequiredFields()); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$actions = FieldList::create( |
52
|
|
|
FormAction::create('goToNextStep', _t('ReservationForm.PAYMENT', 'Continue to payment')) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$required = new RequiredFields($requiredFields); |
56
|
|
|
|
57
|
|
|
parent::__construct($controller, $name, $fields, $actions, $required); |
58
|
|
|
$this->extend('updateForm'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Finish the registration and continue to checkout |
63
|
|
|
* |
64
|
|
|
* @param array $data |
65
|
|
|
* @param ReservationForm $form |
66
|
|
|
* @return bool|SS_HTTPResponse |
67
|
|
|
* @throws ValidationException |
68
|
|
|
*/ |
69
|
|
|
public function goToNextStep(array $data, ReservationForm $form) |
70
|
|
|
{ |
71
|
|
|
$reservation = $form->getReservation(); |
72
|
|
|
foreach ($data['Attendee'] as $attendeeID => $attendeeData) { |
73
|
|
|
/** @var Attendee $attendee */ |
74
|
|
|
$attendee = Attendee::get()->byID($attendeeID); |
75
|
|
|
|
76
|
|
|
// populate the attendees |
77
|
|
|
foreach ($attendeeData as $field => $value) { |
78
|
|
|
// Array value means we have a composite field. |
79
|
|
|
// This is used by the email field, main attendee to check if we have a correct mail address set. |
80
|
|
|
// todo, should be moved somewhere in the field classes. |
81
|
|
|
if (is_array($value)) { |
82
|
|
|
if (count(array_unique($value)) !== 1) { |
83
|
|
|
$form->addErrorMessage($field, 'Make sure your email address is spelled correctly', 'bad'); |
84
|
|
|
return $form->getController()->redirectBack(); |
85
|
|
|
} else { |
86
|
|
|
$value = array_shift($value); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (is_int($field)) { |
91
|
|
|
$attendee->Fields()->add($field, array('Value' => $value)); |
92
|
|
|
} else { |
93
|
|
|
$attendee->setField($field, $value); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$attendee->write(); |
98
|
|
|
|
99
|
|
|
// Set the main contact |
100
|
|
|
if (isset($attendeeData['Main']) && (bool)$attendeeData['Main']) { |
101
|
|
|
$reservation->setMainContact($attendeeID); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// add the tax modifier |
106
|
|
|
$reservation->PriceModifiers()->add(TaxModifier::findOrMake($reservation)); |
|
|
|
|
107
|
|
|
$reservation->calculateTotal(); |
108
|
|
|
$reservation->write(); |
109
|
|
|
|
110
|
|
|
$this->extend('beforeNextStep', $data, $form); |
111
|
|
|
return $this->nextStep(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: