mobizel/sylius-export-plugin

Bulk export of sylius resources

Installs: 15 477

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 4

Forks: 1

Open Issues: 22

Type:sylius-plugin


README

Mobizel Export plugin

Getting started

This plugin add a new bulkAction 'export' to all Sylius resources.
It use the default resource grid definition to export data.
You can also use a specific grid for export.

It allow you to export:

  • All entities
  • All entities filtered by search
  • Selected entities (with checkbox)

IMPORTANT: This plugin does not depend on sylius/sylius but only sylius/resource-bundle and sylius/grid-bundle, so it can be used in other project like the symfony starter monofony.

Installation

  1. Require and install the plugin
  • Run composer require mobizel/sylius-export-plugin
  1. Register the bundle:

=> If you do not use symfony/flex you have to import the plugin in the Kernel.

<?php

// config/bundles.php

return [
    // ...
   Mobizel\SyliusExportPlugin\MobizelSyliusExportPlugin::class => ['all' => true],
];

Configuration

GRID configuration:

Create file config/packages/sylius_grid.yaml if not exist and add new bulk action

sylius_grid:
    templates:
        bulk_action:
            export: "@MobizelSyliusExportPlugin/Admin/Grid/BulkAction/export.html.twig"

Add new button macro

Add this following file to add the new button macro.

# templates/bundles/SyliusUiBundle/Macro/buttons.html.twig

{% extends "@!SyliusUi/Macro/buttons.html.twig" %}

{% macro bulkExport(url, message, labeled = true) %}
    <form action="{{ url }}" method="post" id="bulk-export">
        <a class="ui red {% if labeled %}labeled {% endif %}icon button not_disabled" type="submit" href="#">
            <i class="icon download"></i> {{ ((message is empty and labeled) ? 'sylius.ui.export' : message)|trans }}
        </a>
    </form>
{% endmacro %}

Javascript integration

Integrate vendor/mobizel/sylius-export-plugin/src/Resources/public/js/bulk-export.js in your javascript build (webpack / gulp) or directly in twig (you need to copy file to your assets directory)

Twig integration example:

<script src="{{ asset('bundles/mobizelsyliusexportplugin/js/bulk-export.js') }}"></script>

How to use it

Example

You only have to add export bulk action to your grid, example with customer grid, create file config/grids/admin/customer.yaml to override customer's grid:

sylius_grid:
    grids:
        sylius_admin_customer:
            actions:
                bulk:
                    export:
                        type: export

Next, enable your grid.
Edit config/packages/sylius_grid.yaml and add on the top:

imports:
  - { resource: '../grids/admin/customer.yaml' }

How to enable export of selected entities

Export of selected entities does not work out of the box. You need to override the entity repository.
Example for customer:

  • Create CustomerRepository class:
<?php
declare(strict_types=1);

namespace Tests\Mobizel\SyliusExportPlugin\Application\src\Repository;

use Doctrine\ORM\QueryBuilder;
use Sylius\Bundle\CoreBundle\Doctrine\ORM\CustomerRepository as BaseCustomerRepository;

class CustomerRepository extends BaseCustomerRepository
{
    public function createListQueryBuilderFilteredByIds(?array $ids): QueryBuilder
    {
        $queryBuilder = $this->createQueryBuilder('o');

        if (null !== $ids && count($ids) > 0) {
            $queryBuilder
                ->andWhere($queryBuilder->expr()->in('o.id', ':ids'))
                ->setParameter('ids', $ids);
        }

        return $queryBuilder;
    }
}

Note: We add new method to fetch entities filtered by id

  • Create file config/packages/sylius_customer if not exist

  • Set custom repository in this file

sylius_customer:
    resources:
        customer:
            classes:
                repository: Repository\CustomerRepository
  • Update customer grid to user new method:
sylius_grid:
    grids:
        sylius_admin_customer:
            driver:
                options:
                    class: "%sylius.model.customer.class%" OR App\Entity\Customer\Customer
                    repository:
                        method: createListQueryBuilderFilteredByIds
                        arguments:
                            - $ids

complete file:

sylius_grid:
    grids:
        sylius_admin_customer:
            driver:
                options:
                    class: "%sylius.model.customer.class%" OR App\Entity\Customer\Customer
                    repository:
                        method: createListQueryBuilderFilteredByIds
                        arguments:
                            - $ids
            actions:
                bulk:
                    export:
                        type: export

How to use custom grid

If you want to use a custom grid while export entites, you just have to override the route and specify the grid paramter, example for customer:

sylius_backend_customer_bulk_export:
    path: /customers/bulk-export
    methods: [POST]
    defaults:
        _controller: sylius.controller.customer:exportAction
        _sylius:
            grid: my_custom_grid
        ...

Custom export format

This plugin only use CSV for export, however you can implement your own export. Create a new class that implements Mobizel\SyliusExportPlugin\Exporter\ResourceExporterInterface.

If you create an XmlResourceExport with method

    public function getFormat(): string
    {
        return 'xml';
    }

you can change the export format in the route definition:

sylius_backend_customer_bulk_export:
    path: /customers/bulk-export
    methods: [POST]
    defaults:
        _controller: sylius.controller.customer:exportAction
        _sylius:
            grid: my_custom_grid
            vars:
                export_format: xml
        ...

CSV settings

You can configure setting of the CSV writer.

# config/packages/mobizel_sylius_export.yaml

mobizel_sylius_export:
    csv_settings:
        delimiter: ';'

Contributing

Would like to help us ? Feel free to open a pull-request!

License

Sylius export plugin is completely free and released under the MIT License.

Authors

Sylius export plugin was originally created by Kévin REGNIER.