Skip to content

Commit

Permalink
Merge pull request #5 from araise-dev/feature/batch-actions-defined-i…
Browse files Browse the repository at this point in the history
…n-table-bundle

feat(batch-actions): defiend in table-bundle
  • Loading branch information
tuxes3 authored Aug 7, 2023
2 parents 248f2df + 15e1865 commit 356634a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Resources/views/tailwind_2/_table.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<div class="py-1" role="none">

<span class="block">
{% for batchAction in table.getOption('definition').batchActions | filter(action => not action.getOption('voter_attribute') or is_granted(action.getOption('voter_attribute'), action)) %}
{% for batchAction in table.batchActions | filter(action => not action.getOption('voter_attribute') or is_granted(action.getOption('voter_attribute'), action)) %}
{% if batchAction.hasConfirmation() %}
{{ _self.confirmationLink(batchAction, {}, stimulus_action('@araise/table-bundle/table_select', 'doAction') ~ ' href="' ~ path(batchAction.route, batchAction.routeParameters) ~ '"') }}
{% else %}
Expand Down
59 changes: 47 additions & 12 deletions src/Table/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class Table

protected array $actions = [];

protected array $batchActions = [];

protected \Traversable $rows;

protected bool $loaded = false;
Expand Down Expand Up @@ -135,11 +137,11 @@ public function getSubTables(object|array $row): array
}

$subTables = ($this->getOption(self::OPT_SUB_TABLE_LOADER))($row);
if (! $subTables) {
if (!$subTables) {
return [];
}

if (! is_array($subTables)) {
if (!is_array($subTables)) {
$subTables = [$subTables];
}

Expand Down Expand Up @@ -197,13 +199,13 @@ public function addColumn(string $acronym, $type = null, array $options = [], ?i
}

if ($this->options[self::OPT_DEFINITION]) {
if (! isset($options[Column::OPT_LABEL])) {
if (!isset($options[Column::OPT_LABEL])) {
$options[Column::OPT_LABEL] = sprintf('wwd.%s.property.%s', $this->options[self::OPT_DEFINITION]->getEntityAlias(), $acronym);
}
}

// set link_the_column_content on first column if not set
if (! isset($options[Column::OPT_LINK_THE_COLUMN_CONTENT]) && count($this->columns) === 0) {
if (!isset($options[Column::OPT_LINK_THE_COLUMN_CONTENT]) && count($this->columns) === 0) {
$options[Column::OPT_LINK_THE_COLUMN_CONTENT] = true;
}

Expand Down Expand Up @@ -234,12 +236,15 @@ public function removeColumn($acronym): static
*/
public function getActions(): array
{
uasort(
$this->actions,
static fn (Action $a, Action $b) => $a->getOption(Action::OPT_PRIORITY) <=> $b->getOption(Action::OPT_PRIORITY)
);
return $this->getInternalActions($this->actions);
}

return $this->actions;
/**
* @return Action[]
*/
public function getBatchActions(): array
{
return $this->getInternalActions($this->batchActions);
}

public function addAction(string $acronym, array $options = [], $type = Action::class): static
Expand All @@ -249,6 +254,25 @@ public function addAction(string $acronym, array $options = [], $type = Action::
return $this;
}

public function addBatchAction(string $acronym, array $options = [], string $type = Action::class): static
{
if (! isset($options['voter_attribute'])) {
$options['voter_attribute'] = 'batch_action';
}
$this->batchActions[$acronym] = new $type($acronym, $options);

return $this;
}

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

return $this;
}

public function getRows(): \Traversable
{
$this->loadData();
Expand Down Expand Up @@ -333,9 +357,7 @@ public function setParent(?self $parent): void
public function hasBatchActions(): bool
{
return $this->parent === null
&& $this->getOption(self::OPT_DEFINITION)
&& (enum_exists('araise\CrudBundle\Enums\Page')
&& $this->getOption(self::OPT_DEFINITION)::hasCapability(\araise\CrudBundle\Enums\Page::BATCH))
&& count($this->getBatchActions()) > 0
;
}

Expand Down Expand Up @@ -377,4 +399,17 @@ protected function insertColumnAtPosition($key, $value, $position)
}
$this->columns = $newArray;
}

/**
* @return Action[]
*/
private function getInternalActions(array $actions): array
{
uasort(
$actions,
static fn (Action $a, Action $b) => $a->getOption(Action::OPT_PRIORITY) <=> $b->getOption(Action::OPT_PRIORITY)
);

return $actions;
}
}

0 comments on commit 356634a

Please sign in to comment.