Skip to content

Commit

Permalink
Release 0.70
Browse files Browse the repository at this point in the history
* added: fields kit_identifier and kit_contact_since are now accessible
for the import and for kitForm
* changed: import accept now category values or identifiers
* added: import can activate newsletter automatically
* added: truncate KIT contact database before import new data
* added: import can handle internal categories
* added: import ignore records with status deleted
* added: switch to abort the actual running newsletter cronjob
  • Loading branch information
hertsch committed Mar 20, 2013
1 parent c148838 commit 29ecd1d
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 58 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG
Expand Up @@ -5,8 +5,14 @@ http://phpManufaktur.de - ralf.hertsch@phpManufaktur.de

Release 0.70 - NOT PUBLISHED

* added: fields kit_identifier and kit_contact_since are now accessible for
* added: fields kit_identifier and kit_contact_since are now accessible for
the import and for kitForm
* changed: import accept now category values or identifiers
* added: import can activate newsletter automatically
* added: truncate KIT contact database before import new data
* added: import can handle internal categories
* added: import ignore records with status deleted
* added: switch to abort the actual running newsletter cronjob

Release 0.69 - 2013-01-21

Expand Down
5 changes: 5 additions & 0 deletions backend.css
Expand Up @@ -644,4 +644,9 @@ fieldset.kit_import_fieldset {
font-style: italic;
color: #999999;
background-color: transparent;
}
.cronjob_abort {
margin: 0;
padding: 20px 0 0 0;
text-align: right;
}
201 changes: 156 additions & 45 deletions class.import.csv.php
Expand Up @@ -43,6 +43,9 @@ class kitCSVimport {
const REQUEST_CSV_FIELD_COUNT = 'ccfc';
const REQUEST_CSV_FILE = 'cf';
const REQUEST_KIT_FIELDS = 'ckf';
const REQUEST_ACTIVATE_NEWSLETTER = 'can';
const REQUEST_TRUNCATE_DATABASE = 'ctd';
const REQUEST_IGNORE_DELETED = 'cid';

const ACTION_ASSIGN_FIELDS = 'caf';
const ACTION_DEFAULT = 'def';
Expand Down Expand Up @@ -225,6 +228,26 @@ private function dlgImport() {
'file' => array(
'name' => self::REQUEST_CSV_FILE
)
),
'options' => array(
'newsletter' => array(
'activate' => array(
'name' => self::REQUEST_ACTIVATE_NEWSLETTER,
'value' => 1
)
),
'database' => array(
'truncate' => array(
'name' => self::REQUEST_TRUNCATE_DATABASE,
'value' => 1
)
),
'ignore' => array(
'deleted' => array(
'name' => self::REQUEST_IGNORE_DELETED,
'value' => 1
)
)
)
);
return $this->getTemplate('import.csv.dwoo', $data);
Expand Down Expand Up @@ -349,6 +372,26 @@ private function checkCSVfile() {
'name' => self::REQUEST_KIT_FIELDS,
'value' => implode(',', $kit_fields)
)
),
'options' => array(
'newsletter' => array(
'activate' => array(
'value' => (int) isset($_REQUEST[self::REQUEST_ACTIVATE_NEWSLETTER]),
'name' => self::REQUEST_ACTIVATE_NEWSLETTER
)
),
'database' => array(
'truncate' => array(
'name' => self::REQUEST_TRUNCATE_DATABASE,
'value' => (int) isset($_REQUEST[self::REQUEST_TRUNCATE_DATABASE])
)
),
'ignore' => array(
'deleted' => array(
'name' => self::REQUEST_IGNORE_DELETED,
'value' => (int) isset($_REQUEST[self::REQUEST_IGNORE_DELETED])
)
)
)
);
return $this->getTemplate('import.csv.assign.dwoo', $data);
Expand All @@ -371,6 +414,9 @@ private function assignFields() {
$csvFile = $_REQUEST[self::REQUEST_CSV_FILE];
$kit_fields = explode(',', $_REQUEST[self::REQUEST_KIT_FIELDS]);
$use_fields = array();
$activate_newsletter = (bool) $_REQUEST[self::REQUEST_ACTIVATE_NEWSLETTER];
$truncate_database = (bool) $_REQUEST[self::REQUEST_TRUNCATE_DATABASE];
$ignore_deleted = (bool) $_REQUEST[self::REQUEST_IGNORE_DELETED];

$columns = array();
for ($i=0; $i < $field_count; $i++) {
Expand All @@ -391,6 +437,29 @@ private function assignFields() {
$this->setMessage($this->lang->translate('<p>At minimum you must assign an email address to <b>kit_email</b> or assign a valid <b>ID</b> to <b>kit_id</b>. Can\'t start the import!</p>'));
return $this->checkCSVfile();
}

// get previous messages to the $message var
$message = $this->getMessage();

// truncate the KIT database before import new data?
if ($truncate_database) {
$queries = array(
"TRUNCATE TABLE `".TABLE_PREFIX."mod_kit_contact`",
"TRUNCATE TABLE `".TABLE_PREFIX."mod_kit_contact_address`",
"TRUNCATE TABLE `".TABLE_PREFIX."mod_kit_contact_memos`",
"TRUNCATE TABLE `".TABLE_PREFIX."mod_kit_contact_protocol`",
"TRUNCATE TABLE `".TABLE_PREFIX."mod_kit_register`"
);
foreach ($queries as $query) {
$database->query($query);
if ($database->is_error()) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $database->get_error()));
return false;
}
}
$message .= $this->lang->translate('<p>Truncate the KIT contact database.</p>');
}

// open the CSV file
if (false === ($handle = fopen($csvFile, "r"))) {
// can't open the file
Expand All @@ -399,11 +468,9 @@ private function assignFields() {
return false;
}

// get previous messages to the $message var
$message = $this->getMessage();

$title_array = $dbContact->person_title_array;
$academic_array = $dbContact->person_title_academic_array;

$newsletter_array = array();
$x = $dbContact->newsletter_array;
foreach ($x as $key => $value)
Expand All @@ -414,6 +481,11 @@ private function assignFields() {
foreach ($x as $key => $value)
$distribution_array[] = $key;

$intern_array = array();
$x = $dbContact->category_array;
foreach ($x as $key => $value)
$intern_array[] = $key;

// now we loop through the CSV file and import the data
$line = 0;
$update_records = 0;
Expand All @@ -428,6 +500,7 @@ private function assignFields() {
array('line' => $line));
continue;
}
$ignore_record = false;
// step through the columns
$contact = array();
for ($i=0; $i < $field_count; $i++) {
Expand Down Expand Up @@ -459,6 +532,8 @@ private function assignFields() {
if (empty($news)) continue;
if (in_array($news, $newsletter_array))
$newsletter[] = $news;
elseif (false !== ($key = array_search($news, $dbContact->newsletter_array)))
$newsletter[] = $key;
}
$contact[$columns[$i]] = implode(',', $newsletter);
break;
Expand All @@ -471,9 +546,25 @@ private function assignFields() {
if (empty($dist)) continue;
if (in_array($dist, $distribution_array))
$distribution[] = $dist;
elseif (false !== ($key = array_search($dist, $dbContact->distribution_array)))
$distribution[] = $key;
}
$contact[$columns[$i]] = implode(',', $distribution);
break;
case 'kit_intern':
// check for intern category
$il = explode(',', $data[$i]);
$intern = array();
foreach ($il as $int) {
$dist = trim($int);
if (empty($int)) continue;
if (in_array($int, $intern_array))
$intern[] = $int;
elseif (false !== ($key = array_search($int, $dbContact->category_array)))
$intern[] = $key;
}
$contact[$columns[$i]] = implode(',', $intern);
break;
case 'kit_birthday':
// set the contact birthday
if (empty($data[$i])) {
Expand All @@ -488,6 +579,13 @@ private function assignFields() {
}
$contact[$columns[$i]] = date('Y-m-d H:i:s', $date);
break;
case 'kit_status':
// if deleted records should be ignored still continue ...
if ($ignore_deleted && ($data[$i] == 'statusDeleted')) {
$ignore_record = true;
}
$contact[$columns[$i]] = $data[$i];
break;
default:
// all other KIT fields
$contact[$columns[$i]] = ($charset == 'ANSI') ? utf8_encode($data[$i]) : $data[$i];
Expand All @@ -496,53 +594,66 @@ private function assignFields() {
} // for columns

$kit_id = -1;
$status = dbKITcontact::status_active;
$status = (isset($contact['kit_status'])) ? $contact['kit_status'] : dbKITcontact::status_active;
$register = array();

if (isset($contact['kit_id'])) {
// update an existing KIT record
if (null === ($kit_id = $database->get_one("SELECT `contact_id` FROM `".self::$table_prefix."mod_kit_contact` WHERE `contact_id`='{$contact['kit_id']}'", MYSQL_ASSOC))) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $database->get_error()));
return false;
}
if ($kit_id != $contact['kit_id']) {
$message .= $this->lang->translate('<p>Skipped line <i>{{ line }}</i>, the KIT ID <b>{{ kit_id }}</b> does not exists!</p>',
array('line' => $line, 'kit_id' => $contact['kit_id']));
}
elseif (!$kitContactInterface->updateContact($contact['kit_id'], $contact)) {
if ($kitContactInterface->isError()) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $kitContactInterface->getError()));
return false;
if (!$ignore_record) {
if (isset($contact['kit_id'])) {
// update an existing KIT record
if (null === ($kit_id = $database->get_one("SELECT `contact_id` FROM `".self::$table_prefix."mod_kit_contact` WHERE `contact_id`='{$contact['kit_id']}'", MYSQL_ASSOC))) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $database->get_error()));
return false;
}
if ($kit_id != $contact['kit_id']) {
$message .= $this->lang->translate('<p>Skipped line <i>{{ line }}</i>, the KIT ID <b>{{ kit_id }}</b> does not exists!</p>',
array('line' => $line, 'kit_id' => $contact['kit_id']));
}
elseif (!$kitContactInterface->updateContact($contact['kit_id'], $contact)) {
if ($kitContactInterface->isError()) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $kitContactInterface->getError()));
return false;
}
$message .= $kitContactInterface->getMessage();
}
else
$update_records++;
}
$message .= $kitContactInterface->getMessage();
}
else
$update_records++;
}
elseif ($kitContactInterface->isEMailRegistered($contact['kit_email'], $kit_id, $status)) {
// KIT contact already exists, update the record
if (!$kitContactInterface->updateContact($kit_id, $contact)) {
if ($kitContactInterface->isError()) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $kitContactInterface->getError()));
return false;
elseif ($kitContactInterface->isEMailRegistered($contact['kit_email'], $kit_id, $status)) {
// KIT contact already exists, update the record
if (!$kitContactInterface->updateContact($kit_id, $contact)) {
if ($kitContactInterface->isError()) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $kitContactInterface->getError()));
return false;
}
$message .= $kitContactInterface->getMessage();
}
else
$update_records++;
}
$message .= $kitContactInterface->getMessage();
}
else
$update_records++;
}
else {
// add a new record to KIT
if (!$kitContactInterface->addContact($contact, $kit_id, $register)) {
if ($kitContactInterface->isError()) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $kitContactInterface->getError()));
return false;
else {
// add a new record to KIT
if (!$kitContactInterface->addContact($contact, $kit_id, $register)) {
if ($kitContactInterface->isError()) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $kitContactInterface->getError()));
return false;
}
$message .= $kitContactInterface->getMessage();
}
else
$add_records++;
}
$message .= $kitContactInterface->getMessage();
}
else
$add_records++;
}
// Handling for activation of Newsletters
if ($activate_newsletter && !empty($contact['kit_newsletter'])) {
$SQL = "UPDATE `".TABLE_PREFIX."mod_kit_register` SET `reg_status`='statusActive', ".
"`reg_newsletter`='".$contact['kit_newsletter']."', `reg_register_confirmed`='".date('Y-m-d H:i:s')."'".
" WHERE `reg_email`='".$contact['kit_email']."'";
$database->query($SQL);
if ($database->is_error()) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $database->get_error()));
return false;
}
}
} // !$ignore_record
} // while

// close the CSV file
Expand Down
15 changes: 13 additions & 2 deletions class.interface.php
Expand Up @@ -188,7 +188,9 @@ class kitContactInterface {
self::kit_address_region => 40,
self::kit_distribution => 41,
self::kit_identifier => 42,
self::kit_contact_since => 43 // last added field
self::kit_contact_since => 43,
self::kit_intern => 44,
self::kit_status => 45 // last added field
);

const address_type_private = 'private';
Expand Down Expand Up @@ -506,6 +508,7 @@ public function updateContact($contact_id, &$contact_array = array()) {
switch ($field) :
case self::kit_title:
case self::kit_title_academic:
case self::kit_status:
if (isset($contact_array[$field]) && ($contact_array[$field] !== $contact[$this->field_assign[$field]])) {
$contact[$this->field_assign[$field]] = $contact_array[$field];
$contact_changed = true;
Expand Down Expand Up @@ -614,6 +617,14 @@ public function updateContact($contact_id, &$contact_array = array()) {
}
}
break;
case self::kit_intern:
if (isset($contact_array[$field])) {
if ($contact_array[$field] != $contact[dbKITcontact::field_intern]) {
$contact[dbKITcontact::field_intern] = $contact_array[$field];
$contact_changed = true;
}
}
break;
endswitch;
}
if ($address_changed) {
Expand Down Expand Up @@ -833,7 +844,7 @@ public function addContact($contact_array = array(), &$contact_id = -1, &$regist
$contact[$key] = dbKITcontact::type_person;
break;
case dbKITcontact::field_status:
$contact[$key] = dbKITcontact::status_active;
$contact[$key] = (isset($contact_array[self::kit_status])) ? $contact_array[self::kit_status] : dbKITcontact::status_active;
break;
case dbKITcontact::field_contact_language:
// set contact language
Expand Down

0 comments on commit 29ecd1d

Please sign in to comment.