customer_ajax.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * @var int $id
  4. * @var string $action
  5. */
  6. JSON::header();
  7. $customer = Customer::get($id);
  8. if(!$customer) JSON::fail("Customer not found.");
  9. if(!Core::$user->hasPermission($customer)) throw new Exception(__("No permission."));
  10. switch($action) {
  11. case "get-locations":
  12. $nvp = [
  13. "customer_id" => $customer->id,
  14. "archived" => 0,
  15. "closed" => 0
  16. ];
  17. $id = http::value("id", "int");
  18. $internal_id = http::value("internal_id", "trim");
  19. $name = http::value("name", "trim");
  20. $legal_entity_name = http::value("legal_entity_name", "trim");
  21. $include_archived = http::value("include_archived", "int");
  22. $include_closed = http::value("include_closed", "int");
  23. if($id) $nvp["id"] = $id;
  24. if($internal_id) $nvp["internal_id"] = db::escape($internal_id);
  25. if($name) $nvp["name"] = Constraint::contains($name);
  26. if($legal_entity_name) $nvp["$legal_entity_name"] = Constraint::contains($legal_entity_name);
  27. if($include_archived) unset($nvp["archived"]);
  28. if($include_closed) unset($nvp["closed"]);
  29. $locations = Location::query($nvp, ["sort" => "name", "order" => "asc"]);
  30. $output = [];
  31. foreach($locations as $location) {
  32. $filename = Util::slug($customer->name);
  33. $icon = false;
  34. if(file_exists("public/img/markers/$filename.png")) {
  35. $icon = $filename . ".png";
  36. }
  37. $output[] = [
  38. "id" => $location->id,
  39. "name" => $location->name,
  40. "icon" => $icon,
  41. "latitude" => (float) $location->latitude,
  42. "longitude" => (float) $location->longitude,
  43. "archived" => $location->isArchived()
  44. ];
  45. }
  46. JSON::success([
  47. "locations" => $output
  48. ]);
  49. break;
  50. }