This commit is contained in:
Michael RICOIS 2018-01-19 15:29:39 +01:00
parent 5aff766d38
commit 79221931e4
2 changed files with 30 additions and 228 deletions

View File

@ -1,4 +1,9 @@
<?php <?php
/**
* Import customer details from Emarsys
* - Set newsletter info on customer
*/
$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['SERVER_PORT'] = 80; $_SERVER['SERVER_PORT'] = 80;
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com'; $_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
@ -7,12 +12,18 @@ include_once dirname(__FILE__).'/www/config/config.inc.php';
$db = Db::getInstance(); $db = Db::getInstance();
// List all files in dir
foreach(glob(dirname(__FILE__).'/emarsys/*.csv') as $filename) { foreach(glob(dirname(__FILE__).'/emarsys/*.csv') as $filename) {
$f = fopen($filename, 'r'); $f = fopen($filename, 'r');
// By-pass first line
fgetcsv($f); fgetcsv($f);
// Parse line
while($line = fgetcsv($f, 0, ';', '"')) { 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(in_array((int) $line[1], array(0, 1, 2))
if($customer = $db->getRow(' && !empty($line[2]) && in_array(strtolower($line[3]), array('vrai', 'faux'))
&& Validate::isEmail($line[2])) {
if($customer = $db->getRow('
SELECT `id_customer` SELECT `id_customer`
FROM `'._DB_PREFIX_.'customer` FROM `'._DB_PREFIX_.'customer`
WHERE `email` = "'.pSQL($line[2]).'" WHERE `email` = "'.pSQL($line[2]).'"
@ -33,5 +44,7 @@ foreach(glob(dirname(__FILE__).'/emarsys/*.csv') as $filename) {
} }
} }
fclose($f); fclose($f);
// Move file as done
rename($filename, str_replace('/emarsys/', '/emarsys_done/', $filename)); rename($filename, str_replace('/emarsys/', '/emarsys_done/', $filename));
} }

View File

@ -1,4 +1,9 @@
<?php <?php
/**
* Export customer details to Emarsys
* - Select all customers
*/
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com'; $_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
$_SERVER['SERVER_NAME'] = 'www.bebeboutik.com'; $_SERVER['SERVER_NAME'] = 'www.bebeboutik.com';
$_SERVER['HTTP_PORT'] = 80; $_SERVER['HTTP_PORT'] = 80;
@ -6,165 +11,6 @@ include('www/config/config.inc.php');
ini_set('memory_limit', '6G'); 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,'&lt;![CDATA[')!==false) {
$match = preg_replace('/\s*&lt;!\[CDATA\[/','',$match);
$match = preg_replace('/\]\]&gt;\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(); $customers_to_import = array();
// Customer informations // Customer informations
@ -182,7 +28,7 @@ foreach (Db::getInstance()->ExecuteS('
ORDER BY `id_customer` ASC ORDER BY `id_customer` ASC
') as $row) { ') as $row) {
$customers_to_import[(int)$row['id_customer']] = array( $customers_to_import[(int)$row['id_customer']] = array(
'id_customer' => $row['id_customer'], 'id_customer' => $row['id_customer'],
'firstname' => $row['firstname'], 'firstname' => $row['firstname'],
'lastname' => $row['lastname'], 'lastname' => $row['lastname'],
'email' => $row['email'], 'email' => $row['email'],
@ -194,16 +40,13 @@ foreach (Db::getInstance()->ExecuteS('
'discount' => "" 'discount' => ""
); );
} }
$id_customers = array_keys($customers_to_import); $id_customers = array_keys($customers_to_import);
$filename = date('Y-m-d', mktime()).'.csv'; //@todo : newsletter
$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) { for($i=0, $l=count($id_customers); $i < $l; $i+=5000) {
// first order // First order
foreach (Db::getInstance()->ExecuteS(' foreach (Db::getInstance()->ExecuteS('
SELECT o.`id_customer`, DATE_FORMAT(o.`date_add`,\'%d/%m/%Y\') AS `date_first_order` SELECT o.`id_customer`, DATE_FORMAT(o.`date_add`,\'%d/%m/%Y\') AS `date_first_order`
FROM `'._DB_PREFIX_.'orders` o FROM `'._DB_PREFIX_.'orders` o
@ -217,7 +60,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']; $customers_to_import[(int)$row['id_customer']]['date_first_order'] = $row['date_first_order'];
} }
// last order // Last order
foreach (Db::getInstance()->ExecuteS(' foreach (Db::getInstance()->ExecuteS('
SELECT o.`id_customer`, DATE_FORMAT(MAX(h.`date_add`), \'%d/%m/%Y\') AS `date_last_order` SELECT o.`id_customer`, DATE_FORMAT(MAX(h.`date_add`), \'%d/%m/%Y\') AS `date_last_order`
FROM `'._DB_PREFIX_.'order_history` h FROM `'._DB_PREFIX_.'order_history` h
@ -242,70 +85,16 @@ for($i=0, $l=count($id_customers); $i < $l; $i+=5000) {
$customers_to_import[(int)$row['id_customer']]['discount'] = $row['discount']; $customers_to_import[(int)$row['id_customer']]['discount'] = $row['discount'];
} }
} }
$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'), ';', '"');
foreach ($customers_to_import as $customer) { foreach ($customers_to_import as $customer) {
fwrite($f, implode(';', array_values($customer))."\n"); fwrite($f, implode(';', array_values($customer))."\n");
} }
fclose($f); 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 = ftp_connect('ftp.emarsys.fr');
ftp_login($ftp, 'emarsys-bbb', 'XuCVuK64'); ftp_login($ftp, 'emarsys-bbb', 'XuCVuK64');
ftp_put($ftp, $filename, 'extracts/webdav/'.$filename, FTP_BINARY); ftp_put($ftp, $filename, 'extracts/webdav/'.$filename, FTP_BINARY);