1
0

location.php 4.8 KB

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