Commit a406f1a2 authored by Mat's avatar Mat
Browse files

on récupère le jour du panier (dépôt) pour deviner la date de fin pour une nouvelle lettre

parent 8939c7f2
......@@ -107,4 +107,18 @@ class GroupeAPIController extends MasterAPIController
return $this->api([]);
}
/**
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function getJourPanierAction($id)
{
$em = $this->getDoctrine()->getManager();
$jour = $em->getRepository('PotageBundle:Groupe')->findJourPanier($id);
return $this->api($jour);
}
}
......@@ -308,6 +308,14 @@ api_groupe_delete:
_controller: APIBundle:GroupeAPI:delete
methods: [ DELETE ]
api_groupe_jour_panier:
path: /groupe/{id}/depot/jourpanier
requirements:
id_groupe: \d+
defaults:
_controller: APIBundle:GroupeAPI:getJourPanier
methods: [ GET ]
## CRUD DepotAPI
......
......@@ -63,7 +63,9 @@ class LettreType extends AbstractType
'placeholder' => '',
'label' => "À destination d'un groupe",
'label_attr' => array('class' => 'col-form-label'),
'attr' => array('class' => 'form-control form-control-sm'),
'attr' => array('class' => 'form-control form-control-sm',
'autofocus' => true,
),
));
$builder->add('subject', TextType::class, array(
'label' => "Sujet ",
......
......@@ -64,4 +64,19 @@ class GroupeRepository extends \Doctrine\ORM\EntityRepository
}
/**
* @param $id
* @return mixed
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findJourPanier($id)
{
$qb = $this->createQueryBuilder('g')
->join('g.depot', 'd')
->select('d.jourPanier')
->where('g.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getSingleResult();
}
}
......@@ -163,6 +163,7 @@
let titleForm = document.querySelector('#form-lettre');
{% include '@Potage/Lettre/ajaxDisplay.js' %}
{% include '@Potage/Lettre/dateSlider.js' %}
</script>
{% endblock %}
......@@ -266,8 +266,11 @@ function displayEditLettre(jsonRow)
let json = JSON.parse(request.responseText);
titleForm.textContent = 'Lettre # ' + lettreId;
form.querySelector('#lettre_subject').value = json.subject;
form.querySelector('#lettre_startedAt').value = formatDateISO(json.started_at);
form.querySelector('#lettre_endedAt').value = formatDateISO(json.ended_at);
initDateRangeSlider(json.started_at, json.ended_at);
form.querySelector('#lettre_groupe').value = json.groupe.id;
form.querySelector('#lettre_offre').value = json.offre.id;
form.querySelector('#lettre_status').value = json.status;
......@@ -317,6 +320,42 @@ function displayDeleteLettre(jsonRow)
return deleteBtn;
}
/*
* Propose une date de fin
* si on a passé un groupe, alors il se base sur jourPanier
* sinon, c'est une semaine après le startedAt
*/
function proposeEndDay(groupe = null)
{
let today = new Date();
function jourInSeconds(jour) {
return jour*24*60*60;
}
if (groupe)
{
AJAX('GET', Routing.generate('api_groupe_jour_panier', {'id': groupe}),
function(request) {
json = JSON.parse(request.responseText);
let jumpPanier = firstJump(today.getDay(), json.jourPanier);
let endDate = new Date(today.setSeconds(today.getSeconds() + jourInSeconds(7 + jumpPanier)));
form.querySelector('#lettre_endedAt').value = formatDateISO(endDate);
let sd = form.querySelector('#lettre_startedAt').value.split('-');
$("#dateRuleSlider").dateRangeSlider("values",
new Date(sd[0], (sd[1] > 0 ? sd[1]-1 : 11), sd[2]),
new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate())
);
//return formatDateISO(endDate);
});
} else {
return new Date(today.setSeconds(today.getSeconds() + jourInSeconds(7)));
}
}
/*
* =================================
......@@ -332,8 +371,13 @@ reloadButton(function(){ reloadTables(); });
createButton(function(){
titleForm.textContent = 'Nouvelle infolettre';
form.querySelector('#lettre_subject').value = null;
form.querySelector('#lettre_startedAt').value = null;
form.querySelector('#lettre_endedAt').value = null;
form.querySelector('#lettre_startedAt').value = formatDateISO(new Date());
form.querySelector('#lettre_endedAt').value = formatDateISO(proposeEndDay());
console.log(proposeEndDay());
initDateRangeSlider(new Date(), proposeEndDay());
form.querySelector('#lettre_groupe').value = null;
form.querySelector('#lettre_offre').value = null;
form.querySelector('#lettre_status').value = 'draft';
......@@ -341,20 +385,28 @@ createButton(function(){
});
/*
* Complétion du champs Subject
*/
let selectGroup = document.querySelector('form[name=lettre] select#lettre_groupe');
let inputSubject = document.querySelector('form[name=lettre] input#lettre_subject');
selectGroup.addEventListener('change', function()
{
/*
* Complétion du champs Subject
*/
let nom = this.options[this.selectedIndex].innerHTML;
let etiquette = '[Légumes ' + nom + '] ';
inputSubject.value = inputSubject.value.replace(/\[Légu.*[\]] /, '');
inputSubject.value = inputSubject.value.replace(/^/, etiquette);
/*
* un groupe, c'est un jour de panier, ce qui permet de suggérer une date de fin
* on update le input et le slider
*/
proposeEndDay(this.value);
});
......
/*
*
* jQRangeSlider
*
* glissière jquery pour le formLettre
* started_at <-> ended_at
*
*/
function firstJump(i, d) {
let c = 0;
while (i !== d) {
if (i === 6) { i = 0; } else { i++; }
c++;
}
return c;
}
function initDateRangeSlider(minDate, maxDate)
{
let months = ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"];
let dows = ['di', 'lu', 'ma', 'me', 'je', 've', 'sa'];
let firstDay = 1; //lundi
let beforeMinDate = 20; // l'écart minimal entre le minBounds et le minDate
let lengthBounds = 3; // le range en mois
let minD = new Date(minDate);
let maxD = new Date(maxDate);
let minBounds = new Date(minD.getTime());
minBounds.setDate(minD.getDate() - beforeMinDate);
$("#dateRuleSlider").dateRangeSlider({
bounds: {
min: new Date(minBounds.getFullYear(), minBounds.getMonth(), 1),
max: new Date(2018, (minBounds.getMonth() + lengthBounds), 1)
},
defaultValues: {
min: new Date(minD.getFullYear(), minD.getMonth(), minD.getDate()),
max: new Date(maxD.getFullYear(), maxD.getMonth(), maxD.getDate())
},
arrows: true,
step:{
days: 1
},
formatter: function(valeur){
let dow = dows[valeur.getDay()],
days = valeur.getDate(),
month = valeur.getMonth() + 1,
year = valeur.getFullYear();
return dow + " " + formatNumber(days, 2) + "/" + formatNumber(month, 2);
},
scales: [
{
first: function(value){ return value; },
end: function(value) {return value; },
next: function(value){
let next = new Date(value);
return new Date(next.setMonth(value.getMonth() + 1));
},
label: function(value){
return months[value.getMonth()] + " " + value.getFullYear();
}
},
{
first: function(value){ return value; },
end: function(value) {return value; },
next: function(value){
let next = new Date(value);
if (value.getDay() === firstDay) {
return new Date(next.setDate(value.getDate() + 7));
} else {
let jump = firstJump(value.getDay(), firstDay);
return new Date(next.setDate(value.getDate() + jump));
}
},
label: function() { return null; },
format: function(tickContainer, tickStart, tickEnd){
if (tickStart.getDate() === 1) {
tickContainer.addClass("no-tick");
}
}
},
{
first: function(value){ return value; },
end: function(value) {return value; },
next: function(value){
let next = new Date(value);
return new Date(next.setDate(value.getDate() + 1));
},
label: function() { return null; },
format: function(tickContainer, tickStart, tickEnd){
if (tickStart.getDay() === firstDay || tickStart.getDate() === 1) {
tickContainer.addClass("no-tick");
}
}
}]
});
}
/*
* Slider Event change input
*/
$("#dateRuleSlider").on("valuesChanged", function(e, data)
{
form.querySelector('#lettre_startedAt').value = formatDateISO(data.values.min);
form.querySelector('#lettre_endedAt').value = formatDateISO(data.values.max);
});
/*
* input Event change slider
*/
function updateSlider(sd, ed)
{
$("#dateRuleSlider").dateRangeSlider("values",
new Date(sd[0], (sd[1] > 0 ? sd[1]-1 : 11), sd[2]),
new Date(ed[0], (ed[1] > 0 ? ed[1]-1 : 11), ed[2])
);
}
$('#lettre_startedAt').on('input', function(e, data)
{
let sd = this.value.split('-');
let ed = form.querySelector('#lettre_endedAt').value.split('-');
updateSlider(sd, ed);
});
$('#lettre_endedAt').on('input', function(e, data)
{
let sd = form.querySelector('#lettre_startedAt').value.split('-');
let ed = this.value.split('-');
updateSlider(sd, ed);
});
......@@ -116,8 +116,6 @@
<script type="text/javascript" src="{{ asset('js/ajax_functions.js') }}"></script>
<script type="text/javascript">
// Variables Twig
let id_lettre = {{ id }};
let sortForm = document.querySelector('form[name=lettre_sorted]');
let input = sortForm.querySelector('input#lettre_sorted_sorted');
......@@ -129,6 +127,7 @@
} );
{% include '@Potage/LettreInfos/ajaxDisplay.js' %}
{% include '@Potage/Lettre/dateSlider.js' %}
</script>
{% endblock %}
......
......@@ -172,7 +172,7 @@ function editBlocLettre()
form.querySelector('#lettre_startedAt').value = formatDateISO(json.started_at);
form.querySelector('#lettre_endedAt').value = formatDateISO(json.ended_at);
displayDateRangeSlider(json.started_at, json.ended_at);
initDateRangeSlider(json.started_at, json.ended_at);
form.querySelector('#lettre_status').value = json.status;
submitURL = Routing.generate('api_lettre_update', {'id': id_lettre});
......@@ -199,146 +199,6 @@ form.addEventListener('submit', function(e)
/*
*
* jQRangeSlider
*
* glissière jquery pour le formLettre
* started_at <-> ended_at
*
*/
function displayDateRangeSlider(minDate, maxDate)
{
let months = ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"];
let dows = ['di', 'lu', 'ma', 'me', 'je', 've', 'sa'];
let firstDay = 1; //lundi
let beforeMinDate = 20; // l'écart minimal entre le minBounds et le minDate
let lengthBounds = 3; // le range en mois
let minD = new Date(minDate);
let maxD = new Date(maxDate);
let minBounds = new Date(minD.getTime());
minBounds.setDate(minD.getDate() - beforeMinDate);
$("#dateRuleSlider").dateRangeSlider({
bounds: {
min: new Date(minBounds.getFullYear(), minBounds.getMonth(), 1),
max: new Date(2018, (minBounds.getMonth() + lengthBounds), 1)
},
defaultValues: {
min: new Date(minD.getFullYear(), minD.getMonth(), minD.getDate()),
max: new Date(maxD.getFullYear(), maxD.getMonth(), maxD.getDate())
},
arrows: true,
step:{
days: 1
},
formatter: function(valeur){
let dow = dows[valeur.getDay()],
days = valeur.getDate(),
month = valeur.getMonth() + 1,
year = valeur.getFullYear();
return dow + " " + formatNumber(days, 2) + "/" + formatNumber(month, 2);
},
scales: [
{
first: function(value){ return value; },
end: function(value) {return value; },
next: function(value){
let next = new Date(value);
return new Date(next.setMonth(value.getMonth() + 1));
},
label: function(value){
return months[value.getMonth()] + " " + value.getFullYear();
}
},
{
first: function(value){ return value; },
end: function(value) {return value; },
next: function(value){
let next = new Date(value);
if (value.getDay() === firstDay) {
return new Date(next.setDate(value.getDate() + 7));
} else {
function firstJump(i, d) {
let c = 0;
while (i !== d) {
if (i === 6) { i = 0; } else { i++; }
c++;
}
return c;
}
let jump = firstJump(value.getDay(), firstDay);
return new Date(next.setDate(value.getDate() + jump));
}
},
label: function() { return null; },
format: function(tickContainer, tickStart, tickEnd){
if (tickStart.getDate() === 1) {
tickContainer.addClass("no-tick");
}
}
},
{
first: function(value){ return value; },
end: function(value) {return value; },
next: function(value){
let next = new Date(value);
return new Date(next.setDate(value.getDate() + 1));
},
label: function() { return null; },
format: function(tickContainer, tickStart, tickEnd){
if (tickStart.getDay() === firstDay || tickStart.getDate() === 1) {
tickContainer.addClass("no-tick");
}
}
}]
});
}
/*
* Slider Event change input
*/
$("#dateRuleSlider").on("valuesChanged", function(e, data)
{
form.querySelector('#lettre_startedAt').value = formatDateISO(data.values.min);
form.querySelector('#lettre_endedAt').value = formatDateISO(data.values.max);
});
/*
* input Event change slider
*/
function updateSlider(sd, ed)
{
$("#dateRuleSlider").dateRangeSlider("values",
new Date(sd[0], (sd[1] > 0 ? sd[1]-1 : 11), sd[2]),
new Date(ed[0], (ed[1] > 0 ? ed[1]-1 : 11), ed[2])
);
}
$('#lettre_startedAt').on('input', function(e, data)
{
let sd = this.value.split('-');
let ed = form.querySelector('#lettre_endedAt').value.split('-');
updateSlider(sd, ed);
});
$('#lettre_endedAt').on('input', function(e, data)
{
let sd = form.querySelector('#lettre_startedAt').value.split('-');
let ed = this.value.split('-');
updateSlider(sd, ed);
});
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