location.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. public function logUpdate($old_data, $modified_fields) {
  83. if(empty($modified_fields)) return true;
  84. $entry = new LocationUpdateLogEntry;
  85. $entry->location_id = $this->id;
  86. $entry->user_id = Core::$user->id;
  87. $entry->modifications_json = json_encode($this->getModifiedArray($old_data, $modified_fields));
  88. $entry->dts = Util::getDateTime();
  89. return $entry->commit();
  90. }
  91. /**
  92. * @return LocationUpdateLogEntry[]
  93. */
  94. public function getUpdateLogEntries() {
  95. return LocationUpdateLogEntry::query([
  96. "location_id" => $this->id,
  97. ], [
  98. "sort" => "dts",
  99. "order" => "desc",
  100. ]);
  101. }
  102. private function getModifiedArray($old_data, $modified_fields): array {
  103. $new_data = $this->getModelData();
  104. $modified = [];
  105. foreach($modified_fields as $field) {
  106. $modified[] = [
  107. "field" => $field,
  108. "before" => $old_data->$field,
  109. "after" => $new_data->$field,
  110. ];
  111. }
  112. return $modified;
  113. }
  114. }