Added an option for mass creation of structured data.

This commit is contained in:
Jan Böhmer 2019-08-12 22:41:58 +02:00
parent 51be176418
commit 87e6f641c3
18 changed files with 177 additions and 24 deletions

View file

@ -1,6 +1,6 @@
security:
encoders:
App\Entity\User:
App\Entity\UserSystem\User:
algorithm: auto
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
@ -8,7 +8,7 @@ security:
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: App\Entity\User
class: App\Entity\UserSystem\User
property: name
firewalls:
dev:

View file

@ -33,7 +33,7 @@ namespace App\Controller\AdminPages;
use App\Entity\Attachments\AttachmentType;
use App\Form\BaseEntityAdminForm;
use App\Form\AdminPages\BaseEntityAdminForm;
use App\Services\EntityExporter;
use App\Services\EntityImporter;
use App\Services\StructuralElementRecursionHelper;

View file

@ -33,8 +33,9 @@ namespace App\Controller\AdminPages;
use App\Entity\Base\NamedDBElement;
use App\Entity\Base\StructuralDBElement;
use App\Form\BaseEntityAdminForm;
use App\Form\ImportType;
use App\Form\AdminPages\BaseEntityAdminForm;
use App\Form\AdminPages\ImportType;
use App\Form\AdminPages\MassCreationForm;
use App\Services\EntityExporter;
use App\Services\EntityImporter;
use App\Services\StructuralElementRecursionHelper;
@ -118,10 +119,30 @@ abstract class BaseAdminController extends AbstractController
}
}
//Mass creation form
$mass_creation_form = $this->createForm(MassCreationForm::class, ['entity_class' => $this->entity_class]);
$mass_creation_form->handleRequest($request);
if ($mass_creation_form->isSubmitted() && $mass_creation_form->isValid()) {
$data = $mass_creation_form->getData();
dump($data);
//Create entries based on input
$errors = $importer->massCreation($data['lines'], $this->entity_class, $data['parent']);
//Show errors to user:
foreach ($errors as $name => $error) {
/** @var $error ConstraintViolationList */
$this->addFlash('error', $name . ":" . $error);
}
}
return $this->render($this->twig_template, [
'entity' => $new_entity,
'form' => $form->createView(),
'import_form' => $import_form->createView()
'import_form' => $import_form->createView(),
'mass_creation_form' => $mass_creation_form->createView()
]);
}

View file

@ -34,8 +34,8 @@ namespace App\Controller\AdminPages;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Parts\Category;
use App\Form\BaseEntityAdminForm;
use App\Form\CategoryAdminForm;
use App\Form\AdminPages\BaseEntityAdminForm;
use App\Form\AdminPages\CategoryAdminForm;
use App\Services\EntityExporter;
use App\Services\EntityImporter;
use App\Services\StructuralElementRecursionHelper;

View file

@ -35,7 +35,7 @@ namespace App\Controller\AdminPages;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Devices\Device;
use App\Form\BaseEntityAdminForm;
use App\Form\AdminPages\BaseEntityAdminForm;
use App\Services\EntityExporter;
use App\Services\EntityImporter;
use App\Services\StructuralElementRecursionHelper;

View file

@ -35,7 +35,7 @@ namespace App\Controller\AdminPages;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Parts\Footprint;
use App\Form\BaseEntityAdminForm;
use App\Form\AdminPages\BaseEntityAdminForm;
use App\Services\EntityExporter;
use App\Services\EntityImporter;
use App\Services\StructuralElementRecursionHelper;

View file

@ -36,8 +36,8 @@ use App\Entity\Attachments\AttachmentType;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\Supplier;
use App\Form\BaseEntityAdminForm;
use App\Form\CompanyForm;
use App\Form\AdminPages\BaseEntityAdminForm;
use App\Form\AdminPages\CompanyForm;
use App\Services\EntityExporter;
use App\Services\EntityImporter;
use App\Services\StructuralElementRecursionHelper;

View file

@ -34,8 +34,8 @@ namespace App\Controller\AdminPages;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Parts\Storelocation;
use App\Form\BaseEntityAdminForm;
use App\Form\StorelocationAdminForm;
use App\Form\AdminPages\BaseEntityAdminForm;
use App\Form\AdminPages\StorelocationAdminForm;
use App\Services\EntityExporter;
use App\Services\EntityImporter;
use App\Services\StructuralElementRecursionHelper;

View file

@ -35,8 +35,8 @@ namespace App\Controller\AdminPages;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Parts\Supplier;
use App\Form\BaseEntityAdminForm;
use App\Form\CompanyForm;
use App\Form\AdminPages\BaseEntityAdminForm;
use App\Form\AdminPages\CompanyForm;
use App\Services\EntityExporter;
use App\Services\EntityImporter;
use App\Services\StructuralElementRecursionHelper;

View file

@ -32,7 +32,7 @@ namespace App\Controller;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Parts\Footprint;
use App\Entity\UserSystem\User;
use App\Form\BaseEntityAdminForm;
use App\Form\AdminPages\BaseEntityAdminForm;
use App\Form\UserAdminForm;
use App\Form\UserSettingsType;
use App\Services\EntityExporter;

View file

@ -29,7 +29,7 @@
*
*/
namespace App\Form;
namespace App\Form\AdminPages;
use App\Entity\Base\NamedDBElement;

View file

@ -29,10 +29,11 @@
*
*/
namespace App\Form;
namespace App\Form\AdminPages;
use App\Entity\Base\NamedDBElement;
use App\Form\AdminPages\BaseEntityAdminForm;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

View file

@ -29,10 +29,11 @@
*
*/
namespace App\Form;
namespace App\Form\AdminPages;
use App\Entity\Base\NamedDBElement;
use App\Form\AdminPages\BaseEntityAdminForm;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TelType;

View file

@ -29,7 +29,7 @@
*
*/
namespace App\Form;
namespace App\Form\AdminPages;
use App\Entity\Base\StructuralDBElement;
@ -71,7 +71,7 @@ class ImportType extends AbstractType
'disabled' => $disabled])
->add('csv_separator', TextType::class, ['data' => ';', 'label' => 'import.csv_separator',
'disabled' => $disabled]);
if($data['entity_class'] instanceof StructuralDBElement) {
if($entity instanceof StructuralDBElement) {
$builder->
add('parent', EntityType::class, ['class' => $data['entity_class'], 'choice_label' => 'full_path',
'attr' => ['class' => 'selectpicker', 'data-live-search' => true], 'required' => false,

View file

@ -0,0 +1,81 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 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 General Public License
* as published by the Free Software Foundation; either version 2
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Form\AdminPages;
use App\Entity\Base\StructuralDBElement;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Security\Core\Security;
class MassCreationForm extends AbstractType
{
protected $security;
public function __construct(Security $security)
{
$this->security = $security;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$data = $options['data'];
//Disable import if user is not allowed to create elements.
$entity = new $data['entity_class']();
$perm_name = "create";
$disabled = ! $this->security->isGranted($perm_name, $entity);
$builder
->add('lines', TextareaType::class, ['data' => '', 'label' => 'mass_creation.lines',
'disabled' => $disabled, 'required' => true,
'attr' => ['placeholder' => 'mass_creation.lines.placeholder', 'rows' => 10]]);
if ($entity instanceof StructuralDBElement) {
$builder->
add('parent', EntityType::class, ['class' => $data['entity_class'], 'choice_label' => 'full_path',
'attr' => ['class' => 'selectpicker', 'data-live-search' => true], 'required' => false,
'label' => 'parent.label', 'disabled' => $disabled]);
}
$builder
//Buttons
->add('create', SubmitType::class, ['label' => 'mass_creation.btn', 'disabled' => $disabled]);
}
}

View file

@ -29,10 +29,11 @@
*
*/
namespace App\Form;
namespace App\Form\AdminPages;
use App\Entity\Base\NamedDBElement;
use App\Form\AdminPages\BaseEntityAdminForm;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

View file

@ -32,6 +32,7 @@
namespace App\Services;
use App\Entity\Base\NamedDBElement;
use App\Entity\Base\StructuralDBElement;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\MakerBundle\Str;
@ -65,6 +66,47 @@ class EntityImporter
]);
}
/**
* Creates many entries at once, based on a (text) list of names.
*
* @param string $lines The list of names seperated by \n
* @param string $class_name The name of the class for which the entities should be created
* @param StructuralDBElement|null $parent The element which will be used as parent element for new elements.
* @return array An associative array containing an ConstraintViolationList and the entity name as key are returned,
* if an error happened during validation. When everything was successfull, the array should be empty.
*/
public function massCreation(string $lines, string $class_name, ?StructuralDBElement $parent) : array
{
//Expand every line to a single entry:
$names = explode("\n", $lines);
$errors = array();
foreach ($names as $name) {
$name = trim($name);
/** @var $entity StructuralDBElement */
//Create new element with given name
$entity = new $class_name();
$entity->setName($name);
$entity->setParent($parent);
//Validate entity
$tmp = $this->validator->validate($entity);
//If no error occured, write entry to DB:
if (count($tmp) === 0) {
$this->em->persist($entity);
} else { //Otherwise log error
dump($tmp);
$errors[$entity->getFullPath()] = $tmp;
}
}
//Save changes to database
$this->em->flush();
return $errors;
}
/**
* This methods deserializes the given file and saves it database.
* The imported elements will be checked (validated) before written to database.
@ -96,7 +138,7 @@ class EntityImporter
$tmp = $this->validator->validate($entity);
//When no validation error occured, persist entity to database (cascade must be set in entity)
if ($tmp === null) {
if (count($errors) === 0) {
$this->em->persist($entity);
} else { //Log validation errors to global log.
$errors[$entity->getFullPath()] = $tmp;

View file

@ -45,6 +45,7 @@
<li class="nav-item"><a data-toggle="tab" class="link-anchor nav-link" href="#export">{% trans %}export.label{% endtrans %}</a> </li>
{% else %}
<li class="nav-item"><a data-toggle="tab" class="link-anchor nav-link" href="#import_export">{% trans %}import_export.label{% endtrans %}</a> </li>
<li class="nav-item"><a data-toggle="tab" class="link-anchor nav-link" href="#mass_creation">{% trans %}mass_creation.label{% endtrans %}</a></li>
{% endif %}
</ul>
@ -148,6 +149,11 @@
</fieldset>
</div>
<div id="mass_creation" class="tab-pane fade">
<span class="text-muted">{% trans %}mass_creation.help{% endtrans %}</span>
{{ form(mass_creation_form) }}
</div>
{% endif %}