Commit 3d921b28 authored by Mat's avatar Mat

Template de base pour l'entité Lettre

parent 684d7d4c
......@@ -3,6 +3,7 @@
namespace APIBundle\Controller;
use APIBundle\Form\LettreAPIType;
use APIBundle\Form\LettreStatusAPIType;
use PotageBundle\Entity\Lettre;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
......@@ -10,13 +11,42 @@ use Symfony\Component\HttpFoundation\Response;
class LettreAPIController extends MasterAPIController
{
/**
* @return string
* @throws \Doctrine\ORM\NonUniqueResultException
*/
private function newReference()
{
$now = new \DateTime();
$today = $now->format('ymd');
$em = $this->getDoctrine()->getManager();
$num = $em->getRepository('PotageBundle:Lettre')->findOneLastReference($today);
if ($num !== null)
{
$last = intval(substr($num['reference'], -2));
$last++;
$last = sprintf("%02d", $last);
}
else {
$last = '01';
}
return 'LT' . $today . $last;
}
/**
* @param Request $request
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function createAction(Request $request)
{
$lettre = new Lettre();
$reference = $this->newReference();
$lettre = new Lettre($reference);
$form = $this->createForm(LettreAPIType::class, $lettre);
$form->handleRequest($request);
......@@ -34,12 +64,13 @@ class LettreAPIController extends MasterAPIController
/**
* @param $status
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function readAction()
public function readByStatusAction($status)
{
$em = $this->getDoctrine()->getManager();
$lettres = $em->getRepository('PotageBundle:Lettre')->findAllForAPIRead();
$lettres = $em->getRepository('PotageBundle:Lettre')->findAllByStatusForRead($status);
return $this->api($lettres);
}
......@@ -87,6 +118,33 @@ class LettreAPIController extends MasterAPIController
}
/**
* @param Request $request
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function updateStatusAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$lettre = $em->getRepository('PotageBundle:Lettre')->findOneForAPIUpdate($id);
$form = $this->createForm(LettreStatusAPIType::class, $lettre);
$form->handleRequest($request);
if ($lettre === null) {
return $this->api('Not found', Response::HTTP_NOT_FOUND);
}
if ($form->isSubmitted() && $form->isValid())
{
$em->flush();
return $this->api($lettre);
}
return $this->api($form, Response::HTTP_BAD_REQUEST);
}
/**
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
......
<?php
namespace APIBundle\Form;
use PotageBundle\Form\Lettre\LettreStatusType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class LettreStatusAPIType extends LettreStatusType
{
/**
* @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));
}
}
......@@ -150,10 +150,12 @@ api_offre_legumes_delete:
## CRUD LettreAPI
api_lettre_read:
path: /lettres
api_lettre_read_by_status:
path: /lettres/{status}
requirements:
status: \w+
defaults:
_controller: APIBundle:LettreAPI:read
_controller: APIBundle:LettreAPI:readByStatus
methods: [ GET ]
api_lettre_create:
......@@ -178,6 +180,14 @@ api_lettre_update:
_controller: APIBundle:LettreAPI:update
methods: [ POST ]
api_lettre_update_status:
path: /lettre/{id}/status
requirements:
id: \d+
defaults:
_controller: APIBundle:LettreAPI:updateStatus
methods: [ POST ]
api_lettre_delete:
path: /lettre/{id}
requirements:
......
......@@ -90,13 +90,15 @@ class Lettre
private $updatedAt;
/**
* Lettre constructor.
*
* @param $reference
*/
public function __construct()
public function __construct($reference)
{
$this->infos = new ArrayCollection();
$this->reference = $reference;
}
/**
......@@ -353,14 +355,15 @@ class Lettre
return $this->infos;
}
/**
* Set groupe
*
* @param integer $groupe
* @param \PotageBundle\Entity\Groupe $groupe
*
* @return Lettre
*/
public function setGroupe($groupe)
public function setGroupe(\PotageBundle\Entity\Groupe $groupe)
{
$this->groupe = $groupe;
......@@ -370,7 +373,7 @@ class Lettre
/**
* Get groupe
*
* @return integer
* @return \PotageBundle\Entity\Groupe
*/
public function getGroupe()
{
......
<?php
namespace PotageBundle\Form\Lettre;
use PotageBundle\Entity\Lettre;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class LettreStatusType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('status', ChoiceType::class, array(
'label' => 'Statut',
'choices' => array(
'En préparation' => 'draft',
'Envoyer' => 'current',
),
'label_attr' => array('class' => 'col-form-label'),
'attr' => array(
'class' => 'form-control form-control-sm',
),
));
$builder->add('sauver', SubmitType::class, array(
'label' => 'Changer',
'attr' => array('class' => 'btn btn-dark mb-2')
));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefault('data_class', Lettre::class);
$resolver->setDefault('attr', array(
'class' => 'formulaire formulaire_offre_status',
));
}
/**
* @return null|string
*/
public function getBlockPrefix()
{
return 'offre_status';
}
}
......@@ -51,16 +51,20 @@ class LettreType extends AbstractType
);
// infos
/*
$builder->add('infos', EntityType::class, array(
'class' => Info::class,
'label' => "Dans laquelle on glisse des infos",
'label_attr' => array('class' => 'col-form-label'),
'attr' => array('class' => 'form-control form-control-sm'),
));
*/
// groupe
$builder->add('groupe', EntityType::class, array(
'class' => Groupe::class,
'choice_label' => 'getNom',
'placeholder' => '',
'label' => "À destination d'un groupe",
'label_attr' => array('class' => 'col-form-label'),
'attr' => array('class' => 'form-control form-control-sm'),
......
......@@ -12,12 +12,32 @@ class LettreRepository extends \Doctrine\ORM\EntityRepository
{
/**
* @return array Lettre
* @param $pattern
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneLastReference($pattern)
{
$qb = $this->createQueryBuilder('l')
->select('l.reference')
->where('l.reference LIKE :pattern')
->setParameter(':pattern', '%' . $pattern . '%')
->orderBy('l.reference', 'DESC')
->setMaxResults(1);
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @param $status
* @return array
*/
public function findAllForAPIRead()
public function findAllByStatusForRead($status)
{
$qb = $this->createQueryBuilder('l')
->orderBy('l.id', 'DESC');
->where('l.status = :status')
->setParameter(':status', $status)
->orderBy('l.status')
->addOrderBy('l.reference', 'DESC');
return $qb->getQuery()->getResult();
}
......
......@@ -33,6 +33,7 @@
</div>
</div>
{#
<div class="form-group row">
<div class="col-sm-4">
{{ form_label(formLettre.infos) }}
......@@ -42,6 +43,7 @@
{{ form_errors(formLettre.infos) }}
</div>
</div>
#}
<div class="form-group row">
<div class="col-sm-4">
{{ form_label(formLettre.offre) }}
......
......@@ -325,23 +325,6 @@
return editForm;
}
/*
* Affiche couleur statut
* @param: string status
* @param: array couleurs
* index: status name
* value: bootstrap alert color class
*/
function buildColorSquare(status, couleurs)
{
let fa;
for (let index in couleurs) {
if (index === status) {
fa = 'text-' + couleurs[index];
}}
return '<i class="fas fa-square '+ fa + ' fa-fw"></i> ';
}
/*
* Supprimer une offre
......
......@@ -178,8 +178,8 @@ span#rebours {
table#maintab td {
max-width: 400px;
}
table#maintab span.details {
font-style: italic;
table span.details { /*
font-style: italic; */
font-size: 10pt;
color: #6f6f6f;
}
......
......@@ -185,6 +185,24 @@ function sortToggleByColumn(paginate, callback)
});
}
/*
* Affiche couleur statut
* @param: string status
* @param: array couleurs
* index: status name
* value: bootstrap alert color class
*/
function buildColorSquare(status, couleurs)
{
let fa;
for (let index in couleurs) {
if (index === status) {
fa = 'text-' + couleurs[index];
}}
return '<i class="fas fa-square '+ fa + ' fa-fw"></i> ';
}
/*
* F O N C T I O N S A J A X
*/
......
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