144 lines
3.3 KiB
PHP
144 lines
3.3 KiB
PHP
<?php
|
|
/**
|
|
* 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';
|
|
$_SERVER['SERVER_PORT'] = 80;
|
|
$_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($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";
|
|
}
|
|
}
|
|
} |