Commit af73bfa8 authored by Mat's avatar Mat
Browse files

Offre, mise en place d'une action Sorted, de la route et du form associé

parent b4cf1ea1
......@@ -4,6 +4,7 @@ namespace APIBundle\Controller;
use APIBundle\Form\OffreAPIType;
use APIBundle\Form\OffreSortedAPIType;
use APIBundle\Form\OffreStatusAPIType;
use PotageBundle\Entity\Offre;
use Symfony\Component\HttpFoundation\Request;
......@@ -103,6 +104,34 @@ class OffreAPIController extends MasterAPIController
}
/**
* @param Request $request
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function sortedAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$offre = $em->getRepository('PotageBundle:Offre')->findOneForSorted($id);
if ($offre === null)
{
return $this->api('Not found', Response::HTTP_NOT_FOUND);
}
$form = $this->createForm(OffreSortedAPIType::class, $offre);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$em->flush();
return $this->api($offre);
}
return $this->api($form, Response::HTTP_BAD_REQUEST);
}
/**
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
......
<?php
namespace APIBundle\Form;
use PotageBundle\Form\Offre\OffreSortedType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class OffreSortedAPIType extends OffreSortedType
{
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefault('csrf_protection', false);
// TODO temporaire, le temps de régler les validations au niveau des Asserts et des FormType
$resolver->setDefault('attr', array('novalidate' => true));
}
}
......@@ -96,6 +96,14 @@ api_offre_update_status:
_controller: APIBundle:OffreAPI:updateStatus
methods: [ POST ]
api_offre_update_sorted:
path: /offre/{id}/sort
requirements:
id: \d+
defaults:
_controller: APIBundle:OffreAPI:sorted
methods: [ POST ]
api_offre_delete:
path: /offre/{id}
requirements:
......
......@@ -3,6 +3,7 @@
namespace PotageBundle\Controller;
use APIBundle\Form\OffreLegumesAPIType;
use APIBundle\Form\OffreSortedAPIType;
class OffreLegumesController extends MasterController
{
......@@ -23,10 +24,13 @@ class OffreLegumesController extends MasterController
}
$form = $this->createForm(OffreLegumesAPIType::class);
$formSorted = $this->createForm(OffreSortedAPIType::class);
return $this->render('@Potage/OffreLegumes/ajaxDisplay.html.twig', array(
'id' => $id,
'offre' => $offre,
'formOffreLegumes' => $form->createView()
'formOffreLegumes' => $form->createView(),
'formOffreSorted' => $formSorted->createView()
));
}
}
<?php
namespace PotageBundle\Form\Offre;
use PotageBundle\Entity\Offre;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class OffreSortedType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('token', HiddenType::class, array(
'data' => array(),
));
$builder->add('sauver', SubmitType::class, array(
'label' => 'Sauvegarder',
'attr' => array('class' => 'btn btn-dark mb-2')
));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefault('data_class', Offre::class);
$resolver->setDefault('attr', array(
'class' => 'formulaire formulaire_offre_sorted',
));
}
/**
* @return null|string
*/
public function getBlockPrefix()
{
return "offre_sorted";
}
}
\ No newline at end of file
......@@ -60,6 +60,24 @@ class OffreRepository extends \Doctrine\ORM\EntityRepository
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @param $id
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForSorted($id)
{
$qb = $this->createQueryBuilder('o')
->select('o.sorted')
->where('o.id = :id')
->andWhere('o.status = :status ')
->setParameters(array(
':id'=> $id,
':status' => 'draft'
));
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @param $id
* @return mixed
......
......@@ -106,6 +106,8 @@
</ul>
</div>
{{ form(formOffreSorted) }}
</aside>
</div>
{% endblock %}
......
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