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);
|
||||
|
||||
if ($lastRequest !== null && !$request->isMethod('POST')) {
|
||||
if (null !== $lastRequest && !$request->isMethod('POST')) {
|
||||
$fakeRequest = Request::create(
|
||||
uri: $request->getUri(),
|
||||
method: 'POST',
|
||||
|
@ -284,16 +284,25 @@ abstract class CrudController extends AdminController
|
|||
|
||||
$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) {
|
||||
$isSelection = true;
|
||||
$pager = $query->paginate($page, $configuration->getMaxPerPage($context));
|
||||
} else {
|
||||
$isSelection = false;
|
||||
$pager = $query->find();
|
||||
}
|
||||
|
||||
foreach ($pager as $key => $entity) {
|
||||
if (($isSelection && isset($items[$key + 1])) || !$isSelection) {
|
||||
if (($useSelection && isset($items[$key + 1])) || !$useSelection) {
|
||||
$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])) {
|
||||
$this->batchActions[$page] = [];
|
||||
|
@ -144,6 +162,7 @@ class CrudConfiguration
|
|||
$this->batchActions[$page][$action] = [
|
||||
'label' => $label,
|
||||
'callback' => $callback,
|
||||
'isGlobal' => false
|
||||
];
|
||||
|
||||
return $this;
|
||||
|
|
|
@ -8,6 +8,17 @@ module.exports = () => {
|
|||
const form = $('#form-batch')
|
||||
|
||||
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()
|
||||
|
||||
const route = form.attr('action')
|
||||
|
@ -19,5 +30,6 @@ module.exports = () => {
|
|||
.always(() => {
|
||||
document.location.reload()
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -283,15 +283,18 @@
|
|||
|
||||
{% if configuration.hasBatchAction(context) %}
|
||||
<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]">
|
||||
<option value="selection">{{ 'For selection'|trans }}</option>
|
||||
<option value="all">{{ 'For all items'|trans }}</option>
|
||||
</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>
|
||||
{% for action, conf in configuration.batchActions(context) %}
|
||||
<option value="{{ action }}">
|
||||
<option value="{{ action }}" {% if conf.isGlobal %}data-isglobal="true"{% endif %}>
|
||||
{{ conf.label|trans }}
|
||||
</option>
|
||||
{% endfor %}
|
||||
|
|
Loading…
Reference in a new issue