mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-18 01:45:07 +02:00
Added a 'clone part' function.
This commit is contained in:
parent
33631f16cf
commit
10f39b7f45
7 changed files with 87 additions and 16 deletions
|
@ -121,7 +121,38 @@ class PartController extends AbstractController
|
|||
}
|
||||
|
||||
|
||||
return $this->render('new_part.html.twig',
|
||||
return $this->render('Parts/new_part.html.twig',
|
||||
[
|
||||
"part" => $new_part,
|
||||
"form" => $form->createView()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/part/{id}/clone", name="part_clone")
|
||||
*
|
||||
*/
|
||||
public function clone(Part $part, Request $request, EntityManagerInterface $em, TranslatorInterface $translator)
|
||||
{
|
||||
|
||||
/** @var Part $new_part */
|
||||
$new_part = clone($part);
|
||||
|
||||
$this->denyAccessUnlessGranted('create', $new_part);
|
||||
|
||||
$form = $this->createForm(PartType::class, $new_part);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$em->persist($new_part);
|
||||
$em->flush();
|
||||
$this->addFlash('success', $translator->trans('part.created_flash'));
|
||||
return $this->redirectToRoute('part_edit',['id' => $new_part->getID()]);
|
||||
}
|
||||
|
||||
|
||||
return $this->render('Parts/new_part.html.twig',
|
||||
[
|
||||
"part" => $new_part,
|
||||
"form" => $form->createView()
|
||||
|
|
|
@ -56,6 +56,7 @@ abstract class DBElement
|
|||
return (int) $this->id;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the ID as an string, defined by the element class.
|
||||
* This should have a form like P000014, for a part with ID 14.
|
||||
|
@ -63,4 +64,10 @@ abstract class DBElement
|
|||
*/
|
||||
abstract public function getIDString() : string;
|
||||
|
||||
public function __clone()
|
||||
{
|
||||
//Set ID to null, so that an new entry is created
|
||||
$this->id = null;
|
||||
}
|
||||
|
||||
}
|
|
@ -90,6 +90,16 @@ class EntityURLGenerator
|
|||
throw new EntityNotSupported('The given entity is not supported yet!');
|
||||
}
|
||||
|
||||
public function cloneURL($entity) : string
|
||||
{
|
||||
if($entity instanceof Part)
|
||||
{
|
||||
return $this->urlGenerator->generate('part_clone', ['id' => $entity->getID()]);
|
||||
}
|
||||
|
||||
throw new EntityNotSupported('The given entity is not supported yet!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an HTML link to the info page about the given entity.
|
||||
* @param $entity mixed The entity for which the info link should be generated.
|
||||
|
|
|
@ -68,6 +68,8 @@ class AppExtension extends AbstractExtension
|
|||
return $this->entityURLGenerator->editURL($entity);
|
||||
case 'create':
|
||||
return $this->entityURLGenerator->createURL($entity);
|
||||
case 'clone':
|
||||
return $this->entityURLGenerator->cloneURL($entity);
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException('method is not supported!');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue