1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Obblm\Core\Form\Team; |
4
|
|
|
|
5
|
|
|
use Obblm\Core\Contracts\InducementInterface; |
6
|
|
|
use Obblm\Core\Contracts\RuleHelperInterface; |
7
|
|
|
use Obblm\Core\Entity\Team; |
8
|
|
|
use Obblm\Core\Helper\TeamHelper; |
9
|
|
|
use Obblm\Core\Validator\Constraints\Team\InducementsAmount; |
10
|
|
|
use Obblm\Core\Validator\Constraints\Team\InducementsQuantity; |
11
|
|
|
use Symfony\Component\Form\AbstractType; |
12
|
|
|
use Symfony\Component\Form\DataMapperInterface; |
13
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
14
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
15
|
|
|
use Symfony\Component\Form\FormInterface; |
16
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
17
|
|
|
|
18
|
|
|
class CompositionInducementsForm extends AbstractType implements DataMapperInterface |
19
|
|
|
{ |
20
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
21
|
|
|
{ |
22
|
|
|
/** @var RuleHelperInterface $helper */ |
23
|
|
|
$helper = $options['helper']; |
24
|
|
|
|
25
|
|
|
if ($builder->getData()) { |
26
|
|
|
/** @var Team $team */ |
27
|
|
|
$team = $builder->getData(); |
28
|
|
|
$budget = TeamHelper::getLastVersion($team)->getTreasure(); |
29
|
|
|
if ($team->getCreationOption('inducement_allowed')) { |
30
|
|
|
if ($team->getCreationOption('inducements')) { |
31
|
|
|
foreach ($team->getCreationOption('inducements') as $key) { |
32
|
|
|
$inducement = $helper->getInducement($key); |
33
|
|
|
$budget += $inducement->getValue(); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
$builder->add('inducements', InducementCollection::class, [ |
37
|
|
|
'allow_add' => true, |
38
|
|
|
'allow_delete' => true, |
39
|
|
|
'prototype' => true, |
40
|
|
|
'error_bubbling' => false, |
41
|
|
|
'entry_type' => InducementType::class, |
42
|
|
|
'help' => 'obblm.forms.team.fields.inducement_help', |
43
|
|
|
'help_translation_parameters' => ['%limit%' => $budget], |
44
|
|
|
'constraints' => [ |
45
|
|
|
new InducementsAmount($budget, $budget), |
46
|
|
|
new InducementsQuantity($helper) |
47
|
|
|
], |
48
|
|
|
'entry_options' => [ |
49
|
|
|
'label' => 'inducement', |
50
|
|
|
'choice_value' => 'key', |
51
|
|
|
'group_by' => 'type', |
52
|
|
|
'choices' => $helper->getInducementsFor($team, $budget, ['inducements']), |
53
|
|
|
'choice_translation_domain' => $helper->getAttachedRule()->getRuleKey() |
54
|
|
|
] |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$builder->setDataMapper($this); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function mapDataToForms($viewData, iterable $forms) |
63
|
|
|
{ |
64
|
|
|
if ($viewData === null) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
if (!$viewData instanceof Team) { |
68
|
|
|
throw new UnexpectedTypeException($viewData, Team::class); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @var FormInterface[] $forms */ |
72
|
|
|
$forms = iterator_to_array($forms); |
|
|
|
|
73
|
|
|
if (isset($forms['inducements']) && $viewData->getCreationOption('inducements')) { |
74
|
|
|
$forms['inducements']->setData($viewData->getCreationOption('inducements')); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function mapFormsToData(iterable $forms, &$viewData) |
79
|
|
|
{ |
80
|
|
|
if ($viewData === null) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
if (!$viewData instanceof Team) { |
84
|
|
|
throw new UnexpectedTypeException($viewData, Team::class); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** @var FormInterface[] $forms */ |
88
|
|
|
$forms = iterator_to_array($forms); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$creationOptions = $viewData->getCreationOptions(); |
91
|
|
|
if (isset($forms['inducements'])) { |
92
|
|
|
$inducements = array_map(function (InducementInterface $inducement) { |
93
|
|
|
return $inducement->getKey(); |
94
|
|
|
}, $forms['inducements']->getData()); |
95
|
|
|
$creationOptions['inducements'] = $inducements; |
96
|
|
|
} |
97
|
|
|
$viewData->setCreationOptions($creationOptions); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function configureOptions(OptionsResolver $resolver) |
101
|
|
|
{ |
102
|
|
|
$resolver->setDefaults([ |
103
|
|
|
'translation_domain' => 'obblm', |
104
|
|
|
'data_class' => Team::class, |
105
|
|
|
'helper' => null, |
106
|
|
|
'constraints' => [ |
107
|
|
|
] |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
$resolver->setAllowedTypes('helper', [RuleHelperInterface::class]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|