<?php
namespace App\Service;
use Carbon\Carbon;
use Pimcore\Model\WebsiteSetting;
use GuzzleHttp\Client;
class Akimedia {
public static function login() {
$url = WebsiteSetting::getByName('akimedia-url')->getData();
$login = WebsiteSetting::getByName('akimedia-login')->getData();
$password = WebsiteSetting::getByName('akimedia-password')->getData();
$body = [
"email" => $login,
"password" => $password,
];
$client = new Client();
$response = $client->request('POST', "$url/api/login", [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'body' => json_encode($body)
]);
$results = json_decode($response->getBody(), true);
if($results['success']){
return $results['token'];
}else{
return $results['error'];
}
}
public static function getSites($request) {
$url = WebsiteSetting::getByName('akimedia-url')->getData();
$session = $request->getSession();
$token = $session->get('akimedia-token');
$client = new Client();
$response = $client->request('GET', "$url/api/getPublicSites", [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Bearer '.$token,
]
]);
$results = json_decode($response->getBody(), true);
if($results['success']){
$siteArray = [];
foreach($results['sites'] as $region){
$siteArray = array_merge($siteArray, $region);
}
return $siteArray;
}else{
return $results['error'];
}
}
public static function getSitesByProvinces($request) {
$url = WebsiteSetting::getByName('akimedia-url')->getData();
$session = $request->getSession();
$token = $session->get('akimedia-token');
$client = new Client();
$response = $client->request('GET', "$url/api/getPublicSites", [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Bearer '.$token,
]
]);
$results = json_decode($response->getBody(), true);
if($results['success']){
return $results['sites'];
}else{
return $results['error'];
}
}
public static function getCollectesAndSites($request) {
$url = WebsiteSetting::getByName('akimedia-url')->getData();
$session = $request->getSession();
$token = $session->get('akimedia-token');
if($request->get('cp')){
$distanceMax = \Pimcore\Model\WebsiteSetting::getByName('rayon-max-recherche', null)->getData();
}else{
$distanceMax = \Pimcore\Model\WebsiteSetting::getByName('rayon-max-si-pas-lieu', null)->getData();
}
if(strpos($request->get('du'), "/") !== false){
$du = Carbon::createFromFormat('d/m/Y', $request->get('du'))->format("Y-m-d");
} else{
$du = Carbon::parse($request->get('du'))->format("Y-m-d");
}
if(strpos($request->get('au'), "/") !== false){
$au = Carbon::createFromFormat('d/m/Y', $request->get('au'))->format("Y-m-d");
} else{
$au = Carbon::parse($request->get('au'))->format("Y-m-d");
}
$sang = false;
$plasma = true;
$plaquette = false;
$body = [
"zip" => intval($request->get('cp')) ? intval($request->get('cp')) : 1000,
"distance" => $distanceMax,
"date_begin" => $du,
"date_end" => $au,
"types" => [
"sang" => $sang,
"plaquette" => $plasma,
"plasma" => $plaquette,
],
];
$client = new Client();
$response = $client->request('GET', "$url/api/getPublicSearch", [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Bearer '.$token,
],
'body' => json_encode($body)
]);
$results = json_decode($response->getBody(), true);
if($results['success']){
$return = [
"collectes" => $results["collectes"],
"sites" => $results["sites"]
];
return $return;
}else{
return $results['error'];
}
}
public static function getCollectesByCode($request) {
$url = WebsiteSetting::getByName('akimedia-url')->getData();
$session = $request->getSession();
$token = $session->get('akimedia-token');
$body = [
"code" => $request->get('code'),
"lang" => strtoupper($request->getLocale())
];
$client = new Client();
$response = $client->request('GET', "$url/api/getPublicCollecteByCode", [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Bearer '.$token,
],
'body' => json_encode($body)
]);
$results = json_decode($response->getBody(), true);
if($results['success']){
$return = [
"collectes" => $results["collectes"]
];
return $return;
}else{
return $results['error'];
}
}
public static function getCollectesByCodeAndDate($request, $collecteCode, $date) {
$url = WebsiteSetting::getByName('akimedia-url')->getData();
$session = $request->getSession();
$token = $session->get('akimedia-token');
$body = [
"code" => $collecteCode,
"date" => $date,
"lang" => strtoupper($request->getLocale())
];
$client = new Client();
$response = $client->request('GET', "$url/api/getPublicCollecteByDateAndCode", [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Bearer '.$token,
],
'body' => json_encode($body)
]);
$results = json_decode($response->getBody(), true);
if($results['success']){
return $results;
}else{
return $results['error'];
}
}
public static function getSiteHoraire($request, $siteid) {
$url = WebsiteSetting::getByName('akimedia-url')->getData();
$session = $request->getSession();
$token = $session->get('akimedia-token');
$body = [
"site_id" => $siteid,
"lang" => strtoupper($request->getLocale()),
];
$client = new Client();
$response = $client->request('GET', "$url/api/getPublicSiteInfo", [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Bearer '.$token,
],
'body' => json_encode($body)
]);
$results = json_decode($response->getBody(), true);
if($results['success']){
return $results["days"] ;
}else{
return $results['error'];
}
}
public static function getSiteAvailabilities($request, $siteid) {
$url = WebsiteSetting::getByName('akimedia-url')->getData();
$session = $request->getSession();
$token = $session->get('akimedia-token');
$body = [
"site_id" => $siteid,
"lang" => strtoupper($request->getLocale()),
];
$client = new Client();
$response = $client->request('GET', "$url/api/getPublicSiteInfo", [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Bearer '.$token,
],
'body' => json_encode($body)
]);
$results = json_decode($response->getBody(), true);
if($results['success']){
$dates = [];
foreach($results["dates_available"] as $month){
if($month){
foreach($month as $key => $date){
if($date["template_id"] > 0){
$dates[] = $key;
}
}
}
}
return $dates;
}else{
return $results['error'];
}
}
public static function getSiteHorairesByDate($request, $siteid) {
$url = WebsiteSetting::getByName('akimedia-url')->getData();
$session = $request->getSession();
$token = $session->get('akimedia-token');
$date = $request->get("date");
$body = [
"site_id" => $siteid,
"date" => $date,
"type" => "plasma"
];
$client = new Client();
$response = $client->request('GET', "$url/api/getPublicSiteHorairesByDate", [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Bearer '.$token,
],
'body' => json_encode($body)
]);
$results = json_decode($response->getBody(), true);
if($results['success']){
return $results;
}else{
if(isset($results['errorCode']) && $results['errorCode'] == "NO_SCHEDULE_FOUND"){
return [];
}
return $results['error'];
}
}
public static function getSiteInfos($request, $siteid) {
$url = WebsiteSetting::getByName('akimedia-url')->getData();
$session = $request->getSession();
$token = $session->get('akimedia-token');
$body = [
"site_id" => $siteid,
"lang" => strtoupper($request->getLocale()),
];
$client = new Client();
$response = $client->request('GET', "$url/api/getPublicSiteInfo", [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Bearer '.$token,
],
'body' => json_encode($body)
]);
$results = json_decode($response->getBody(), true);
if($results['success']){
return $results;
}else{
return $results['error'];
}
}
public static function sendIncriptionCollecte($request, $data, $collecteID) {
$url = WebsiteSetting::getByName('akimedia-url')->getData();
$session = $request->getSession();
$token = $session->get('akimedia-token');
if($data["alreadyBlood"]=="blood_yes"){
$data["dejaDonne"] = "OUI";
}else{
$data["dejaDonne"] = "NON";
}
$body = [
"collecte_id" => $collecteID,
"horaire" => $data['heure'],
"firstname" => $data['firstname'],
"lastname" => $data['lastname'],
"birthdate" => $data['datenaissance'],
"email" => $data['email'],
"phone" => $data['phone'],
"has_already_donated" => $data["dejaDonne"]
];
$client = new Client();
$response = $client->request('GET', "$url/api/getPublicInscriptionCollecte", [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Bearer '.$token,
],
'body' => json_encode($body)
]);
$results = json_decode($response->getBody(), true);
if($results['success']){
return true;
}else{
$message = "";
switch ($results['errorCode']){
case "MISSING_SITE_ID":
$message = "Il manque l'id du site";
break;
case "MISSING_SCHEDULE":
$message = "Il manque l'horaire";
break;
case "MISSING_DATE":
$message = "Il manque la date";
break;
case "RDV_DATE_PAST":
$message = "La date ne peut être dans le passé";
break;
case "MISSING_LASTNAME":
$message = "Il manque le nom du donneur";
break;
case "MISSING_FIRSTNAME":
$message = "Il manque le prénom du donneur";
break;
case "MISSING_BIRTHDATE":
$message = "Il manque la date de naissance du donneur";
break;
case "MISSING_EMAIL_OR_PHONE":
$message = "Vous devez compléter soit l'email, soit le téléphone";
break;
case "EXISTING_DONOR":
$message = "Un donneur existe déjà avec les mêmes Nom / Prénom / Date de naissance";
break;
case "BUG_CREATE_DONOR":
$message = "Problème lors de la sauvegarde";
break;
case "BUG_DONOR_ID":
$message = "Id du donneur manquant.";
break;
case "BUG_GET_SCHEDULES":
$message = "Erreur lors de la récupération des horaires";
break;
case "RDV_SCHEDULE_NOT_FOUND":
$message = "Cet horaire n'existe pas pour cette journée.";
break;
case "RDV_ALREADY_BOOKED":
$message = "Il n'y a plus de disponibilité pour cet horaire.";
break;
}
return $message;
}
}
public static function sendIncriptionSite($request, $data, $siteid) {
$url = WebsiteSetting::getByName('akimedia-url')->getData();
$session = $request->getSession();
$token = $session->get('akimedia-token');
$body = [
"site_id" => $siteid,
"date" => $data['date'],
"horaire" => $data['heure'],
"firstname" => $data['firstname'],
"lastname" => $data['lastname'],
"birthdate" => Carbon::createFromFormat("d/m/Y", $data['datenaissance'])->format("Y-m-d"),
"email" => $data['email'],
"phone" => $data['phone'],
"has_already_donated" => $data["dejaDonnePlasma"],
//"has_already_donated" => $data["dejaDonneSang"],
//"has_already_donated_plasma" => $data["dejaDonnePlasma"],
"type" => $data["type"],
"language" => strtoupper($request->getLocale()),
"origine" => "donneurdeplasma.be",
];
$client = new Client();
$response = $client->request('GET', "$url/api/getPublicInscriptionSite", [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Bearer '.$token,
],
'body' => json_encode($body)
]);
$results = json_decode($response->getBody(), true);
if($results['success']){
return true;
}else{
$message = "";
switch ($results['errorCode']){
case "MISSING_SITE_ID":
$message = "Il manque l'id du site";
break;
case "MISSING_SCHEDULE":
$message = "Il manque l'horaire";
break;
case "MISSING_DATE":
$message = "Il manque la date";
break;
case "RDV_DATE_PAST":
$message = "La date ne peut être dans le passé";
break;
case "MISSING_LASTNAME":
$message = "Il manque le nom du donneur";
break;
case "MISSING_FIRSTNAME":
$message = "Il manque le prénom du donneur";
break;
case "MISSING_BIRTHDATE":
$message = "Il manque la date de naissance du donneur";
break;
case "MISSING_EMAIL_OR_PHONE":
$message = "Vous devez compléter soit l'email, soit le téléphone";
break;
case "EXISTING_DONOR":
$message = "Un donneur existe déjà avec les mêmes Nom / Prénom / Date de naissance";
break;
case "BUG_CREATE_DONOR":
$message = "Problème lors de la sauvegarde";
break;
case "BUG_DONOR_ID":
$message = "Id du donneur manquant.";
break;
case "BUG_GET_SCHEDULES":
$message = "Erreur lors de la récupération des horaires";
break;
case "RDV_SCHEDULE_NOT_FOUND":
$message = "Cet horaire n'existe pas pour cette journée.";
break;
case "RDV_ALREADY_BOOKED":
$message = "Il n'y a plus de disponibilité pour cet horaire.";
break;
}
return $message;
}
}
public static function getPlasmaPlages($request, $translator) {
$url = WebsiteSetting::getByName('akimedia-url')->getData();
$session = $request->getSession();
$token = $session->get('akimedia-token');
$body = [
];
$client = new Client();
$response = $client->request('GET', "$url/api/getPublicBarometre", [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Bearer '.$token,
],
'body' => json_encode($body)
]);
$results = json_decode($response->getBody(), true);
$slots = $results["sfs_slots_availables"];
if($results["rate"] == 1){
$txtstatus = $translator->trans("Largement insuffisant");
$status = "tres-critique";
}
elseif($results["rate"] == 2){
$txtstatus = $translator->trans("Insuffisant");
$status = "critique";
}
elseif($results["rate"] == 3){
$txtstatus = $translator->trans("Faible");
$status = "tres-fragile";
}
elseif($results["rate"] == 4){
$txtstatus = $translator->trans("Bonne");
$status = "fragile";
}
elseif($results["rate"] == 5){
$txtstatus = $translator->trans("Au Top");
$status = "stable";
}
return [
"status" => $status,
"texte_status" => $txtstatus,
"plages" => $slots
];
}
}