Commit 6e57f9c3 authored by Alexander Stehlik's avatar Alexander Stehlik
Browse files

[FEATURE] Update members during import

The unique subscription_no property is used to detect existing
member records. If a member record exists, it is updated, instead
of truncating the members table and inserting all records as new.
parent da293504
......@@ -35,7 +35,6 @@ class Tx_T3oMembership_Task_ImportMembersTask extends tx_scheduler_Task
*/
public function execute()
{
t3lib_div::devLog('[tx_scheduler_ImportMember]: execute', 't3o_membership', 0);
$membershipRecords = $this->getDatabaseConnection()->exec_SELECTgetRows(
'uid, name',
......@@ -44,7 +43,7 @@ class Tx_T3oMembership_Task_ImportMembersTask extends tx_scheduler_Task
);
foreach ($membershipRecords as $membershipRecord) {
$this->memberships[$membershipRecord['name']] = $membershipRecord['uid'];
$this->memberships[$membershipRecord['name']] = (int)$membershipRecord['uid'];
}
// does the import file exist?
......@@ -52,59 +51,97 @@ class Tx_T3oMembership_Task_ImportMembersTask extends tx_scheduler_Task
if (!t3lib_div::isAbsPath($importFile)) {
$importFile = t3lib_div::getFileAbsFileName($importFile);
}
if (!file_exists($importFile)) {
t3lib_div::devLog('[tx_scheduler_ImportMember]: no importfile - given value: ' . $importFile,
't3o_membership', 0);
if (!file_exists($importFile)) {
t3lib_div::devLog(
'[tx_scheduler_ImportMember]: no importfile - given value: ' . $importFile,
't3o_membership',
0
);
return false;
} else {
$this->getDatabaseConnection()->exec_TRUNCATEquery('tx_t3omembership_domain_model_member');
$fileData = file($importFile);
array_shift($fileData);
foreach ($fileData as $key => $line) {
$fields = t3lib_div::trimExplode("\t", $line);
$endTime = strtotime($fields[15]);
$member = array(
'name' => $fields[6],
'external_id' => (int)$fields[0],
'address' => $fields[7] !== '' ? $fields[7] : $fields[8],
'zip' => $fields[10],
'city' => $fields[11],
'country' => $fields[13],
'end_date' => $endTime,
'endtime' => $endTime,
'starttime' => strtotime(date('d.m.Y', $endTime) . ' -1 year'),
'membership' => $this->getMembershipUid($fields[12]),
'pid' => $this->getMembershipStoragePid(),
'crdate' => time(),
'tstamp' => time(),
'invoice_email' => $fields[84],
'email' => $fields[79],
'url' => $fields[80],
'firstname' => $fields[82],
'lastname' => $fields[83]
);
$resource = $this->getDatabaseConnection()->exec_INSERTquery(
'tx_t3omembership_domain_model_member',
$member
);
$this->getDatabaseConnection()->sql_free_result($resource);
}
$fileData = file($importFile);
array_shift($fileData);
foreach ($fileData as $key => $line) {
$line = iconv('ISO-8859-15', 'UTF-8', $line);
$fields = t3lib_div::trimExplode("\t", $line);
$membershipUid = $this->getMembershipUid($fields[12]);
// Skip records with unknown membership types.
if (empty($membershipUid)) {
continue;
}
$subscriptionNo = (int)$fields[14];
$endTime = strtotime($fields[15]);
$member = [
'name' => $fields[6],
'subscription_no' => $subscriptionNo,
'external_id' => (int)$fields[0],
'address' => $fields[7] !== '' ? $fields[7] : $fields[8],
'zip' => $fields[10],
'city' => $fields[11],
'country' => $fields[13],
'end_date' => $endTime,
'endtime' => $endTime,
'starttime' => strtotime(date('d.m.Y', $endTime) . ' -1 year'),
'membership' => $membershipUid,
'pid' => $this->getMembershipStoragePid(),
'crdate' => time(),
'tstamp' => time(),
'invoice_email' => $fields[84],
'email' => $fields[79],
'url' => $fields[80],
'firstname' => $fields[82],
'lastname' => $fields[83]
];
$this->createOrUpdateMemeber($subscriptionNo, $member);
}
return true;
}
/**
* Checks if the member with the given subscription number already exists in the database.
* If he exists, his data will be updated, otherwise a new record will be inserted.
*
* @param int $subscriptionNo
* @param array $memberData
*/
protected function createOrUpdateMemeber($subscriptionNo, array $memberData)
{
$existingMember = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
'uid',
'tx_t3omembership_domain_model_member',
'subscription_no=' . $subscriptionNo
);
if (!empty($existingMember['uid'])) {
$resource = $this->getDatabaseConnection()->exec_UPDATEquery(
'tx_t3omembership_domain_model_member',
'uid=' . (int)$existingMember['uid'],
$memberData
);
} else {
$resource = $this->getDatabaseConnection()->exec_INSERTquery(
'tx_t3omembership_domain_model_member',
$memberData
);
}
$this->getDatabaseConnection()->sql_free_result($resource);
}
/**
* @param string $membershipName
* @return integer
* @return integer|null
*/
protected function getMembershipUid($membershipName)
{
$membershipName = trim(str_replace('Membership', '', $membershipName));
if (empty($this->memberships[$membershipName])) {
return null;
}
return $this->memberships[$membershipName];
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment