Added basic possibility to change the manufacturer, footprint, category and part unit of multiple parts.

This commit is contained in:
Jan Böhmer 2020-05-24 18:26:10 +02:00
parent 6f6ac0f128
commit ca74dd84f0
4 changed files with 195 additions and 2 deletions

View file

@ -74,6 +74,7 @@ class PartListsController extends AbstractController
$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');
@ -84,7 +85,7 @@ class PartListsController extends AbstractController
$this->addFlash('error', 'part.table.actions.no_params_given');
} else {
$parts = $actionHandler->idStringToArray($ids);
$actionHandler->handleAction($action, $parts, null);
$actionHandler->handleAction($action, $parts, $target ? (int) $target : null);
//Save changes
$this->entityManager->flush();

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

@ -21,6 +21,10 @@
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;
@ -77,6 +81,22 @@ final class PartsTableActionHandler
$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 . ')');

View file

@ -8,10 +8,19 @@
<div class="d-none" id="select_panel">
<p><span id="select_count"></span> elements selected!</p>
<select name="action">
<select class="selectpicker" name="action" id="select_action" onchange="updateTargetSelect()">
<option value="favorite">Favorite</option>
<option value="unfavorite">Unfavorite</option>
<option value="delete">Delete</option>
<option value="change_category" data-url="{{ path('select_category') }}">Change category</option>
<option value="change_footprint" data-url="{{ path('select_footprint') }}">Change footprint</option>
<option value="change_manufacturer" data-url="{{ path('select_manufacturer') }}">Change Manufacturer</option>
<option value="change_unit" data-url="{{ path('select_measurement_unit') }}">Change Unit</option>
</select>
<select class="" style="display: none;" data-live-search="true" name="target" id="select_target">
</select>
<button type="submit" class="btn btn-secondary">Submit</button>
@ -29,3 +38,46 @@
</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>