location.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @method Location get(int $id)
  4. */
  5. class Location extends Model {
  6. protected static $table = "location";
  7. protected static $id_field = "id";
  8. protected static $fillable_fields = ["customer_id", "name", "legal_entity_name", "address_1", "address_2", "city", "county", "postal_code", "state", "country", "email", "phone", "opening_hours", "shop_left", "shop_right", "brand", "region", "region_name", "internal_id", "area_manager", "area_manager_name", "area_manager_email", "operations_manager_name", "closed", "latitude", "longitude", "dts_verified"];
  9. // Object methods
  10. public function assignVenueTypes($types) {
  11. $links = LocationVenueType::query(["location_id" => $this->id]);
  12. foreach($links as $link) {
  13. $link->delete();
  14. }
  15. foreach($types as $type) {
  16. $link = new LocationVenueType;
  17. $link->location_id = $this->id;
  18. $link->venue_type_id = $type->id;
  19. $link->commit();
  20. }
  21. return true;
  22. }
  23. public function getVenueTypes() {
  24. $links = LocationVenueType::query(["location_id" => $this->id]);
  25. $ids = array_column($links, "venue_type_id");
  26. return VenueType::query(["id" => $ids]);
  27. }
  28. public function getCustomer() {
  29. return Customer::get($this->customer_id);
  30. }
  31. public function getDirectory() {
  32. $directory = ROOT_DIR . "/files/locations/" . $this->id;
  33. if(!file_exists($directory)) mkdir($directory, 0777, true);
  34. return $directory;
  35. }
  36. public function getDisplayAddress($fields = ["address_1", "address_2", "city", "county", "postal_code", "state", "country"], $glue = ", ") {
  37. $parts = [];
  38. foreach($fields as $field) {
  39. $parts[] = $this->$field;
  40. }
  41. return implode($glue, array_filter($parts));
  42. }
  43. public function getGeolocation() {
  44. return [
  45. doubleval($this->latitude),
  46. doubleval($this->longitude)
  47. ];
  48. }
  49. /**
  50. * @return LocationFile[]
  51. */
  52. public function getPhotos() {
  53. return LocationFile::query([
  54. "location_id" => $this->id,
  55. "type" => LocationFile::TYPE_PHOTO
  56. ]);
  57. }
  58. /**
  59. * @return Test[]
  60. */
  61. public function getTests() {
  62. return Test::query([
  63. "location_id" => $this->id,
  64. "status" => (new Constraint("MORE_THAN_OR_EQUAL"))->setValues(Test::STATUS_UNASSIGNED),
  65. ], [
  66. "sort" => "dts_start",
  67. "order" => "desc",
  68. ]);
  69. }
  70. /**
  71. * @return Test[]
  72. */
  73. public function getCompletedTests() {
  74. return Test::query([
  75. "location_id" => $this->id,
  76. "status" => Test::STATUS_COMPLETED,
  77. ], [
  78. "sort" => "dts_start",
  79. "order" => "desc",
  80. ]);
  81. }
  82. }