Commit eff46c93 authored by Mat's avatar Mat

CRUD image complet, avec update, delete, et checks

parent 935102ad
......@@ -47,14 +47,42 @@ class ImageController extends Controller
}
public function updateAction()
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$image = $em->getRepository('TechnoBundle:Image')->findOneByUpdate($id);
if ($image === null) {
throw $this->createNotFoundException('Image non trouvée');
}
$form = $this->createForm(ImageType::class, $image); // hydratation
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$em->flush();
return $this->redirectToRoute('techno_gallery_read');
}
return $this->render('@Techno/Image/update.html.twig', array(
'id' => $id,
'formImage' => $form->createView()
));
}
public function deleteAction()
public function deleteAction($id)
{
$em = $this->getDoctrine()->getManager();
$image = $em->getRepository('TechnoBundle:Image')->findOneByDelete($id);
if ($image !== null)
{
$em->remove($image);
$em->flush();
}
return $this->redirectToRoute('techno_gallery_read');
}
}
......@@ -3,6 +3,8 @@
namespace TechnoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Image
......
......@@ -11,6 +11,7 @@ use \TechnoBundle\Entity\Image;
*/
class ImageRepository extends \Doctrine\ORM\EntityRepository
{
public function getLastImages($nb)
{
$dql = "SELECT I FROM TechnoBundle\Entity\Image I ORDER BY I.createdAt DESC";
......@@ -22,37 +23,49 @@ class ImageRepository extends \Doctrine\ORM\EntityRepository
return $result;
}
public function create()
/**
* @return Image[]
*/
public function findAllForIndex()
{
$image = new Image();
$image->setPath('https://picsum.photos/400/?image=111'); // viendra du formulaire
$image->setName("Image 111");
$image->setLegend("Légende de l'image 111");
$owner = $this->getEntityManager()->getRepository('TechnoBundle:Owner')->find(1);
$image->setOwner($owner);
$qb = $this->createQueryBuilder('i')
->join('i.owner', 'o')
->addSelect('o')
->orderBy('i.id', 'ASC');
return $qb->getQuery()->getResult();
}
$em = $this->getEntityManager();
$em->persist($image);
try
{
$em->flush();
return $image;
}
catch (Exception $e)
{
echo $e->getMessage();
}
return null;
/**
* @param $id
* @return Image
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneByUpdate($id)
{
$qb = $this->createQueryBuilder('i')
->join('i.owner', 'o')
->addSelect('o')
->where('i.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @return array
*/
public function findAllForIndex()
/**
* @param $id
* @return Image
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneByDelete($id)
{
$qb = $this->createQueryBuilder('i')->orderBy('i.id', 'ASC');
return $qb->getQuery()->getResult();
$qb = $this->createQueryBuilder('i')
->where('i.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getOneOrNullResult();
}
}
......@@ -17,6 +17,16 @@ techno_gallery_create:
defaults:
_controller: TechnoBundle:Image:create
techno_gallery_update:
path: /gallery/edit/{id}
defaults:
_controller: TechnoBundle:Image:update
techno_gallery_delete:
path: /gallery/delete/{id}
defaults:
_controller: TechnoBundle:Image:delete
techno_owner_read:
path: /owner
defaults:
......
......@@ -11,9 +11,21 @@
<h1>{{ block('title') }}</h1>
{% for image in images %}
<a href="#">
<img src="{{ image.path }}" alt="{{ image.name }}" title="{{ image.name }}" class="image" />
</a>
<div class="image">
<img src="{{ image.path }}" alt="{{ image.name }}" title="{{ image.name }}" />
<div class="modify">
<a href="{{ path('techno_gallery_update', {'id': image.id }) }}" class="btn">
<i class="fas fa-pen-square"></i>
</a>
<a href="{{ path('techno_gallery_delete', {'id': image.id }) }}" class="btn btn-danger">
<i class="fas fa-trash"></i>
</a>
</div>
</div>
{% endfor %}
{% endblock %}
{% extends '@Techno/base.html.twig' %}
{% block title %}
Editer une image
{% endblock %}
{% block content %}
{{ block('breadcrumb') }}
<h1>{{ block('title') }}</h1>
{{ form(formImage) }}
{% endblock %}
......@@ -64,10 +64,6 @@
<i class="fas fa-table"></i> Table Image
</a>
</li>
<li><a href=""><i class="fas fa-images"></i> Page 1</a></li>
<li><a href=""><i class="fas fa-images"></i> Page 2</a></li>
<li><a href=""><i class="fas fa-images"></i> Page 3</a></li>
#}
<li class="{{ app.request.get('_route') == 'techno_gallery_create' ? 'active' }}">
......@@ -75,6 +71,7 @@
<i class="fas fa-plus"></i> Ajouter une image
</a>
</li>
<li class="{{ app.request.get('_route') == 'techno_owner_create' ? 'active' }}">
<a href="{{ path('techno_owner_create') }}">
<i class="fas fa-plus"></i> Ajouter un auteur
......
......@@ -218,10 +218,22 @@ ul.vignettes li img {
width: 60px;
height: 60px;
}
#gallery_read img.image {
#gallery_read div.image {
width: 200px;
height: 200px;
margin-right: 2px;
margin: 2px;
position: relative;
float: left;
}
#gallery_read div.image img {
width: 100%;
height: 100%;
}
#gallery_read div.image div.modify {
position: absolute;
z-index: 10;
top: 1em;
right: 1em;
}
form {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment