SummaryField::Field()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 27

Duplication

Lines 7
Ratio 25.93 %

Importance

Changes 0
Metric Value
dl 7
loc 27
rs 9.1768
c 0
b 0
f 0
cc 5
nc 12
nop 1
1
<?php
2
/**
3
 * SummaryField.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 10/03/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use ArrayList;
12
use CheckboxField;
13
use DBField;
14
use FormField;
15
16
/**
17
 * Class SummaryField
18
 *
19
 * @package Broarm\EventTickets
20
 */
21
class SummaryField extends FormField
22
{
23
    /**
24
     * @var bool
25
     */
26
    protected $editable = false;
27
28
    /**
29
     * @var Reservation
30
     */
31
    protected $reservation;
32
33
    protected $template = 'SummaryField';
34
35
    public function __construct($name, $title, Reservation $reservation, $editable = false)
36
    {
37
        $this->editable = (boolean)$editable;
38
        $this->reservation = $reservation;
39
        parent::__construct($name, $title);
40
    }
41
42
    public function getReservation()
43
    {
44
        return $this->reservation;
45
    }
46
47
    /**
48
     * Get a list of editable tickets
49
     * These have an numeric input field
50
     *
51
     * @return ArrayList
52
     */
53
    private function getEditableAttendees()
54
    {
55
        $attendees = ArrayList::create();
56
        /** @var Attendee $attendee */
57
        foreach ($this->getReservation()->Attendees() as $key => $attendee) {
58
            $fieldName = $this->name . "[{$attendee->ID}][TicketReceiver]";
59
            $attendee->TicketReceiverField = CheckboxField::create($fieldName)
60
                ->setAttribute('Title', _t('SummaryField.TICKET_RECEIVER_HELP', 'Send tickets to this person'))
61
                ->setDisabled(empty($attendee->Email));
62
            // Select the first by default
63
            if ($this->getReservation()->MainContact()->ID === $attendee->ID) {
64
                $attendee->TicketReceiverField->setValue(1);
65
            }
66
            $attendees->push($attendee);
67
        }
68
        return $attendees;
69
    }
70
71
    /**
72
     * Get the field customized with tickets and reservation
73
     *
74
     * @param array $properties
75
     *
76
     * @return \HTMLText|string
77
     */
78
    public function Field($properties = array())
79
    {
80
        $context = $this;
81
        $properties['Editable'] = $this->editable;
82
        $properties['Reservation'] = $this->getReservation();
83
        $properties['Attendees'] = $this->editable
84
            ? $this->getEditableAttendees()
85
            : $this->getReservation()->Attendees();
86
87
        if (count($properties)) {
88
            $context = $context->customise($properties);
89
        }
90
91
        $this->extend('onBeforeRender', $context);
92
93
        $result = $context->renderWith($this->getTemplates());
94
95 View Code Duplication
        if (is_string($result)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
            $result = trim($result);
97
        } else {
98
            if ($result instanceof DBField) {
99
                $result->setValue(trim($result->getValue()));
100
            }
101
        }
102
103
        return $result;
104
    }
105
}