Completed
Push — master ( c782d0...996639 )
by Bram
02:17
created

AttendeeField::__construct()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 42
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 20
nc 10
nop 3
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
    private static $required_fields = array(
0 ignored issues
show
Unused Code introduced by
The property $required_fields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
29
        'FirstName',
30
        'Surname',
31
        'Email'
32
    );
33
34
    protected $name = 'Attendee';
35
36
    protected $requiredFields = array();
37
38
    public function __construct(Attendee $attendee, $main = false, $required = true)
39
    {
40
        parent::__construct();
41
        $this->setTag('fieldset');
42
        $this->setLegend(_t('AttendeeField.VALUED', '{title} valued {price}', null, array(
0 ignored issues
show
Documentation introduced by
array('title' => $attend...')->NiceDecimalPoint()) is of type array<string,?,{"title":"?","price":"?"}>, but the function expects a string.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
            'title' => $attendee->Ticket()->Title,
0 ignored issues
show
Bug introduced by
The method Ticket() does not exist on Broarm\EventTickets\Attendee. Did you maybe mean generateTicketCode()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
44
            'price' => $attendee->Ticket()->dbObject('Price')->NiceDecimalPoint())
0 ignored issues
show
Bug introduced by
The method Ticket() does not exist on Broarm\EventTickets\Attendee. Did you maybe mean generateTicketCode()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
45
        ));
46
        if ($required) $this->addExtraClass('required');
47
48
        $children = FieldList::create();
49
        $savableFields = Attendee::config()->get('savable_fields');
50
        foreach ($savableFields as $field => $fieldClass) {
51
            // Generate a unique field name
52
            $fieldName = "{$this->name}[{$attendee->ID}][$field]";
53
54
            // Check if the field is required
55
            if (in_array($field, self::config()->get('required_fields')) && $required) {
56
                $this->addRequiredField($fieldName);
57
            }
58
59
            // Create the field
60
            $formField = $fieldClass::create($fieldName, _t("AttendeeField.$field", $field));
61
62
            // Pre fill the form if a member is logged in
63
            if ($main && $member = Member::currentUser()) {
64
                $formField->setValue($member->getField($field));
65
            }
66
67
            // Add the form
68
            $children->add($formField);
69
        }
70
71
        // Set the main field
72
        $children->add(HiddenField::create("{$this->name}[{$attendee->ID}][Main]", 'Main', (int)$main));
73
74
        // set the children
75
        $this->setChildren($children);
76
77
        // Add a hook to modify the added fields if needed
78
        $this->extend('updateAttendeeField');
79
    }
80
81
    /**
82
     * Update the required fields array
83
     *
84
     * @param $fieldName
85
     */
86
    private function addRequiredField($fieldName) {
87
        array_push($this->requiredFields, $fieldName);
88
    }
89
90
    /**
91
     * Get the required fields
92
     *
93
     * @return array
94
     */
95
    public function getRequiredFields()
96
    {
97
        return $this->requiredFields;
98
    }
99
}
100