product_handler.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. $res = 0;
  3. // Try main.inc.php into web root known defined into CONTEXT_DOCUMENT_ROOT (not always defined)
  4. if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) {
  5. $res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"] . "/main.inc.php";
  6. }
  7. // Try main.inc.php into web root detected using web root calculated from SCRIPT_FILENAME
  8. $tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME'];
  9. $tmp2 = realpath(__FILE__);
  10. $i = strlen($tmp) - 1;
  11. $j = strlen($tmp2) - 1;
  12. while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
  13. $i--;
  14. $j--;
  15. }
  16. if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1)) . "/main.inc.php")) {
  17. $res = @include substr($tmp, 0, ($i + 1)) . "/main.inc.php";
  18. }
  19. if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1))) . "/main.inc.php")) {
  20. $res = @include dirname(substr($tmp, 0, ($i + 1))) . "/main.inc.php";
  21. }
  22. // Try main.inc.php using relative path
  23. if (!$res && file_exists("../main.inc.php")) {
  24. $res = @include "../main.inc.php";
  25. }
  26. if (!$res && file_exists("../../main.inc.php")) {
  27. $res = @include "../../main.inc.php";
  28. }
  29. if (!$res && file_exists("../../../main.inc.php")) {
  30. $res = @include "../../../main.inc.php";
  31. }
  32. if (!$res) {
  33. die("Include of main fails");
  34. }
  35. $llx_product_schema = getSchema('llx_product');
  36. $llx_product_extrafields_schema = getSchema('llx_product_extrafields');
  37. $llx_product_price_schema = getSchema('llx_product_price');
  38. $llx_product_association_schema = getSchema('llx_product_association');
  39. //print_r($llx_product_schema);
  40. //var_dump($llx_product_schema['ref_ext']);
  41. function getSchema($table)
  42. {
  43. global $db;
  44. $array = [];
  45. $sql_llx_product_schema = "SELECT column_name, data_type
  46. FROM information_schema.columns
  47. WHERE table_name = '{$table}'";
  48. $result = $db->query($sql_llx_product_schema);
  49. while ($row = $db->fetch_object($result)) {
  50. $array[$row->column_name] = $row->data_type;
  51. }
  52. return $array;
  53. }
  54. function productsCreator($csvFile, $table, $schema)
  55. {
  56. $sql = '';
  57. if (($handle = fopen($csvFile, 'r')) !== FALSE) {
  58. $header_InsertInto = '';
  59. $rowCounter = 0;
  60. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  61. if ($rowCounter == 0) {
  62. $header_array = explode(';', $data[0]);
  63. $header_InsertInto = implode(',', $header_array);
  64. } else {
  65. $sql .= "INSERT INTO {$table} ({$header_InsertInto}) VALUES (";
  66. $fieldCounter = 0;
  67. $row_array = explode(';', $data[0]);
  68. $fieldsValues = [];
  69. foreach ($header_array as $field) {
  70. //print $field . ' = ';
  71. //var_dump($row_array[$fieldCounter]);
  72. //print '<br>';
  73. if ($schema[$field] == 'character varying' || $schema[$field] == 'timestamp without time zone' || $schema[$field] == 'text') {
  74. if ($row_array[$fieldCounter] == "NULL") {
  75. $fieldsValues[] = 'NULL';
  76. } elseif ($row_array[$fieldCounter] == "") {
  77. $fieldsValues[] = '\'\'';
  78. } else {
  79. $fieldsValues[] = '\'' . $row_array[$fieldCounter] . '\'';
  80. }
  81. } else {
  82. $fieldsValues[] = $row_array[$fieldCounter];
  83. }
  84. $fieldCounter++;
  85. }
  86. $sql .= implode(',', $fieldsValues);
  87. $sql .= ") ON CONFLICT (rowid) DO ";
  88. $sql .= "UPDATE SET ";
  89. $updatFieldsValues = [];
  90. foreach ($header_array as $field) {
  91. if ($field != 'rowid') {
  92. $updatFieldsValues[] = $field . ' = EXCLUDED.' . $field;
  93. }
  94. }
  95. $sql .= implode(', ', $updatFieldsValues) . ";" . '<br><br>';
  96. }
  97. $rowCounter++;
  98. }
  99. fclose($handle);
  100. } else {
  101. echo "Nem lehet megnyitni a fájlt!";
  102. }
  103. return $sql;
  104. }
  105. $csvFile = 'csv/llx_product_excelia_staging_Excel.csv';
  106. $sql = productsCreator($csvFile, 'llx_product_temp', $llx_product_schema);
  107. print $sql;