mirror of
https://github.com/ToxicCrack/PrintABrick.git
synced 2025-05-28 01:30:11 -07:00
Add set filter form
This commit is contained in:
parent
f1fc90e80f
commit
cdc7150e36
@ -5,6 +5,11 @@ services:
|
||||
client.rebrickable:
|
||||
class: AppBundle\Client\Rebrickable\Rebrickable
|
||||
arguments: ['%rebrickable_apikey%']
|
||||
service.collection:
|
||||
app.collection_service:
|
||||
class: AppBundle\Service\CollectionService
|
||||
arguments: ['@client.brickset','@client.rebrickable']
|
||||
arguments: ['@client.brickset','@client.rebrickable']
|
||||
app.form.filter_set:
|
||||
class: AppBundle\Form\FilterSetType
|
||||
arguments: ['@app.collection_service']
|
||||
tags:
|
||||
- { name: form.type }
|
81
src/AppBundle/Form/FilterSetType.php
Normal file
81
src/AppBundle/Form/FilterSetType.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace AppBundle\Form;
|
||||
|
||||
use AppBundle\Client\Brickset\Entity\Subtheme;
|
||||
use AppBundle\Client\Brickset\Entity\Theme;
|
||||
use AppBundle\Client\Brickset\Entity\Year;
|
||||
use AppBundle\Service\CollectionService;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\Form;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
|
||||
class FilterSetType extends AbstractType
|
||||
{
|
||||
private $collectionService;
|
||||
|
||||
public function __construct(CollectionService $collectionService)
|
||||
{
|
||||
$this->collectionService = $collectionService;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->add("theme", ChoiceType::class, [
|
||||
'choices' => $this->collectionService->getThemes(),
|
||||
'choice_label' => function(Theme $theme = null) {
|
||||
return $theme ? $theme->getTheme().' ('.$theme->getSetCount().')' : '';
|
||||
},
|
||||
'placeholder' => '',
|
||||
'required' => false
|
||||
|
||||
]);
|
||||
|
||||
$formModifier = function (Form $form, Theme $theme = null) {
|
||||
$subthemes = null === $theme ? [] : $this->collectionService->getSubthemesByTheme($theme);
|
||||
$years = null === $theme ? [] : $this->collectionService->getYearsByTheme($theme);
|
||||
|
||||
$form->add("subtheme", ChoiceType::class, [
|
||||
'choices' => $subthemes,
|
||||
'choice_label' => function(Subtheme $subtheme = null) {
|
||||
return $subtheme ? $subtheme->getSubtheme() : '';
|
||||
},
|
||||
'placeholder' => '',
|
||||
'required' => false
|
||||
]);
|
||||
|
||||
$form->add("years", ChoiceType::class, [
|
||||
'choices' => $years,
|
||||
'choice_label' => function(Year $year = null) {
|
||||
return $year ? $year->getYear() : '';
|
||||
},
|
||||
'placeholder' => '',
|
||||
'required' => false
|
||||
]);
|
||||
};
|
||||
|
||||
$builder->addEventListener(
|
||||
FormEvents::PRE_SET_DATA,
|
||||
function (FormEvent $event) use ($formModifier) {
|
||||
$data = $event->getData();
|
||||
$theme = $data === null ? null : $this->collectionService->getThemes()[$data['theme']];
|
||||
$formModifier($event->getForm(), $theme);
|
||||
}
|
||||
);
|
||||
|
||||
$builder->get('theme')->addEventListener(
|
||||
FormEvents::POST_SUBMIT,
|
||||
function (FormEvent $event) use ($formModifier) {
|
||||
$theme = $event->getForm()->getData();
|
||||
$formModifier($event->getForm()->getParent(), $theme);
|
||||
}
|
||||
);
|
||||
|
||||
$builder->add('submit',SubmitType::class);
|
||||
}
|
||||
}
|
@ -7,26 +7,55 @@ use AppBundle\Client\Rebrickable\Rebrickable;
|
||||
|
||||
class CollectionService
|
||||
{
|
||||
/**
|
||||
* @var Brickset client
|
||||
*/
|
||||
protected $bricksetClient;
|
||||
|
||||
/**
|
||||
* @var Brickset
|
||||
*/
|
||||
private $bricksetClient;
|
||||
/**
|
||||
* @var Rebrickable client
|
||||
*/
|
||||
protected $rebrickableClient;
|
||||
|
||||
/**
|
||||
* @var Rebrickable
|
||||
*/
|
||||
private $rebrickableClient;
|
||||
/**
|
||||
* CollectionService constructor.
|
||||
*
|
||||
* @param $bricksetClient
|
||||
* @param $rebrickableClient
|
||||
*/
|
||||
public function __construct($bricksetClient, $rebrickableClient)
|
||||
{
|
||||
$this->bricksetClient = $bricksetClient;
|
||||
$this->rebrickableClient = $rebrickableClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* CollectionService constructor.
|
||||
*
|
||||
* @param $bricksetClient
|
||||
* @param $rebrickableClient
|
||||
*/
|
||||
public function __construct($bricksetClient, $rebrickableClient)
|
||||
{
|
||||
$this->bricksetClient = $bricksetClient;
|
||||
$this->rebrickableClient = $rebrickableClient;
|
||||
}
|
||||
}
|
||||
public function getThemes()
|
||||
{
|
||||
return $this->bricksetClient->getThemes();
|
||||
}
|
||||
|
||||
public function getSubthemesByTheme($theme)
|
||||
{
|
||||
return $this->bricksetClient->getSubthemes($theme);
|
||||
}
|
||||
|
||||
public function getYearsByTheme($theme)
|
||||
{
|
||||
return $this->bricksetClient->getYears($theme);
|
||||
}
|
||||
|
||||
public function getSetById($id)
|
||||
{
|
||||
return $this->bricksetClient->getSet($id);
|
||||
}
|
||||
|
||||
public function getSetParts($setNumber)
|
||||
{
|
||||
return $this->rebrickableClient->getSetParts($setNumber);
|
||||
}
|
||||
|
||||
public function getPartById($id)
|
||||
{
|
||||
return $this->rebrickableClient->getPart($id);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user