Added a 'clone part' function.

This commit is contained in:
Jan Böhmer 2019-03-19 19:53:23 +01:00
parent 33631f16cf
commit 10f39b7f45
7 changed files with 87 additions and 16 deletions

View file

@ -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()

View file

@ -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;
}
}

View file

@ -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.

View file

@ -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!');

View file

@ -1,4 +1,4 @@
{% extends "edit_part_info.html.twig" %}
{% extends "Parts/edit_part_info.html.twig" %}
{% block card_border %}border-success{% endblock %}
{% block card_type %}bg-success text-white{% endblock %}

View file

@ -68,8 +68,8 @@
<ul class="nav nav-tabs" id="partTab" role="tablist">
{% if part.comment is not empty %}
<li class="nav-item">
<a class="nav-link active" id="attachment-tab" data-toggle="tab"
href="#comment" role="tab" aria-controls="home" aria-selected="true">
<a class="nav-link active" id="comment-tab" data-toggle="tab"
href="#comment" role="tab">
<i class="fas fa-comment-alt fa-fw"></i>
{% trans %}comment.label{% endtrans %}
</a>
@ -77,25 +77,25 @@
{% endif %}
<li class="nav-item">
<a class="nav-link {% if part.comment is empty%} active{% endif %}" id="attachment-tab" data-toggle="tab"
href="#attachments" role="tab" aria-controls="home" aria-selected="true">
href="#attachments" role="tab">
<i class="fas fa-paperclip fa-fw"></i>
{% trans %}attachment.labelp{% endtrans %}
</a>
</li>
<li class="nav-item">
<a class="nav-link" id="supplier-tab" data-toggle="tab" href="#suppliers" role="tab" aria-controls="profile" aria-selected="false">
<a class="nav-link" id="supplier-tab" data-toggle="tab" href="#suppliers" role="tab">
<i class="fas fa-shopping-cart fa-fw"></i>
{% trans %}vendor.partinfo.shopping_infos{% endtrans %}
</a>
</li>
<li class="nav-item">
<a class="nav-link" id="history-tab" data-toggle="tab" href="#history" role="tab" aria-controls="contact" aria-selected="false">
<a class="nav-link" id="history-tab" data-toggle="tab" href="#history" role="tab">
<i class="fas fa-history"></i>
{% trans %}vendor.partinfo.history{% endtrans %}
</a>
</li>
<li class="nav-item">
<a class="nav-link" id="tools-tab" data-toggle="tab" href="#tools" role="tab" aria-controls="contact" aria-selected="false">
<a class="nav-link" id="tools-tab" data-toggle="tab" href="#tools" role="tab">
<i class="fas fa-tools"></i>
{% trans %}tools.label{% endtrans %}
</a>
@ -119,12 +119,33 @@
TODO
</div>
<div class="tab-pane fade" id="tools" role="tabpanel" aria-labelledby="contact-tab">
<form class="mt-3">
<div class="form-group">
<label>{% trans %}part.delete.caption{% endtrans %}:</label>
<button class="btn btn-danger">{% trans %}part.delete.btn{% endtrans %}</button>
{% if is_granted('edit', part) %}
<a href="{{ part|entityURL('edit') }}" class="btn btn-primary mt-3">
<i class="fas fa-fw fa-edit"></i>
{% trans %}part.edit.btn{% endtrans %}
</a>
{% endif %}
{% if is_granted('create', part) %}
<br>
<div class="btn-group mt-2">
<a class="btn btn-primary" href="{{ part|entityURL('clone') }}">
<i class="fas fa-clone"></i>
{% trans %}part.clone.btn{% endtrans %}
</a>
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<div class="dropdown-menu" role="menu">
<a class="dropdown-item" href="{{ part|entityURL('create') }}">
<i class="fas fa-plus-square"></i>
{% trans %}part.create.btn{% endtrans %}
</a>
</div>
</form>
</div>
{% endif %}
</div>
</div>
</div>

View file

@ -141,7 +141,7 @@
<div class="collapse d-md-block bg-light" id="sidebar-container">
<nav class="fixed-sidebar col-md-3 col-lg-2 " id="fixed-sidebar">
<ul class="nav flex-column">
<li id="categories">
<li id="treeBox-categories">
<div class="dropdown">
<button class="btn-text dropdown-toggle mb-2" type="button" id="dropdownCat" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span class="sidebar-title" id="tree-categories-title">{% trans %}category.labelp{% endtrans %}</span>
@ -163,7 +163,7 @@
</div>
<div id="tree-categories"></div>
</li>
<li id="devices">
<li id="treeBox-devices">
<div class="dropdown">
<button class="btn-text dropdown-toggle mb-2 mt-2" type="button" id="dropdownDev" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span class="sidebar-title" id="tree-devices-title">{% trans %}device.labelp{% endtrans %}</span>
@ -186,7 +186,7 @@
<div id="tree-devices"></div>
</li>
<li id="tools">
<li id="treeBox-tools">
<div class="dropdown">
<button class="btn-text dropdown-toggle mb-2 mt-2" type="button" id="dropdownTools" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span class="sidebar-title" id="tree-tools-title">{% trans %}tools.label{% endtrans %}</span>