Saisie.php 3.91 KB
Newer Older
1
2
3
4
<?php

namespace Application\Form\Service;

5
6
use Application\Entity\Db\Periode;
use Application\Entity\Db\Service;
7
use Application\Entity\Db\Traits\TypeVolumeHoraireAwareTrait;
8
use Application\Form\AbstractForm;
9
10
use Application\Form\Service\Traits\SaisieFieldsetAwareTrait;
use Application\Form\VolumeHoraire\Traits\SaisieMultipleFieldsetAwareTrait;
11
use Application\Service\Traits\PeriodeServiceAwareTrait;
12
use Application\Entity\Db\Etablissement;
lecluse's avatar
#994    
lecluse committed
13
use Zend\Form\Element\Hidden;
14
use Zend\Stdlib\Hydrator\HydratorInterface;
15

16

17
18
19
20
21
/**
 * Description of Saisie
 *
 * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr>
 */
22
class Saisie extends AbstractForm
23
{
24
    use TypeVolumeHoraireAwareTrait;
25
    use PeriodeServiceAwareTrait;
26
27
    use SaisieFieldsetAwareTrait;
    use SaisieMultipleFieldsetAwareTrait;
28

29
30
31
32
33
34
35
36
    /**
     * etablissement par défaut
     *
     * @var Etablissement
     */
    protected $etablissement;


37

38
39
40
    /**
     * Retourne la liste des périodes d'enseignement
     *
41
     * @return Periode[]
42
43
44
     */
    public function getPeriodes()
    {
45
        return $this->getServicePeriode()->getEnseignement();
46
47
    }

48

49

50
51
52
53
54
55
    /**
     * Bind an object to the form
     *
     * Ensures the object is populated with validated values.
     *
     * @param  object $object
56
57
     * @param  int    $flags
     *
58
     * @return mixed|void
lecluse's avatar
#994    
lecluse committed
59
     */
60
61
    public function bind($object, $flags = \Zend\Form\FormInterface::VALUES_NORMALIZED)
    {
62
63
        if ($object instanceof Service && $object->getTypeVolumeHoraire()) {
            $this->get('type-volume-horaire')->setValue($object->getTypeVolumeHoraire()->getId());
64
        }
65

66
        return parent::bind($object, $flags);
lecluse's avatar
#994    
lecluse committed
67
    }
68
69


70

71
72
    public function init()
    {
73
74
        $this->setName('service')
            ->setAttribute('class', 'service-form');
75

76
77
78
        $hydrator = new SaisieHydrator();
        $hydrator->setServicePeriode($this->getServicePeriode());
        $this->setHydrator($hydrator);
lecluse's avatar
#3577    
lecluse committed
79

80
        // Product Fieldset
81
82
83
84
        $this->add($this->getFieldsetServiceSaisie());

        foreach ($this->getPeriodes() as $periode) {
            $pf = $this->getFieldsetVolumeHoraireSaisieMultiple();
85
86
87
88
            $pf->setName($periode->getCode());
            $this->add($pf);
        }

89
        $this->add(new Hidden('type-volume-horaire'));
lecluse's avatar
#994    
lecluse committed
90

91
        $this->add([
92
93
            'name'       => 'submit',
            'type'       => 'Submit',
94
            'attributes' => [
95
96
                'value' => 'Enregistrer',
                'class' => 'btn btn-primary',
97
98
            ],
        ]);
99

100
        $this->setAttribute('action', $this->getCurrentUrl());
101
102
    }

103
104


105
106
107
108
109
    public function initFromContext()
    {
        $this->get('service')->initFromContext();
    }

110
111


112
113
114
115
116
    public function saveToContext()
    {
        $this->get('service')->saveToContext();
    }

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135


    public function getInputFilterSpecification()
    {
        return [];
    }
}





/**
 *
 *
 * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr>
 */
class SaisieHydrator implements HydratorInterface
{
136
    use PeriodeServiceAwareTrait;
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170



    /**
     * Retourne la liste des périodes d'enseignement
     *
     * @return Periode[]
     */
    public function getPeriodes()
    {
        $periodes = $this->getServicePeriode()->getEnseignement();

        return $periodes;
    }



    /**
     * Hydrate $object with the provided $data.
     *
     * @param  array   $data
     * @param  Service $object
     *
     * @return object
     */
    public function hydrate(array $data, $object)
    {
        $object = $data['service'];

        return $object;
    }



171
    /**
172
173
174
     * Extract values from an object
     *
     * @param  Service $object
175
176
177
     *
     * @return array
     */
178
179
180
181
182
183
184
185
186
    public function extract($object)
    {
        $data            = [];
        $data['service'] = $object;
        foreach ($this->getPeriodes() as $periode) {
            $data[$periode->getCode()] = $object->getVolumeHoraireListe($periode);
        }

        return $data;
187
    }
188
}