fix: cleanup stale teamer pickups
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\BusProNet\DataProvider\PickupDataProvider;
|
||||
use App\Entity\Teamer;
|
||||
use App\Enum\MealPreference;
|
||||
use App\Model\UploadSessionDto;
|
||||
@@ -13,12 +14,18 @@ use Symfony\Component\Form\Extension\Core\Type\CollectionType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\EnumType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class TeamerProfileType extends AbstractType
|
||||
{
|
||||
public function __construct(private readonly PickupDataProvider $pickups)
|
||||
{
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
@@ -101,6 +108,36 @@ class TeamerProfileType extends AbstractType
|
||||
'choice_label' => 'label',
|
||||
])
|
||||
;
|
||||
|
||||
// The stored pickup ids are free-form and never reconciled with the
|
||||
// BusProNet pickup base data, so drop ids that no longer resolve (and
|
||||
// duplicates) before the collection builds its rows. Skipped while the
|
||||
// base data is unavailable so a failed fetch cannot wipe real data.
|
||||
// Priority 1 so this runs before the CollectionType's ResizeFormListener
|
||||
// turns the array into child rows.
|
||||
$builder->get('pickups')->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
|
||||
$ids = $event->getData();
|
||||
|
||||
if (!is_array($ids)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$known = $this->pickups->getAll();
|
||||
|
||||
if ([] === $known) {
|
||||
return;
|
||||
}
|
||||
|
||||
$normalized = [];
|
||||
foreach ($ids as $id) {
|
||||
$id = (int) $id;
|
||||
if (isset($known[$id]) && !in_array($id, $normalized, true)) {
|
||||
$normalized[] = $id;
|
||||
}
|
||||
}
|
||||
|
||||
$event->setData($normalized);
|
||||
}, 1);
|
||||
}
|
||||
|
||||
public function buildView(FormView $view, FormInterface $form, array $options): void
|
||||
|
||||
@@ -29,9 +29,16 @@
|
||||
Zustiege
|
||||
</h2>
|
||||
<ul class="pb-4 divide-y divide-gray-200">
|
||||
{% set pickupLabels = [] %}
|
||||
{% for pickup in teamer.pickups %}
|
||||
{% set label = pickup|bpn_pickup_label %}
|
||||
{% if label is not empty %}
|
||||
{% set pickupLabels = pickupLabels|merge([label]) %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% for label in pickupLabels %}
|
||||
<li class="py-2">
|
||||
{{ pickup|bpn_pickup_label }}
|
||||
{{ label }}
|
||||
</li>
|
||||
{% else %}
|
||||
<li class="py-2">
|
||||
|
||||
@@ -141,9 +141,16 @@
|
||||
Buszustiege
|
||||
</h2>
|
||||
<ul class="list-disc pl-4">
|
||||
{% set pickupLabels = [] %}
|
||||
{% for pickup in teamer.pickups %}
|
||||
{% set label = pickup|bpn_pickup_label %}
|
||||
{% if label is not empty %}
|
||||
{% set pickupLabels = pickupLabels|merge([label]) %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% for label in pickupLabels %}
|
||||
<li>
|
||||
{{ pickup|bpn_pickup_label }}
|
||||
{{ label }}
|
||||
</li>
|
||||
{% else %}
|
||||
<li>
|
||||
|
||||
Reference in New Issue
Block a user