customer_ajax.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var int $id
  4. * @var string $action
  5. */
  6. use Helpers\LocationHelper;
  7. JSON::header();
  8. $customer = Customer::get($id);
  9. if(!$customer) JSON::fail("Customer not found.");
  10. if(!Core::$user->hasPermission($customer)) throw new Exception(__("No permission."));
  11. switch($action) {
  12. case "get-locations":
  13. $nvp = [
  14. "customer_id" => $customer->id,
  15. "archived" => 0,
  16. "closed" => 0
  17. ];
  18. $id = http::value("id", "int");
  19. $internal_id = http::value("internal_id", "trim");
  20. $name = http::value("name", "trim");
  21. $legal_entity_name = http::value("legal_entity_name", "trim");
  22. $include_archived = http::value("include_archived", "int");
  23. $include_closed = http::value("include_closed", "int");
  24. $hide_tested = Http::value("hide_tested", "int");
  25. $min_date = http::value("min_date");
  26. $max_date = http::value("max_date");
  27. if($id) $nvp["id"] = $id;
  28. if($internal_id) $nvp["internal_id"] = db::escape($internal_id);
  29. if($name) $nvp["name"] = Constraint::contains($name);
  30. if($legal_entity_name) $nvp["$legal_entity_name"] = Constraint::contains($legal_entity_name);
  31. if($include_archived) unset($nvp["archived"]);
  32. if($include_closed) unset($nvp["closed"]);
  33. $locations = Location::query($nvp, ["sort" => "name", "order" => "asc"]);
  34. $output = LocationHelper::getMarkerDataForLocations($locations, $min_date, $max_date);
  35. if($hide_tested) {
  36. $output = array_filter($output, static function(array $result) {
  37. return !isset($result["test"]);
  38. });
  39. $output = array_values($output);
  40. }
  41. JSON::success([
  42. "locations" => $output
  43. ]);
  44. break;
  45. }