Allow to mass generate labels via part table multiselect.

This commit is contained in:
Jan Böhmer 2023-01-15 23:36:22 +01:00
parent 82896ec0e5
commit cbe010ee67
5 changed files with 148 additions and 12 deletions

View file

@ -21,12 +21,15 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\Base\AbstractStructuralDBElement; use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\Contracts\NamedElementInterface;
use App\Entity\LabelSystem\LabelProfile;
use App\Entity\Parts\Category; use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint; use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer; use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\MeasurementUnit; use App\Entity\Parts\MeasurementUnit;
use App\Entity\ProjectSystem\Project; use App\Entity\ProjectSystem\Project;
use App\Services\Trees\NodesListBuilder; use App\Services\Trees\NodesListBuilder;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
@ -88,6 +91,48 @@ class SelectAPIController extends AbstractController
return $this->getResponseForClass(Project::class, false); return $this->getResponseForClass(Project::class, false);
} }
/**
* @Route("/label_profiles", name="select_label_profiles")
* @return Response
*/
public function labelProfiles(EntityManagerInterface $entityManager): Response
{
$this->denyAccessUnlessGranted('@labels.create_labels');
if ($this->isGranted('@labels.read_profiles')) {
$profiles = $entityManager->getRepository(LabelProfile::class)->getPartLabelProfiles();
$nodes = $this->buildJSONStructure($profiles);
} else {
$nodes = [];
}
//Add the empty option
$this->addEmptyNode($nodes, 'part_list.action.generate_label.empty');
return $this->json($nodes);
}
/**
* @Route("/label_profiles_lot", name="select_label_profiles_lot")
* @return Response
*/
public function labelProfilesLot(EntityManagerInterface $entityManager): Response
{
$this->denyAccessUnlessGranted('@labels.create_labels');
if ($this->isGranted('@labels.read_profiles')) {
$profiles = $entityManager->getRepository(LabelProfile::class)->getPartLotsLabelProfiles();
$nodes = $this->buildJSONStructure($profiles);
} else {
$nodes = [];
}
//Add the empty option
$this->addEmptyNode($nodes, 'part_list.action.generate_label.empty');
return $this->json($nodes);
}
protected function getResponseForClass(string $class, bool $include_empty = false): Response protected function getResponseForClass(string $class, bool $include_empty = false): Response
{ {
$test_obj = new $class(); $test_obj = new $class();
@ -98,27 +143,41 @@ class SelectAPIController extends AbstractController
$json = $this->buildJSONStructure($nodes); $json = $this->buildJSONStructure($nodes);
if ($include_empty) { if ($include_empty) {
array_unshift($json, [ $this->addEmptyNode($json);
'text' => '',
'value' => null,
'data-subtext' => $this->translator->trans('part_list.action.select_null'),
]);
} }
return $this->json($json); return $this->json($json);
} }
protected function addEmptyNode(array &$arr, string $text = 'part_list.action.select_null'): array
{
array_unshift($arr, [
'text' => $this->translator->trans($text),
'value' => null,
]);
return $arr;
}
protected function buildJSONStructure(array $nodes_list): array protected function buildJSONStructure(array $nodes_list): array
{ {
$entries = []; $entries = [];
foreach ($nodes_list as $node) { foreach ($nodes_list as $node) {
/** @var AbstractStructuralDBElement $node */ if ($node instanceof AbstractStructuralDBElement) {
$entry = [ $entry = [
'text' => str_repeat('   ', $node->getLevel()).htmlspecialchars($node->getName()), 'text' => str_repeat('   ', $node->getLevel()).htmlspecialchars($node->getName()),
'value' => $node->getID(), 'value' => $node->getID(),
'data-subtext' => $node->getParent() ? $node->getParent()->getFullPath() : null, 'data-subtext' => $node->getParent() ? $node->getParent()->getFullPath() : null,
]; ];
} elseif ($node instanceof NamedElementInterface) {
$entry = [
'text' => htmlspecialchars($node->getName()),
'value' => $node->getID(),
];
} else {
throw new \InvalidArgumentException('Invalid node type!');
}
$entries[] = $entry; $entries[] = $entry;
} }

View file

@ -109,4 +109,31 @@ class LabelProfileRepository extends NamedDBElementRepository
return $this->findBy(['options.supported_element' => $type], $order_by); return $this->findBy(['options.supported_element' => $type], $order_by);
} }
/**
* Returns all LabelProfiles that can be used for parts
* @return array
*/
public function getPartLabelProfiles(): array
{
return $this->getDropdownProfiles('part');
}
/**
* Returns all LabelProfiles that can be used for part lots
* @return array
*/
public function getPartLotsLabelProfiles(): array
{
return $this->getDropdownProfiles('part_lot');
}
/**
* Returns all LabelProfiles that can be used for storelocations
* @return array
*/
public function getStorelocationsLabelProfiles(): array
{
return $this->getDropdownProfiles('storelocation');
}
} }

View file

@ -25,6 +25,7 @@ use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer; use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\MeasurementUnit; use App\Entity\Parts\MeasurementUnit;
use App\Entity\Parts\Part; use App\Entity\Parts\Part;
use App\Entity\Parts\PartLot;
use App\Repository\DBElementRepository; use App\Repository\DBElementRepository;
use App\Repository\PartRepository; use App\Repository\PartRepository;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
@ -80,6 +81,27 @@ final class PartsTableActionHandler
); );
} }
if ($action === 'generate_label' || $action === 'generate_label_lot') {
//For parts we can just use the comma separated part IDs
if ($action === 'generate_label') {
$targets = implode(',', array_map(static fn (Part $part) => $part->getID(), $selected_parts));
} else { //For lots we have to extract the part lots
$targets = implode(',', array_map(static function (Part $part) {
//We concat the lot IDs of every part with a comma (which are later concated with a comma too per part)
return implode(',', array_map(static fn (PartLot $lot) => $lot->getID(), $part->getPartLots()->toArray()));
}, $selected_parts));
}
return new RedirectResponse(
$this->urlGenerator->generate($target_id !== 0 && $target_id !== null ? 'label_dialog_profile' : 'label_dialog', [
'profile' => $target_id,
'target_id' => $targets,
'generate' => '1',
'target_type' => $action === 'generate_label_lot' ? 'part_lot' : 'part',
])
);
}
//Iterate over the parts and apply the action to it: //Iterate over the parts and apply the action to it:
foreach ($selected_parts as $part) { foreach ($selected_parts as $part) {

View file

@ -48,6 +48,10 @@
<option {% if not is_granted('@manufacturers.read') %}disabled{% endif %} value="change_manufacturer" data-url="{{ path('select_manufacturer') }}">{% trans %}part_list.action.action.change_manufacturer{% endtrans %}</option> <option {% if not is_granted('@manufacturers.read') %}disabled{% endif %} value="change_manufacturer" data-url="{{ path('select_manufacturer') }}">{% trans %}part_list.action.action.change_manufacturer{% endtrans %}</option>
<option {% if not is_granted('@measurement_units.read') %}disabled{% endif %} value="change_unit" data-url="{{ path('select_measurement_unit') }}">{% trans %}part_list.action.action.change_unit{% endtrans %}</option> <option {% if not is_granted('@measurement_units.read') %}disabled{% endif %} value="change_unit" data-url="{{ path('select_measurement_unit') }}">{% trans %}part_list.action.action.change_unit{% endtrans %}</option>
</optgroup> </optgroup>
<optgroup label="{% trans %}part_list.action.group.labels{% endtrans %}">
<option {% if not is_granted('@labels.create_labels') %}disabled{% endif %} value="generate_label_lot" data-url="{{ path('select_label_profiles_lot')}}">{% trans %}part_list.action.projects.generate_label_lot{% endtrans %}</option>
<option {% if not is_granted('@labels.create_labels') %}disabled{% endif %} value="generate_label" data-url="{{ path('select_label_profiles')}}">{% trans %}part_list.action.projects.generate_label{% endtrans %}</option>
</optgroup>
<optgroup label="{% trans %}part_list.action.group.projects{% endtrans %}"> <optgroup label="{% trans %}part_list.action.group.projects{% endtrans %}">
<option {% if not is_granted('@projects.read') %}disabled{% endif %} value="add_to_project" data-url="{{ path('select_project')}}">{% trans %}part_list.action.projects.add_to_project{% endtrans %}</option> <option {% if not is_granted('@projects.read') %}disabled{% endif %} value="add_to_project" data-url="{{ path('select_project')}}">{% trans %}part_list.action.projects.add_to_project{% endtrans %}</option>
</optgroup> </optgroup>

View file

@ -9316,7 +9316,7 @@ Element 3</target>
<unit id="EieqscF" name="part_list.action.select_null"> <unit id="EieqscF" name="part_list.action.select_null">
<segment> <segment>
<source>part_list.action.select_null</source> <source>part_list.action.select_null</source>
<target>No elements existing!</target> <target>Empty element</target>
</segment> </segment>
</unit> </unit>
<unit id="4IH1qgL" name="part_list.action.delete-title"> <unit id="4IH1qgL" name="part_list.action.delete-title">
@ -10267,5 +10267,29 @@ Element 3</target>
<target>IC logos</target> <target>IC logos</target>
</segment> </segment>
</unit> </unit>
<unit id="9a9Yu9u" name="part_list.action.group.labels">
<segment>
<source>part_list.action.group.labels</source>
<target>Labels</target>
</segment>
</unit>
<unit id="gaFZMnW" name="part_list.action.projects.generate_label">
<segment>
<source>part_list.action.projects.generate_label</source>
<target>Generate labels (for parts)</target>
</segment>
</unit>
<unit id="cKJeBZl" name="part_list.action.projects.generate_label_lot">
<segment>
<source>part_list.action.projects.generate_label_lot</source>
<target>Generate labels (for part lots)</target>
</segment>
</unit>
<unit id="obOVNB9" name="part_list.action.generate_label.empty">
<segment>
<source>part_list.action.generate_label.empty</source>
<target>Empty label</target>
</segment>
</unit>
</file> </file>
</xliff> </xliff>