location.php 4.9 KB

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