Added constraints for selectable attribute.

This commit is contained in:
Jan Böhmer 2019-08-12 21:47:25 +02:00
parent 408d98c6e1
commit 51be176418
12 changed files with 169 additions and 0 deletions

View file

@ -25,6 +25,7 @@ namespace App\Entity\Attachments;
use App\Entity\Base\DBElement;
use App\Entity\Base\NamedDBElement;
use App\Validator\Constraints\Selectable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Intl\Exception\NotImplementedException;
@ -61,6 +62,7 @@ abstract class Attachment extends NamedDBElement
* @var AttachmentType
* @ORM\ManyToOne(targetEntity="AttachmentType", inversedBy="attachments")
* @ORM\JoinColumn(name="type_id", referencedColumnName="id")
* @Selectable()
*/
protected $attachement_type;

View file

@ -275,6 +275,14 @@ abstract class StructuralDBElement extends AttachmentContainingDBElement
return $this->children;
}
/**
* @return bool
*/
public function isNotSelectable(): bool
{
return $this->not_selectable;
}
/******************************************************************************
*
* Setters
@ -317,6 +325,16 @@ abstract class StructuralDBElement extends AttachmentContainingDBElement
return $this;
}
/**
* @param bool $not_selectable
* @return StructuralDBElement
*/
public function setNotSelectable(bool $not_selectable): StructuralDBElement
{
$this->not_selectable = $not_selectable;
return $this;
}
public function clearChildren() : self
{
$this->children = new ArrayCollection();

View file

@ -66,6 +66,7 @@ use App\Entity\Attachments\AttachmentContainingDBElement;
use App\Entity\Devices\Device;
use App\Entity\PriceInformations\Orderdetail;
use App\Security\Annotations\ColumnSecurity;
use App\Validator\Constraints\Selectable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
@ -89,6 +90,7 @@ class Part extends AttachmentContainingDBElement
* @var Category
* @ORM\ManyToOne(targetEntity="Category", inversedBy="parts")
* @ORM\JoinColumn(name="id_category", referencedColumnName="id")
* @Selectable()
*/
protected $category;
@ -98,6 +100,7 @@ class Part extends AttachmentContainingDBElement
* @ORM\JoinColumn(name="id_footprint", referencedColumnName="id")
*
* @ColumnSecurity(prefix="footprint", type="object")
* @Selectable()
*/
protected $footprint;
@ -107,6 +110,7 @@ class Part extends AttachmentContainingDBElement
* @ORM\JoinColumn(name="id_manufacturer", referencedColumnName="id")
*
* @ColumnSecurity(prefix="manufacturer", type="object")
* @Selectable()
*/
protected $manufacturer;

View file

@ -35,6 +35,7 @@ namespace App\Entity\Parts;
use App\Entity\Base\DBElement;
use App\Entity\Base\NamedDBElement;
use App\Entity\Base\TimestampTrait;
use App\Validator\Constraints\Selectable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
@ -74,6 +75,7 @@ class PartLot extends DBElement
* @var Storelocation The storelocation of this lot
* @ORM\ManyToOne(targetEntity="Storelocation")
* @ORM\JoinColumn(name="id_store_location", referencedColumnName="id")
* @Selectable()
*/
protected $storage_location;

View file

@ -63,6 +63,7 @@ namespace App\Entity\Parts;
use App\Entity\Base\Company;
use App\Entity\PriceInformations\Currency;
use App\Validator\Constraints\Selectable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
@ -95,6 +96,7 @@ class Supplier extends Company
* Set to null, to use global base currency.
* @ORM\ManyToOne(targetEntity="App\Entity\PriceInformations\Currency")
* @ORM\JoinColumn(name="default_currency_id", referencedColumnName="id", nullable=true)
* @Selectable()
*/
protected $default_currency;

View file

@ -62,6 +62,7 @@ declare(strict_types=1);
namespace App\Entity\PriceInformations;
use App\Entity\Base\DBElement;
use App\Validator\Constraints\Selectable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
@ -94,6 +95,7 @@ class Pricedetail extends DBElement
* If this is null, the global base unit is assumed.
* @ORM\ManyToOne(targetEntity="Currency")
* @ORM\JoinColumn(name="id_currency", referencedColumnName="id", nullable=true)
* @Selectable()
*/
protected $currency;

View file

@ -63,6 +63,7 @@ namespace App\Entity\UserSystem;
use App\Entity\Base\NamedDBElement;
use App\Security\Interfaces\HasPermissionsInterface;
use App\Validator\Constraints\Selectable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
@ -156,6 +157,7 @@ class User extends NamedDBElement implements UserInterface, HasPermissionsInterf
* @var Group|null the group this user belongs to
* @ORM\ManyToOne(targetEntity="Group", inversedBy="users", fetch="EAGER")
* @ORM\JoinColumn(name="group_id", referencedColumnName="id")
* @Selectable()
*/
protected $group;

View file

@ -37,6 +37,7 @@ use App\Entity\Base\StructuralDBElement;
use FOS\CKEditorBundle\Form\Type\CKEditorType;
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\ResetType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
@ -69,6 +70,10 @@ class BaseEntityAdminForm extends AbstractType
'attr' => ['class' => 'selectpicker', 'data-live-search' => true], 'required' => false, 'label' => 'parent.label',
'disabled' => !$this->security->isGranted($is_new ? 'create' : 'move', $entity), ])
->add('not_selectable', CheckboxType::class, ['required' => false,
'label' => 'not_selectable.label', 'help' => 'not_selectable.help', 'label_attr'=> ['class' => 'checkbox-custom'],
'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity) ])
->add('comment', CKEditorType::class, ['required' => false,
'label' => 'comment.label', 'attr' => ['rows' => 4], 'help' => 'bbcode.hint',
'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity)]);

View file

@ -37,6 +37,7 @@ use Symfony\Bundle\MakerBundle\Str;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
/**
* The validator for the NoneOfItsChildren annotation.
@ -62,6 +63,11 @@ class NoneOfItsChildrenValidator extends ConstraintValidator
return;
}
//Check type of value. Validating only works for StructuralDBElements
if (!$value instanceof StructuralDBElement) {
throw new UnexpectedValueException($value, 'StructuralDBElement');
}
//Check if the object is assigned to itself
/** @var StructuralDBElement $entity */
$entity = $this->context->getObject();

View file

@ -0,0 +1,47 @@
<?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\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* If a property is marked with this constraint, the choosen value (of type StructuralDBElement)
* must NOT be marked as not selectable.
*
* @package App\Validator\Constraints
* @Annotation
*/
class Selectable extends Constraint
{
public $message = 'validator.isSelectable';
}

View file

@ -0,0 +1,78 @@
<?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\Validator\Constraints;
use App\Entity\Base\StructuralDBElement;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
/**
* The validator for the Selectable constraint.
* @package App\Validator\Constraints
*/
class SelectableValidator extends ConstraintValidator
{
/**
* Checks if the passed value is valid.
*
* @param mixed $value The value that should be validated
* @param \Symfony\Component\Validator\Constraint $constraint The constraint for the validation
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Selectable) {
throw new UnexpectedTypeException($constraint, Selectable::class);
}
// custom constraints should ignore null and empty values to allow
// other constraints (NotBlank, NotNull, etc.) take care of that
if (null === $value || '' === $value) {
return;
}
//Check type of value. Validating only works for StructuralDBElements
if (!$value instanceof StructuralDBElement) {
throw new UnexpectedValueException($value, 'StructuralDBElement');
}
//Check if the value is not selectable -> show error message then.
if ($value->isNotSelectable()) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ name }}', $value->getName())
->setParameter('{{ full_path }}', $value->getFullPath())
->addViolation();
}
}
}

View file

@ -68,6 +68,7 @@
{% if form.parent%}
{{ form_row(form.parent) }}
{% endif %}
{{ form_row(form.not_selectable) }}
{% block additional_controls %}{% endblock %}