Last active
May 27, 2018 13:22
-
-
Save bazooka07/6f6f103bb7a8bae3145a6d7b20eb909f to your computer and use it in GitHub Desktop.
Voir forum http://forum.pluxml.org/viewtopic.php?id=6267 - Gestion des inscriptions à un studio de danses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Orientale": { | |
"places": 20, | |
"titre": "Danse orientale moderne / Bollywood [mardi 19h - 15 ans et plus]", | |
"cotisation": 90, | |
"inscrits": [ | |
"Lolita", | |
"Séhérazade", | |
"Salima", | |
"Patricia" | |
] | |
}, | |
"Salsa": { | |
"places": 25, | |
"titre": "Salsa [mardi 20h - 15 ans et plus]", | |
"cotisation": 80, | |
"inscrits": [ | |
"Lolita", | |
"Nabilla", | |
"Patricia" | |
] | |
}, | |
"Tango": { | |
"places": 8, | |
"titre": "Tango [mardi 22h - 21 ans et plus]", | |
"cotisation": 50, | |
"inscrits": [ | |
"Lolita", | |
"Patricia", | |
"Camila", | |
"Agustina", | |
"Sol", | |
"Florencia", | |
"Micaela", | |
"Fernanda" | |
] | |
}, | |
"Rumba": { | |
"places": 10, | |
"titre": "Rumba [mardi 24h - 21 ans et plus]", | |
"cotisation": 120, | |
"inscrits": [ | |
"Adriana", | |
"Lolita", | |
"Nabilla", | |
"Patricia" | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Page statique pour la gestion des inscriptions d'un studio de danses | |
* Voir forum : http://forum.pluxml.org/viewtopic.php?pid=57289#p57289 | |
* | |
* la constante INSCRIPTIONS_FILENAME définit le fichier qui stocke les inscriptions | |
* | |
* Author bazooka07 | |
* Created 2018-05-27 | |
* */ | |
define('INSCRIPTIONS_FILENAME', PLX_ROOT.$this->plxMotor->aConf['medias'].'inscriptions.json'); | |
if(!file_exists(INSCRIPTIONS_FILENAME)) { | |
$inscriptions = array( | |
'Orientale' => array( | |
'places' => 20, | |
'titre' => 'Danse orientale moderne / Bollywood [mardi 19h - 15 ans et plus]', | |
'cotisation' => 90 | |
), | |
'Salsa' => array( | |
'places' => 25, | |
'titre' => 'Salsa [mardi 20h - 15 ans et plus]', | |
'cotisation' => 80 | |
), | |
'Tango' => array( | |
'places' => 8, | |
'titre' => 'Tango [mardi 22h - 21 ans et plus]', | |
'cotisation' => 50 | |
), | |
'Rumba' => array( | |
'places' => 10, | |
'titre' => 'Rumba [mardi 24h - 21 ans et plus]', | |
'cotisation' => 120 | |
) | |
); | |
$inscriptions_ouvertes = true; | |
} else { | |
$inscriptions = json_decode(file_get_contents(INSCRIPTIONS_FILENAME), true); | |
// On vérifie qu'il reste des places libres | |
$inscriptions_ouvertes = false; | |
foreach($inscriptions as $danse => $infos) { | |
if($infos['places'] - count('inscrits') > 0) { | |
$inscriptions_ouvertes = true; | |
break; | |
} | |
} | |
} | |
if($inscriptions_ouvertes and filter_has_var(INPUT_POST, 'danses')) { | |
// On enregistre l'inscription | |
$inscrit = trim(filter_input(INPUT_POST, 'inscrit', FILTER_SANITIZE_STRING)); | |
if(!empty($inscrit)) { | |
$vos_inscriptions = array(); | |
foreach($_POST['danses'] as $danse) { | |
if( | |
array_key_exists($danse, $inscriptions) and ( | |
empty($inscriptions[$danse]['inscrits']) or | |
$inscriptions[$danse]['places'] - count($inscriptions[$danse]['inscrits']) > 0 | |
) | |
) { | |
if(!empty($inscriptions[$danse]['inscrits'])) { | |
$inscriptions[$danse]['inscrits'][] = $inscrit; | |
} else { | |
$inscriptions[$danse]['inscrits'] = array($inscrit); | |
} | |
$vos_inscriptions[] = $danse; | |
} | |
} | |
// On sauvegarde | |
file_put_contents( | |
INSCRIPTIONS_FILENAME, | |
json_encode($inscriptions, JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE) | |
); | |
// On se congratule ! | |
?> | |
<div> | |
<div> | |
Félicitations <?php echo $inscrit; ?> !<br /> | |
Nous sommes très heureux de vous compter parmi nous. | |
</div> | |
<div> | |
<p>Vous êtes inscrit-e pour : </p> | |
<ul> | |
<?php | |
$total_cotisations = 0; | |
foreach($vos_inscriptions as $danse) { | |
$total_cotisations += $inscriptions[$danse]['cotisation']; | |
echo <<< DANSE | |
<li>{$inscriptions[$danse]['titre']} - {$inscriptions[$danse]['cotisation']}€</li>\n | |
DANSE; | |
} | |
?> | |
</ul> | |
</div> | |
<div> | |
Votre cotisation s'élève à <?php echo $total_cotisations; ?> €. | |
</div> | |
</div> | |
<?php | |
} else { | |
?> | |
<div> | |
Inscription invalidée. Nous n'avons pas bien saisi votre nom. | |
</div> | |
<?php | |
} | |
} elseif($inscriptions_ouvertes === true) { | |
?> | |
<form id="inscription_danses" method="post" class="inline-form"> | |
<div> | |
<input type="text" id="id_inscrit" name="inscrit" placeholder="Votre nom" required /> | |
</div> | |
<?php | |
foreach($inscriptions as $danse => $infos) { | |
$places_libres = (!empty($infos['inscrits'])) ? $infos['places'] - count($infos['inscrits']) : $infos['places']; | |
$disabled = ($places_libres <= 0) ? ' disabled' : ''; | |
$comment = ($places_libres <= 0) ? "Il n'y a plus de place" : "Reste {$places_libres} places / {$infos['places']}"; | |
echo <<< DANSE | |
<div> | |
<input type="checkbox" id="id_{$danse}" name="danses[]" value="{$danse}"{$disabled} /> | |
<label for="id_{$danse}">{$infos['titre']}</label> | |
- <span>{$comment}</span> | |
<span>( {$infos['cotisation']}€ )</span> | |
</div>\n | |
DANSE; | |
} | |
?> | |
<div> | |
<input type="submit" value="S'inscrire" /> | |
</div> | |
</form> | |
<script type="text/javascript"> | |
(function() { | |
'use strict'; | |
const form1 = document.getElementById('inscription_danses'); | |
if(form1 != null) { | |
form1.addEventListener('submit', function(event) { | |
const chks = form1.elements['danses[]']; | |
var ok = false; | |
for(var i=0, iMax=chks.length; i <iMax; i++) { | |
if(chks[i].checked) { | |
ok = true; | |
break; | |
} | |
} | |
if(!ok) { | |
event.preventDefault(); | |
alert("Vous n'avez choisi aucune danse"); | |
return false; | |
} else { | |
return true; | |
} | |
}); | |
} | |
})(); | |
</script> | |
<?php | |
} else { | |
?> | |
<div> | |
Désolé, plus de places - Inscriptions clôturées | |
</div> | |
<?php | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment