1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AttendeesField.php |
4
|
|
|
* |
5
|
|
|
* @author Bram de Leeuw |
6
|
|
|
* Date: 10/03/17 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Broarm\EventTickets; |
10
|
|
|
|
11
|
|
|
use ArrayList; |
12
|
|
|
use CompositeField; |
13
|
|
|
use DBField; |
14
|
|
|
use FieldGroup; |
15
|
|
|
use FieldList; |
16
|
|
|
use FormField; |
17
|
|
|
use HiddenField; |
18
|
|
|
use LiteralField; |
19
|
|
|
use Member; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class AttendeeField |
23
|
|
|
* |
24
|
|
|
* @package Broarm\EventTickets |
25
|
|
|
*/ |
26
|
|
|
class AttendeeField extends CompositeField |
27
|
|
|
{ |
28
|
|
|
protected $name = 'Attendee'; |
29
|
|
|
|
30
|
|
|
protected $requiredFields = array(); |
31
|
|
|
|
32
|
|
|
public function __construct(Attendee $attendee, $main = false, $required = true) |
33
|
|
|
{ |
34
|
|
|
parent::__construct(); |
35
|
|
|
$this->setTag('fieldset'); |
36
|
|
|
$this->setLegend(_t('AttendeeField.VALUED', '{title} valued {price}', null, array( |
|
|
|
|
37
|
|
|
'title' => $attendee->Ticket()->Title, |
38
|
|
|
'price' => $attendee->Ticket()->dbObject('Price')->NiceDecimalPoint()) |
39
|
|
|
)); |
40
|
|
|
|
41
|
|
|
$children = FieldList::create(); |
42
|
|
|
$savableFields = $attendee->Event()->Fields(); |
43
|
|
|
|
44
|
|
|
/** @var UserField $field */ |
45
|
|
|
foreach ($savableFields as $field) { |
46
|
|
|
// Generate a unique field name |
47
|
|
|
$fieldName = "{$this->name}[{$attendee->ID}][$field->ID]"; |
48
|
|
|
|
49
|
|
|
// Create the field an fill with attendee data |
50
|
|
|
$hasField = $attendee->Fields()->find('ID', $field->ID); |
51
|
|
|
$value = $hasField ? $hasField->getField('Value') : null; |
52
|
|
|
$formField = $field->createField($fieldName, $value, $main); |
53
|
|
|
|
54
|
|
|
// Check if the field is required |
55
|
|
|
if ($field->Required && $required) { |
56
|
|
|
if ($formField instanceof CompositeField) { |
57
|
|
|
foreach ($formField->getChildren()->column('Name') as $requiredField) { |
58
|
|
|
$this->addRequiredField($requiredField); |
59
|
|
|
} |
60
|
|
|
} else { |
61
|
|
|
$this->addRequiredField($fieldName); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Pre fill the field if a member is logged in |
66
|
|
|
if ($main && empty($formField->value) && $member = Member::currentUser()) { |
67
|
|
|
$formField->setValue($member->getField($field->Name)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Add the form |
71
|
|
|
$children->add($formField); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Set the main field |
75
|
|
|
$children->add(HiddenField::create("{$this->name}[{$attendee->ID}][Main]", 'Main', (int)$main)); |
76
|
|
|
|
77
|
|
|
// set the children |
78
|
|
|
$this->setChildren($children); |
79
|
|
|
|
80
|
|
|
// Add a hook to modify the added fields if needed |
81
|
|
|
$this->extend('updateAttendeeField', $attendee, $main, $required); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Update the required fields array |
86
|
|
|
* |
87
|
|
|
* @param $fieldName |
88
|
|
|
*/ |
89
|
|
|
private function addRequiredField($fieldName) { |
90
|
|
|
array_push($this->requiredFields, $fieldName); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the required fields |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public function getRequiredFields() |
99
|
|
|
{ |
100
|
|
|
return $this->requiredFields; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: