summaryrefslogtreecommitdiff
path: root/import
diff options
context:
space:
mode:
authorLester Caine <lester@lsces.co.uk>2026-06-06 09:41:03 +0100
committerLester Caine <lester@lsces.co.uk>2026-06-06 09:41:03 +0100
commitfa31eb01f523e1c1e9eb7e4492ba8c308a98447b (patch)
treebc228d73d6bb333ee2f7178ecc3aac8ac364b157 /import
parent17ee494db0a95e791d71261cedf0478e8a40cf4b (diff)
downloadcontact-fa31eb01f523e1c1e9eb7e4492ba8c308a98447b.tar.gz
contact-fa31eb01f523e1c1e9eb7e4492ba8c308a98447b.tar.bz2
contact-fa31eb01f523e1c1e9eb7e4492ba8c308a98447b.zip
xref item templates: drop dead Link/nbsp column; move generics to liberty
- view_xref_address_item.tpl, view_xref_phone_item.tpl: remove &nbsp; second column - view_xref_contact_group.tpl: add 30/30/40% widths to Type/Value/Note - view_xref_text_item.tpl, list_xref_generic.tpl: deleted — fall through to liberty equivalents - ImportContactCSV.php: set xorder explicitly (#P/#F → 1, others → 0) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'import')
-rw-r--r--import/ImportContactCSV.php123
1 files changed, 123 insertions, 0 deletions
diff --git a/import/ImportContactCSV.php b/import/ImportContactCSV.php
new file mode 100644
index 0000000..b12fff9
--- /dev/null
+++ b/import/ImportContactCSV.php
@@ -0,0 +1,123 @@
+<?php
+/**
+ * Contact CSV importer — matches the export_contacts.php column layout.
+ *
+ * CSV column layout (0-based, header row skipped by loader):
+ * 0 title Contact name — match key (lc.title)
+ * 1 type xref item code: $00 (person), $01–$05 (business types)
+ * 2 person_name Pipe-separated name for $00: prefix|forename|surname|suffix
+ * 3 scref SCREF xkey (stock source reference / short code)
+ * 4 phone #P xkey
+ * 5 address #C xkey_ext (full address text)
+ * 6 postcode #C xkey
+ * 7 fax #F xkey
+ * 8 website #W xkey_ext (URLs exceed xkey C(32))
+ * 9 email #E xkey_ext (addresses exceed xkey C(32))
+ * 10 accno ACCNO xkey
+ *
+ * Existing contacts (matched by title) are updated in place.
+ * New contacts are created via Contact::store() — liberty handles all required fields.
+ *
+ * @package contact
+ */
+
+namespace Bitweaver\Liberty;
+
+use Bitweaver\Contact\Contact;
+
+function contactCsvUpsertXref( int $contentId, string $item, string $xkey = '', string $xkeyExt = '', int $xorder = 0 ): void {
+ global $gBitDb;
+ $gBitDb->query(
+ "DELETE FROM `" . BIT_DB_PREFIX . "liberty_xref` WHERE `content_id` = ? AND `item` = ?",
+ [ $contentId, $item ]
+ );
+ if( $xkey !== '' || $xkeyExt !== '' ) {
+ $gBitDb->associateInsert( BIT_DB_PREFIX . 'liberty_xref', [
+ 'xref_id' => $gBitDb->GenID( 'liberty_xref_seq' ),
+ 'content_id' => $contentId,
+ 'item' => $item,
+ 'xorder' => $xorder,
+ 'xkey' => $xkey !== '' ? substr( $xkey, 0, 32 ) : null,
+ 'xkey_ext' => $xkeyExt !== '' ? substr( $xkeyExt, 0, 250 ) : null,
+ 'last_update_date' => $gBitDb->NOW(),
+ ] );
+ }
+}
+
+function contactCsvImportRow( array $row, int $rowNum ): array {
+ global $gBitDb;
+
+ $result = [ 'loaded' => 0, 'updated' => 0, 'skipped' => 0, 'errors' => [] ];
+
+ $title = trim( $row[0] ?? '' );
+ $type = trim( $row[1] ?? '' );
+ $personName = trim( $row[2] ?? '' );
+ $scref = trim( $row[3] ?? '' );
+ $phone = trim( $row[4] ?? '' );
+ // Restore leading zero stripped by Excel on 10-digit UK numbers
+ if( strlen( $phone ) === 10 && ctype_digit( $phone ) ) {
+ $phone = '0' . $phone;
+ }
+ $address = trim( $row[5] ?? '' );
+ $postcode = trim( $row[6] ?? '' );
+ $fax = trim( $row[7] ?? '' );
+ if( strlen( $fax ) === 10 && ctype_digit( $fax ) ) {
+ $fax = '0' . $fax;
+ }
+ $website = trim( $row[8] ?? '' );
+ $email = trim( $row[9] ?? '' );
+ $accno = trim( $row[10] ?? '' );
+
+ if( empty( $title ) ) {
+ $result['skipped']++;
+ return $result;
+ }
+
+ // --- Find existing or create new via Contact class ---
+ $contentId = $gBitDb->getOne(
+ "SELECT `content_id` FROM `" . BIT_DB_PREFIX . "liberty_content`
+ WHERE `content_type_guid` = 'contact' AND `title` = ?",
+ [ $title ]
+ );
+
+ $contact = new Contact( null, $contentId ?: null );
+ if( $contentId ) {
+ $contact->load();
+ }
+
+ $pHash = [
+ 'title' => $title,
+ 'edit' => '',
+ 'format_guid' => 'bithtml',
+ ];
+ if( $contentId ) {
+ $pHash['content_id'] = $contentId;
+ }
+
+ if( !empty( $type ) && $type[0] === '$' ) {
+ $pHash['contact_types'] = [ $type ];
+ if( $type === '$00' ) {
+ $pHash['name'] = $personName;
+ }
+ }
+
+ if( !$contact->store( $pHash ) ) {
+ $result['skipped']++;
+ $result['errors'][] = "Row $rowNum: failed to store '$title': " . implode( ', ', $contact->mErrors ?? [] );
+ return $result;
+ }
+
+ $contentId ? $result['updated']++ : $result['loaded']++;
+ $contentId = $contact->mContentId;
+
+ // --- Remaining xref items ---
+ contactCsvUpsertXref( $contentId, 'SCREF', $scref );
+ contactCsvUpsertXref( $contentId, '#P', $phone, '', 1 );
+ contactCsvUpsertXref( $contentId, '#C', $postcode, $address );
+ contactCsvUpsertXref( $contentId, '#F', $fax, '', 1 );
+ contactCsvUpsertXref( $contentId, '#W', '', $website );
+ contactCsvUpsertXref( $contentId, '#E', '', $email );
+ contactCsvUpsertXref( $contentId, 'ACCNO', $accno );
+
+ return $result;
+}