mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-22 09:53:35 +02:00
Added simple possibility to favorite/unfavorite and delete multiple parts at once.
This commit is contained in:
parent
123b229638
commit
6f6ac0f128
4 changed files with 185 additions and 7 deletions
|
@ -529,6 +529,7 @@ class AjaxUI {
|
|||
'className': 'mr-2 btn-light',
|
||||
"text": "<i class='fa fa-cog'></i>"
|
||||
}],
|
||||
"select": $table.data('select') ?? false,
|
||||
"rowCallback": function( row, data, index ) {
|
||||
//Check if we have a level, then change color of this row
|
||||
if (data.level) {
|
||||
|
@ -564,6 +565,30 @@ class AjaxUI {
|
|||
$('#part-card-header').html(title.html());
|
||||
$(document).trigger('ajaxUI:dt_loaded');
|
||||
|
||||
|
||||
if($table.data('part_table')) {
|
||||
//@ts-ignore
|
||||
$('#dt').on( 'select.dt deselect.dt', function ( e, dt, items ) {
|
||||
let selected_elements = dt.rows({selected: true});
|
||||
let count = selected_elements.count();
|
||||
|
||||
if(count > 0) {
|
||||
$('#select_panel').removeClass('d-none');
|
||||
} else {
|
||||
$('#select_panel').addClass('d-none');
|
||||
}
|
||||
|
||||
$('#select_count').text(count);
|
||||
|
||||
let selected_ids_string = selected_elements.data().map(function(value, index) {
|
||||
return value['id']; }
|
||||
).join(",");
|
||||
|
||||
$('#select_ids').val(selected_ids_string);
|
||||
|
||||
} );
|
||||
}
|
||||
|
||||
//Attach event listener to update links after new page selection:
|
||||
$('#dt').on('draw.dt column-visibility.dt', function() {
|
||||
ajaxUI.registerLinks();
|
||||
|
|
|
@ -48,6 +48,7 @@ use App\Entity\Parts\Footprint;
|
|||
use App\Entity\Parts\Manufacturer;
|
||||
use App\Entity\Parts\Storelocation;
|
||||
use App\Entity\Parts\Supplier;
|
||||
use App\Services\Parts\PartsTableActionHandler;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Omines\DataTablesBundle\DataTableFactory;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
|
@ -65,6 +66,36 @@ class PartListsController extends AbstractController
|
|||
$this->entityManager = $entityManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/table/action", name="table_action", methods={"POST"})
|
||||
*/
|
||||
public function tableAction(Request $request, PartsTableActionHandler $actionHandler): Response
|
||||
{
|
||||
$redirect = $request->request->get('redirect_back');
|
||||
$ids = $request->request->get('ids');
|
||||
$action = $request->request->get('action');
|
||||
|
||||
if (!$this->isCsrfTokenValid('table_action', $request->request->get('_token'))) {
|
||||
$this->addFlash('error', 'csfr_invalid');
|
||||
return $this->redirect($redirect);
|
||||
}
|
||||
|
||||
if ($action === null || $ids === null) {
|
||||
$this->addFlash('error', 'part.table.actions.no_params_given');
|
||||
} else {
|
||||
$parts = $actionHandler->idStringToArray($ids);
|
||||
$actionHandler->handleAction($action, $parts, null);
|
||||
|
||||
//Save changes
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->addFlash('success', 'Done');
|
||||
}
|
||||
|
||||
|
||||
return $this->redirect($redirect);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/category/{id}/parts", name="part_list_category")
|
||||
*
|
||||
|
|
103
src/Services/Parts/PartsTableActionHandler.php
Normal file
103
src/Services/Parts/PartsTableActionHandler.php
Normal file
|
@ -0,0 +1,103 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Services\Parts;
|
||||
|
||||
|
||||
use App\Entity\Parts\Part;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
|
||||
final class PartsTableActionHandler
|
||||
{
|
||||
private $entityManager;
|
||||
private $security;
|
||||
|
||||
public function __construct(EntityManagerInterface $entityManager, Security $security)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->security = $security;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given array to an array of Parts
|
||||
* @param string $ids A comma separated list of Part IDs.
|
||||
* @return Part[]
|
||||
*/
|
||||
public function idStringToArray(string $ids): array
|
||||
{
|
||||
$id_array = explode(',', $ids);
|
||||
|
||||
$repo = $this->entityManager->getRepository(Part::class);
|
||||
return $repo->getElementsFromIDArray($id_array);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $action
|
||||
* @param Part[] $selected_parts
|
||||
* @param int|null $target_id
|
||||
*/
|
||||
public function handleAction(string $action, array $selected_parts, ?int $target_id): void
|
||||
{
|
||||
//Iterate over the parts and apply the action to it:
|
||||
foreach ($selected_parts as $part) {
|
||||
if (!$part instanceof Part) {
|
||||
throw new \InvalidArgumentException('$selected_parts must be an array of Part elements!');
|
||||
}
|
||||
|
||||
//We modify parts, so you have to have the permission to modify it
|
||||
$this->denyAccessUnlessGranted('edit', $part);
|
||||
|
||||
switch ($action) {
|
||||
case 'favorite':
|
||||
$part->setFavorite(true);
|
||||
break;
|
||||
case 'unfavorite':
|
||||
$part->setFavorite(false);
|
||||
break;
|
||||
case 'delete':
|
||||
$this->denyAccessUnlessGranted('delete', $part);
|
||||
$this->entityManager->remove($part);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \InvalidArgumentException('The given action is unknown! (' . $action . ')');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception unless the attributes are granted against the current authentication token and optionally
|
||||
* supplied subject.
|
||||
*
|
||||
* @throws AccessDeniedException
|
||||
*/
|
||||
private function denyAccessUnlessGranted($attributes, $subject = null, string $message = 'Access Denied.'): void
|
||||
{
|
||||
if (!$this->security->isGranted($attributes, $subject)) {
|
||||
$exception = new AccessDeniedException($message);
|
||||
$exception->setAttributes($attributes);
|
||||
$exception->setSubject($subject);
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +1,31 @@
|
|||
<form method="post" action="{{ url("table_action") }}">
|
||||
<input type="hidden" name="_token" value="{{ csrf_token('table_action') }}">
|
||||
|
||||
<div id="part_list" class="" data-datatable data-settings='{{ datatable_settings(datatable)|escape('html_attr') }}'>
|
||||
<div class="card-body">
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<h4>{% trans %}part_list.loading.caption{% endtrans %}</h4>
|
||||
<h6>{% trans %}part_list.loading.message{% endtrans %}</h6>
|
||||
<input type="hidden" name="redirect_back" value="{{ app.request.uri }}">
|
||||
|
||||
<input type="hidden" name="ids" id="select_ids" value="">
|
||||
|
||||
<div class="d-none" id="select_panel">
|
||||
<p><span id="select_count"></span> elements selected!</p>
|
||||
|
||||
<select name="action">
|
||||
<option value="favorite">Favorite</option>
|
||||
<option value="unfavorite">Unfavorite</option>
|
||||
<option value="delete">Delete</option>
|
||||
</select>
|
||||
|
||||
<button type="submit" class="btn btn-secondary">Submit</button>
|
||||
</div>
|
||||
|
||||
<div id="part_list" class="" data-select="true" data-part_table="true" data-datatable data-settings='{{ datatable_settings(datatable)|escape('html_attr') }}'>
|
||||
<div class="card-body">
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<h4>{% trans %}part_list.loading.caption{% endtrans %}</h4>
|
||||
<h6>{% trans %}part_list.loading.message{% endtrans %}</h6>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue