Rework emarsys unsubscribe script

This commit is contained in:
Michael RICOIS 2018-01-24 12:45:06 +01:00
parent 79221931e4
commit 0ec8703fb9

View File

@ -2,6 +2,7 @@
/**
* Import customer details from Emarsys
* - Set newsletter info on customer
* - File format : user_id;newsletter_bebeboutik;E-mail;Opt-in
*/
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
@ -10,41 +11,134 @@ $_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
include_once dirname(__FILE__).'/www/config/config.inc.php';
$longopts = array(
'verbose',
'dry-run',
'file:',
);
$shortopts = "";
$options = getopt($shortopts, $longopts);
// Options
$optVerbose = false;
if (isset($options['verbose'])) {
$optVerbose = true;
}
$optTest = false;
if (isset($options['dry-run'])) {
$optTest = true;
}
$optFile = false;
if (isset($options['file'])) {
$optFile = $options['file'];
}
if ($optFile === false) {
$fileList = glob(dirname(__FILE__).'/emarsys/*.csv');
}
else {
$fileList = array();
$fileList[] = $optFile;
}
if ($fileList === false) {
exit;
}
if (count($fileList) == 0) {
exit;
}
$cols = array(
'id_customer',
'newsletter',
'email',
'optin',
);
$db = Db::getInstance();
// List all files in dir
foreach(glob(dirname(__FILE__).'/emarsys/*.csv') as $filename) {
$f = fopen($filename, 'r');
// By-pass first line
fgetcsv($f);
// Parse line
while($line = fgetcsv($f, 0, ';', '"')) {
if(in_array((int) $line[1], array(0, 1, 2))
&& !empty($line[2]) && in_array(strtolower($line[3]), array('vrai', 'faux'))
&& Validate::isEmail($line[2])) {
if($customer = $db->getRow('
SELECT `id_customer`
FROM `'._DB_PREFIX_.'customer`
WHERE `email` = "'.pSQL($line[2]).'"
')) {
if(strtolower($line[3]) == 'faux') {
$active = 0;
} else {
$active = (int) $line[1];
}
$db->ExecuteS('
UPDATE `'._DB_PREFIX_.'customer`
SET `newsletter` = '.(int) $active.',
`newsletter_date_add` = NOW()
WHERE `email` = "'.pSQL($line[2]).'"
LIMIT 1
');
}
}
}
fclose($f);
// Move file as done
rename($filename, str_replace('/emarsys/', '/emarsys_done/', $filename));
foreach($fileList as $filename) {
if ($optVerbose) {
echo $filename."\n";
}
$row = 0;
if (($handle = fopen($filename, "r")) !== false) {
while (($data = fgetcsv($handle, 0, ';', '"')) !== false) {
$row++;
// By-pass first line
if ($row == 1) {
continue;
}
// Get Data
$num = count($data);
// Check line integrity
if ($num != count($cols)) {
continue;
}
// Assign var
$id_customer = null;
$newsletter = null;
$email = null;
$optin = null;
for ($c=0; $c<$num; $c++) {
${$cols[$c]} = $data[$c];
}
// Business logic - check
if (empty($email)) {
continue;
}
if (!Validate::isEmail($email)) {
continue;
}
if (!in_array(strtolower($optin), array('vrai', 'faux'))) {
continue;
}
// Looks for a customer
$customer = $db->getRow('SELECT `id_customer`
FROM `'._DB_PREFIX_.'customer`
WHERE `email` = "'.pSQL($email).'"
');
if (!$customer) {
continue;
}
// Opt-In
if(strtolower($optin) == 'faux') {
$active = 0;
} else {
$active = (int) $newsletter;
}
if ($optTest) {
echo "Customer ID: ".$customer['id_customer']." - Active: ".$active."\n";
}
// Update customer
else {
$db->ExecuteS('UPDATE `'._DB_PREFIX_.'customer`
SET `newsletter` = '.(int) $active.', `newsletter_date_add` = NOW()
WHERE `id_customer` = '.$customer['id_customer'].' LIMIT 1
');
}
}
fclose($handle);
if ($optTest) {
continue;
}
// Move file as done
rename($filename, str_replace('/emarsys/', '/emarsys_done/', $filename));
}
else {
if ($optVerbose) {
echo "File $filename can't be open.\n";
}
}
}