mirror of
https://github.com/ToxicCrack/PrintABrick.git
synced 2025-05-18 13:10:08 -07:00
Add LDraw Part filter
This commit is contained in:
parent
b6295d34ea
commit
f2cfaf197f
@ -9,8 +9,16 @@
|
|||||||
<link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
|
<link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
{{ knp_menu_render('AppBundle:Builder:mainMenu') }}
|
{% block body %}
|
||||||
{% block body %}{% endblock %}
|
<div class="ui fixed inverted menu">
|
||||||
|
</div>
|
||||||
|
<div class="ui main container">
|
||||||
|
{% block content %}
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
{% block javascripts %}
|
{% block javascripts %}
|
||||||
|
|
||||||
<script type="text/javascript" src="{{ asset('resources/js/three.js') }}"></script>
|
<script type="text/javascript" src="{{ asset('resources/js/three.js') }}"></script>
|
||||||
|
31
app/Resources/views/ldraw/part/index.html.twig
Normal file
31
app/Resources/views/ldraw/part/index.html.twig
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
{% extends 'base.html.twig' %}
|
||||||
|
|
||||||
|
{% import 'macro/part.html.twig' as macro %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<form method="get" action="." class="ui form">
|
||||||
|
{{ form_start(form) }}
|
||||||
|
|
||||||
|
{{ form_row(form.search) }}
|
||||||
|
<div class="field fluid search selection">
|
||||||
|
{{ form_label(form.category.id) }}
|
||||||
|
{{ form_widget(form.category.id) }}
|
||||||
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<input class="ui submit button" type="submit" value="filter"/>
|
||||||
|
</div>
|
||||||
|
{{ form_end(form) }}
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<div class="ui ten column grid">
|
||||||
|
<div class="row">
|
||||||
|
{% for part in parts %}
|
||||||
|
<div class="column">
|
||||||
|
{{ macro.part(part) }}
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{{ knp_pagination_render(parts) }}
|
||||||
|
{% endblock %}
|
90
app/Resources/views/ldraw/part/show.html.twig
Normal file
90
app/Resources/views/ldraw/part/show.html.twig
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
{% extends 'base.html.twig' %}
|
||||||
|
|
||||||
|
{% import 'macro/part.html.twig' as macro %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<dl>
|
||||||
|
<dt>number:</dt><dd>{{ part.number }}</dd>
|
||||||
|
<dt>name:</dt><dd>{{ part.name }}</dd>
|
||||||
|
<dt>category:</dt><dd>{{ part.category.name }}</dd>
|
||||||
|
<dt>type:</dt><dd>{{ part.type.name }}</dd>
|
||||||
|
<dt>model:</dt><dd>{{ part.model ? part.model.number }}</dd>
|
||||||
|
</dl>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
{% for keyword in part.keywords %}
|
||||||
|
<span class="ui label">{{ keyword.name }}</span>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style="display: flex; flex-wrap: wrap">
|
||||||
|
{% if apiPart is not null %}
|
||||||
|
<div style="height: 300px; width: 300px; padding: 5px; display: inline-block">
|
||||||
|
<img src="{{ apiPart.imgUrl }}" style="max-height: 90%; max-width: 100%">
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% if part is defined and part is not null %}
|
||||||
|
<div id="model" style="height: 300px; width: 300px; padding: 5px; display: inline-block"></div>
|
||||||
|
<div style="height: 300px; width: 300px; padding: 5px; display: inline-block">
|
||||||
|
<img src="{{ url('part_image', {number:part.number}) }}" style="max-height: 90%; max-width: 100%">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h2 style="width: 100%">alias of</h2>
|
||||||
|
{% if part.aliasOf is not null %}
|
||||||
|
{{ macro.part(part.aliasOf) }}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
<h2 style="width: 100%">aliases</h2>
|
||||||
|
{% for alias in part.aliases %}
|
||||||
|
{{ macro.part(alias) }}
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
<h2 style="width: 100%">subparts</h2>
|
||||||
|
{% for subpart in part.subparts %}
|
||||||
|
{{ macro.part(subpart) }}
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
<h2 style="width: 100%">subpart of</h2>
|
||||||
|
{% for subpart in part.subpartOf %}
|
||||||
|
{{ macro.part(subpart) }}
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
{% if part.printOf is not null %}
|
||||||
|
<h2 style="width: 100%">print of</h2>
|
||||||
|
{{ macro.part(part.printOf) }}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% if part.prints is not empty %}
|
||||||
|
<h2 style="width: 100%">prints</h2>
|
||||||
|
{% for print in part.prints %}
|
||||||
|
{{ macro.part(print) }}
|
||||||
|
{% endfor %}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% endif %}
|
||||||
|
<h2 style="width: 100%">in sets</h2>
|
||||||
|
{% for set in sets %}
|
||||||
|
<span style="margin: 5px"><a href="{{ url('set_detail', {number:set.number}) }}">{{ set.number }}</a></span>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{{ dump(rbPart) }}
|
||||||
|
{{ dump(apiPart) }}
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block javascripts %}
|
||||||
|
{{ parent() }}
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
window.onload = function() {
|
||||||
|
modelView = new ModelViewer();
|
||||||
|
var scene = modelView.initScene($('#model'));
|
||||||
|
{% if part is defined and part is not null and part.model is not null %}
|
||||||
|
modelView.loadStl('{{ path('model_stl', {'id' : part.model.number })}}');
|
||||||
|
{% endif %}
|
||||||
|
modelView.render();
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
{% endblock %}
|
6
app/Resources/views/macro/part.html.twig
Normal file
6
app/Resources/views/macro/part.html.twig
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{% macro part(part) %}
|
||||||
|
<div style="height: 100px; width: 100px; padding: 5px; display: inline-block;">
|
||||||
|
<img src="{{ url('part_image', {number:part.number}) }}" style="max-height: 90%; max-width: 100%;">
|
||||||
|
<p><a href="{{ url('ldraw_part_show', {number:part.number}) }}">{{ part.number }}</a></p>
|
||||||
|
</div>
|
||||||
|
{% endmacro %}
|
@ -1,96 +0,0 @@
|
|||||||
{% extends 'base.html.twig' %}
|
|
||||||
|
|
||||||
{% block body %}
|
|
||||||
|
|
||||||
{{ dump(part) }}
|
|
||||||
{{ dump(rbPart) }}
|
|
||||||
|
|
||||||
{{ dump(apiPart) }}
|
|
||||||
|
|
||||||
|
|
||||||
<div style="display: flex; flex-wrap: wrap">
|
|
||||||
{% if apiPart is not null %}
|
|
||||||
<div style="height: 300px; width: 300px; padding: 5px; display: inline-block">
|
|
||||||
<img src="{{ apiPart.imgUrl }}" style="max-height: 90%; max-width: 100%">
|
|
||||||
</div>
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
{% if part is not null %}
|
|
||||||
<div id="model" style="height: 300px; width: 300px; padding: 5px; display: inline-block"></div>
|
|
||||||
<div style="height: 300px; width: 300px; padding: 5px; display: inline-block">
|
|
||||||
<img src="{{ url('part_image', {id:part.id}) }}" style="max-height: 90%; max-width: 100%">
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h2 style="width: 100%">alias of</h2>
|
|
||||||
{% if part.aliasOf is not null %}
|
|
||||||
<div style="height: 300px; width: 300px; padding: 5px; display: inline-block;">
|
|
||||||
<img src="{{ url('part_image', {id:part.aliasOf.id}) }}" style="max-height: 90%; max-width: 100%;">
|
|
||||||
<p><a href="{{ url('part_detail', {id:part.aliasOf.id}) }}">{{ part.aliasOf.id }}</a></p>
|
|
||||||
</div>
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
<h2 style="width: 100%">aliases</h2>
|
|
||||||
{% for alias in part.aliases %}
|
|
||||||
<div style="height: 300px; width: 300px; padding: 5px; display: inline-block;">
|
|
||||||
<img src="{{ url('part_image', {id:alias.id}) }}" style="max-height: 90%; max-width: 100%;">
|
|
||||||
<p><a href="{{ url('part_detail', {id:alias.id}) }}">{{ alias.id }}</a></p>
|
|
||||||
</div>
|
|
||||||
{% endfor %}
|
|
||||||
|
|
||||||
<h2 style="width: 100%">subparts</h2>
|
|
||||||
{% for subpart in part.subparts %}
|
|
||||||
<div style="height: 300px; width: 300px; padding: 5px; display: inline-block;">
|
|
||||||
<img src="{{ url('part_image', {id:subpart.id}) }}" style="max-height: 90%; max-width: 100%;">
|
|
||||||
<p><a href="{{ url('part_detail', {id:subpart.id}) }}">{{ subpart.id }}</a></p>
|
|
||||||
</div>
|
|
||||||
{% endfor %}
|
|
||||||
|
|
||||||
<h2 style="width: 100%">subpart of</h2>
|
|
||||||
{% for subpart in part.subpartOf %}
|
|
||||||
<div style="height: 300px; width: 300px; padding: 5px; display: inline-block;">
|
|
||||||
<img src="{{ url('part_image', {id:subpart.id}) }}" style="max-height: 90%; max-width: 100%;">
|
|
||||||
<p><a href="{{ url('part_detail', {id:subpart.id}) }}">{{ subpart.id }}</a></p>
|
|
||||||
</div>
|
|
||||||
{% endfor %}
|
|
||||||
|
|
||||||
{% if part.printOf is not null %}
|
|
||||||
<h2 style="width: 100%">print of</h2>
|
|
||||||
<div style="height: 300px; width: 300px; padding: 5px; display: inline-block;">
|
|
||||||
<img src="{{ url('part_image', {id:part.printOf.id}) }}" style="max-height: 90%; max-width: 100%;">
|
|
||||||
<p><a href="{{ url('part_detail', {id:part.printOf.id}) }}">{{ part.printOf.id }}</a></p>
|
|
||||||
</div>
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
{% if part.prints is not empty %}
|
|
||||||
<h2 style="width: 100%">prints</h2>
|
|
||||||
{% for print in part.prints %}
|
|
||||||
<div style="height: 300px; width: 300px; padding: 5px; display: inline-block;">
|
|
||||||
<img src="{{ url('part_image', {id:print.id}) }}" style="max-height: 90%; max-width: 100%;">
|
|
||||||
<p><a href="{{ url('part_detail', {id:print.id}) }}">{{ print.id }}</a></p>
|
|
||||||
</div>
|
|
||||||
{% endfor %}
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
{% endif %}
|
|
||||||
<h2 style="width: 100%">in sets</h2>
|
|
||||||
{% for inventory in inventories %}
|
|
||||||
<span style="margin: 5px"><a href="{{ url('set_detail', {id:inventory.set.number}) }}">{{ inventory.set.number }}</a></span>
|
|
||||||
{% endfor %}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{% endblock %}
|
|
||||||
|
|
||||||
{% block javascripts %}
|
|
||||||
{{ parent() }}
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
window.onload = function() {
|
|
||||||
modelView = new ModelViewer();
|
|
||||||
var scene = modelView.initScene($('#model'));
|
|
||||||
{% if part is not null and part.model is not null %}
|
|
||||||
modelView.loadStl('{{ path('model_stl', {'id' : part.model.id })}}');
|
|
||||||
{% endif %}
|
|
||||||
modelView.render();
|
|
||||||
};
|
|
||||||
</script>
|
|
||||||
{% endblock %}
|
|
@ -1,30 +0,0 @@
|
|||||||
{% extends 'base.html.twig' %}
|
|
||||||
|
|
||||||
|
|
||||||
{% block body %}
|
|
||||||
<h2 style="width: 100%; text-align: center">
|
|
||||||
total parts {{ parts.TotalItemCount }}
|
|
||||||
<div class="navigation text-center">
|
|
||||||
{{ knp_pagination_render(parts) }}
|
|
||||||
</div>
|
|
||||||
</h2>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="display: flex; flex-wrap: wrap;">
|
|
||||||
{% for part in parts %}
|
|
||||||
<div style="display: inline-block; width: 160px; margin: 5px; padding: 5px; border: solid 1px gray;{% if part.sub is defined and not part.sub %}background:red{% endif %}">
|
|
||||||
<img src="{{ url('part_image', {id:part.id}) }}" style="max-height: 100px; max-width: 100%">
|
|
||||||
<h4><a href="{{ url('part_detail', {id:part.id}) }}">{{ part.id }}</a></h4>
|
|
||||||
<p>{{ part.name }}</p>
|
|
||||||
</div>
|
|
||||||
{% endfor %}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h2 style="width: 100%; text-align: center">
|
|
||||||
<div class="navigation text-center">
|
|
||||||
{{ knp_pagination_render(parts) }}
|
|
||||||
</div>
|
|
||||||
</h2>
|
|
||||||
|
|
||||||
|
|
||||||
{% endblock %}
|
|
7
app/config/service/form.yml
Normal file
7
app/config/service/form.yml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
services:
|
||||||
|
form.filter.category:
|
||||||
|
class: AppBundle\Form\Filter\CategoryFilterType
|
||||||
|
arguments:
|
||||||
|
- '@manager.ldraw.category'
|
||||||
|
tags:
|
||||||
|
- { name: form.type }
|
@ -4,3 +4,4 @@ imports:
|
|||||||
- { resource: service/loader.yml }
|
- { resource: service/loader.yml }
|
||||||
- { resource: service/manager.yml }
|
- { resource: service/manager.yml }
|
||||||
- { resource: service/service.yml }
|
- { resource: service/service.yml }
|
||||||
|
- { resource: service/form.yml }
|
||||||
|
88
src/AppBundle/Controller/LDraw/PartController.php
Normal file
88
src/AppBundle/Controller/LDraw/PartController.php
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AppBundle\Controller\LDraw;
|
||||||
|
|
||||||
|
use AppBundle\Entity\LDraw\Part;
|
||||||
|
use AppBundle\Entity\Rebrickable\Set;
|
||||||
|
use AppBundle\Form\Filter\PartFilterType;
|
||||||
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
|
||||||
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Part controller.
|
||||||
|
*
|
||||||
|
* @Route("ldraw_part")
|
||||||
|
*/
|
||||||
|
class PartController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Lists all part entities.
|
||||||
|
*
|
||||||
|
* @Route("/", name="ldraw_part_index")
|
||||||
|
* @Method("GET")
|
||||||
|
*/
|
||||||
|
public function indexAction(Request $request)
|
||||||
|
{
|
||||||
|
$em = $this->getDoctrine()->getManager();
|
||||||
|
|
||||||
|
$form = $this->get('form.factory')->create(PartFilterType::class);
|
||||||
|
|
||||||
|
$filterBuilder = $this->get('repository.ldraw.part')
|
||||||
|
->createQueryBuilder('part');
|
||||||
|
|
||||||
|
// $filterBuilder->where('part.type = 1');
|
||||||
|
|
||||||
|
if ($request->query->has($form->getName())) {
|
||||||
|
// manually bind values from the request
|
||||||
|
$form->submit($request->query->get($form->getName()));
|
||||||
|
|
||||||
|
// build the query from the given form object
|
||||||
|
$this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder);
|
||||||
|
}
|
||||||
|
|
||||||
|
$paginator = $this->get('knp_paginator');
|
||||||
|
$parts = $paginator->paginate(
|
||||||
|
$filterBuilder->getQuery(),
|
||||||
|
$request->query->getInt('page', 1)/*page number*/,
|
||||||
|
$request->query->getInt('limit', 100)/*limit per page*/
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->render('ldraw/part/index.html.twig', [
|
||||||
|
'parts' => $parts,
|
||||||
|
'form' => $form->createView(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds and displays a part entity.
|
||||||
|
*
|
||||||
|
* @Route("/{number}", name="ldraw_part_show")
|
||||||
|
* @Method("GET")
|
||||||
|
*/
|
||||||
|
public function showAction($number)
|
||||||
|
{
|
||||||
|
$em = $this->getDoctrine()->getManager();
|
||||||
|
|
||||||
|
$rbPart = $em->getRepository(\AppBundle\Entity\Rebrickable\Part::class)->find($number);
|
||||||
|
|
||||||
|
$part = $em->getRepository(Part::class)->find($number);
|
||||||
|
|
||||||
|
$apiPart = null;
|
||||||
|
try {
|
||||||
|
$apiPart = $this->get('manager.rebrickable')->getPart($number);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
dump($e);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sets = $em->getRepository(Set::class)->findAllByPartNumber($number);
|
||||||
|
|
||||||
|
return $this->render('ldraw/part/show.html.twig', [
|
||||||
|
'part' => $part,
|
||||||
|
'rbPart' => $rbPart,
|
||||||
|
'apiPart' => $apiPart,
|
||||||
|
'sets' => $sets,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
@ -1,80 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace AppBundle\Controller;
|
|
||||||
|
|
||||||
use AppBundle\Entity\LDraw\Part;
|
|
||||||
use AppBundle\Entity\LDraw\Type;
|
|
||||||
use AppBundle\Entity\Rebrickable\Inventory;
|
|
||||||
use AppBundle\Entity\Rebrickable\Inventory_Part;
|
|
||||||
use Doctrine\DBAL\Query\QueryBuilder;
|
|
||||||
use Doctrine\ORM\Query\Expr\Join;
|
|
||||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Route("/parts")
|
|
||||||
*/
|
|
||||||
class PartController extends Controller
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @Route("/detail/{id}", name="part_detail")
|
|
||||||
*/
|
|
||||||
public function showAction(Request $request, $id)
|
|
||||||
{
|
|
||||||
$em = $this->get('doctrine.orm.default_entity_manager');
|
|
||||||
|
|
||||||
$part = $em->getRepository(Part::class)->find($id);
|
|
||||||
$rbPart = $em->getRepository(\AppBundle\Entity\Rebrickable\Part::class)->find($id);
|
|
||||||
|
|
||||||
$apiPart = null;
|
|
||||||
try {
|
|
||||||
$apiPart = $this->get('manager.rebrickable')->getPart($id);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
dump($e);
|
|
||||||
}
|
|
||||||
|
|
||||||
$qb = $em->getRepository('AppBundle:Rebrickable\Inventory')->createQueryBuilder('i');
|
|
||||||
|
|
||||||
$qb->innerJoin(Inventory_Part::class, 'ip', Join::WITH, 'i.id = ip.inventory')
|
|
||||||
->where('ip.part = :part')
|
|
||||||
->setParameter('part', $id)->distinct(true);
|
|
||||||
|
|
||||||
$inventries = $qb->getQuery()->getResult();
|
|
||||||
|
|
||||||
return $this->render('part/detail.html.twig', [
|
|
||||||
'part' => $part,
|
|
||||||
'rbPart' => $rbPart,
|
|
||||||
'apiPart' => $apiPart,
|
|
||||||
'inventories' => $inventries,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Route("/", name="parts_index")
|
|
||||||
*/
|
|
||||||
public function indexAction(Request $request)
|
|
||||||
{
|
|
||||||
$em = $this->getDoctrine()->getManager();
|
|
||||||
|
|
||||||
$queryBuilder = $em->getRepository(Part::class)->createQueryBuilder('p');
|
|
||||||
|
|
||||||
/** @var QueryBuilder $queryBuilder */
|
|
||||||
// $queryBuilder->where('p.model is not null');
|
|
||||||
$queryBuilder->join(Type::class,'type', JOIN::WITH, 'p.type = type.id')->where( $queryBuilder->expr()->notIn('type.name', ['Alias', 'Obsolete/Subpart']));
|
|
||||||
|
|
||||||
$query = $queryBuilder->getQuery();
|
|
||||||
|
|
||||||
$paginator = $this->get('knp_paginator');
|
|
||||||
|
|
||||||
$parts = $paginator->paginate(
|
|
||||||
$query,
|
|
||||||
$request->query->getInt('page', 1)/*page number*/,
|
|
||||||
$request->query->getInt('limit', 100)/*limit per page*/
|
|
||||||
);
|
|
||||||
|
|
||||||
return $this->render('part/index.html.twig', [
|
|
||||||
'parts' => $parts,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
49
src/AppBundle/Form/Filter/CategoryFilterType.php
Normal file
49
src/AppBundle/Form/Filter/CategoryFilterType.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AppBundle\Form\Filter;
|
||||||
|
|
||||||
|
use AppBundle\Entity\LDraw\Category;
|
||||||
|
use AppBundle\Manager\LDraw\CategoryManager;
|
||||||
|
use Lexik\Bundle\FormFilterBundle\Filter\Form\Type as Filters;
|
||||||
|
use Symfony\Component\Form\AbstractType;
|
||||||
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
|
|
||||||
|
class CategoryFilterType extends AbstractType
|
||||||
|
{
|
||||||
|
private $categoryManager;
|
||||||
|
|
||||||
|
public function __construct(CategoryManager $categoryManager)
|
||||||
|
{
|
||||||
|
$this->categoryManager = $categoryManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||||
|
{
|
||||||
|
$builder->add('id', Filters\ChoiceFilterType::class, [
|
||||||
|
'choices' => $this->categoryManager->findAll(),
|
||||||
|
'choice_label' => 'name',
|
||||||
|
'label' => 'filter.part.category',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getParent()
|
||||||
|
{
|
||||||
|
return Filters\SharedableFilterType::class; // this allow us to use the "add_shared" option
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getBlockPrefix()
|
||||||
|
{
|
||||||
|
return 'category_filter';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setDefaultOptions(OptionsResolver $resolver)
|
||||||
|
{
|
||||||
|
$resolver->setDefaults([
|
||||||
|
'data_class' => Category::class,
|
||||||
|
'csrf_protection' => false,
|
||||||
|
'validation_groups' => ['filtering'], // avoid NotBlank() constraint-related message
|
||||||
|
'method' => 'GET',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
58
src/AppBundle/Form/Filter/PartFilterType.php
Normal file
58
src/AppBundle/Form/Filter/PartFilterType.php
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AppBundle\Form\Filter;
|
||||||
|
|
||||||
|
use Doctrine\ORM\QueryBuilder;
|
||||||
|
use Lexik\Bundle\FormFilterBundle\Filter\FilterBuilderExecuterInterface;
|
||||||
|
use Lexik\Bundle\FormFilterBundle\Filter\Form\Type as Filters;
|
||||||
|
use Lexik\Bundle\FormFilterBundle\Filter\Query\QueryInterface;
|
||||||
|
use Symfony\Component\Form\AbstractType;
|
||||||
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
|
|
||||||
|
class PartFilterType extends AbstractType
|
||||||
|
{
|
||||||
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||||
|
{
|
||||||
|
$builder->add('search', Filters\TextFilterType::class, [
|
||||||
|
'apply_filter' => [$this, 'partSearchCallback'],
|
||||||
|
'label' => 'filter.part.search',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$builder->add('category', CategoryFilterType::class, [
|
||||||
|
'add_shared' => function (FilterBuilderExecuterInterface $builderExecuter) {
|
||||||
|
$builderExecuter->addOnce($builderExecuter->getAlias().'.category', 'c', function (QueryBuilder $filterBuilder, $alias, $joinAlias, $expr) {
|
||||||
|
$filterBuilder->leftJoin($alias.'.category', $joinAlias);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getBlockPrefix()
|
||||||
|
{
|
||||||
|
return 'part_filter';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function configureOptions(OptionsResolver $resolver)
|
||||||
|
{
|
||||||
|
$resolver->setDefaults([
|
||||||
|
'csrf_protection' => false,
|
||||||
|
'validation_groups' => ['filtering'], // avoid NotBlank() constraint-related message
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function partSearchCallback(QueryInterface $filterQuery, $field, $values)
|
||||||
|
{
|
||||||
|
if (empty($values['value'])) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// expression that represent the condition
|
||||||
|
$expression = $filterQuery->getExpr()->orX(
|
||||||
|
$filterQuery->getExpr()->like('part.number', ':value'),
|
||||||
|
$filterQuery->getExpr()->like('part.name', ':value')
|
||||||
|
);
|
||||||
|
|
||||||
|
return $filterQuery->createCondition($expression, ['value' => '%'.$values['value'].'%']);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user