Added links between edit page and show page.

This commit is contained in:
Jan Böhmer 2019-03-13 18:41:32 +01:00
parent 63c7beb05b
commit 061ca799e3
6 changed files with 105 additions and 9 deletions

View file

@ -37,6 +37,7 @@ use App\Entity\Category;
use App\Entity\Part; use App\Entity\Part;
use App\Form\PartType; use App\Form\PartType;
use App\Services\AttachmentFilenameService; use App\Services\AttachmentFilenameService;
use App\Services\EntityURLGenerator;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -70,7 +71,7 @@ class PartController extends AbstractController
* @param Part $part * @param Part $part
* @return \Symfony\Component\HttpFoundation\Response * @return \Symfony\Component\HttpFoundation\Response
*/ */
public function edit(Part $part, Request $request, EntityManagerInterface $em, TranslatorInterface $translator) public function edit(Part $part, Request $request, EntityManagerInterface $em)
{ {
$form = $this->createForm(PartType::class, $part); $form = $this->createForm(PartType::class, $part);
@ -85,7 +86,7 @@ class PartController extends AbstractController
return $this->render('edit_part_info.html.twig', return $this->render('edit_part_info.html.twig',
[ [
"part" => $part, "part" => $part,
"form" => $form->createView() "form" => $form->createView(),
]); ]);
} }

View file

@ -33,7 +33,6 @@ namespace App\Form;
use App\Entity\Part; use App\Entity\Part;
use phpDocumentor\Reflection\Types\Integer;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\UrlType; use Symfony\Component\Form\Extension\Core\Type\UrlType;
@ -49,14 +48,18 @@ class PartType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$builder $builder
->add('name', TextType::class, ['empty_data'=>'', 'label'=> 'name.label']) ->add('name', TextType::class, ['empty_data'=>'', 'label'=> 'name.label',
->add('description', TextType::class, ['required'=>false, 'empty_data'=>'', 'label'=> 'description.label']) 'attr' => ['placeholder' => 'part.name.placeholder']])
->add('instock', IntegerType::class, ['attr' => ['min'=>0], 'label'=> 'instock.label']) ->add('description', TextType::class, ['required'=>false, 'empty_data'=>'',
->add('mininstock', IntegerType::class, ['attr' => ['min'=>0], 'label'=> 'mininstock.label']) 'label'=> 'description.label', 'help' => 'bbcode.hint', 'attr' => ['placeholder' => 'part.description.placeholder']])
->add('instock', IntegerType::class,
['attr' => ['min'=>0, 'placeholder' => 'part.instock.placeholder'], 'label'=> 'instock.label'])
->add('mininstock', IntegerType::class,
['attr' => ['min'=>0, 'placeholder' => 'part.mininstock.placeholder'], 'label'=> 'mininstock.label'])
->add('manufacturer_product_url', UrlType::class, ['required'=>false, 'empty_data' => '', ->add('manufacturer_product_url', UrlType::class, ['required'=>false, 'empty_data' => '',
'label'=> 'manufacturer_url.label']) 'label'=> 'manufacturer_url.label'])
->add('comment', TextareaType::class, ['required'=>false, ->add('comment', TextareaType::class, ['required'=>false,
'label'=> 'comment.label', 'attr' => ['rows'=> 4]]) 'label'=> 'comment.label', 'attr' => ['rows'=> 4], 'help' => 'bbcode.hint'])
//Buttons //Buttons
->add('save', SubmitType::class, ['label' => 'part.edit.save']) ->add('save', SubmitType::class, ['label' => 'part.edit.save'])

View file

@ -69,6 +69,27 @@ class EntityURLGenerator
throw new EntityNotSupported('The given entity is not supported yet!'); throw new EntityNotSupported('The given entity is not supported yet!');
} }
public function editURL($entity) : string
{
if($entity instanceof Part)
{
return $this->urlGenerator->generate('part_edit', ['id' => $entity->getID()]);
}
//Otherwise throw an error
throw new EntityNotSupported('The given entity is not supported yet!');
}
public function createURL($entity) : string
{
if($entity instanceof Part)
{
return $this->urlGenerator->generate('part_new');
}
throw new EntityNotSupported('The given entity is not supported yet!');
}
/** /**
* Generates an HTML link to the info page about the given entity. * 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. * @param $entity mixed The entity for which the info link should be generated.

70
src/Twig/AppExtension.php Normal file
View file

@ -0,0 +1,70 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Twig;
use App\Entity\DBElement;
use App\Services\EntityURLGenerator;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
class AppExtension extends AbstractExtension
{
protected $entityURLGenerator;
public function __construct(EntityURLGenerator $entityURLGenerator)
{
$this->entityURLGenerator = $entityURLGenerator;
}
public function getFilters()
{
return [
new TwigFilter('entityURL', [$this, 'generateEntityURL'])
];
}
public function generateEntityURL(DBElement $entity, string $method = 'info') : string
{
switch($method) {
case 'info':
return $this->entityURLGenerator->infoURL($entity);
case 'edit':
return $this->entityURLGenerator->editURL($entity);
case 'create':
return $this->entityURLGenerator->createURL($entity);
}
throw new \InvalidArgumentException('method is not supported!');
}
}

View file

@ -7,6 +7,7 @@
{% block card_title %} {% block card_title %}
<i class="fas fa-edit fa-fw" aria-hidden="true"></i> <i class="fas fa-edit fa-fw" aria-hidden="true"></i>
{% trans with {'%name%': part.name} %}part.edit.card_title{% endtrans %} {% trans with {'%name%': part.name} %}part.edit.card_title{% endtrans %}
<b><a href="{{ part|entityURL('info') }}" class="text-white">{{ part.name }}</a></b>
<div class="float-right"> <div class="float-right">
{% trans %}id.label{% endtrans %}: {{ part.id }} {% trans %}id.label{% endtrans %}: {{ part.id }}
</div> </div>

View file

@ -23,7 +23,7 @@
</div> </div>
<div class="col-md-9"> <div class="col-md-9">
<h5 class="text-muted pt-2" title="{% trans %}manufacturer.label{% endtrans %}">{{ part.manufacturer.name ?? ""}}</h5> <h5 class="text-muted pt-2" title="{% trans %}manufacturer.label{% endtrans %}">{{ part.manufacturer.name ?? ""}}</h5>
<h3>{{ part.name }}</h3> <h3>{{ part.name }} <a href="{{ part|entityURL('edit') }}"><i class="fas fa-fw fa-sm fa-edit"></i></a></h3>
<h6 class="text-muted"><span title="{% trans %}description.label{% endtrans %}">{{ part.description }}</span></h6> <h6 class="text-muted"><span title="{% trans %}description.label{% endtrans %}">{{ part.description }}</span></h6>
<h6 class="" title="{% trans %}storelocation.label{% endtrans %}"> <h6 class="" title="{% trans %}storelocation.label{% endtrans %}">
<i class="fas fa-cube fa-fw"></i> <i class="fas fa-cube fa-fw"></i>