$this->id]); foreach($links as $link) { $link->delete(); } foreach($types as $type) { $link = new LocationVenueType; $link->location_id = $this->id; $link->venue_type_id = $type->id; $link->commit(); } return true; } /** * @return VenueType[] * @throws Exception */ public function getVenueTypes(): array { $links = LocationVenueType::query(["location_id" => $this->id]); $ids = array_column($links, "venue_type_id"); return VenueType::query(["id" => $ids]); } /** * @return Customer|null * @throws Exception */ public function getCustomer(): ?Customer { return Customer::get($this->customer_id); } public function getDirectory() { $directory = ROOT_DIR . "/files/locations/" . $this->id; if(!file_exists($directory)) mkdir($directory, 0777, true); return $directory; } public function getDisplayAddress($fields = ["address_1", "address_2", "city", "county", "postal_code", "state", "country"], $glue = ", ") { $parts = []; foreach($fields as $field) { $parts[] = $this->$field; } return implode($glue, array_filter($parts)); } public function getGeolocation() { return [ doubleval($this->latitude), doubleval($this->longitude) ]; } /** * @return LocationFile[] * @throws Exception */ public function getPhotos() { return LocationFile::query([ "location_id" => $this->id, "type" => LocationFile::TYPE_PHOTO ]); } /** * @return Test[] * @throws Exception */ public function getTests() { return Test::query([ "location_id" => $this->id, "status" => (new Constraint("MORE_THAN_OR_EQUAL"))->setValues(Test::STATUS_UNASSIGNED), ], [ "sort" => "dts_start", "order" => "desc", ]); } /** * @return Test[] * @throws Exception */ public function getCompletedTests() { return Test::query([ "location_id" => $this->id, "status" => Test::STATUS_COMPLETED, ], [ "sort" => "dts_start", "order" => "desc", ]); } /** * @param object $old_data * @param array $modified_fields * @return bool * @throws Exception */ public function logUpdate(object $old_data, array $modified_fields): bool { if(empty($modified_fields)) return true; $entry = new LocationUpdateLogEntry; $entry->location_id = $this->id; $entry->user_id = Core::$user->id; $entry->modifications_json = json_encode($this->getModifiedArray($old_data, $modified_fields)); $entry->dts = Util::getDateTime(); return $entry->commit(); } /** * @return LocationUpdateLogEntry[] * @throws Exception */ public function getUpdateLogEntries() { return LocationUpdateLogEntry::query([ "location_id" => $this->id, ], [ "sort" => "dts", "order" => "desc", ]); } /** * @param object $old_data * @param array $modified_fields * @return array */ private function getModifiedArray(object $old_data, array $modified_fields): array { $new_data = $this->getModelData(); $modified = []; foreach($modified_fields as $field) { $modified[] = [ "field" => $field, "before" => $old_data->$field, "after" => $new_data->$field, ]; } return $modified; } public function isArchived(): bool { return (bool) $this->archived; } /** * @return LocationNote[] * @throws Exception */ public function getNotes(): array { return LocationNote::query([ "location_id" => $this->id, "dts_deleted" => null, ], [ "sort" => "id", "order" => "desc", ]); } }