summaryrefslogtreecommitdiff
path: root/import/load_contacts_csv.php
blob: 3a750300384f71d447f8467ade69c00e6b08a4ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
 * Load contacts from storage/contact/Contacts.csv.
 * Matches on title; updates existing, creates new.
 * Append ?clear=y to delete and re-import all rows.
 *
 * @package contact
 */

namespace Bitweaver\Liberty;

use Bitweaver\KernelTools;

require_once '../../kernel/includes/setup_inc.php';

if( !$gBitSystem->isPackageActive( 'contact' ) ) {
	$gBitSystem->fatalError( 'Contact package not active' );
}
if( !$gBitUser->hasPermission( 'p_contact_admin' ) ) {
	$gBitSystem->fatalError( KernelTools::tra( 'Permission denied' ) );
}

require_once __DIR__ . '/ImportContactCSV.php';

$csvFile = CONTACT_IMPORT_PATH . 'Contacts.csv';
$doClear = ( ( $_REQUEST['clear'] ?? '' ) === 'y' );
$loaded  = 0;
$updated = 0;
$skipped = 0;
$deleted = 0;
$errors  = [];

if( !file_exists( $csvFile ) ) {
	$errors[] = 'CSV file not found: ' . $csvFile;
} else {
	$handle = fopen( $csvFile, 'r' );
	if( $handle === false ) {
		$errors[] = 'Cannot open CSV file.';
	} else {
		$rows   = [];
		$rowNum = 0;
		while( ( $data = fgetcsv( $handle, 0, ',', '"', '' ) ) !== false ) {
			$rowNum++;
			if( $rowNum === 1 ) continue; // skip header
			$rows[] = $data;
		}
		fclose( $handle );

		if( $doClear ) {
			foreach( $rows as $data ) {
				$title = trim( $data[0] ?? '' );
				if( empty( $title ) ) continue;
				$contentId = $gBitDb->getOne(
					"SELECT `content_id` FROM `" . BIT_DB_PREFIX . "liberty_content`
					 WHERE `content_type_guid` = 'contact' AND `title` = ?",
					[ $title ]
				);
				if( $contentId ) {
					$contact = new \Bitweaver\Contact\Contact( (int)$contentId );
					$contact->load();
					$contact->expunge();
					$deleted++;
				}
			}
		}

		foreach( $rows as $idx => $data ) {
			$result   = 
			contactCsvImportRow( $data, $idx + 2 );
			$loaded  += $result['loaded'];
			$updated += $result['updated'];
			$skipped += $result['skipped'];
			$errors   = array_merge( $errors, $result['errors'] );
		}
	}
}

$gBitSmarty->assign( 'loaded',  $loaded );
$gBitSmarty->assign( 'updated', $updated );
$gBitSmarty->assign( 'skipped', $skipped );
$gBitSmarty->assign( 'deleted', $deleted );
$gBitSmarty->assign( 'errors',  $errors );
$gBitSmarty->assign( 'csvFile', $csvFile );

$gBitSystem->display( 'bitpackage:stock/import_results.tpl', KernelTools::tra( 'Import Contacts' ) );