7d684b3624
- add 2 scripts (1 to add the id, the second to fix unsubscriptions if any) - get rid of a long commented sql query in the original script - add methods in mailjet library to get the return status of the batch performed in the 2 scripts
227 lines
6.0 KiB
PHP
227 lines
6.0 KiB
PHP
<?php
|
|
die('script disabled');
|
|
/**
|
|
* Permet de syncro des clients dans les contacts mailjet
|
|
*/
|
|
/*
|
|
if (!isset($_SERVER['REMOTE_ADDR'])) {
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
|
}
|
|
|
|
$authorized_ip = array(
|
|
'88.163.22.99',
|
|
'90.63.178.63',
|
|
'127.0.0.1'
|
|
);
|
|
|
|
if (!in_array($_SERVER['REMOTE_ADDR'], $authorized_ip)) {
|
|
die('You are not allowed');
|
|
}
|
|
include(dirname(__FILE__).'/../../config/config.inc.php');
|
|
include(dirname(__FILE__).'/mailjet_sync.php');
|
|
|
|
$check_jobid = '';
|
|
if (count($argv)>1) {
|
|
$check_jobid = $argv[1];
|
|
}
|
|
|
|
$api_version = (Configuration::get('MAILJETSYNC_OLD_API') == true) ? 1 : 3;
|
|
$api_key = Configuration::get('MAILJETSYNC_API_KEY');
|
|
$api_secret = Configuration::get('MAILJETSYNC_SECRET_KEY');
|
|
$mj = MailjetSyncApiClientFactory::create($api_version, $api_key, $api_secret);
|
|
|
|
if ($check_jobid!='') {
|
|
print_r($mj->getJobStatus($check_jobid));
|
|
die();
|
|
}
|
|
|
|
$offset = 150000;
|
|
$customers = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
|
|
SELECT c.`id_customer`, c.`email`, c.`firstname`, c.`lastname`, c.`newsletter`, c.`frequence_nw`, c.`date_add`
|
|
FROM '. _DB_PREFIX_ .'customer c
|
|
WHERE c.`active` = 1
|
|
AND c.`newsletter` = 1
|
|
LIMIT 5000 OFFSET '.$offset.'
|
|
');
|
|
// $customers = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
|
|
// SELECT c.`id_customer`, c.`email`, c.`firstname`, c.`lastname`, c.`newsletter`, c.`frequence_nw`, c.`date_add`
|
|
// FROM '. _DB_PREFIX_ .'customer c
|
|
// WHERE c.`active` = 1
|
|
// AND c.`newsletter` = 1
|
|
// AND c.`id_customer` = 145858
|
|
// ');
|
|
// foreach($customers as $i => $customer) {
|
|
// echo $i .' - '.$customer['id_customer'].' - "'.$customer['email'].'"';
|
|
// echo PHP_EOL;
|
|
// }
|
|
// print_r($customers);
|
|
// echo PHP_EOL;
|
|
|
|
$contacts_to_add = array(
|
|
'q' => array(),
|
|
'h' => array(),
|
|
'h3' => array(),
|
|
);
|
|
$customer_pro = array();
|
|
$customer_part = array();
|
|
|
|
foreach ($customers as $customer) {
|
|
$fai = explode("@", $customer['email']);
|
|
$fai = explode(".", $fai[1]);
|
|
$fai = $fai[0];
|
|
if ($fai == 'antadis' && $customer['email']!='figaro+pdm@antadis.com') {
|
|
continue;
|
|
}
|
|
|
|
$isPro = Customer::isGroupPro((int) $customer['id_customer']);
|
|
|
|
switch ($customer['frequence_nw']) {
|
|
case '0':
|
|
$frequence = 'quotidien';
|
|
$index_frequence = 'q';
|
|
break;
|
|
case '1':
|
|
$frequence = 'hebdomadaire';
|
|
$index_frequence = 'h';
|
|
break;
|
|
case '2':
|
|
$frequence = '3 fois par semaine';
|
|
$index_frequence = 'h3';
|
|
break;
|
|
default:
|
|
$frequence = 'quotidien';
|
|
$index_frequence = 'q';
|
|
break;
|
|
}
|
|
|
|
$details = array(
|
|
"Email" => $customer['email'],
|
|
"Properties" => array(
|
|
"token" => Customer::getUrlAutologin((int) $customer['id_customer'], $customer['email']),
|
|
"frequence" => $frequence,
|
|
"prenom" => $customer['firstname'],
|
|
"nom" => $customer['lastname'],
|
|
"fai" => $fai,
|
|
"group" => $isPro ? 'Pro' : 'Particulier',
|
|
"date_inscription" => date('Y-m-d\TH:i:s\Z',strtotime($customer['date_add'])),
|
|
"ref_inscription" => (int)strtotime($customer['date_add']),
|
|
"id" => $customer['id_customer']
|
|
)
|
|
);
|
|
|
|
$contacts_to_add[$index_frequence][] = $details;
|
|
if ($isPro) {
|
|
$customer_pro[] = $details;
|
|
} else {
|
|
$customer_part[] = $details;
|
|
}
|
|
}
|
|
|
|
|
|
$listAdd = array(
|
|
'q' => array(
|
|
array(
|
|
"ListID" => Configuration::get('MAILJETSYNC_LIST_ID_Q'),
|
|
"Action" => "addnoforce"
|
|
)
|
|
),
|
|
'h' => array(
|
|
array(
|
|
"ListID" => Configuration::get('MAILJETSYNC_LIST_ID_H'),
|
|
"Action" => "addnoforce"
|
|
)
|
|
),
|
|
'h3' => array(
|
|
array(
|
|
"ListID" => Configuration::get('MAILJETSYNC_LIST_ID_3'),
|
|
"Action" => "addnoforce"
|
|
)
|
|
)
|
|
);
|
|
$listAddPart = array(
|
|
array(
|
|
"ListID" => Configuration::get('MAILJETSYNC_LIST_ID'),
|
|
"Action" => "addnoforce"
|
|
),
|
|
);
|
|
$listAddPro = array(
|
|
array(
|
|
"ListID" => Configuration::get('MAILJETSYNC_LIST_ID_PRO'),
|
|
"Action" => "addnoforce"
|
|
),
|
|
);
|
|
|
|
echo 'NB TO UPDATE'.PHP_EOL;
|
|
var_dump(
|
|
'q : '.count($contacts_to_add['q']),
|
|
'h : '.count($contacts_to_add['h']),
|
|
'h3 : '.count($contacts_to_add['h3']),
|
|
'part : '.count($customer_part),
|
|
'pro : '.count($customer_pro)
|
|
);
|
|
|
|
$jobIDs = array();
|
|
$adds_q = $mj->addDetailedContactToList($contacts_to_add['q'], $listAdd['q']);
|
|
foreach($mj->getResponseJobIDs() as $JobID) {
|
|
$jobIDs[] = $JobID;
|
|
}
|
|
|
|
$adds_h = $mj->addDetailedContactToList($contacts_to_add['h'], $listAdd['h']);
|
|
foreach($mj->getResponseJobIDs() as $JobID) {
|
|
$jobIDs[] = $JobID;
|
|
}
|
|
|
|
$adds_h3 = $mj->addDetailedContactToList($contacts_to_add['h3'], $listAdd['h3']);
|
|
foreach($mj->getResponseJobIDs() as $JobID) {
|
|
$jobIDs[] = $JobID;
|
|
}
|
|
|
|
$addsPart = $mj->addDetailedContactToList($customer_part, $listAddPart);
|
|
foreach($mj->getResponseJobIDs() as $JobID) {
|
|
$jobIDs[] = $JobID;
|
|
}
|
|
|
|
$addsPro = $mj->addDetailedContactToList($customer_pro, $listAddPro);
|
|
foreach($mj->getResponseJobIDs() as $JobID) {
|
|
$jobIDs[] = $JobID;
|
|
}
|
|
|
|
echo "UPDATING RESULT".PHP_EOL;
|
|
var_dump(
|
|
$adds_q,
|
|
$adds_h,
|
|
$adds_h3,
|
|
$addsPart,
|
|
$addsPro
|
|
);
|
|
|
|
$nb_second_sleep = 10;
|
|
echo "SLEEP $nb_second_sleep SECONDS BEFORE CHECKING JOB STATUS...";
|
|
sleep($nb_second_sleep);
|
|
|
|
echo "JOB STATUS".PHP_EOL;
|
|
echo PHP_EOL;
|
|
|
|
$is_complete = true;
|
|
|
|
foreach($jobIDs as $JobID) {
|
|
$status = $mj->getJobStatus($JobID);
|
|
echo "JobID : ".$JobID;
|
|
echo PHP_EOL;
|
|
echo "Count : ".$status->Data[0]->Count;
|
|
echo " - status : ".$status->Data[0]->Status;
|
|
echo PHP_EOL;
|
|
|
|
$is_complete = $is_complete && ($status->Data[0]->Status == 'Completed');
|
|
}
|
|
echo PHP_EOL;
|
|
|
|
if ($is_complete) {
|
|
echo "UPDATE COMPLETED";
|
|
}
|
|
else {
|
|
echo "UPDATE IN PROGRESS";
|
|
}
|
|
echo PHP_EOL;
|
|
|
|
*/ |