location.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. class Location extends Model {
  3. protected static $table = "location";
  4. protected static $id_field = "id";
  5. 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"];
  6. // Object methods
  7. public function assignVenueTypes($types) {
  8. $links = LocationVenueType::query(["location_id" => $this->id]);
  9. foreach($links as $link) {
  10. $link->delete();
  11. }
  12. foreach($types as $type) {
  13. $link = new LocationVenueType;
  14. $link->location_id = $this->id;
  15. $link->venue_type_id = $type->id;
  16. $link->commit();
  17. }
  18. return true;
  19. }
  20. public function getVenueTypes() {
  21. $links = LocationVenueType::query(["location_id" => $this->id]);
  22. $ids = array_column($links, "venue_type_id");
  23. return VenueType::query(["id" => $ids]);
  24. }
  25. public function getCustomer() {
  26. return Customer::get($this->customer_id);
  27. }
  28. public function getDirectory() {
  29. $directory = ROOT_DIR . "/files/locations/" . $this->id;
  30. if(!file_exists($directory)) mkdir($directory, 0777, true);
  31. return $directory;
  32. }
  33. public function getDisplayAddress($fields = ["address_1", "address_2", "city", "county", "postal_code", "state", "country"], $glue = ", ") {
  34. $parts = [];
  35. foreach($fields as $field) {
  36. $parts[] = $this->$field;
  37. }
  38. return implode($glue, array_filter($parts));
  39. }
  40. public function getGeolocation() {
  41. return [
  42. doubleval($this->latitude),
  43. doubleval($this->longitude)
  44. ];
  45. }
  46. public function getPhotos() {
  47. return LocationFile::query([
  48. "location_id" => $this->id,
  49. "type" => LocationFile::TYPE_PHOTO
  50. ]);
  51. }
  52. }
  53. ?>