location.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * @property int $id
  4. * @property int $customer_id
  5. * @property string $name
  6. * @property string $legal_entity_name
  7. * @property string $address_1
  8. * @property string $address_2
  9. * @property string $city
  10. * @property string $county
  11. * @property string $postal_code
  12. * @property string $state
  13. * @property string $country
  14. * @property string $email
  15. * @property string $phone
  16. * @property string $opening_hours
  17. * @property string $shop_left
  18. * @property string $shop_right
  19. * @property string $brand
  20. * @property string $region
  21. * @property string $region_name
  22. * @property string $internal_id
  23. * @property string $area_manager
  24. * @property string $area_manager_name
  25. * @property string $area_manager_email
  26. * @property string $operations_manager_name
  27. * @property bool $closed
  28. * @property double $latitude
  29. * @property double $longitude
  30. * @property string $dts_verified
  31. */
  32. class Location extends Model {
  33. protected static $table = "location";
  34. protected static $id_field = "id";
  35. 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"];
  36. // Object methods
  37. /**
  38. * @param array $types
  39. * @return bool
  40. * @throws Exception
  41. */
  42. public function assignVenueTypes(array $types): bool {
  43. $links = LocationVenueType::query(["location_id" => $this->id]);
  44. foreach($links as $link) {
  45. $link->delete();
  46. }
  47. foreach($types as $type) {
  48. $link = new LocationVenueType;
  49. $link->location_id = $this->id;
  50. $link->venue_type_id = $type->id;
  51. $link->commit();
  52. }
  53. return true;
  54. }
  55. /**
  56. * @return VenueType[]
  57. * @throws Exception
  58. */
  59. public function getVenueTypes(): array {
  60. $links = LocationVenueType::query(["location_id" => $this->id]);
  61. $ids = array_column($links, "venue_type_id");
  62. return VenueType::query(["id" => $ids]);
  63. }
  64. /**
  65. * @return Customer|null
  66. * @throws Exception
  67. */
  68. public function getCustomer(): ?Customer {
  69. return Customer::get($this->customer_id);
  70. }
  71. public function getDirectory() {
  72. $directory = ROOT_DIR . "/files/locations/" . $this->id;
  73. if(!file_exists($directory)) mkdir($directory, 0777, true);
  74. return $directory;
  75. }
  76. public function getDisplayAddress($fields = ["address_1", "address_2", "city", "county", "postal_code", "state", "country"], $glue = ", ") {
  77. $parts = [];
  78. foreach($fields as $field) {
  79. $parts[] = $this->$field;
  80. }
  81. return implode($glue, array_filter($parts));
  82. }
  83. public function getGeolocation() {
  84. return [
  85. doubleval($this->latitude),
  86. doubleval($this->longitude)
  87. ];
  88. }
  89. /**
  90. * @return LocationFile[]
  91. * @throws Exception
  92. */
  93. public function getPhotos() {
  94. return LocationFile::query([
  95. "location_id" => $this->id,
  96. "type" => LocationFile::TYPE_PHOTO
  97. ]);
  98. }
  99. /**
  100. * @return Test[]
  101. * @throws Exception
  102. */
  103. public function getTests() {
  104. return Test::query([
  105. "location_id" => $this->id,
  106. "status" => (new Constraint("MORE_THAN_OR_EQUAL"))->setValues(Test::STATUS_UNASSIGNED),
  107. ], [
  108. "sort" => "dts_start",
  109. "order" => "desc",
  110. ]);
  111. }
  112. /**
  113. * @return Test[]
  114. * @throws Exception
  115. */
  116. public function getCompletedTests() {
  117. return Test::query([
  118. "location_id" => $this->id,
  119. "status" => Test::STATUS_COMPLETED,
  120. ], [
  121. "sort" => "dts_start",
  122. "order" => "desc",
  123. ]);
  124. }
  125. /**
  126. * @param object $old_data
  127. * @param array $modified_fields
  128. * @return bool
  129. * @throws Exception
  130. */
  131. public function logUpdate(object $old_data, array $modified_fields): bool {
  132. if(empty($modified_fields)) return true;
  133. $entry = new LocationUpdateLogEntry;
  134. $entry->location_id = $this->id;
  135. $entry->user_id = Core::$user->id;
  136. $entry->modifications_json = json_encode($this->getModifiedArray($old_data, $modified_fields));
  137. $entry->dts = Util::getDateTime();
  138. return $entry->commit();
  139. }
  140. /**
  141. * @return LocationUpdateLogEntry[]
  142. * @throws Exception
  143. */
  144. public function getUpdateLogEntries() {
  145. return LocationUpdateLogEntry::query([
  146. "location_id" => $this->id,
  147. ], [
  148. "sort" => "dts",
  149. "order" => "desc",
  150. ]);
  151. }
  152. /**
  153. * @param object $old_data
  154. * @param array $modified_fields
  155. * @return array
  156. */
  157. private function getModifiedArray(object $old_data, array $modified_fields): array {
  158. $new_data = $this->getModelData();
  159. $modified = [];
  160. foreach($modified_fields as $field) {
  161. $modified[] = [
  162. "field" => $field,
  163. "before" => $old_data->$field,
  164. "after" => $new_data->$field,
  165. ];
  166. }
  167. return $modified;
  168. }
  169. }