Completed
Push — master ( 3c5c0f...ec5ff6 )
by Laurent
01:40
created

FlightForm::buildOptionsfromConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 */
5
6
namespace flightlog\form;
7
8
/**
9
 * FlightForm class
10
 *
11
 * @author Laurent De Coninck <[email protected]>
12
 */
13
class FlightForm extends Form
14
{
15
    /**
16
     * @param \ValidatorInterface $validator
17
     * @param \Bbcvols            $baseObject
18
     * @param \DoliDB             $db
19
     */
20
    public function __construct(\ValidatorInterface $validator, $baseObject, \DoliDB $db, $options)
21
    {
22
23
        parent::__construct('flight_form', FormInterface::METHOD_POST, $this->buildOptionsfromConfiguration($options));
24
25
        $this->setValidator($validator)
26
            ->bind($baseObject);
27
28
        $this
29
            ->add(new Hidden('idBBC_vols'))
30
            ->add((new Input('lieuD'))->required())
31
            ->add((new Input('lieuA'))->required())
32
            ->add((new BalloonSelect('BBC_ballons_idBBC_ballons', $db))->required())
33
            ->add(new Number('nbrPax'))
34
            ->add(new InputTextarea('remarque'))
35
            ->add(new InputTextarea('incidents'))
36
            ->add(new InputTextarea('passengerNames'))
37
            ->add((new FlightTypeSelect('fk_type', [], $db)))
38
            ->add(new UserSelect('fk_pilot', $this->getOptions(), $db))
39
            ->add(new UserSelect('fk_organisateur', $this->getOptions(), $db))
40
            ->add(new UserSelect('fk_receiver', $this->getOptions(), $db))
41
            ->add(new Number('kilometers'))
42
            ->add(new Number('cost'))
43
            ->add(new InputTextarea('justif_kilometers'));
44
    }
45
46
    /**
47
     * @param \stdClass $options
48
     *
49
     * @return array
50
     */
51
    private function buildOptionsfromConfiguration($options)
52
    {
53
        $values = [];
54
55
        foreach ($options as $value) {
0 ignored issues
show
Bug introduced by
The expression $options of type object<stdClass> is not traversable.
Loading history...
56
            $values[] = $value;
57
        }
58
59
        return $values;
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function bind($object)
66
    {
67
        /** @var \Bbcvols $flight */
68
        $flight = $object;
69
70
        if ($flight->isBilled()) {
71
            $this
72
                ->remove('fk_receiver')
73
                ->remove('cost')
74
                ->remove('nbrPax')
75
                ->remove('passengerNames');
76
        }
77
78
        $endOfYearDate = $flight->getDate()->setDate($flight->getDate()->format('Y'), 12, 31);
79
        if (new \DateTime() >= $endOfYearDate) {
80
            $this
81
                ->remove('fk_receiver')
82
                ->remove('cost')
83
                ->remove('BBC_ballons_idBBC_ballons')
84
                ->remove('nbrPax')
85
                ->remove('passengerNames')
86
                ->remove('fk_type')
87
                ->remove('fk_pilot')
88
                ->remove('fk_organisateur')
89
                ->remove('kilometers')
90
                ->remove('justif_kilometers');
91
        }
92
93
        return parent::bind($object);
94
    }
95
96
97
}