Commit dd512838 authored by Mat's avatar Mat

requêtes ajax séparées pour compter les légumes et les abonnés d'une lettre

parent b27cbe82
......@@ -236,5 +236,31 @@ class LettreAPIController extends MasterAPIController
}
return $this->api([]);
}
/**
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function countReceiversAction($id)
{
$em = $this->getDoctrine()->getManager();
$receivers = $em->getRepository('PotageBundle:Lettre')->countReceivers($id);
return $this->api($receivers);
}
/**
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function countLegumesAction($id)
{
$em = $this->getDoctrine()->getManager();
$legumes = $em->getRepository('PotageBundle:Lettre')->countLegumes($id);
return $this->api($legumes);
}
}
......@@ -236,6 +236,22 @@ api_lettre_delete:
_controller: APIBundle:LettreAPI:delete
methods: [ DELETE ]
api_lettre_count_receivers:
path: /lettre/{id}/members/count
requirements:
id: \d+
defaults:
_controller: APIBundle:LettreAPI:countReceivers
methods: [ GET ]
api_lettre_count_legumes:
path: /lettre/{id}/legumes/count
requirements:
id: \d+
defaults:
_controller: APIBundle:LettreAPI:countLegumes
methods: [ GET ]
## CRUD InfoAPI
......
......@@ -159,4 +159,39 @@ class LettreRepository extends \Doctrine\ORM\EntityRepository
));
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @param $id
* @return mixed
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function countReceivers($id)
{
$qb = $this->createQueryBuilder('l')
->join('l.groupe', 'g')
->join('g.utilisateurs', 'u')
->select('count(u.id)')
->where('l.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getSingleScalarResult();
}
/**
* @param $id
* @return mixed
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function countLegumes($id)
{
$qb = $this->createQueryBuilder('l')
->join('l.offre', 'o')
->join('o.offreLegumes', 'ol')
->select('count(ol.id)')
->where('l.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getSingleScalarResult();
}
}
......@@ -76,6 +76,9 @@ function buildRowLettre(jsonRow)
td3.innerHTML = displayHeaderLettre(jsonRow);
tr.appendChild(td3);
countAbonnes(jsonRow);
countLegumes(jsonRow);
let td4 = document.createElement('td');
let toolbarBtn = document.createElement('div');
toolbarBtn.classList.add('btn-toolbar');
......
......@@ -166,6 +166,8 @@ function reloadBlocLettre()
introLettre.innerHTML =
`Semaine du <strong>${formatStringDate(json.started_at, true, true, false, false)}</strong><br>
au <strong>${formatStringDate(json.ended_at, true, true, false, false)}</strong>`;
countAbonnes(json);
countLegumes(json);
}
);
}
......
......@@ -271,46 +271,63 @@ function displayBodyOneInfo(body, info)
*/
function displayHeaderLettre(lettre, facultatif = false)
{
//let abonnes = lettre.groupe.utilisateurs.length + ' abonné' + (lettre.groupe.utilisateurs.length > 1 ? 's' : '');
let abonnes;
let periode = facultatif ? '' : displayPeriodAndOffre(lettre);
return `<div class="lettre-header"><dl>
<dt>Pour:</dt>
<dd>
groupe ${lettre.groupe.nom}
<span class="details parenth">${abonnes}</span>
</dd>
<dd>groupe ${lettre.groupe.nom}
<span class="details parenth" id="abonnes-lettre-${lettre.id}"></span></dd>
<dt>Sujet:</dt>
<dd>
${lettre.subject}
</dd>
<dd>${lettre.subject}</dd>
${periode}
</dl></div>`;
}
/*
* Affiche aussi la période et l'offre
* @param lettre json
*/
function displayPeriodAndOffre(lettre)
{
let offreLink = Routing.generate('potage_offre_legumes_ajax_display', {'id': lettre.offre.id });
//let legumes = lettre.offre.offre_legumes.length + ' légume' + (lettre.offre.offre_legumes.length > 1 ? 's' : '');
let legumes;
return `<dt>Période:</dt>
<dd>
${formatStringDate(lettre.started_at, true, true, false, false)}
${formatStringDate(lettre.ended_at, true, true, false, false)}
</dd>
<dd>${formatStringDate(lettre.started_at, true, true, false, false)}
${formatStringDate(lettre.ended_at, true, true, false, false)}</dd>
<dt>Offre:</dt>
<dd>
<a href="${offreLink}" title="Voir l'offre" class="btn btn-outline-dark btn-sm">
<dd><a href="${offreLink}" title="Voir l'offre" class="btn btn-outline-dark btn-sm">
<i class="fas fa-paperclip fa-fw"></i> ${lettre.offre.reference}</a>
<span class="details parenth">${legumes}</span>
</dd>
<span class="details parenth" id="offre-lettre-${lettre.id}"></span></dd>
`;
}
/*
* Compte le nombre d'abonnés pour une lettre
* @param lettre json
*/
function countAbonnes(lettre)
{
AJAX('GET', Routing.generate('api_lettre_count_receivers', {'id': lettre.id }), function(data)
{
let nbReceivers = JSON.parse(data.responseText);
let abonnes = document.querySelector('span#abonnes-lettre-' + lettre.id);
abonnes.innerHTML = nbReceivers + ' abonné' + (nbReceivers > 1 ? 's' : '');
});
}
/*
* Compte le nombre de légumes pour l'offre insérée dans une lettre
* @param lettre json
*/
function countLegumes(lettre)
{
AJAX('GET', Routing.generate('api_lettre_count_legumes', {'id': lettre.id }), function(data)
{
let nbLegumes = JSON.parse(data.responseText);
let legumes = document.querySelector('span#offre-lettre-' + lettre.id);
legumes.innerHTML = nbLegumes + ' légume' + (nbLegumes > 1 ? 's' : '');
});
}
......
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