Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: allow multiple exports #44

Merged
merged 1 commit into from
Apr 8, 2024
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
14 changes: 14 additions & 0 deletions src/Exporter/ExporterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace araise\TableBundle\Exporter;


use araise\TableBundle\Table\Table;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

interface ExporterInterface
{
public function createSpreadsheet(Table $table, $spreadsheet = new Spreadsheet()): Spreadsheet;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace araise\TableBundle\Manager;
namespace araise\TableBundle\Exporter;

use araise\CoreBundle\Manager\FormatterManager;
use araise\TableBundle\Table\Column;
Expand All @@ -13,7 +13,7 @@
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Contracts\Translation\TranslatorInterface;

class ExportManager
class TableExporter implements ExporterInterface
{
private array $reports = [];

Expand All @@ -23,9 +23,8 @@ public function __construct(
) {
}

public function createSpreadsheet(Table $table): Spreadsheet
public function createSpreadsheet(Table $table, $spreadsheet = new Spreadsheet()): Spreadsheet
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$tableColumns = $table->getColumns();
Expand Down
19 changes: 19 additions & 0 deletions src/Resources/assets/controllers/exporter_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Controller } from '@hotwired/stimulus';

export default class extends Controller {
static targets = ['link']

select(event) {
document.querySelectorAll('input[name="exporter"]').forEach((checkbox) => {
if (checkbox !== event.target) {
checkbox.checked = false;
}
});

this.linkTargets.forEach((link) => {
const url = new URL(link.href);
url.searchParams.set('exporter', event.target.value);
link.href = url.toString();
});
}
}
6 changes: 6 additions & 0 deletions src/Resources/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
"fetch": "eager",
"enabled": true
},
"exporter": {
"main": "controllers/exporter_controller.js",
"webpackMode": "eager",
"fetch": "eager",
"enabled": true
},
"table_select": {
"main": "controllers/table_select_controller.js",
"webpackMode": "eager",
Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/messages.de.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
araise_table:
download:
info: Was möchten Sie herunterladen?
choices: Export wählen
all: Alle Seiten
page: Aktuelle Seite
or: Oder
Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
araise_table:
download:
info: What would you like to download?
choices: Choose Export
all: All pages
page: Current page
or: Or
Expand Down
44 changes: 32 additions & 12 deletions src/Resources/views/tailwind_2/_header.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,38 @@
data-transition-leave-to="opacity-0 scale-95"
tabindex="-1"
>
<p class="whatwedo-utility-bold whitespace-nowrap mb-3">{{ 'araise_table.download.info' | trans }}</p>

<div class="flex space-x-2">
{% for label,queryParameters in {'araise_table.page' : app.request.query.all, 'araise_table.all' : {'all':1}|merge(app.request.query.all)} %}
<a
href="{{ path(view.definition.getRoute(constant('araise\\CrudBundle\\Enums\\Page::EXPORT')), queryParameters) }}"
class="whatwedo_table:header__button whatwedo_table:header__button--download whatwedo_table-button"

>
{{ label | trans }}
</a>
{% endfor %}
<div class="space-y-2" {% if table.exporters|length > 1 %} {{ stimulus_controller('araise/table-bundle/exporter') }} {% endif %}>
{% if table.exporters|length > 1 %}
<p class="whatwedo-utility-heading-3 mb-3">{{ 'araise_table.download.choices' | trans }}</p>
<div>
<ul role="list">
{% for acronym,exporter in table.exporters %}
<li>
<input
type="checkbox"
id="{{ acronym }}"
name="exporter"
value="{{ acronym }}"
{% if table.exporters|length > 1 %} {{ stimulus_action('araise/table-bundle/exporter', 'select')}} {% endif %}
>
<label for="{{ acronym }}">{{ exporter.label | trans }}</label>
</li>
{% endfor %}
</ul>
</div>
{% endif %}
<p class="whatwedo-utility-bold whitespace-nowrap mb-3 mt-3">{{ 'araise_table.download.info' | trans }}</p>
<div>
{% for label,queryParameters in {'araise_table.page' : app.request.query.all, 'araise_table.all' : {'all':1}|merge(app.request.query.all)} %}
<a
href="{{ path(view.definition.getRoute(constant('araise\\CrudBundle\\Enums\\Page::EXPORT')), queryParameters) }}"
class="whatwedo_table:header__button whatwedo_table:header__button--download whatwedo_table-button"
{% if table.exporters|length > 1 %} {{ stimulus_target('araise/table-bundle/exporter', 'link') }} {% endif %}
>
{{ label | trans }}
</a>
{% endfor %}
</div>
</div>
</div>
</div>
Expand Down
33 changes: 33 additions & 0 deletions src/Table/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use araise\CoreBundle\Manager\FormatterManager;
use araise\TableBundle\DataLoader\DataLoaderInterface;
use araise\TableBundle\Event\DataLoadEvent;
use araise\TableBundle\Exporter\ExporterInterface;
use araise\TableBundle\Extension\ExtensionInterface;
use araise\TableBundle\Extension\FilterExtension;
use araise\TableBundle\Extension\PaginationExtension;
Expand Down Expand Up @@ -68,6 +69,8 @@ class Table

protected ?Table $parent = null;

protected array $exporter = [];

public function __construct(
protected string $identifier,
protected array $options,
Expand Down Expand Up @@ -298,6 +301,36 @@ public function removeBatchAction(string $acronym): static
return $this;
}

public function addExporter(string $acronym, ExporterInterface $exporter): void
{
$this->exporter[$acronym]['exporter'] = $exporter;
$label = $this->options[self::OPT_DEFINITION] ? sprintf('wwd.%s.exporter.%s', $this->options[self::OPT_DEFINITION]->getEntityAlias(), $acronym) : $acronym;
$this->exporter[$acronym]['label'] = $label;
}

public function removeExporter(string $acronym): static
{
if (isset($this->exporter[$acronym])) {
unset($this->exporter[$acronym]);
}

return $this;
}

public function getExporter(string $acronym): ?ExporterInterface
{
if (isset($this->exporter[$acronym])) {
return $this->exporter[$acronym]['exporter'];
}

return null;
}

public function getExporters(): array
{
return $this->exporter;
}

public function getRows(): \Traversable
{
$this->loadData();
Expand Down
2 changes: 1 addition & 1 deletion tests/App/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ services:
public: true


araise\TableBundle\Manager\ExportManager:
araise\TableBundle\Exporter\TableExporter:
public: true
8 changes: 4 additions & 4 deletions tests/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace araise\TableBundle\Tests;

use araise\TableBundle\DataLoader\DoctrineDataLoader;
use araise\TableBundle\Exporter\TableExporter;
use araise\TableBundle\Factory\TableFactory;
use araise\TableBundle\Manager\ExportManager;
use araise\TableBundle\Table\Table;
use araise\TableBundle\Tests\App\Entity\Company;
use araise\TableBundle\Tests\App\Factory\CompanyFactory;
Expand Down Expand Up @@ -36,10 +36,10 @@ public function testDoctrineDataLoderTable()
->addColumn('country')
->addColumn('taxIdentificationNumber');

/** @var ExportManager $exportManager */
$exportManager = self::getContainer()->get(ExportManager::class);
/** @var TableExporter $tableExporter */
$tableExporter = self::getContainer()->get(TableExporter::class);

$sheet = $exportManager->createSpreadsheet($table);
$sheet = $tableExporter->createSpreadsheet($table);

$this->assertNotNull($sheet);

Expand Down
Loading