Files
cwsvJudo/homepage/mams/lib/api.php

153 lines
4.0 KiB
PHP

<?php
function processPostData($db, $post, $redirectLocation = "."){
if($post['action']){
if($post['action'] == "giveAttendance"){
giveJudokasAttendence($db, $post['attandanceDate'], $post['judokaIdsInTraining']);
}
if($post['action'] == "updateCoronaData"){
updateCoronaData($db, $post['userId'], $post['columnName'], $post['columnValue']);
}
if($post['action'] == "addCoronaUser"){
if(
array_keys_exist(
$post,
['name', 'vorname', 'corona_PLZ', 'corona_telephon', 'corona_eMail']
)
&& isValid($post['corona_PLZ'], "plz")
&& isValid($post['corona_telephon'], "phonenumber")
&& isValid($post['corona_eMail'], "email")
){
addCoronaUser(
$db,
$post['name'],
$post['vorname'],
$post['corona_PLZ'],
$post['corona_telephon'],
$post['corona_eMail']
);
$redirectLocation .= "?addCoronaUserSuccess=true";
}
else{
$redirectLocation .= "?addCoronaUserSuccess=false";
}
$redirectLocation .= "#addCoronaUser";
}
if($post['action'] == "sendAttandeesPerEmail"){
sendEmail(
$post['toEmail'],
$post['emailText']
);
}
header("Location: ".$redirectLocation);
}
return;
}
function sendEmail($toEmail, $emailText){
try{
$date=new DateTime();
mail(
$toEmail,
"Kontakliste CWSV-Judo vom ".$date->format("Y-m-d"),
$emailText
);
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
}
function attendancesAssocArray2text($attendancesAssocArray){
$ret = "";
foreach($attendancesAssocArray as $date => $attendees){
$ret .= $date."\n";
foreach($attendees as $a){
$ret .= "\n";
$ret .= "Name: ".$a['name'].", ".$a['vorname']."\n";
$ret .= "PLZ: ".$a['corona_PLZ']."\n";
$ret .= "Tel.: ".$a['corona_telephon']."\n";
$ret .= "eMail: ".$a['corona_eMail']."\n";
}
$ret .= "\n";
}
return $ret;
}
function attendancesAssocArray2mdList($attendancesAssocArray, $date=null){
if($date == null)
$date=new DateTime();
$ret = "# Anwesenheitsliste zur Corona-Kontaktverfolgung der Abteilung Judo des CWSV vom ".$date->format("Y-m-d")."\n\n";
foreach($attendancesAssocArray as $d => $attendees){
$ret .= "## ".$d."\n";
$i=0;
foreach($attendees as $a){
$i += 1;
$ret .= "\n";
$ret .= $i." ".$a['name'].", ".$a['vorname']."\n";
$ret .= " - PLZ: ".$a['corona_PLZ']."\n";
$ret .= " - Tel.: ".$a['corona_telephon']."\n";
$ret .= " - eMail: ".$a['corona_eMail']."\n";
}
$ret .= "\n";
}
return $ret;
}
/// Validaing a phone number
/// true if it validates, false if not
function validate_phone_number($phone)
{
// Allow +, - and . in phone number
$filtered_phone_number = filter_var($phone, FILTER_SANITIZE_NUMBER_INT);
// Remove "-" from number
$phone_to_check = str_replace("-", "", $filtered_phone_number);
// Check the lenght of number
// This can be customized if you want phone number from a specific country
if (strlen($phone_to_check) < 10 || strlen($phone_to_check) > 14) {
return false;
} else {
return true;
}
}
/// validate different types of input
function isValid($toValidate, $type){
// for now we disable the name validation: what do i know how people can be called!
// $regexName="/^[A-Z][a-zA-Z]*$/";
$regexPlz ="/^[0-9]{5}$/";
switch( $type ){
case "plz":
return preg_match($regexPlz, $toValidate) > 0;
case "phonenumber":
return validate_phone_number($toValidate);
case "email":
return filter_var($toValidate, FILTER_VALIDATE_EMAIL);
default:
return false;
}
return false;
}
//! Checks if multiple keys exist in an array
//!
//! @param array $array array to check for key
//! @param array|string $keys keys to check for
//!
//! @return bool true, if *all* keys are set in the array
function array_keys_exist( array $array, $keys ) {
if ( ! is_array( $keys ) ) {
$keys = func_get_args();
array_shift( $keys );
}
$count = 0;
foreach ( $keys as $key ) {
if ( isset( $array[$key] ) || array_key_exists( $key, $array ) ) {
$count++;
}
}
return count( $keys ) === $count;
}
?>