location.php 4.9 KB

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