Show messages, when an element can not be deleted.

Fixes issue #43.
This commit is contained in:
Jan Böhmer 2020-05-17 21:52:47 +02:00
parent be47680b60
commit 8951940be1
6 changed files with 973 additions and 888 deletions

View file

@ -43,10 +43,14 @@ declare(strict_types=1);
namespace App\Controller\AdminPages; namespace App\Controller\AdminPages;
use App\DataTables\LogDataTable; use App\DataTables\LogDataTable;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Base\AbstractNamedDBElement; use App\Entity\Base\AbstractNamedDBElement;
use App\Entity\Base\AbstractPartsContainingDBElement;
use App\Entity\Base\AbstractStructuralDBElement; use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\Base\PartsContainingRepositoryInterface; use App\Entity\Base\PartsContainingRepositoryInterface;
use App\Entity\LabelSystem\LabelProfile; use App\Entity\LabelSystem\LabelProfile;
use App\Entity\PriceInformations\Currency;
use App\Entity\UserSystem\Group;
use App\Entity\UserSystem\User; use App\Entity\UserSystem\User;
use App\Events\SecurityEvent; use App\Events\SecurityEvent;
use App\Events\SecurityEvents; use App\Events\SecurityEvents;
@ -385,6 +389,35 @@ abstract class BaseAdminController extends AbstractController
if ($this->isCsrfTokenValid('delete'.$entity->getId(), $request->request->get('_token'))) { if ($this->isCsrfTokenValid('delete'.$entity->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager(); $entityManager = $this->getDoctrine()->getManager();
//Check if we can delete the part (it must not contain Parts)
if ($entity instanceof AbstractPartsContainingDBElement) {
/** @var AbstractPartsContainingRepository $repo */
$repo = $this->entityManager->getRepository($this->entity_class);
if ($repo->getPartsCount($entity) > 0) {
$this->addFlash('error', 'entity.delete.must_not_contain_parts');
return $this->redirectToRoute($this->route_base.'_new');
}
} elseif ($entity instanceof AttachmentType) {
if ($entity->getAttachmentsForType()->count() > 0) {
$this->addFlash('error', 'entity.delete.must_not_contain_attachments');
return $this->redirectToRoute($this->route_base.'_new');
}
} elseif ($entity instanceof Currency) {
if ($entity->getPricedetails()->count() > 0) {
$this->addFlash('error', 'entity.delete.must_not_contain_prices');
return $this->redirectToRoute($this->route_base.'_new');
}
} elseif ($entity instanceof Group) {
if ($entity->getUsers()->count() > 0) {
$this->addFlash('error', 'entity.delete.must_not_contain_users');
return $this->redirectToRoute($this->route_base.'_new');
}
} elseif ($entity instanceof User) {
//TODO: Find a better solution
$this->addFlash('error', 'Currently it is not possible to delete a user, as this would break the log... This will be implemented later...');
return $this->redirectToRoute($this->route_base.'_new');
}
//Check if we need to remove recursively //Check if we need to remove recursively
if ($entity instanceof AbstractStructuralDBElement && $request->get('delete_recursive', false)) { if ($entity instanceof AbstractStructuralDBElement && $request->get('delete_recursive', false)) {
$recursionHelper->delete($entity, false); $recursionHelper->delete($entity, false);

View file

@ -91,7 +91,7 @@ class AttachmentType extends AbstractStructuralDBElement
*/ */
public function getAttachmentsForType(): Collection public function getAttachmentsForType(): Collection
{ {
return $this->attachments; return $this->attachments_with_type;
} }
/** /**

View file

@ -45,6 +45,7 @@ namespace App\Entity\PriceInformations;
use App\Entity\Attachments\CurrencyAttachment; use App\Entity\Attachments\CurrencyAttachment;
use App\Entity\Base\AbstractStructuralDBElement; use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\Parameters\CurrencyParameter; use App\Entity\Parameters\CurrencyParameter;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
@ -103,6 +104,25 @@ class Currency extends AbstractStructuralDBElement
*/ */
protected $parameters; protected $parameters;
/** @var Collection<int, Pricedetail>
* @ORM\OneToMany(targetEntity="App\Entity\PriceInformations\Pricedetail", mappedBy="currency")
*/
protected $pricedetails;
public function __construct()
{
$this->pricedetails = new ArrayCollection();
parent::__construct();
}
/**
* @return Collection
*/
public function getPricedetails(): Collection
{
return $this->pricedetails;
}
/** /**
* Returns the 3 letter ISO code of this currency. * Returns the 3 letter ISO code of this currency.
* *

View file

@ -82,7 +82,7 @@ class Pricedetail extends AbstractDBElement implements TimeStampableInterface
/** /**
* @var ?Currency The currency used for the current price information. * @var ?Currency The currency used for the current price information.
* If this is null, the global base unit is assumed. * If this is null, the global base unit is assumed.
* @ORM\ManyToOne(targetEntity="Currency") * @ORM\ManyToOne(targetEntity="Currency", inversedBy="pricedetails")
* @ORM\JoinColumn(name="id_currency", referencedColumnName="id", nullable=true) * @ORM\JoinColumn(name="id_currency", referencedColumnName="id", nullable=true)
* @Selectable() * @Selectable()
*/ */

View file

@ -47,6 +47,7 @@ use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\Parameters\GroupParameter; use App\Entity\Parameters\GroupParameter;
use App\Security\Interfaces\HasPermissionsInterface; use App\Security\Interfaces\HasPermissionsInterface;
use App\Validator\Constraints\ValidPermission; use App\Validator\Constraints\ValidPermission;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
@ -102,10 +103,12 @@ class Group extends AbstractStructuralDBElement implements HasPermissionsInterfa
*/ */
protected $parameters; protected $parameters;
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
$this->permissions = new PermissionsEmbed(); $this->permissions = new PermissionsEmbed();
$this->users = new ArrayCollection();
} }
/** /**
@ -136,4 +139,9 @@ class Group extends AbstractStructuralDBElement implements HasPermissionsInterfa
{ {
return $this->permissions; return $this->permissions;
} }
public function getUsers(): Collection
{
return $this->users;
}
} }

File diff suppressed because it is too large Load diff