mirror of
https://github.com/ToxicCrack/PrintABrick.git
synced 2025-06-01 03:30:18 -07:00
Add set filter form
This commit is contained in:
parent
f1fc90e80f
commit
cdc7150e36
@ -5,6 +5,11 @@ services:
|
|||||||
client.rebrickable:
|
client.rebrickable:
|
||||||
class: AppBundle\Client\Rebrickable\Rebrickable
|
class: AppBundle\Client\Rebrickable\Rebrickable
|
||||||
arguments: ['%rebrickable_apikey%']
|
arguments: ['%rebrickable_apikey%']
|
||||||
service.collection:
|
app.collection_service:
|
||||||
class: AppBundle\Service\CollectionService
|
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
|
class CollectionService
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var Brickset client
|
||||||
|
*/
|
||||||
|
protected $bricksetClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Brickset
|
* @var Rebrickable client
|
||||||
*/
|
*/
|
||||||
private $bricksetClient;
|
protected $rebrickableClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Rebrickable
|
* CollectionService constructor.
|
||||||
*/
|
*
|
||||||
private $rebrickableClient;
|
* @param $bricksetClient
|
||||||
|
* @param $rebrickableClient
|
||||||
|
*/
|
||||||
|
public function __construct($bricksetClient, $rebrickableClient)
|
||||||
|
{
|
||||||
|
$this->bricksetClient = $bricksetClient;
|
||||||
|
$this->rebrickableClient = $rebrickableClient;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
public function getThemes()
|
||||||
* CollectionService constructor.
|
{
|
||||||
*
|
return $this->bricksetClient->getThemes();
|
||||||
* @param $bricksetClient
|
}
|
||||||
* @param $rebrickableClient
|
|
||||||
*/
|
public function getSubthemesByTheme($theme)
|
||||||
public function __construct($bricksetClient, $rebrickableClient)
|
{
|
||||||
{
|
return $this->bricksetClient->getSubthemes($theme);
|
||||||
$this->bricksetClient = $bricksetClient;
|
}
|
||||||
$this->rebrickableClient = $rebrickableClient;
|
|
||||||
}
|
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