Merge branch 'feature/batch' into develop
This commit is contained in:
commit
521ed5ce64
|
@ -162,7 +162,7 @@ abstract class CrudController extends AdminController
|
||||||
|
|
||||||
$lastRequest = $session->get($lastRequestId);
|
$lastRequest = $session->get($lastRequestId);
|
||||||
|
|
||||||
if ($lastRequest !== null && !$request->isMethod('POST')) {
|
if (null !== $lastRequest && !$request->isMethod('POST')) {
|
||||||
$fakeRequest = Request::create(
|
$fakeRequest = Request::create(
|
||||||
uri: $request->getUri(),
|
uri: $request->getUri(),
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
@ -284,16 +284,25 @@ abstract class CrudController extends AdminController
|
||||||
|
|
||||||
$query->useFilters($this->filters);
|
$query->useFilters($this->filters);
|
||||||
|
|
||||||
|
$useSelection = 'selection' === $target;
|
||||||
|
|
||||||
|
if ($batchAction['isGlobal']) {
|
||||||
|
$result = $callback($query, $useSelection, $entityManager);
|
||||||
|
|
||||||
|
if ($result instanceof Response) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->redirect($request->query->get('redirectTo'));
|
||||||
|
}
|
||||||
if ('selection' === $target) {
|
if ('selection' === $target) {
|
||||||
$isSelection = true;
|
|
||||||
$pager = $query->paginate($page, $configuration->getMaxPerPage($context));
|
$pager = $query->paginate($page, $configuration->getMaxPerPage($context));
|
||||||
} else {
|
} else {
|
||||||
$isSelection = false;
|
|
||||||
$pager = $query->find();
|
$pager = $query->find();
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($pager as $key => $entity) {
|
foreach ($pager as $key => $entity) {
|
||||||
if (($isSelection && isset($items[$key + 1])) || !$isSelection) {
|
if (($useSelection && isset($items[$key + 1])) || !$useSelection) {
|
||||||
$callback($entity, $entityManager);
|
$callback($entity, $entityManager);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -135,7 +135,25 @@ class CrudConfiguration
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setBatchAction(string $page, string $action, string $label, callable $callback): self
|
public function setGlobalBatchAction(
|
||||||
|
string $page,
|
||||||
|
string $action,
|
||||||
|
string $label,
|
||||||
|
callable $callback
|
||||||
|
): self
|
||||||
|
{
|
||||||
|
$this->setBatchAction($page, $action, $name);
|
||||||
|
$this->batchActions[$page][$action]['isGlobal'] = true;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setBatchAction(
|
||||||
|
string $page,
|
||||||
|
string $action,
|
||||||
|
string $label,
|
||||||
|
callable $callback
|
||||||
|
): self
|
||||||
{
|
{
|
||||||
if (!isset($this->batchActions[$page])) {
|
if (!isset($this->batchActions[$page])) {
|
||||||
$this->batchActions[$page] = [];
|
$this->batchActions[$page] = [];
|
||||||
|
@ -144,6 +162,7 @@ class CrudConfiguration
|
||||||
$this->batchActions[$page][$action] = [
|
$this->batchActions[$page][$action] = [
|
||||||
'label' => $label,
|
'label' => $label,
|
||||||
'callback' => $callback,
|
'callback' => $callback,
|
||||||
|
'isGlobal' => false
|
||||||
];
|
];
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|
|
@ -8,6 +8,17 @@ module.exports = () => {
|
||||||
const form = $('#form-batch')
|
const form = $('#form-batch')
|
||||||
|
|
||||||
form.submit((e) => {
|
form.submit((e) => {
|
||||||
|
const select = document.querySelector('#form-batch-action')
|
||||||
|
const options = select.querySelectorAll('#form-batch-action option')
|
||||||
|
let doPrevent = true
|
||||||
|
|
||||||
|
options.forEach((option) => {
|
||||||
|
if (option.value === select.value && option.getAttribute('data-isglobal') === 'true') {
|
||||||
|
doPrevent = false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if (doPrevent) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
|
||||||
const route = form.attr('action')
|
const route = form.attr('action')
|
||||||
|
@ -19,5 +30,6 @@ module.exports = () => {
|
||||||
.always(() => {
|
.always(() => {
|
||||||
document.location.reload()
|
document.location.reload()
|
||||||
})
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -283,15 +283,18 @@
|
||||||
|
|
||||||
{% if configuration.hasBatchAction(context) %}
|
{% if configuration.hasBatchAction(context) %}
|
||||||
<div class="mb-2">
|
<div class="mb-2">
|
||||||
<form class="form-inline" action="{{ path(configuration.pageRoute('batch'), {page: pager.currentPageNumber}|merge(configuration.pageRouteParams('batch'))) }}" id="form-batch" method="POST">
|
<form class="form-inline" action="{{ path(configuration.pageRoute('batch'), {
|
||||||
|
page: pager.currentPageNumber,
|
||||||
|
redirectTo: app.request.uri
|
||||||
|
}|merge(configuration.pageRouteParams('batch'))) }}" id="form-batch" method="POST">
|
||||||
<select class="form-control my-1 mr-sm-2" name="batch[target]">
|
<select class="form-control my-1 mr-sm-2" name="batch[target]">
|
||||||
<option value="selection">{{ 'For selection'|trans }}</option>
|
<option value="selection">{{ 'For selection'|trans }}</option>
|
||||||
<option value="all">{{ 'For all items'|trans }}</option>
|
<option value="all">{{ 'For all items'|trans }}</option>
|
||||||
</select>
|
</select>
|
||||||
<select class="form-control my-1 mr-sm-2" name="batch[action]">
|
<select class="form-control my-1 mr-sm-2" id="form-batch-action" name="batch[action]">
|
||||||
<option value=""></option>
|
<option value=""></option>
|
||||||
{% for action, conf in configuration.batchActions(context) %}
|
{% for action, conf in configuration.batchActions(context) %}
|
||||||
<option value="{{ action }}">
|
<option value="{{ action }}" {% if conf.isGlobal %}data-isglobal="true"{% endif %}>
|
||||||
{{ conf.label|trans }}
|
{{ conf.label|trans }}
|
||||||
</option>
|
</option>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
Loading…
Reference in a new issue