customer_ajax.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. JSON::header();
  3. $customer = Customer::get($id);
  4. if(!$customer) JSON::fail("Customer not found.");
  5. if(!Core::$user->hasPermission($customer)) throw new Exception(__("No permission."));
  6. switch($action) {
  7. case "get-locations":
  8. $nvp = ["customer_id" => $customer->id];
  9. $id = http::value("id", "int");
  10. $internal_id = http::value("internal_id", "trim");
  11. $name = http::value("name", "trim");
  12. if($id) $nvp["id"] = $id;
  13. if($internal_id) $nvp["internal_id"] = db::escape($internal_id);
  14. if($name) $nvp["name"] = (new Constraint("CONTAINS"))->setValues(db::escape($name));
  15. $locations = Location::query($nvp, ["sort" => "name", "order" => "asc"]);
  16. $output = [];
  17. foreach($locations as $location) {
  18. $filename = Util::slug($customer->name);
  19. $icon = false;
  20. if(file_exists("public/img/markers/$filename.png")) {
  21. $icon = $filename . ".png";
  22. }
  23. $output[] = [
  24. "id" => $location->id,
  25. "name" => $location->name,
  26. "icon" => $icon,
  27. "latitude" => (float) $location->latitude,
  28. "longitude" => (float) $location->longitude
  29. ];
  30. }
  31. JSON::success([
  32. "locations" => $output
  33. ]);
  34. break;
  35. }