136 lines
3.7 KiB
PHP
136 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Lib;
|
|
|
|
class MesBR
|
|
{
|
|
|
|
private $endpoint;
|
|
|
|
private $refSite;
|
|
|
|
private $refCamp;
|
|
|
|
public function __construct($endpoint, $refSite, $refCamp)
|
|
{
|
|
$this->endpoint = $endpoint;
|
|
$this->refSite = $refSite;
|
|
$this->refCamp = $refCamp;
|
|
}
|
|
|
|
/**
|
|
* Undocumented function
|
|
* updateContact
|
|
*
|
|
* @param [type] $oCrmUser
|
|
* @return void
|
|
*/
|
|
public function updateContact($oCrmUser)
|
|
{
|
|
if (!$oCrmUser->birth_date || $oCrmUser->birth_date === '0000-00-00') {
|
|
$newDateNaissance = '01011990';
|
|
} else {
|
|
$tmpDate = date_create_from_format('Y-m-d', $oCrmUser->birth_date);
|
|
$newDateNaissance = $tmpDate->format('dmY');
|
|
}
|
|
if ($oCrmUser->country === null) {
|
|
$pays = 250;
|
|
} else {
|
|
$pays = $this->getIsoCountry($oCrmUser->country);
|
|
}
|
|
$url = '/updatecontact?refsite=' . trim($this->refSite) .
|
|
'&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;
|
|
|
|
$this->callApi("GET", $url, false);
|
|
// TODO : Verif statut du call ?
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Génère le lien vers le mur (ou panier préprogrammé)
|
|
*
|
|
* @param object $crmUser
|
|
* @return string url
|
|
*/
|
|
public function getMurBdrLink($crmUser)
|
|
{
|
|
return sprintf(
|
|
$this->endpoint . "/?refsite=%s&refcamp=%d&hash=%s&now=%s",
|
|
$this->refSite,
|
|
$this->refCamp,
|
|
$crmUser->hashcode,
|
|
time()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Undocumented function
|
|
*
|
|
* @return object
|
|
*/
|
|
public function getDetailCampaign()
|
|
{
|
|
return $this->callApi('GET', sprintf(
|
|
'/getdetailcampaign?refsite=%s&refcamp=%s',
|
|
$this->refSite,
|
|
$this->refCamp
|
|
), false);
|
|
}
|
|
|
|
private function getIsoCountry($country)
|
|
{
|
|
$country = trim(ucfirst(strtolower($country)));
|
|
switch ($country) {
|
|
case 'Belgique':
|
|
return 56;
|
|
case 'Luxembourg':
|
|
return 442;
|
|
default:
|
|
return 250; //France
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
if ($data) {
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
|
|
}
|
|
break;
|
|
case "PUT":
|
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
|
|
if ($data) {
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
|
|
}
|
|
break;
|
|
default:
|
|
if ($data) {
|
|
$url = sprintf("%s?%s", $url, http_build_query($data));
|
|
}
|
|
}
|
|
curl_setopt($curl, CURLOPT_URL, $this->endpoint . $url);
|
|
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
$result = curl_exec($curl);
|
|
if (!$result) {
|
|
die("Connection Failure");
|
|
}
|
|
curl_close($curl);
|
|
return json_decode($result);
|
|
}
|
|
}
|