customer_ajax.php 1.2 KB

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