product_handler.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. function getSchema($table)
  40. {
  41. global $db;
  42. $array = [];
  43. $sql_llx_product_schema = "SELECT column_name, data_type
  44. FROM information_schema.columns
  45. WHERE table_name = '{$table}'";
  46. $result = $db->query($sql_llx_product_schema);
  47. while ($row = $db->fetch_object($result)) {
  48. $array[$row->column_name] = $row->data_type;
  49. }
  50. return $array;
  51. }
  52. function productsCreator($table, $schema)
  53. {
  54. $sql = '';
  55. $header_array = [];
  56. if (($handle = fopen('csv/' . $table . '_excelia_staging_Excel.csv', 'r')) !== FALSE) {
  57. $header_InsertInto = '';
  58. $rowCounter = 0;
  59. if ($table == 'llx_product_association') {
  60. $sql .= "DELETE FROM llx_product_association;";
  61. //$sql .= '<br><br>';
  62. }
  63. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  64. if ($rowCounter == 0) {
  65. $header_array = explode(';', $data[0]);
  66. $header_InsertInto = implode(',', $header_array);
  67. } else {
  68. $fieldCounter = 0;
  69. $row_array = explode(';', $data[0]);
  70. $fieldsValues = [];
  71. if ($table == 'llx_product_extrafields') {
  72. $sql .= "DELETE FROM llx_product_extrafields WHERE fk_object = {$row_array[2]};";
  73. //$sql .= '<br><br>';
  74. }
  75. if ($table == 'llx_product_price') {
  76. $sql .= "DELETE FROM llx_product_price WHERE fk_product = {$row_array[3]};";
  77. //$sql .= '<br><br>';
  78. }
  79. $sql .= "INSERT INTO {$table} ({$header_InsertInto}) VALUES (";
  80. foreach ($header_array as $field) {
  81. if ($schema[$field] == 'character varying' || $schema[$field] == 'timestamp without time zone' || $schema[$field] == 'text') {
  82. if ($row_array[$fieldCounter] == "NULL") {
  83. $fieldsValues[] = 'NULL';
  84. } elseif ($row_array[$fieldCounter] == "") {
  85. $fieldsValues[] = '\'\'';
  86. } else {
  87. $fieldsValues[] = '\'' . $row_array[$fieldCounter] . '\'';
  88. }
  89. } else {
  90. if ($field == 'fk_user_author') {
  91. $fieldsValues[] = 1;
  92. } else {
  93. $fieldsValues[] = $row_array[$fieldCounter];
  94. }
  95. }
  96. $fieldCounter++;
  97. }
  98. $sql .= implode(',', $fieldsValues);
  99. $sql .= ") ON CONFLICT (rowid) DO ";
  100. $sql .= "UPDATE SET ";
  101. $updatFieldsValues = [];
  102. foreach ($header_array as $field) {
  103. if ($field != 'rowid') {
  104. $updatFieldsValues[] = $field . ' = EXCLUDED.' . $field;
  105. }
  106. }
  107. $sql .= implode(', ', $updatFieldsValues) . ";";
  108. //$sql .= "<br><br>";
  109. //}
  110. }
  111. $rowCounter++;
  112. }
  113. fclose($handle);
  114. } else {
  115. echo "Nem lehet megnyitni a fájlt!";
  116. }
  117. return $sql;
  118. }
  119. function runQuery($sql, $table)
  120. {
  121. global $db;
  122. echo 'START query runnning: <b>' . $table . '</b><br>';
  123. $sql_commands = explode(';', $sql);
  124. foreach ($sql_commands as $sql_command) {
  125. $sql_command = trim($sql_command); // Üres parancsok eltávolítása
  126. if (!empty($sql_command)) {
  127. $resql = $db->query($sql_command);
  128. if (!$resql) {
  129. echo $sql . '<br>';
  130. echo "Hiba történt: " . $db->lasterror() . '<br>';
  131. echo $sql_command . '<br><br>';
  132. }
  133. }
  134. }
  135. echo 'KÉSZ!<br><br>';
  136. }
  137. $files = ['llx_product_excelia_staging_Excel', 'llx_product_extrafields_excelia_staging_Excel', 'llx_product_price_excelia_staging_Excel', 'llx_product_association_excelia_staging_Excel'];
  138. print '<form method="post" action="' . $_SERVER["PHP_SELF"] . '">';
  139. print '<input type="hidden" name="token" value="' . newToken() . '">';
  140. if (isset($_REQUEST['submit_button'])) {
  141. $debug = false;
  142. $sql_llx_product = productsCreator('llx_product', $llx_product_schema);
  143. $sql_llx_product_extrafields = productsCreator('llx_product_extrafields', $llx_product_extrafields_schema);
  144. $sql_llx_product_price = productsCreator('llx_product_price', $llx_product_price_schema);
  145. $sql_llx_product_association = productsCreator('llx_product_association', $llx_product_association_schema);
  146. if ($debug) {
  147. print $sql_llx_product;
  148. print $sql_llx_product_extrafields;
  149. print $sql_llx_product_price;
  150. print $sql_llx_product_association;
  151. } else {
  152. runQuery($sql_llx_product, 'llx_product');
  153. runQuery($sql_llx_product_extrafields, 'llx_product_extrafields');
  154. runQuery($sql_llx_product_price, 'llx_product_price');
  155. runQuery($sql_llx_product_association, 'llx_product_association');
  156. echo 'KÉSZ!';
  157. }
  158. print '<input type="submit" name="done" value="OK">';
  159. } elseif ($_REQUEST['done'] || !isset($_REQUEST['submit_button'])) {
  160. foreach ($files as $file) {
  161. if (file_exists('csv/' . $file . '.csv')) {
  162. print $file . '.csv - <font style="color: green;"><b>OK</b></font><br>';
  163. } else {
  164. print $file . '.csv - <font style="color: RED;"><b>PROBLEM</b></font><br>';
  165. }
  166. }
  167. print '<br><br>';
  168. print '<input type="submit" name="submit_button" value="Termékek és szolgáltatások generálása!">';
  169. }
  170. print '</form>';