Merge branch 'ticket/r15258-newsletter-emarsys'
This commit is contained in:
commit
8ac2ec0797
@ -1,37 +1,144 @@
|
||||
<?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();
|
||||
|
||||
foreach(glob(dirname(__FILE__).'/emarsys/*.csv') as $filename) {
|
||||
$f = fopen($filename, 'r');
|
||||
fgetcsv($f);
|
||||
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);
|
||||
rename($filename, str_replace('/emarsys/', '/emarsys_done/', $filename));
|
||||
// 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";
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,9 @@
|
||||
<?php
|
||||
/**
|
||||
* Export customer details to Emarsys
|
||||
* - Select all customers
|
||||
*/
|
||||
|
||||
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
|
||||
$_SERVER['SERVER_NAME'] = 'www.bebeboutik.com';
|
||||
$_SERVER['HTTP_PORT'] = 80;
|
||||
@ -6,165 +11,6 @@ include('www/config/config.inc.php');
|
||||
|
||||
ini_set('memory_limit', '6G');
|
||||
|
||||
|
||||
class WebDAV {
|
||||
|
||||
var $host;
|
||||
var $user;
|
||||
var $pass;
|
||||
var $connected;
|
||||
var $fp;
|
||||
|
||||
function WebDAV($host,$user,$pass) {
|
||||
$this->host = $host;
|
||||
$this->user = $user;
|
||||
$this->pass = $pass;
|
||||
$this->connected = false;
|
||||
}
|
||||
|
||||
|
||||
function connect() {
|
||||
$this->fp = fsockopen($this->host, 80, $errno, $errstr, 5);
|
||||
if(!$this->fp) {
|
||||
$this->connected = false;
|
||||
return false;
|
||||
} else {
|
||||
$this->connected = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* check if the connection was established
|
||||
* returns true if connected otherwise false
|
||||
*/
|
||||
function isConnected() {
|
||||
return $this->connected;
|
||||
}
|
||||
|
||||
/*
|
||||
* get the file listing from webdav
|
||||
*/
|
||||
function getListing($directory='/') {
|
||||
$listing = array();
|
||||
if ($this->isConnected() == false) {
|
||||
$this->connect();
|
||||
}
|
||||
if ($this->isConnected() == false) {
|
||||
return $listing;
|
||||
} else {
|
||||
$data = 'PROPFIND '.$directory.' HTTP/1.0
|
||||
Host: '.$this->host.'
|
||||
Connection: close
|
||||
Authorization: Basic '.base64_encode($this->user.':'.$this->pass).'
|
||||
Depth: 1
|
||||
Content-Type: text/xml; charset="utf-8"
|
||||
Content-Length: 93
|
||||
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<D:propfind xmlns:D="DAV:"><D:allprop/></D:propfind>
|
||||
';
|
||||
|
||||
fwrite($this->fp,$data);
|
||||
|
||||
$xml = '';
|
||||
while (!feof($this->fp)) {
|
||||
$xml .= fgets($this->fp, 8192);
|
||||
}
|
||||
|
||||
$responses = $this->getContentInTag('d:response',$xml,true);
|
||||
foreach ($responses as $response) {
|
||||
$hrefs = $this->getContentInTag('d:href',$response,true);
|
||||
$rtype = $this->getContentInTag('d:resourcetype',$response,true);
|
||||
if (trim($rtype[0]) == '') {
|
||||
$listing[$hrefs[0]] = 'file';
|
||||
} else {
|
||||
$listing[$hrefs[0]] = 'dir';
|
||||
}
|
||||
}
|
||||
|
||||
$this->disconnect();
|
||||
return $listing;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
function storeFile($filename, $destFile) {
|
||||
if (!file_exists($filename)) {
|
||||
print "No such file for upload: $filename\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->isConnected() == false) {
|
||||
$this->connect();
|
||||
}
|
||||
if ($this->isConnected() == false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = 'PUT '.$destFile.' HTTP/1.0
|
||||
Host: '.$this->host.'
|
||||
Connection: close
|
||||
Authorization: Basic '.base64_encode($this->user.':'.$this->pass).'
|
||||
Content-Type: application/octet-stream
|
||||
Content-Length: '.filesize($filename).'
|
||||
|
||||
';
|
||||
fwrite($this->fp,$data);
|
||||
// print $data;
|
||||
|
||||
$fh = fopen($filename,'rb');
|
||||
while (!feof($fh)) {
|
||||
$data = fread($fh,8192);
|
||||
fwrite($this->fp,$data);
|
||||
// print $data;
|
||||
}
|
||||
fclose($fh);
|
||||
$this->disconnect();
|
||||
}
|
||||
|
||||
|
||||
function disconnect() {
|
||||
if ($this->isConnected() == true) {
|
||||
fclose($this->fp);
|
||||
$this->connected = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function getContentInTag($tag,&$xml,$stripcdata=false) {
|
||||
$ret = array();
|
||||
if ($xml == '') {
|
||||
print "No XML for tag ".$tag."\n";
|
||||
}
|
||||
$matches = array();
|
||||
if (preg_match_all('|<'.$tag.'[^>]*>(.*?)</'.$tag.'>|is',$xml,$matches)) {
|
||||
if ($stripcdata == true) {
|
||||
foreach ($matches[1] as $match) {
|
||||
if (strpos($match,'<![CDATA[')!==false) {
|
||||
$match = preg_replace('/\s*<!\[CDATA\[/','',$match);
|
||||
$match = preg_replace('/\]\]>\s*/','',$match);
|
||||
$ret[] = $match;
|
||||
} else {
|
||||
$ret[] = $match;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$ret = array();
|
||||
foreach ($matches[1] as $match) {
|
||||
$ret[] = html_entity_decode ($match);
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
$customers_to_import = array();
|
||||
|
||||
// Customer informations
|
||||
@ -182,7 +28,7 @@ foreach (Db::getInstance()->ExecuteS('
|
||||
ORDER BY `id_customer` ASC
|
||||
') as $row) {
|
||||
$customers_to_import[(int)$row['id_customer']] = array(
|
||||
'id_customer' => $row['id_customer'],
|
||||
'id_customer' => $row['id_customer'],
|
||||
'firstname' => $row['firstname'],
|
||||
'lastname' => $row['lastname'],
|
||||
'email' => $row['email'],
|
||||
@ -194,16 +40,10 @@ foreach (Db::getInstance()->ExecuteS('
|
||||
'discount' => ""
|
||||
);
|
||||
}
|
||||
|
||||
$id_customers = array_keys($customers_to_import);
|
||||
|
||||
$filename = date('Y-m-d', mktime()).'.csv';
|
||||
|
||||
$f = fopen('extracts/webdav/'.$filename, 'w');
|
||||
|
||||
fputcsv($f, array('id_customer', 'firstname', 'lastname', 'email', 'ip_registration_newsletter', 'newsletter', 'langue', 'date de premier achat', 'Date de dernier achat', 'discount'), ';', '"');
|
||||
|
||||
for($i=0, $l=count($id_customers); $i < $l; $i+=5000) {
|
||||
// first order
|
||||
// First order
|
||||
foreach (Db::getInstance()->ExecuteS('
|
||||
SELECT o.`id_customer`, DATE_FORMAT(o.`date_add`,\'%d/%m/%Y\') AS `date_first_order`
|
||||
FROM `'._DB_PREFIX_.'orders` o
|
||||
@ -217,7 +57,7 @@ for($i=0, $l=count($id_customers); $i < $l; $i+=5000) {
|
||||
$customers_to_import[(int)$row['id_customer']]['date_first_order'] = $row['date_first_order'];
|
||||
}
|
||||
|
||||
// last order
|
||||
// Last order
|
||||
foreach (Db::getInstance()->ExecuteS('
|
||||
SELECT o.`id_customer`, DATE_FORMAT(MAX(h.`date_add`), \'%d/%m/%Y\') AS `date_last_order`
|
||||
FROM `'._DB_PREFIX_.'order_history` h
|
||||
@ -242,70 +82,28 @@ for($i=0, $l=count($id_customers); $i < $l; $i+=5000) {
|
||||
$customers_to_import[(int)$row['id_customer']]['discount'] = $row['discount'];
|
||||
}
|
||||
}
|
||||
|
||||
$filename = date('Y-m-d', mktime()).'.csv';
|
||||
$f = fopen('extracts/webdav/'.$filename, 'w');
|
||||
$header = array(
|
||||
'id_customer',
|
||||
'firstname',
|
||||
'lastname',
|
||||
'email',
|
||||
'ip_registration_newsletter',
|
||||
'newsletter',
|
||||
'langue',
|
||||
'date de premier achat',
|
||||
'Date de dernier achat',
|
||||
'discount',
|
||||
);
|
||||
fputcsv($f, $header, ';', '"');
|
||||
foreach ($customers_to_import as $customer) {
|
||||
fwrite($f, implode(';', array_values($customer))."\n");
|
||||
}
|
||||
fclose($f);
|
||||
// Ancienne requete trop longue
|
||||
// foreach(Db::getInstance()->ExecuteS('
|
||||
// SELECT
|
||||
// c.`id_customer`,
|
||||
// c.`firstname`,
|
||||
// c.`lastname`,
|
||||
// c.`email`,
|
||||
// c.`ip_registration_newsletter`,
|
||||
// IF(c.`deleted` = 1, 0, IF(c.`active` = 0, 0, c.`newsletter`)) AS `newsletter`,
|
||||
// IFNULL(v.`version`, "fr") AS `version`,
|
||||
// IFNULL(
|
||||
// (
|
||||
// SELECT DATE_FORMAT(o.`date_add`,\'%m/%d/%Y\')
|
||||
// FROM `'._DB_PREFIX_.'orders` o
|
||||
// LEFT JOIN `'._DB_PREFIX_.'order_history` oh ON (oh.`id_order` = o.`id_order`)
|
||||
// WHERE o.`id_customer` = c.`id_customer`
|
||||
// AND oh.`id_order_history` = (SELECT MAX(`id_order_history`) FROM `'._DB_PREFIX_.'order_history` moh WHERE moh.`id_order` = o.`id_order` GROUP BY moh.`id_order`)
|
||||
// AND oh.id_order_state NOT IN (1,14,15,18,6,8,10,11)
|
||||
// ORDER BY o.`date_add` ASC LIMIT 1
|
||||
// )
|
||||
// ,"") AS `date_first_order`,
|
||||
// IFNULL(
|
||||
// (
|
||||
// SELECT DATE_FORMAT(o.`date_add`,\'%m/%d/%Y\')
|
||||
// FROM `'._DB_PREFIX_.'orders` o
|
||||
// LEFT JOIN `'._DB_PREFIX_.'order_history` oh ON (oh.`id_order` = o.`id_order`)
|
||||
// WHERE o.`id_customer` = c.`id_customer`
|
||||
// AND oh.`id_order_history` = (SELECT MAX(`id_order_history`) FROM `'._DB_PREFIX_.'order_history` moh WHERE moh.`id_order` = o.`id_order` GROUP BY moh.`id_order`)
|
||||
// AND oh.id_order_state NOT IN (1,14,15,18,6,8,10,11)
|
||||
// ORDER BY o.`date_add` DESC LIMIT 1
|
||||
// )
|
||||
// ,"") AS `date_last_order`,
|
||||
// IFNULL(
|
||||
// (
|
||||
// SELECT dh.`code`
|
||||
// FROM `'._DB_PREFIX_.'ant_discount_history` dh
|
||||
// WHERE dh.`id_customer` = c.`id_customer`
|
||||
// AND dh.`used` = 0
|
||||
// ORDER BY dh.`date_add` DESC LIMIT 1
|
||||
// )
|
||||
// ,"") AS `discount`
|
||||
// FROM `'._DB_PREFIX_.'customer` c
|
||||
// LEFT JOIN `'._DB_PREFIX_.'customer_version` v ON v.`id_customer` = c.`id_customer`
|
||||
// ORDER BY `id_customer` ASC LIMIT '.$i.','.($i+10000).'
|
||||
// ') as $customer) {
|
||||
// fputcsv($f, $customer, ';', '"');
|
||||
// }
|
||||
|
||||
|
||||
/*
|
||||
$user = 'admin';
|
||||
$pw = 'v6S385Vf';
|
||||
$account = 'bebeboutik';
|
||||
$host = 'suite.emarsys.net';
|
||||
|
||||
$w = new WebDAV($host, $user, $pw);
|
||||
$w->connect();
|
||||
$w->storeFile('extracts/webdav/'.$filename, '/storage/'.$account.'/newsletter.csv');
|
||||
*/
|
||||
|
||||
// Send to FTP
|
||||
$ftp = ftp_connect('ftp.emarsys.fr');
|
||||
ftp_login($ftp, 'emarsys-bbb', 'XuCVuK64');
|
||||
ftp_put($ftp, $filename, 'extracts/webdav/'.$filename, FTP_BINARY);
|
||||
|
Loading…
Reference in New Issue
Block a user