omlinkimporter.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * @author Open Mind
  4. * @date 11/04/2018
  5. * @time 09:48
  6. */
  7. const DISTINTA_COLONNE = 20;
  8. const ARTICLES_COLONNE = 15;
  9. class CSVImporter
  10. {
  11. private $currentFileName;
  12. private $folderToWatch;
  13. private $dbMysql;
  14. public function __construct($folderPath, $databaseName, $dbusername, $dbpassword)
  15. {
  16. list($this->dbname, $this->dbuser, $this->dbpass) = [$databaseName, $dbusername, $dbpassword];
  17. $this->currentFileName = '';
  18. $this->folderToWatch = $folderPath;
  19. $this->dbMysql = new mysqli('localhost', $dbusername, $dbpassword, $databaseName);
  20. if ($this->dbMysql->connect_error) {
  21. die('Database connection error (' . $this->dbMysql->connect_errno . ') ' . $this->dbMysql->connect_error);
  22. }
  23. }
  24. public function ScanAndStoreData()
  25. {
  26. foreach (glob("{$this->folderToWatch}/*.csv") as $fname) {
  27. if (is_file($fname)) {
  28. $allData = $this->loadCSV($fname);
  29. // Esce se il file non è una distinta, lo riconosce solamente dal numero di colonne del file CSV
  30. if ($allData == NULL)
  31. return;
  32. switch (count($allData[0])) {
  33. case DISTINTA_COLONNE:
  34. $this->importOnDatabase($fname, $allData);
  35. break;
  36. case ARTICLES_COLONNE:
  37. $this->mysqlLoadCSV(addslashes(realpath($fname)), 'articles');
  38. break;
  39. }
  40. }
  41. }
  42. }
  43. private function loadCSV(string $path): ?array
  44. {
  45. $fh = fopen($path, "r");
  46. $csvData = [];
  47. while (($d = fgetcsv($fh, 0, ';'))) {
  48. if ($d)
  49. $csvData[] = $d;
  50. }
  51. fclose($fh);
  52. return $csvData;
  53. }
  54. private function importOnDatabase(string $fileName, array $data)
  55. {
  56. foreach ($data as $dataRow) {
  57. $sql = <<<EOL
  58. INSERT INTO distinte (
  59. origine, commessa, ordine_produzione, data_creazione, time_creazione, destinazione_merci, barcode, descrizione, qta_prelevare, qta_mancante
  60. ) VALUES (
  61. '$dataRow[16]', '$dataRow[0]', '$dataRow[3]', str_to_date('$dataRow[6]','%d/%m/%Y'), '$dataRow[7]', '$dataRow[15]','$dataRow[9]', '$dataRow[10]', '$dataRow[11]', 0
  62. );
  63. EOL;
  64. if (!$this->dbMysql->query($sql)) {
  65. echo "Error on query: {$sql}" . PHP_EOL;
  66. echo $this->dbMysql->error . PHP_EOL;
  67. return;
  68. }
  69. }
  70. echo "{$fileName} successfully imported" . PHP_EOL;
  71. $this->markFileAsDone($fileName);
  72. }
  73. private function markFileAsDone(string $csvPath)
  74. {
  75. rename($csvPath, "{$csvPath}_imported");
  76. }
  77. private function mysqlLoadCSV(string $CSVPath, string $tableName)
  78. {
  79. $sql = <<<EOF
  80. LOAD DATA INFILE '$CSVPath' INTO TABLE `$tableName`
  81. FIELDS TERMINATED BY ';' ENCLOSED BY '' LINES TERMINATED BY '\\n';
  82. EOF;
  83. if (!$this->dbMysql->query($sql)) {
  84. echo "Error on query: {$sql}" . PHP_EOL;
  85. echo $this->dbMysql->error . PHP_EOL;
  86. return;
  87. }
  88. echo "{$CSVPath} successfully imported" . PHP_EOL;
  89. $this->markFileAsDone($CSVPath);
  90. }
  91. }
  92. echo PHP_EOL . 'Scanning import folder for new CSV to import...' . PHP_EOL . PHP_EOL;
  93. $impoter = new CSVImporter('import', 'omlink_lamborghini', 'root', 'giancarlo');
  94. $impoter->ScanAndStoreData();
  95. echo PHP_EOL . '... end import' . PHP_EOL;