No Description

listFlowers.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. require_once('config.php');
  3. // Filter unsupported HTTP requests
  4. if ($_SERVER["REQUEST_METHOD"] !== "GET") {
  5. http_response_code(400); // use appropriate status
  6. echo json_encode(array("error" => "Endpoint only supports GET requests"));
  7. exit();
  8. }
  9. // Get user input
  10. $userID = isset($_GET['user_id']) ? $_GET['user_id'] : '';
  11. $q = isset($_GET['q']) ? $_GET['q'] : '';
  12. $page = isset($_GET['page']) ? $_GET['page'] : '';
  13. $edible = isset($_GET['edible']) ? $_GET['edible'] : '';
  14. $vegetable = isset($_GET['vegetable']) ? $_GET['vegetable'] : '';
  15. $scientificName = isset($_GET['scientific_name']) ? $_GET['scientific_name'] : '';
  16. $growthMonths = isset($_GET['growth_months']) ? $_GET['growth_months'] : '';
  17. $bloomMonths = isset($_GET['bloom_months']) ? $_GET['bloom_months'] : '';
  18. $color = isset($_GET['flower_color']) ? $_GET['flower_color'] : '';
  19. // Clean user input
  20. $userID = $connection->real_escape_string($userID);
  21. function prepareQueryParams($q = '', $edible = '', $vegetable = '', $scientificName = '', $growthMonths = '', $bloomMonths = '', $color = '', $page = '') {
  22. // QUERY PARAMETERS
  23. // - q: String
  24. // - page: Int
  25. // - edible: Bool
  26. // - vegetable: Bool
  27. // - scientific_name: String
  28. // - growth_months: Int
  29. // - bloom_months: Int
  30. global $TREFFLE_TOKEN;
  31. // Define preliminar round of params
  32. $queryParams = array(
  33. "token" => $TREFFLE_TOKEN,
  34. "page" => $page ? $page : '1',
  35. "filter[edible]" => $edible ? $edible : 'false',
  36. "filter[vegetable]" => $vegetable ? $vegetable : 'false',
  37. );
  38. // Set the rest of the params
  39. if($q) {
  40. $queryParams['q'] = $q;
  41. }
  42. if($scientificName) {
  43. $queryParams['filter[scientific_name]'] = $scientificName;
  44. }
  45. if($growthMonths) {
  46. $queryParams['filter[growth_months]'] = $growthMonths;
  47. }
  48. if($bloomMonths) {
  49. $queryParams['filter[bloom_months]'] = $bloomMonths;
  50. }
  51. if($color) {
  52. $queryParams['filter[flower_color]'] = $color;
  53. }
  54. return $queryParams;
  55. }
  56. function callAPI($queryParams) {
  57. global $TREFFLE_BASE_URL, $TREFFLE_LIST_PATH, $TREFFLE_SEARCH_PATH;
  58. if(isset($queryParams['q']) && $queryParams['q'] != '') {
  59. $endpoint = $TREFFLE_BASE_URL . $TREFFLE_SEARCH_PATH;
  60. } else {
  61. $endpoint = $TREFFLE_BASE_URL . $TREFFLE_LIST_PATH;
  62. }
  63. $curl = curl_init();
  64. $url = sprintf("%s?%s", $endpoint, http_build_query($queryParams));
  65. curl_setopt($curl, CURLOPT_URL, $url);
  66. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  67. $response = curl_exec($curl);
  68. $http_response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  69. curl_close($curl);
  70. return array("data" => json_decode($response), "code" => $http_response_code);
  71. }
  72. function listFlowers($userID, $q = '', $edible = '', $vegetable = '', $scientificName = '', $growthMonths = '', $bloomMonths = '', $color = '', $page = '') {
  73. if ($userID == '') {
  74. http_response_code(400); // use appropriate status code
  75. echo json_encode(array("error" => "Field 'user_id' is required"));
  76. return;
  77. }
  78. global $connection;
  79. $query = "SELECT *
  80. FROM UserHistory AS H
  81. WHERE H.user_id = '$userID';";
  82. if ($result = $connection->query($query)) {
  83. // Query API
  84. $queryParams = prepareQueryParams($q, $edible, $vegetable, $scientificName, $growthMonths, $bloomMonths, $color, $page);
  85. $apiOutput = callAPI($queryParams);
  86. $data = $apiOutput['data'];
  87. $code = $apiOutput['code'];
  88. // Handle API errors
  89. if ($code >= 300 || $code < 200) {
  90. http_response_code(500);
  91. echo json_encode(array("error" => "Unknown error occurred with Treffle API (Code: $code)"));
  92. return;
  93. } elseif (isset($data->error) && $data->error == "true") {
  94. http_reponse_code(400);
  95. echo json_encode(array("error" => isset($data->messages) ? $data->messages : $data->message));
  96. return;
  97. }
  98. // Extract flowers
  99. $flowers = $data->data;
  100. // Initialize all both fields to false for all flowers
  101. foreach($flowers as $f) {
  102. $f->isFavorite = false;
  103. $f->hasBeenFound = false;
  104. }
  105. // Loop through each "favorited"/"marked" flower and update accordingly
  106. // NOTE: WOULD'VE BEEN EASIER IF F WERE A DICT :(
  107. while($row = $result->fetch_assoc()) {
  108. foreach($flowers as $f) {
  109. if($f->id == $row['flower_id']) {
  110. $f->isFavorite = $row['in_wishlist'] == "1" ? true : false;
  111. $f->hasBeenFound = $row['has_been_found'] == "1" ? true : false;
  112. }
  113. }
  114. }
  115. // Return response
  116. http_response_code(200);
  117. $response = new stdClass;
  118. $response->data = $flowers;
  119. $response->links = $data->links;
  120. $response->meta = $data->meta;
  121. echo json_encode($response);
  122. } else {
  123. http_response_code(500); // use appropriate status code
  124. echo json_encode(array("error" => $connection->error));
  125. }
  126. }
  127. listFlowers($userID, $q, $edible, $vegetable, $scientificName, $growthMonths, $bloomMonths, $color, $page);