customer_ajax.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 = ["customer_id" => $customer->id];
  13. $id = http::value("id", "int");
  14. $internal_id = http::value("internal_id", "trim");
  15. $name = http::value("name", "trim");
  16. $legal_entity_name = http::value("legal_entity_name", "trim");
  17. if($id) $nvp["id"] = $id;
  18. if($internal_id) $nvp["internal_id"] = db::escape($internal_id);
  19. if($name) $nvp["name"] = Constraint::contains($name);
  20. if($legal_entity_name) $nvp["$legal_entity_name"] = Constraint::contains($legal_entity_name);
  21. $locations = Location::query($nvp, ["sort" => "name", "order" => "asc"]);
  22. $output = [];
  23. foreach($locations as $location) {
  24. $filename = Util::slug($customer->name);
  25. $icon = false;
  26. if(file_exists("public/img/markers/$filename.png")) {
  27. $icon = $filename . ".png";
  28. }
  29. $output[] = [
  30. "id" => $location->id,
  31. "name" => $location->name,
  32. "icon" => $icon,
  33. "latitude" => (float) $location->latitude,
  34. "longitude" => (float) $location->longitude,
  35. "archived" => $location->isArchived()
  36. ];
  37. }
  38. JSON::success([
  39. "locations" => $output
  40. ]);
  41. break;
  42. }