Merge branch 'multi_action'

This commit is contained in:
Jan Böhmer 2020-05-24 20:20:52 +02:00
commit 6ed3d5524c
6 changed files with 457 additions and 7 deletions

View file

@ -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();

View file

@ -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,37 @@ 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');
$target = $request->request->get('target');
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, $target ? (int) $target : null);
//Save changes
$this->entityManager->flush();
$this->addFlash('success', 'part.table.actions.success');
}
return $this->redirect($redirect);
}
/**
* @Route("/category/{id}/parts", name="part_list_category")
*

View file

@ -0,0 +1,120 @@
<?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\Controller;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\MeasurementUnit;
use App\Services\Trees\NodesListBuilder;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @Route("/select_api")
* @package App\Controller
*/
class SelectAPIController extends AbstractController
{
private $nodesListBuilder;
private $translator;
public function __construct(NodesListBuilder $nodesListBuilder, TranslatorInterface $translator)
{
$this->nodesListBuilder = $nodesListBuilder;
$this->translator = $translator;
}
/**
* @Route("/category", name="select_category")
*/
public function category(): Response
{
return $this->getResponseForClass(Category::class);
}
/**
* @Route("/footprint", name="select_footprint")
*/
public function footprint(): Response
{
return $this->getResponseForClass(Footprint::class, true);
}
/**
* @Route("/manufacturer", name="select_manufacturer")
*/
public function manufacturer(): Response
{
return $this->getResponseForClass(Manufacturer::class, true);
}
/**
* @Route("/measurement_unit", name="select_measurement_unit")
*/
public function measurement_unit(): Response
{
return $this->getResponseForClass(MeasurementUnit::class, true);
}
protected function getResponseForClass(string $class, bool $include_empty = false): Response
{
$test_obj = new $class;
$this->denyAccessUnlessGranted('read', $test_obj);
$nodes = $this->nodesListBuilder->typeToNodesList($class);
$json = $this->buildJSONStructure($nodes);
if ($include_empty) {
array_unshift($json, [
'text' => '',
'value' => null,
'data-subtext' => $this->translator->trans('part_list.action.select_null'),
]);
}
return $this->json($json);
}
protected function buildJSONStructure(array $nodes_list): array
{
$entries = [];
foreach ($nodes_list as $node) {
/** @var AbstractStructuralDBElement $node */
$entry = [
'text' => str_repeat('&nbsp;&nbsp;&nbsp;', $node->getLevel()) . htmlspecialchars($node->getName()),
'value' => $node->getID(),
'data-subtext' => $node->getParent() ? $node->getParent()->getFullPath() : null,
];
$entries[] = $entry;
}
return $entries;
}
}

View file

@ -0,0 +1,123 @@
<?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\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\MeasurementUnit;
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;
case 'change_category':
$this->denyAccessUnlessGranted('category.edit', $part);
$part->setCategory($this->entityManager->find(Category::class, $target_id));
break;
case 'change_footprint':
$this->denyAccessUnlessGranted('footprint.edit', $part);
$part->setFootprint($target_id === null ? null : $this->entityManager->find(Footprint::class, $target_id));
break;
case 'change_manufacturer':
$this->denyAccessUnlessGranted('manufacturer.edit', $part);
$part->setManufacturer($target_id === null ? null : $this->entityManager->find(Manufacturer::class, $target_id));
break;
case 'change_unit':
$this->denyAccessUnlessGranted('unit.edit', $part);
$part->setPartUnit($target_id === null ? null : $this->entityManager->find(MeasurementUnit::class, $target_id));
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;
}
}
}

View file

@ -1,12 +1,90 @@
<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 mb-2" id="select_panel">
{# <span id="select_count"></span> #}
<span class="badge badge-secondary">{% trans with {'%count%': '<span id="select_count"></span>'} %}part_list.action.part_count{% endtrans %}</span>
<select class="selectpicker" name="action" id="select_action"
title="{% trans %}part_list.action.action.title{% endtrans %}" onchange="updateTargetSelect()" required>
<optgroup label="{% trans %}part_list.action.action.group.favorite{% endtrans %}">
<option {% if not is_granted('@parts.edit') %}disabled{% endif %} value="favorite">{% trans %}part_list.action.action.favorite{% endtrans %}</option>
<option {% if not is_granted('@parts.edit') %}disabled{% endif %} value="unfavorite">{% trans %}part_list.action.action.unfavorite{% endtrans %}</option>
</optgroup>
<optgroup label="{% trans %}part_list.action.action.group.change_field{% endtrans %}">
<option {% if not is_granted('@parts_category.edit') %}disabled{% endif %} value="change_category" data-url="{{ path('select_category') }}">{% trans %}part_list.action.action.change_category{% endtrans %}</option>
<option {% if not is_granted('@parts_footprint.edit') %}disabled{% endif %} value="change_footprint" data-url="{{ path('select_footprint') }}">{% trans %}part_list.action.action.change_footprint{% endtrans %}</option>
<option {% if not is_granted('@parts_manufacturer.edit') %}disabled{% endif %} value="change_manufacturer" data-url="{{ path('select_manufacturer') }}">{% trans %}part_list.action.action.change_manufacturer{% endtrans %}</option>
<option {% if not is_granted('@parts_unit.edit') %}disabled{% endif %} value="change_unit" data-url="{{ path('select_measurement_unit') }}">{% trans %}part_list.action.action.change_unit{% endtrans %}</option>
</optgroup>
<option {% if not is_granted('@parts.delete') %}disabled{% endif %} value="delete">{% trans %}part_list.action.action.delete{% endtrans %}</option>
</select>
<select class="" style="display: none;" data-live-search="true" name="target" id="select_target">
{# This is left empty, as this will be filled by Javascript #}
</select>
<button type="submit" class="btn btn-secondary">{% trans %}part_list.action.submit{% endtrans %}</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>
<script>
function updateOptions(selector, json)
{
var select = document.querySelector(selector);
//Clear options
select.innerHTML = null;
for(i=0; i<json.length; i++) {
var json_opt = json[i];
var opt = document.createElement('option');
opt.value = json_opt.value;
opt.innerHTML = json_opt.text;
if(json_opt['data-subtext']) {
opt.dataset.subtext = json_opt['data-subtext'];
}
select.appendChild(opt);
}
}
function updateTargetSelect() {
var element = document.querySelector('#select_action');
var selected = element.options[element.options.selectedIndex];
var url = selected.dataset.url;
if (url) {
fetch(url)
.then(response => response.json())
.then(data => updateOptions('#select_target', data))
.then(data => $('#select_target').selectpicker('refresh'));
} else {
$('#select_target').selectpicker('hide');
}
}
</script>

View file

@ -9111,5 +9111,77 @@ Element 3</target>
<target>Edit part</target>
</segment>
</unit>
<unit id="uAVyHXQ" name="part_list.action.action.title">
<segment>
<source>part_list.action.action.title</source>
<target>Select action</target>
</segment>
</unit>
<unit id="bAPfHqL" name="part_list.action.action.group.favorite">
<segment>
<source>part_list.action.action.group.favorite</source>
<target>Favorite status</target>
</segment>
</unit>
<unit id="P5wWUv3" name="part_list.action.action.favorite">
<segment>
<source>part_list.action.action.favorite</source>
<target>Favorite</target>
</segment>
</unit>
<unit id="sfOQX5T" name="part_list.action.action.unfavorite">
<segment>
<source>part_list.action.action.unfavorite</source>
<target>Unfavorite</target>
</segment>
</unit>
<unit id="xJa8HMi" name="part_list.action.action.group.change_field">
<segment>
<source>part_list.action.action.group.change_field</source>
<target>Change field</target>
</segment>
</unit>
<unit id="vzWZTcS" name="part_list.action.action.change_category">
<segment>
<source>part_list.action.action.change_category</source>
<target>Change category</target>
</segment>
</unit>
<unit id="o0usAp." name="part_list.action.action.change_footprint">
<segment>
<source>part_list.action.action.change_footprint</source>
<target>Change footprint</target>
</segment>
</unit>
<unit id="5KmoqAw" name="part_list.action.action.change_manufacturer">
<segment>
<source>part_list.action.action.change_manufacturer</source>
<target>Change manufacturer</target>
</segment>
</unit>
<unit id="3i7xim8" name="part_list.action.action.change_unit">
<segment>
<source>part_list.action.action.change_unit</source>
<target>Change part unit</target>
</segment>
</unit>
<unit id="CjaC2GZ" name="part_list.action.action.delete">
<segment>
<source>part_list.action.action.delete</source>
<target>Delete</target>
</segment>
</unit>
<unit id="TdvpOc5" name="part_list.action.submit">
<segment>
<source>part_list.action.submit</source>
<target>Submit</target>
</segment>
</unit>
<unit id="qKDo_nI" name="part_list.action.part_count">
<segment>
<source>part_list.action.part_count</source>
<target>%count% parts selected!</target>
</segment>
</unit>
</file>
</xliff>