|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
| 4 | + * |
| 5 | + * Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics) |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Affero General Public License as published |
| 9 | + * by the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +declare(strict_types=1); |
| 22 | + |
| 23 | + |
| 24 | +namespace App\EntityListeners; |
| 25 | + |
| 26 | +use App\Entity\Parts\Part; |
| 27 | +use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener; |
| 28 | +use Doctrine\ORM\Event\PreRemoveEventArgs; |
| 29 | + |
| 30 | +/** |
| 31 | + * If an part is deleted, this listener makes sure that all ProjectBOMEntries that reference this part, are updated |
| 32 | + * to not reference the part anymore, but instead store the part name in the name field. |
| 33 | + */ |
| 34 | +#[AsEntityListener(event: "preRemove", entity: Part::class)] |
| 35 | +class PartProjectBOMEntryUnlinkListener |
| 36 | +{ |
| 37 | + public function preRemove(Part $part, PreRemoveEventArgs $event): void |
| 38 | + { |
| 39 | + // Iterate over all ProjectBOMEntries that use this part and put the part name into the name field |
| 40 | + foreach ($part->getProjectBomEntries() as $bom_entry) { |
| 41 | + $old_name = $bom_entry->getName(); |
| 42 | + if ($old_name === null || trim($old_name) === '') { |
| 43 | + $bom_entry->setName($part->getName()); |
| 44 | + } else { |
| 45 | + $bom_entry->setName($old_name . ' (' . $part->getName() . ')'); |
| 46 | + } |
| 47 | + |
| 48 | + $old_comment = $bom_entry->getComment(); |
| 49 | + if ($old_comment === null || trim($old_comment) === '') { |
| 50 | + $bom_entry->setComment('Part was deleted: ' . $part->getName()); |
| 51 | + } else { |
| 52 | + $bom_entry->setComment($old_comment . "\n\n Part was deleted: " . $part->getName()); |
| 53 | + } |
| 54 | + |
| 55 | + //Remove the part reference |
| 56 | + $bom_entry->setPart(null); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments