Skip to content

Commit 4903ee2

Browse files
committed
OP-289: Add grid type and filter to handle bundles
1 parent 5366129 commit 4903ee2

9 files changed

Lines changed: 134 additions & 0 deletions

File tree

src/Grid/Filter/IsBundleFilter.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusProductBundlePlugin\Grid\Filter;
13+
14+
use Sylius\Component\Grid\Data\DataSourceInterface;
15+
use Sylius\Component\Grid\Filter\BooleanFilter;
16+
use Sylius\Component\Grid\Filtering\FilterInterface;
17+
18+
final class IsBundleFilter implements FilterInterface
19+
{
20+
//TODO fix handling bundles with no products added
21+
public function apply(DataSourceInterface $dataSource, string $name, $data, array $options): void
22+
{
23+
switch ($data) {
24+
case BooleanFilter::TRUE:
25+
$dataSource->restrict('productBundleItems IS NOT NULL');
26+
27+
break;
28+
case BooleanFilter::FALSE:
29+
$dataSource->restrict('productBundleItems IS NULL');
30+
31+
break;
32+
}
33+
}
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusProductBundlePlugin\Repository;
13+
14+
use Doctrine\ORM\QueryBuilder;
15+
16+
interface ProductBundlesAwareInterface
17+
{
18+
public function createSearchListQueryBuilder(string $locale, $taxonId = null): QueryBuilder;
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusProductBundlePlugin\Repository;
13+
14+
use Doctrine\ORM\QueryBuilder;
15+
16+
/**
17+
* @method QueryBuilder createListQueryBuilder(string $locale, $taxonId = null)
18+
*/
19+
trait ProductBundlesAwareTrait
20+
{
21+
public function createSearchListQueryBuilder(string $locale, $taxonId = null): QueryBuilder
22+
{
23+
return $this->createListQueryBuilder($locale, $taxonId)
24+
->leftJoin('o.productBundle', 'productBundle')
25+
->leftJoin('productBundle.productBundleItems', 'productBundleItems');
26+
}
27+
}

src/Resources/config/grids.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
sylius_grid:
22
grids:
33
sylius_admin_product:
4+
driver:
5+
name: doctrine/orm
6+
options:
7+
class: "%sylius.model.product.class%"
8+
repository:
9+
method: createSearchListQueryBuilder
10+
arguments: [ "expr:service('sylius.context.locale').getLocaleCode()", $taxonId ]
11+
fields:
12+
bundle:
13+
type: twig
14+
label: bitbag_sylius_product_bundle.ui.is_bundle
15+
options:
16+
template: "@SyliusUi/Grid/Field/yesNo.html.twig"
17+
filters:
18+
bundle:
19+
type: is_bundle
20+
label: bitbag_sylius_product_bundle.ui.is_bundle
421
actions:
522
main:
623
create:
@@ -25,3 +42,6 @@ sylius_grid:
2542
label: bitbag_sylius_product_bundle.ui.bundle
2643
icon: plus
2744
route: bitbag_product_bundle_admin_product_create_bundle
45+
templates:
46+
filter:
47+
is_bundle: "@SyliusUi/Grid/Filter/boolean.html.twig"

src/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<import resource="services/form.xml"/>
66
<import resource="services/menu.xml"/>
77
<import resource="services/factory.xml"/>
8+
<import resource="services/filter.xml"/>
89
<import resource="services/controller.xml"/>
910
<import resource="services/handler.xml"/>
1011
<import resource="services/processor.xml"/>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
<services>
6+
<service id="bitbag_sylius_product_bundle.grid.filter.is_bundle" class="BitBag\SyliusProductBundlePlugin\Grid\Filter\IsBundleFilter">
7+
<tag name="sylius.grid_filter" type="is_bundle" form_type="Sylius\Bundle\GridBundle\Form\Type\Filter\BooleanFilterType" />
8+
</service>
9+
</services>
10+
</container>

src/Resources/translations/messages.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ bitbag_sylius_product_bundle:
66
delete: Delete
77
products_in_bundle: Products in bundle
88
is_packed_product: Is packed product
9+
is_bundle: Is bundle

tests/Application/config/packages/_sylius.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ sylius_product:
1818
product:
1919
classes:
2020
model: Tests\BitBag\SyliusProductBundlePlugin\Entity\Product
21+
repository: Tests\BitBag\SyliusProductBundlePlugin\Application\src\Repository\ProductRepository
2122

2223
sylius_order:
2324
resources:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Tests\BitBag\SyliusProductBundlePlugin\Application\src\Repository;
13+
14+
use BitBag\SyliusProductBundlePlugin\Repository\ProductBundlesAwareInterface;
15+
use BitBag\SyliusProductBundlePlugin\Repository\ProductBundlesAwareTrait;
16+
use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository as BaseProductRepository;
17+
18+
class ProductRepository extends BaseProductRepository implements ProductBundlesAwareInterface
19+
{
20+
use ProductBundlesAwareTrait;
21+
}

0 commit comments

Comments
 (0)