/fieclient/fie_ajax.php

https://bitbucket.org/rkandpal/nustechgmgitfork · PHP · 200 lines · 158 code · 26 blank · 16 comment · 57 complexity · 8f9a222b49cff7c89536f6e4ed9fe788 MD5 · raw file

  1. <?php
  2. /**
  3. * A simple wrapper to provide AJAX access to the functions in
  4. * fieclient.php.
  5. *
  6. * @author Lincoln Maskey <lincoln@maskey.net>
  7. * @copyright 2012
  8. */
  9. require_once(dirname(__FILE__) . '/fieclient.php');
  10. require_once(dirname(__FILE__) . '/../config.php');
  11. function SendJSONOutput($the_array) {
  12. echo json_encode($the_array);
  13. die();
  14. }
  15. $result = array();
  16. $result['success'] = false;
  17. $result['error_text'] = '';
  18. $result['result'] = array();
  19. if (! (isset($_REQUEST) && isset($_REQUEST['action']))) {
  20. $result['error_text'] = 'Action not posted';
  21. SendJSONOutput($result);
  22. }
  23. if ($_REQUEST['action'] == 'get_matching_products') {
  24. if ($_REQUEST['use_dlibrary']) {
  25. $call_array = array();
  26. $products = GetMatchingProducts($_REQUEST['supplier'], $_REQUEST['product_name'], $call_array);
  27. if ($call_array['return_val'] == 0) {
  28. $result['success'] = true;
  29. $result['result'] = array_values($products);
  30. } else {
  31. $result['error_text'] = $call_array['result_text'];
  32. }
  33. } else {
  34. $attempt_number = 1;
  35. while ((count($result['result']) == 0) && ($attempt_number <= 5)) {
  36. $result = GetMatchingProducts_Database($_REQUEST['supplier'], $_REQUEST['product_name'], $attempt_number);
  37. $attempt_number++;
  38. }
  39. // 'success' just means there was no errors...
  40. }
  41. SendJSONOutput($result);
  42. }
  43. if ($_REQUEST['action'] == 'get_image_for_ean') {
  44. $call_array = array();
  45. $the_ean = (isset($_REQUEST['ean']) ? trim($_REQUEST['ean']) : '');
  46. if ((strlen($the_ean) != 8) && (strlen($the_ean) != 13)) {
  47. $result['error_text'] = 'Length of EAN ' . $the_ean . ' is not valid';
  48. SendJSONOutput($result);
  49. }
  50. // What would be the target directory here?
  51. $target_directory = 'EAN-' . strlen($the_ean) . '/';
  52. $exploded_ean = str_split($the_ean, 4);
  53. $target_directory .= implode('/', $exploded_ean);
  54. $images_root_directory = realpath(dirname(__FILE__) . '/../images/') . '/';
  55. $full_directory = ($images_root_directory . $target_directory);
  56. if (! file_exists($full_directory)) {
  57. mkdir($full_directory, 0777, true);
  58. }
  59. $image_path = CopyImageForGivenEANToGivenDirectory($_REQUEST['ean'], $full_directory, $call_array);
  60. if ($image_path && ($call_array['return_val'] == 0)) {
  61. $result['success'] = true;
  62. // We need to remove the stuff that doesn't matter to the AJAX client from the path
  63. $image_path = realpath($image_path);
  64. $image_path = str_ireplace($images_root_directory, '', $image_path);
  65. $result['result'] = $image_path;
  66. } else {
  67. $result['error_text'] = $call_array['result_text'];
  68. }
  69. SendJSONOutput($result);
  70. }
  71. $result['error_text'] = 'Unknown action: "' . $_REQUEST['action'] . '"';
  72. SendJSONOutput($result);
  73. function GetMatchingProducts_Database($supplier_name, $product_name, $attempt_number) {
  74. $result = array();
  75. $result['success'] = true;
  76. $result['error_text'] = '';
  77. $result['result'] = array();
  78. $db_link = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  79. $lookup_strings = array('brands' => array(), 'products' => array());
  80. $supplier_name = trim($supplier_name);
  81. $product_name = trim($product_name);
  82. if ($attempt_number == 1) {
  83. // Look for the supplier in the supplier field, and the description in the description field...
  84. if (strlen($supplier_name) > 0) {
  85. $lookup_strings['brands'][] = $supplier_name;
  86. }
  87. if (strlen($product_name) > 0) {
  88. $lookup_strings['products'][] = $product_name;
  89. }
  90. } else if ($attempt_number == 2) {
  91. // Look for both the supplier and the description in the description field...
  92. if (strlen($supplier_name) > 0) {
  93. $lookup_strings['products'][] = $supplier_name;
  94. }
  95. if (strlen($product_name) > 0) {
  96. $lookup_strings['products'][] = $product_name;
  97. }
  98. } else if ($attempt_number == 3) {
  99. // Just look for the description in the description field...
  100. if (strlen($product_name) > 0) {
  101. $lookup_strings['products'][] = $product_name;
  102. }
  103. } else if ($attempt_number == 4) {
  104. // Take the supplier and the description, and just look for anything with the same
  105. // words in the appropriate spot...
  106. $supplier_name_parts = explode(' ', $supplier_name);
  107. foreach ($supplier_name_parts as $supplier_name_part) {
  108. $supplier_name_part = trim($supplier_name_part);
  109. if ($supplier_name_part != '') {
  110. $lookup_strings['brands'][] = $supplier_name_part;
  111. }
  112. }
  113. $product_name_parts = explode(' ', $product_name);
  114. foreach ($product_name_parts as $product_name_part) {
  115. $product_name_part = trim($product_name_part);
  116. if ($product_name_part != '') {
  117. $lookup_strings['products'][] = $product_name_part;
  118. }
  119. }
  120. } else if ($attempt_number == 5) {
  121. // Take the description, and just look for anything with the same words...
  122. $product_name_parts = explode(' ', $product_name);
  123. foreach ($product_name_parts as $product_name_part) {
  124. $product_name_part = trim($product_name_part);
  125. if ($product_name_part != '') {
  126. $lookup_strings['products'][] = $product_name_part;
  127. }
  128. }
  129. }
  130. if (count($lookup_strings['brands']) || count($lookup_strings['products'])) {
  131. $query = "SELECT product_types.name AS product_type_name, products.aisle_id AS aisle_id, ean, description, brands.name AS supplier
  132. FROM products
  133. INNER JOIN brands ON brands.id=products.brand
  134. INNER JOIN product_types ON product_types.id=products.type
  135. WHERE ";
  136. if (count($lookup_strings['brands'])) {
  137. $query_parts = array();
  138. foreach ($lookup_strings['brands'] as $lookup_brand) {
  139. $query_parts[] = "brands.name LIKE '%" . $db_link->real_escape_string($lookup_brand) . "%'";
  140. }
  141. $query .= '(' . implode(' OR ', $query_parts) . ') ';
  142. if (count($lookup_strings['products'])) {
  143. $query .= 'AND ';
  144. }
  145. }
  146. if (count($lookup_strings['products'])) {
  147. $query_parts_name = array();
  148. $query_parts_technical_name = array();
  149. $query_parts_description = array();
  150. foreach ($lookup_strings['products'] as $lookup_product) {
  151. $query_parts_name[] = "products.name LIKE '%" . $db_link->real_escape_string($lookup_product) . "%'";
  152. $query_parts_technical_name[] = "products.technical_name LIKE '%" . $db_link->real_escape_string($lookup_product) . "%'";
  153. $query_parts_description[] = "products.description LIKE '%" . $db_link->real_escape_string($lookup_product) . "%'";
  154. }
  155. $query .= '(';
  156. $query .= '(' . implode(' OR ', $query_parts_name) . ') OR ';
  157. $query .= '(' . implode(' OR ', $query_parts_technical_name) . ') OR ';
  158. $query .= '(' . implode(' OR ', $query_parts_description) . ')';
  159. $query .= ') ';
  160. }
  161. $query .= 'ORDER BY brands.name, products.name';
  162. $db_result = $db_link->query($query);
  163. if (! $db_result) {
  164. $result['success'] = false;
  165. $result['error_text'] = 'There was an error looking up this product';
  166. } else if ($db_result->num_rows) {
  167. while ($db_row = $db_result->fetch_assoc()) {
  168. $db_row['aisle_id'] = intval($db_row['aisle_id']);
  169. $result['result'][] = $db_row;
  170. }
  171. }
  172. }
  173. return $result;
  174. }