208 lines
5.8 KiB
PHP
208 lines
5.8 KiB
PHP
<?php
|
|
|
|
/** Customer Importer
|
|
* imports customer data from a csv file
|
|
*
|
|
* @version 2.0.0
|
|
* @since 2008-04-07
|
|
*
|
|
*/
|
|
class CustomerImporter extends CmsObject{
|
|
|
|
var $upload;
|
|
|
|
var $addedCustomers;
|
|
var $updatedCustomers;
|
|
|
|
/** C'tor
|
|
*/
|
|
//-----------------------------------------------
|
|
function CustomerImporter($id, $parent){
|
|
//-----------------------------------------------
|
|
parent::CmsObject($id, $parent);
|
|
$this->name = "Besucherdaten importieren";
|
|
$this->upload = false;
|
|
$this->addedCustomers = array();
|
|
$this->updatedCustomers = array();
|
|
}
|
|
|
|
|
|
/** @see CmsObject::update()
|
|
*/
|
|
//----------------------------------------------
|
|
function update(){
|
|
//----------------------------------------------
|
|
if ($_FILES['excel']['error'] == 0) {
|
|
$f = fopen($_FILES['excel']['tmp_name'], "r");
|
|
$line = fgets($f);
|
|
while ($line = fgets($f)) {
|
|
$c = explode(";", $line);
|
|
$this->importCustomer($c);
|
|
}
|
|
fclose($f);
|
|
$this->upload = true;
|
|
}
|
|
}
|
|
|
|
|
|
/** @see CmsObject::printContent()
|
|
*/
|
|
//-----------------------------------------------
|
|
function printContent(){
|
|
//-----------------------------------------------
|
|
$t = new Template(CMS_TEMPLATE_DIR."customerimport.html");
|
|
if (!$this->upload) {
|
|
$t->removeBlock("SUCCESS");
|
|
}
|
|
else {
|
|
$t->setVar("ADDED", htmlspecialchars(implode(", ", $this->addedCustomers)));
|
|
$t->setVar("UPDATED", htmlspecialchars(implode(", ", $this->updatedCustomers)));
|
|
$t->setVar("NUM_ADDED", sizeof($this->addedCustomers));
|
|
$t->setVar("NUM_UPDATED", sizeof($this->updatedCustomers));
|
|
}
|
|
return $t->toString();
|
|
}
|
|
|
|
|
|
/** @see CmsObject::getCssClass()
|
|
*/
|
|
|
|
//-----------------------------------------------
|
|
function getCssClass(){
|
|
//-----------------------------------------------
|
|
return "itemCustomers";
|
|
}
|
|
|
|
|
|
// === ADDITIONAL METHODS ================================================================= //
|
|
|
|
|
|
/** imports customer data
|
|
* @param customer customer
|
|
*/
|
|
//-----------------------------------------------
|
|
function importCustomer($customer){
|
|
//-----------------------------------------------
|
|
// fields: acad, firstname, surname, address, zip, city, e-mail, group
|
|
// check if customer exists
|
|
$query = sprintf("SELECT acad, firstname, surname, address, zip, city, email, id, loose FROM bruckm_ticketcustomer
|
|
WHERE firstname = %s AND surname = %s",
|
|
sqlstring($customer[1]),
|
|
sqlstring($customer[2]));
|
|
$result = dbQuery($query);
|
|
if (mysql_num_rows($result) > 0) {
|
|
// find best correlation
|
|
$best = array();
|
|
$correlations = 0;
|
|
while ($line = mysqli_fetch_array($result, MYSQL_BOTH)) {
|
|
$c = 0;
|
|
for ($i = 0; $i < 7; $i++) {
|
|
if ($line[$i] == $customer[$i]) {
|
|
$c++;
|
|
}
|
|
}
|
|
if ($c > $correlations) {
|
|
$correlations = $c;
|
|
$best = $line;
|
|
}
|
|
}
|
|
|
|
if (empty($best)) {
|
|
logError(1, "no correlation found for $line[1] $line[2]", __FILE__, __LINE__);
|
|
return;
|
|
}
|
|
|
|
// only loose customers can be edited
|
|
if ($best['loose'] == 'false') {
|
|
return;
|
|
}
|
|
|
|
// perfect correlation: no need to update any field
|
|
if ($correlations == 7) {
|
|
return;
|
|
}
|
|
|
|
// keep old values where the new value is empty
|
|
for ($i = 0; $i < 7; $i++) {
|
|
if (empty($customer[$i])) {
|
|
$customer[$i] = $best[$i];
|
|
}
|
|
}
|
|
|
|
// update customer
|
|
$query = sprintf("UPDATE bruckm_ticketcustomer SET acad = %s, firstname = %s, surname = %s, address = %s, zip = %d, city = %s, email = %s, changeDate = NOW()
|
|
WHERE id = %d",
|
|
sqlstring($customer[0]),
|
|
sqlstring($customer[1]),
|
|
sqlstring($customer[2]),
|
|
sqlstring($customer[3]),
|
|
sqlnum($customer[4]),
|
|
sqlstring($customer[5]),
|
|
sqlstring($customer[6]),
|
|
sqlnum($best['id']));
|
|
dbQuery($query);
|
|
|
|
if (!empty($customer[7])) {
|
|
$group = $this->findGroup($customer[7]);
|
|
$query = sprintf("INSERT IGNORE INTO bruckm_ticketcustomergroupmemberships (groupId, customerId) VALUES (%d, %d)",
|
|
sqlnum($group),
|
|
sqlnum($best['id']));
|
|
dbQuery($query);
|
|
}
|
|
$this->updatedCustomers[] = $customer[1] . ' ' . $customer[2];
|
|
return;
|
|
}
|
|
|
|
// customer not found: insert into database
|
|
$query = sprintf("INSERT INTO bruckm_ticketcustomer (acad, firstname, surname, address, zip, city, email, creationDate, newsletter, locked, loose)
|
|
VALUES (%s, %s, %s, %s, %d, %s, %s, NOW(), 'true', 'false', 'true')",
|
|
sqlstring($customer[0]),
|
|
sqlstring($customer[1]),
|
|
sqlstring($customer[2]),
|
|
sqlstring($customer[3]),
|
|
sqlnum($customer[4]),
|
|
sqlstring($customer[5]),
|
|
sqlstring($customer[6]));
|
|
dbQuery($query);
|
|
|
|
if (!empty($customer[7])) {
|
|
$group = $this->findGroup($customer[7]);
|
|
$query = sprintf("INSERT INTO bruckm_ticketcustomergroupmemberships (groupId, customerId) VALUES (%d, %d)",
|
|
sqlnum($group),
|
|
sqlnum(mysql_insert_id()));
|
|
dbQuery($query);
|
|
}
|
|
$this->addedCustomers[] = $customer[1] . ' ' . $customer[2];
|
|
}
|
|
|
|
|
|
/** finds the id of a group and creates the group if it doesn't exist yet
|
|
* @param group group name
|
|
* @return group id
|
|
*/
|
|
//-----------------------------------------------
|
|
function findGroup($group){
|
|
//-----------------------------------------------
|
|
$query = sprintf("SELECT id FROM bruckm_ticketcustomergroup WHERE name = %s LIMIT 1", sqlstring($group));
|
|
$result = dbQuery($query);
|
|
if ($line = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
|
|
return $line['id'];
|
|
}
|
|
$query = sprintf("INSERT INTO bruckm_ticketcustomergroup (name) VALUES (%s)", sqlstring($group));
|
|
dbQuery($query);
|
|
return mysql_insert_id();
|
|
}
|
|
|
|
|
|
/** sets the id
|
|
* @param id id
|
|
*/
|
|
//-----------------------------------------------
|
|
function setId($id){
|
|
//-----------------------------------------------
|
|
$this->id = $id;
|
|
}
|
|
|
|
};
|
|
|
|
?>
|