1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class TrainingSignupForm extends Form |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
public function __construct($controller, $name, $title = "Training") |
6
|
|
|
{ |
7
|
|
|
if ($member = Member::currentUser()) { |
|
|
|
|
8
|
|
|
} else { |
9
|
|
|
$member = new Member(); |
10
|
|
|
} |
11
|
|
|
$fields = new FieldList( |
12
|
|
|
new HeaderField($title) |
13
|
|
|
); |
14
|
|
|
$extraFields = $member->getTrainingFields(); |
15
|
|
|
foreach ($extraFields as $field) { |
16
|
|
|
if ("Password" == $field->title() && $member->ID) { |
|
|
|
|
17
|
|
|
} elseif ("Password" == $field->title()) { |
18
|
|
|
$fields->push(new ConfirmedPasswordField("Password")); |
19
|
|
|
} else { |
20
|
|
|
$fields->push($field); |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
$actions = new FieldList( |
24
|
|
|
new FormAction("doSave", "Sign Up Now") |
25
|
|
|
); |
26
|
|
|
$requiredFields = new RequiredFields( |
27
|
|
|
"FirstName", |
28
|
|
|
"Surname", |
29
|
|
|
"Email", |
30
|
|
|
"Password" |
31
|
|
|
); |
32
|
|
|
if ($controller->Options) { |
33
|
|
|
$array = array(); |
34
|
|
|
$explodedOptions = explode(",", $controller->Options); |
35
|
|
|
foreach ($explodedOptions as $option) { |
36
|
|
|
$option = trim(Convert::raw2att($option)); |
37
|
|
|
$array[$option] = $option; |
38
|
|
|
} |
39
|
|
|
if (count($array)) { |
40
|
|
|
$fields->push(new DropdownField("SelectedOption", "Select Option", $array)); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
$fields->push(new TextField("BookingCode", "Booking Code (if any)")); |
44
|
|
|
parent::__construct($controller, $name, $fields, $actions, $requiredFields); |
45
|
|
|
$this->loadNonBlankDataFrom($member); |
|
|
|
|
46
|
|
|
return $this; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function doSave($data, $form) |
|
|
|
|
50
|
|
|
{ |
51
|
|
View Code Duplication |
if (isset($data['Password']) && is_array($data['Password'])) { |
|
|
|
|
52
|
|
|
$data['Password'] = $data['Password']['_Password']; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// We need to ensure that the unique field is never overwritten |
56
|
|
|
$uniqueField = Member::get_unique_identifier_field(); |
|
|
|
|
57
|
|
View Code Duplication |
if (isset($data[$uniqueField])) { |
|
|
|
|
58
|
|
|
$SQL_unique = Convert::raw2sql($data[$uniqueField]); |
59
|
|
|
$existingUniqueMember = Member::get()->filter(array($uniqueField => $SQL_unique))->first(); |
60
|
|
|
if ($existingUniqueMember && $existingUniqueMember->exists()) { |
61
|
|
|
if (Member::currentUserID() != $existingUniqueMember->ID) { |
62
|
|
|
die("current member does not match enrolled member."); |
|
|
|
|
63
|
|
|
return false; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
$member = Member::currentUser(); |
68
|
|
|
if (!$member) { |
69
|
|
|
$member = new Member(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$member->update($data); |
73
|
|
|
$member->write(); |
74
|
|
|
$arrayExtraFields = array(); |
75
|
|
|
if (isset($data["SelectedOption"])) { |
76
|
|
|
$arrayExtraFields["SelectedOption"] = $data["SelectedOption"]; |
77
|
|
|
} |
78
|
|
|
if (isset($data["BookingCode"])) { |
79
|
|
|
$arrayExtraFields["BookingCode"] = $data["BookingCode"]; |
80
|
|
|
} |
81
|
|
|
$this->controller->addAttendee($member, $arrayExtraFields); |
82
|
|
|
$this->redirect($this->getController()->Link("thankyou")); |
|
|
|
|
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/* |
87
|
|
|
|
88
|
|
|
I don't think saveInto can deal with saving many_many relations |
89
|
|
|
currently, unless your Post class has a customised method called |
90
|
|
|
saveCategories($data) in which you simply add the code like this: |
91
|
|
|
|
92
|
|
|
$this->Categories->add($item, $extraFields), |
93
|
|
|
where $item should be the category's ID (you can get it from $data), |
94
|
|
|
depending on how your Form is stuctured, your $extraFields should be |
95
|
|
|
either from $data, or assigned from backend by you before you call |
96
|
|
|
saveInto in doPostForm or before you call $this->Categories->add |
97
|
|
|
saveCetegories. |
98
|
|
|
|
99
|
|
|
The name of saving many_many relation (here refer to saveCategories) |
100
|
|
|
must bond with your field name, ie, if your field is called: |
101
|
|
|
new DropdownField('GoneWithWind',"Category", $catOptions), the save |
102
|
|
|
method must name as saveGoneWithWind(). |
103
|
|
|
|
104
|
|
|
if PrimeCategory need to be also submitted from from, your your can |
105
|
|
|
get CategoryID and PrimeCategory in an array from $data, try this: |
106
|
|
|
new DropdownField('Categories[CetegoryID]',"Category", $catOptions), |
107
|
|
|
new CheckboxField('Categories[PrimeCategory]', "Prime Category?"), |
108
|
|
|
The $data[Categories] submitted from from is a array. |
109
|
|
|
|
110
|
|
|
In addition, dropdown field is not supposed to simulate many_many |
111
|
|
|
relation, CheckboxSetField is aim to submit multiple IDs from a form |
112
|
|
|
(maybe you have you special consideration here?). If this is the case, |
113
|
|
|
$this->Categories->add need to change to $this->Categories- |
114
|
|
|
|
115
|
|
|
*/ |
116
|
|
|
} |
117
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.