-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvimport.php
78 lines (58 loc) · 1.76 KB
/
csvimport.php
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
<?php
/**
* CSV import script
* Load data from csv to database.
* Config there: app/Config.php
* Run: $ php csvimport.php
*/
require_once('bootstrap.php');
// Get environment instances
$config = new Config();
$file = new File($config::FILE_DIRECTORY, $config::FILE_EXTENSION);
$csv = new CSV($config::CSV_MAX_ROWS, $config::CSV_SEPARATOR);
$dbi = DB::getInstance($config::DB_HOST, $config::DB_NAME, $config::DB_USER, $config::DB_PASSWORD);
$db = $dbi->connect();
$importLog = new ImportLog($db);
if (empty($files = $file->getAll())) {
print_r('No matching files found');
exit;
}
$imported_files = 0;
foreach ($files as $filePath) {
if ($importLog->isFileImport($filePath)) continue;
$fields = implode(",", $config::CSV_FIELDS);
$csvFile = fopen($filePath, "r");
$rowIndex = 0;
$values = [];
$valuesToBind = [];
while ($rows = $csv->getRows($csvFile)) {
$params = new RowParams($rows);
$params->make($rowIndex++);
$values[] = "(" . implode(", ", $params->values) . ")";
$valuesToBind += $params->valuesToBind;
$params;
}
try {
$db->beginTransaction();
// Save log
if (!($importLog->logFileImport($filePath)))
throw new Exception('File not logged.');
$imported_files++;
// Save rows
$dbStatement = $db->prepare("INSERT INTO `user` ($fields) VALUES " . implode(", ", $values));
foreach ($valuesToBind as $param => $val) {
$dbStatement->bindValue($param, $val);
}
$dbStatement->execute();
$db->commit();
} catch (Exception $e) {
$db->rollback();
throw $e;
}
}
print_r(
'Work is completed. ' . PHP_EOL .
'Processed new files: ' . $imported_files . PHP_EOL
);
exit;
?>