ldc-legaulois/app/Lib/MesBR.php

137 lines
3.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Lib;
class MesBR
{
2021-03-20 14:38:15 +01:00
2021-06-17 14:45:34 +02:00
private $endpoint;
private $refSite;
2021-03-20 14:38:15 +01:00
private $refCamp;
2021-06-17 14:45:34 +02:00
public function __construct($endpoint, $refSite, $refCamp)
{
2021-06-17 14:45:34 +02:00
$this->endpoint = $endpoint;
$this->refSite = $refSite;
$this->refCamp = $refCamp;
}
2021-03-21 22:51:19 +01:00
/**
* Undocumented function
* updateContact
*
* @param [type] $oCrmUser
* @return void
*/
public function updateContact($oCrmUser)
{
2021-05-03 12:16:54 +02:00
if (!$oCrmUser->birth_date || $oCrmUser->birth_date === '0000-00-00') {
$newDateNaissance = '01011990';
} else {
2021-05-03 12:16:54 +02:00
$tmpDate = date_create_from_format('Y-m-d', $oCrmUser->birth_date);
$newDateNaissance = $tmpDate->format('dmY');
}
2021-05-03 12:16:54 +02:00
if ($oCrmUser->country === null) {
$pays = 250;
} else {
2021-05-03 12:16:54 +02:00
$pays = $this->getIsoCountry($oCrmUser->country);
}
2021-06-17 14:45:34 +02:00
$url = '/updatecontact?refsite=' . trim($this->refSite) .
2021-05-03 12:16:54 +02:00
'&refadherent=' . urlencode($oCrmUser->crm_id) . '&nom=' . urlencode($oCrmUser->lastname) .
'&prenom=' . urlencode($oCrmUser->firstname) . '&email='
. $oCrmUser->email . '&date_naissance=' . $newDateNaissance .
'&code_postal=' . $oCrmUser->postcode . '&commune=' . urlencode($oCrmUser->city) .
'&pays=' . $pays . '&hashcode=' . $oCrmUser->hashcode;
2021-03-21 22:51:19 +01:00
$this->callApi("GET", $url, false);
// TODO : Verif statut du call ?
return true;
}
2021-03-21 22:51:19 +01:00
/**
* Génère le lien vers le mur (ou panier préprogrammé)
*
* @param object $crmUser
* @return string url
*/
2021-05-03 12:16:54 +02:00
public function getMurBdrLink($crmUser)
2021-03-21 22:51:19 +01:00
{
return sprintf(
2021-06-17 14:45:34 +02:00
$this->endpoint . "/?refsite=%s&refcamp=%d&hash=%s&now=%s",
2021-05-03 12:16:54 +02:00
$this->refSite,
$this->refCamp,
$crmUser->hashcode,
time()
2021-03-21 22:51:19 +01:00
);
}
/**
* Undocumented function
*
* @return object
*/
public function getDetailCampaign()
{
2021-03-21 22:51:19 +01:00
return $this->callApi('GET', sprintf(
2021-06-17 14:45:34 +02:00
'/getdetailcampaign?refsite=%s&refcamp=%s',
2021-03-21 22:51:19 +01:00
$this->refSite,
$this->refCamp
), false);
}
2021-03-21 22:51:19 +01:00
2021-05-03 12:16:54 +02:00
private function getIsoCountry($country)
{
2021-05-03 12:16:54 +02:00
$country = trim(ucfirst(strtolower($country)));
switch ($country) {
case 'Belgique':
return 56;
case 'Luxembourg':
return 442;
default:
2021-05-03 12:16:54 +02:00
return 250; //France
}
}
2021-03-21 22:51:19 +01:00
/**
* Undocumented function
*
* @param string $method
* @param string $url
* @param string $data
* @return object
*/
private function callApi(string $method, string $url, string $data)
{
$curl = curl_init();
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
2021-03-20 14:38:15 +01:00
if ($data) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
2021-03-20 14:38:15 +01:00
}
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
2021-03-20 14:38:15 +01:00
if ($data) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
2021-03-20 14:38:15 +01:00
}
break;
default:
2021-03-20 14:38:15 +01:00
if ($data) {
$url = sprintf("%s?%s", $url, http_build_query($data));
2021-03-20 14:38:15 +01:00
}
}
2021-06-17 14:45:34 +02:00
curl_setopt($curl, CURLOPT_URL, $this->endpoint . $url);
2021-03-20 14:38:15 +01:00
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
if (!$result) {
die("Connection Failure");
}
curl_close($curl);
2021-03-31 11:04:04 +02:00
return json_decode($result);
}
}