Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
imports:
- { resource: "./config/sylius_grid.yaml" }
- { resource: "./config/sylius_resource.yaml" }
- { resource: "./config/twig_hooks.yaml" }
29 changes: 19 additions & 10 deletions config/config/sylius_grid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ sylius_grid:
templates:
action:
export: '@SyliusGridImportExport\admin\import_export\grid\action\export.html.twig'
download: '@SyliusGridImportExport\admin\import_export\grid\action\download.html.twig'
bulk_action:
export: '@SyliusGridImportExport\admin\import_export\grid\bulk_action\export.html.twig'

Expand All @@ -14,24 +15,32 @@ sylius_grid:
sorting:
createdAt: desc
fields:
uuid:
type: string
label: sylius.ui.id
sortable: ~
createdAt:
type: datetime
label: sylius.ui.created_at
type:
type: string
type: twig
label: sylius.ui.type
options:
template: '@SyliusGridImportExport\admin\import_export\grid\field\type.html.twig'
resource:
type: string
label: sylius.ui.resource
label: sylius_grid_import_export.ui.resource
status:
type: string
type: twig
label: sylius.ui.status
createdAt:
type: datetime
label: sylius.ui.created_at
options:
template: '@SyliusGridImportExport\admin\import_export\grid\field\status.html.twig'
actions:
item:
download:
type: download
label: sylius_grid_import_export.ui.download
options:
link:
route: sylius_grid_import_export_admin_process_download
parameters:
uuid: resource.uuid
show:
type: show
delete:
Expand Down
47 changes: 47 additions & 0 deletions config/config/twig_hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
sylius_twig_hooks:
hooks:
'sylius_grid_import_export.admin.process.show.content':
sections:
template: '@SyliusGridImportExport/admin/import_export/show/content/sections.html.twig'
priority: 0

'sylius_grid_import_export.admin.process.show.content.sections':
general:
template: '@SyliusGridImportExport/admin/import_export/show/content/sections/general.html.twig'
priority: 0

'sylius_grid_import_export.admin.process.show.content.sections.general':
header:
template: '@SyliusGridImportExport/admin/import_export/show/content/sections/general/header.html.twig'
priority: 100
table:
template: '@SyliusGridImportExport/admin/import_export/show/content/sections/general/table.html.twig'
priority: 0

'sylius_grid_import_export.admin.process.show.content.sections.general.header':
title:
template: '@SyliusGridImportExport/admin/import_export/show/content/sections/general/header/title.html.twig'
priority: 0

'sylius_grid_import_export.admin.process.show.content.sections.general.table':
uuid:
template: '@SyliusGridImportExport/admin/import_export/show/content/sections/general/uuid.html.twig'
priority: 500
type:
template: '@SyliusGridImportExport/admin/import_export/show/content/sections/general/type.html.twig'
priority: 400
resource:
template: '@SyliusGridImportExport/admin/import_export/show/content/sections/general/resource.html.twig'
priority: 300
format:
template: '@SyliusGridImportExport/admin/import_export/show/content/sections/general/format.html.twig'
priority: 200
status:
template: '@SyliusGridImportExport/admin/import_export/show/content/sections/general/status.html.twig'
priority: 150
output:
template: '@SyliusGridImportExport/admin/import_export/show/content/sections/general/output.html.twig'
priority: 100
timestamps:
template: '@SyliusGridImportExport/admin/import_export/show/content/sections/general/timestamps.html.twig'
priority: 0
1 change: 1 addition & 0 deletions config/doctrine/Process.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<field name="status" column="status" type="string" />
<field name="resource" column="resource" type="string" />
<field name="output" column="output" type="text" nullable="true" />
<field name="errorMessage" column="error_message" type="text" nullable="true" />
<field name="format" column="format" type="string" />
<field name="resourceIds" column="resource_ids" type="json" nullable="true" />
<field name="createdAt" column="created_at" type="datetime">
Expand Down
14 changes: 10 additions & 4 deletions config/routes/admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ sylius_grid_import_export_admin_process:
redirect: index
grid: sylius_grid_import_export_admin_process
permission: true
vars:
all:
hook_prefix: 'sylius_b2b.admin.import_export.process'
type: sylius.resource

sylius_grid_import_export_admin_process_show:
Expand All @@ -30,4 +27,13 @@ sylius_grid_import_export_admin_process_show:
template: '@SyliusAdmin/shared/crud/show.html.twig'
permission: true
vars:
hook_prefix: 'sylius_b2b.admin.import_export.process'
hook_prefix: 'sylius_grid_import_export.admin.process'

sylius_grid_import_export_admin_process_download:
path: /import-export/processes/{uuid}/download
methods: [ GET ]
defaults:
_controller: sylius_import_export.controller.download_export_action
_sylius:
section: admin
permission: true
6 changes: 6 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
<tag name="controller.service_arguments" />
</service>

<service id="sylius_import_export.controller.download_export_action" class="Sylius\GridImportExport\Controller\DownloadExportAction" public="true">
<argument type="service" id="sylius_grid_import_export.repository.process" />

<tag name="controller.service_arguments" />
</service>

<service id="sylius_import_export.custom_factory.process" class="Sylius\GridImportExport\Factory\ProcessFactory" decorates="sylius_grid_import_export.factory.process">
<argument type="service" id=".inner" />
</service>
Expand Down
67 changes: 67 additions & 0 deletions src/Controller/DownloadExportAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\GridImportExport\Controller;

use Sylius\GridImportExport\Entity\ProcessInterface;
use Sylius\Resource\Doctrine\Persistence\RepositoryInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

final class DownloadExportAction
{
/**
* @param RepositoryInterface<ProcessInterface> $processRepository
*/
public function __construct(
private RepositoryInterface $processRepository,
) {
}

public function __invoke(string $uuid): Response
{
$process = $this->processRepository->find($uuid);

if (null === $process) {
throw new NotFoundHttpException(sprintf('Export process "%s" not found', $uuid));
}

if ('success' !== $process->getStatus()) {
throw new NotFoundHttpException('Export file is not ready for download');
}

$filePath = $process->getOutput();
if (empty($filePath)) {
throw new NotFoundHttpException('No output file available for this export');
}

if (!file_exists($filePath)) {
throw new NotFoundHttpException(sprintf('Export file "%s" does not exist', basename($filePath)));
}

$response = new BinaryFileResponse($filePath);

// Sanitize filename by removing invalid characters
$sanitizedResource = str_replace(['/', '\\', '.'], '_', $process->getResource());
$sanitizedUuid = str_replace(['/', '\\'], '_', $process->getUuid());

$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
sprintf('export_%s_%s.%s', $sanitizedResource, $sanitizedUuid, $process->getFormat()),
);

return $response;
}
}
5 changes: 5 additions & 0 deletions src/Controller/ExportAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Messenger\MessageBusInterface;

final class ExportAction
Expand Down Expand Up @@ -69,6 +70,10 @@ public function __invoke(Request $request, string $grid): Response
parameters: $parameters,
));

/** @var Session $session */
$session = $request->getSession();
$session->getFlashBag()->add('success', 'sylius_grid_import_export.export_started');

return new RedirectResponse($request->headers->get('referer') ?? '/');
}
}
12 changes: 12 additions & 0 deletions src/Entity/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Process implements ProcessInterface

protected string $output;

protected ?string $errorMessage = null;

public function getId(): string
{
return $this->uuid;
Expand Down Expand Up @@ -107,4 +109,14 @@ public function setOutput(string $output): void
{
$this->output = $output;
}

public function getErrorMessage(): ?string
{
return $this->errorMessage;
}

public function setErrorMessage(?string $errorMessage): void
{
$this->errorMessage = $errorMessage;
}
}
4 changes: 4 additions & 0 deletions src/Entity/ProcessInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ public function setResourceIds(array $resourceIds): void;
public function getOutput(): string;

public function setOutput(string $output): void;

public function getErrorMessage(): ?string;

public function setErrorMessage(?string $errorMessage): void;
}
5 changes: 2 additions & 3 deletions src/Messenger/Handler/ExportCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Sylius\GridImportExport\Messenger\Handler;

use Sylius\GridImportExport\Entity\ProcessInterface;
use Sylius\GridImportExport\Exception\ExportFailedException;
use Sylius\GridImportExport\Factory\ProcessFactoryInterface;
use Sylius\GridImportExport\Messenger\Command\ExportCommand;
use Sylius\GridImportExport\Provider\Registry\ResourceDataProviderRegistryInterface;
Expand Down Expand Up @@ -56,9 +55,9 @@ public function __invoke(ExportCommand $command): void

$process->setStatus('success');
$process->setOutput($outputPath);
} catch (ExportFailedException $e) {
} catch (\Throwable $e) {
$process->setStatus('failed');
$process->setOutput($e->getTraceAsString());
$process->setErrorMessage($e->getMessage());
}
}
}
5 changes: 5 additions & 0 deletions templates/admin/import_export/grid/action/download.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% if data.status == 'success' and data.output %}
<a href="{{ path(options.link.route, options.link.parameters) }}" class="btn btn-icon" data-bs-toggle="tooltip" data-bs-title="{{ action.label|trans }}" aria-label="{{ action.label|trans }}">
{{ ux_icon(action.icon|default('tabler:download')) }}
</a>
{% endif %}
10 changes: 10 additions & 0 deletions templates/admin/import_export/grid/field/status.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{%
set viewOptions = {
failed: { color: 'bg-red-lt' },
success: { color: 'bg-green-lt' },
}
%}

<span class="badge {{ viewOptions[data]['color'] }}">
{{ data }}
</span>
3 changes: 3 additions & 0 deletions templates/admin/import_export/grid/field/type.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<span class="badge bg-info-lt">
{{ ('sylius_grid_import_export.ui.type.' ~ data)|trans }}
</span>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% hook 'sylius_grid_import_export.admin.process.show.content.sections' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="ui segment">
<h4 class="ui dividing header">{{ 'sylius.ui.general_info'|trans }}</h4>

<table class="ui definition table">
<tbody>
{% sylius_hook 'sylius_grid_import_export.admin.process.show.content.sections.general' %}
</tbody>
</table>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<tr>
<td>{{ 'sylius_grid_import_export.ui.format'|trans }}</td>
<td>{{ resource.format }}</td>
</tr>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<tr>
<td>{{ 'sylius_grid_import_export.ui.resource'|trans }}</td>
<td>{{ resource.resource }}</td>
</tr>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<tr>
<td>{{ 'sylius.ui.status'|trans }}</td>
<td>{% include '@SyliusGridImportExport/admin/import_export/grid/field/status.html.twig' with {'data': resource.status} %}</td>
</tr>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<tr>
<td>{{ 'sylius.ui.created_at'|trans }}</td>
<td>{{ resource.createdAt|date('Y-m-d H:i:s') }}</td>
</tr>

{% if resource.updatedAt %}
<tr>
<td>{{ 'sylius.ui.updated_at'|trans }}</td>
<td>{{ resource.updatedAt|date('Y-m-d H:i:s') }}</td>
</tr>
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<tr>
<td>{{ 'sylius.ui.type'|trans }}</td>
<td>{{ resource.type }}</td>
</tr>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<tr>
<td>{{ 'sylius.ui.id'|trans }}</td>
<td>{{ resource.uuid }}</td>
</tr>
5 changes: 5 additions & 0 deletions templates/admin/import_export/show/content/sections.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="page-body">
<div class="container-xl">
{% hook 'sections' %}
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="card mb-2">
{% hook 'general' %}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% set resource = hookable_metadata.context.resource %}

<div class="row">
<div class="col-12 col-md-4 fw-bold">{{ 'sylius_grid_import_export.ui.format'|trans }}</div>
<div class="col-12 col-md-8">{{ resource.format }}</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="card-header">
<div class="card-title">
{% hook 'header' %}
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ 'sylius_grid_import_export.ui.process'|trans }} {{ 'sylius.ui.details'|trans }}
Loading