product_handler.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. if (!mb_check_encoding($row_array[$fieldCounter], 'UTF-8')) {
  88. $row_array[$fieldCounter] = iconv('ISO-8859-2', 'UTF-8', $row_array[$fieldCounter]);
  89. }
  90. //print $row_array[$fieldCounter] . '<br>';
  91. $fieldsValues[] = '\'' . $row_array[$fieldCounter] . '\'';
  92. }
  93. } else {
  94. if ($field == 'fk_user_author') {
  95. $fieldsValues[] = 1;
  96. } else {
  97. $fieldsValues[] = $row_array[$fieldCounter];
  98. }
  99. }
  100. $fieldCounter++;
  101. }
  102. $sql .= implode(',', $fieldsValues);
  103. $sql .= ") ON CONFLICT (rowid) DO ";
  104. $sql .= "UPDATE SET ";
  105. $updatFieldsValues = [];
  106. foreach ($header_array as $field) {
  107. if ($field != 'rowid') {
  108. $updatFieldsValues[] = $field . ' = EXCLUDED.' . $field;
  109. }
  110. }
  111. $sql .= implode(', ', $updatFieldsValues) . ";";
  112. //$sql .= "<br><br>";
  113. //}
  114. }
  115. $rowCounter++;
  116. }
  117. fclose($handle);
  118. } else {
  119. echo "Nem lehet megnyitni a fájlt!";
  120. }
  121. return $sql;
  122. }
  123. function runQuery($sql, $table)
  124. {
  125. global $db;
  126. echo 'START query runnning: <b>' . $table . '</b><br>';
  127. $sql_commands = explode(';', $sql);
  128. foreach ($sql_commands as $sql_command) {
  129. $sql_command = trim($sql_command); // Üres parancsok eltávolítása
  130. if (!empty($sql_command)) {
  131. $resql = $db->query($sql_command);
  132. if (!$resql) {
  133. //echo $sql . '<br>';
  134. echo '<br>';
  135. echo "Hiba történt: " . $db->lasterror() . '<br>';
  136. echo $sql_command . '<br><br>';
  137. }
  138. }
  139. }
  140. echo 'KÉSZ!<br><br>';
  141. }
  142. $files = ['llx_product_excelia_staging_Excel', 'llx_product_extrafields_excelia_staging_Excel', 'llx_product_price_excelia_staging_Excel', 'llx_product_association_excelia_staging_Excel'];
  143. print '<form method="post" action="' . $_SERVER["PHP_SELF"] . '">';
  144. print '<input type="hidden" name="token" value="' . newToken() . '">';
  145. if (isset($_REQUEST['submit_button'])) {
  146. $debug = false;
  147. $sql_llx_product = productsCreator('llx_product', $llx_product_schema);
  148. $sql_llx_product_extrafields = productsCreator('llx_product_extrafields', $llx_product_extrafields_schema);
  149. $sql_llx_product_price = productsCreator('llx_product_price', $llx_product_price_schema);
  150. $sql_llx_product_association = productsCreator('llx_product_association', $llx_product_association_schema);
  151. if ($debug) {
  152. print $sql_llx_product;
  153. print $sql_llx_product_extrafields;
  154. print $sql_llx_product_price;
  155. print $sql_llx_product_association;
  156. } else {
  157. runQuery($sql_llx_product, 'llx_product');
  158. runQuery($sql_llx_product_extrafields, 'llx_product_extrafields');
  159. runQuery($sql_llx_product_price, 'llx_product_price');
  160. runQuery($sql_llx_product_association, 'llx_product_association');
  161. echo 'KÉSZ!';
  162. }
  163. print '<input type="submit" name="done" value="OK">';
  164. } elseif ($_REQUEST['done'] || !isset($_REQUEST['submit_button'])) {
  165. foreach ($files as $file) {
  166. if (file_exists('csv/' . $file . '.csv')) {
  167. print $file . '.csv - <font style="color: green;"><b>OK</b></font><br>';
  168. } else {
  169. print $file . '.csv - <font style="color: RED;"><b>PROBLEM</b></font><br>';
  170. }
  171. }
  172. print '<br><br>';
  173. print '<input type="submit" name="submit_button" value="Termékek és szolgáltatások generálása!">';
  174. }
  175. print '</form>';