diff --git a/src/Controller/PartController.php b/src/Controller/PartController.php index b45f1fab..dc5b1bf8 100644 --- a/src/Controller/PartController.php +++ b/src/Controller/PartController.php @@ -31,6 +31,7 @@ use App\Entity\Parts\PartLot; use App\Entity\Parts\Storelocation; use App\Entity\Parts\Supplier; use App\Entity\PriceInformations\Orderdetail; +use App\Entity\ProjectSystem\Project; use App\Exceptions\AttachmentDownloadException; use App\Form\Part\PartBaseType; use App\Services\Attachments\AttachmentSubmitHandler; @@ -40,10 +41,12 @@ use App\Services\LogSystem\HistoryHelper; use App\Services\LogSystem\TimeTravel; use App\Services\Parameters\ParameterExtractor; use App\Services\Parts\PricedetailHelper; +use App\Services\ProjectSystem\ProjectBuildPartHelper; use DateTime; use Doctrine\ORM\EntityManagerInterface; use Exception; use Omines\DataTablesBundle\DataTableFactory; +use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -203,14 +206,26 @@ class PartController extends AbstractController /** * @Route("/new", name="part_new") * @Route("/{id}/clone", name="part_clone") + * @Route("/new_build_part/{project_id}", name="part_new_build_part") + * @ParamConverter("part", options={"id" = "id"}) + * @ParamConverter("project", options={"id" = "project_id"}) */ public function new(Request $request, EntityManagerInterface $em, TranslatorInterface $translator, - AttachmentSubmitHandler $attachmentSubmitHandler, ?Part $part = null): Response + AttachmentSubmitHandler $attachmentSubmitHandler, ProjectBuildPartHelper $projectBuildPartHelper, + ?Part $part = null, ?Project $project = null): Response { - if (null === $part) { - $new_part = new Part(); - } else { + + if ($part) { //Clone part $new_part = clone $part; + } else if ($project) { //Initialize a new part for a build part from the given project + //Ensure that the project has not already a build part + if ($project->getBuildPart() !== null) { + $this->addFlash('error', 'part.new_build_part.error.build_part_already_exists'); + return $this->redirectToRoute('part_edit', ['id' => $project->getBuildPart()->getID()]); + } + $new_part = $projectBuildPartHelper->getPartInitialization($project); + } else { //Create an empty part from scratch + $new_part = new Part(); } $this->denyAccessUnlessGranted('create', $new_part); @@ -280,6 +295,11 @@ class PartController extends AbstractController $em->flush(); $this->addFlash('success', 'part.created_flash'); + //If a redirect URL was given, redirect there + if ($request->query->get('_redirect')) { + return $this->redirect($request->query->get('_redirect')); + } + //Redirect to clone page if user wished that... //@phpstan-ignore-next-line if ('save_and_clone' === $form->getClickedButton()->getName()) { diff --git a/src/Entity/ProjectSystem/Project.php b/src/Entity/ProjectSystem/Project.php index 26a3f7ee..f51d34b5 100644 --- a/src/Entity/ProjectSystem/Project.php +++ b/src/Entity/ProjectSystem/Project.php @@ -66,10 +66,10 @@ class Project extends AbstractStructuralDBElement /** * @var string The current status of the project - * @ORM\Column(type="string", length=64) - * @Assert\Choice({"draft","planning","in_production","finished","archived", ""}) + * @ORM\Column(type="string", length=64, nullable=true) + * @Assert\Choice({"draft","planning","in_production","finished","archived"}) */ - protected string $status; + protected ?string $status = null; /** @@ -237,7 +237,7 @@ class Project extends AbstractStructuralDBElement /** * @return string */ - public function getStatus(): string + public function getStatus(): ?string { return $this->status; } @@ -245,7 +245,7 @@ class Project extends AbstractStructuralDBElement /** * @param string $status */ - public function setStatus(string $status): void + public function setStatus(?string $status): void { $this->status = $status; } diff --git a/src/Form/AdminPages/ProjectAdminForm.php b/src/Form/AdminPages/ProjectAdminForm.php index e9938933..3547d094 100644 --- a/src/Form/AdminPages/ProjectAdminForm.php +++ b/src/Form/AdminPages/ProjectAdminForm.php @@ -34,9 +34,9 @@ class ProjectAdminForm extends BaseEntityAdminForm { $builder->add('description', RichTextEditorType::class, [ 'required' => false, - 'empty_data' => '', 'label' => 'part.edit.description', 'mode' => 'markdown-single_line', + 'empty_data' => '', 'attr' => [ 'placeholder' => 'part.edit.description.placeholder', 'rows' => 2, @@ -51,6 +51,7 @@ class ProjectAdminForm extends BaseEntityAdminForm ], 'label' => 'project.edit.status', 'required' => false, + 'empty_data' => '', 'choices' => [ 'project.status.draft' => 'draft', 'project.status.planning' => 'planning', diff --git a/src/Services/ProjectSystem/ProjectBuildPartHelper.php b/src/Services/ProjectSystem/ProjectBuildPartHelper.php new file mode 100644 index 00000000..5ec1537b --- /dev/null +++ b/src/Services/ProjectSystem/ProjectBuildPartHelper.php @@ -0,0 +1,34 @@ +setBuiltProject($project); + + //Set the name of the part to the name of the project + $part->setName($project->getName()); + + //Set the description of the part to the description of the project + $part->setDescription($project->getDescription()); + + //Add a tag to the part that indicates that it is a build part + $part->setTags('project-build'); + + return $part; + } +} \ No newline at end of file diff --git a/templates/AdminPages/DeviceAdmin.html.twig b/templates/AdminPages/DeviceAdmin.html.twig index b1375a89..17502956 100644 --- a/templates/AdminPages/DeviceAdmin.html.twig +++ b/templates/AdminPages/DeviceAdmin.html.twig @@ -1,5 +1,7 @@ {% extends "AdminPages/EntityAdminBase.html.twig" %} +{# @var entity App\Entity\ProjectSystem\Project #} + {% block card_title %} {% trans %}device.caption{% endtrans %} {% endblock %} @@ -19,6 +21,21 @@ {% block additional_controls %} {{ form_row(form.description) }} {{ form_row(form.status) }} + {% if entity.id %} +
{% trans %}project.edit.associated_build.hint{% endtrans %}
+