<?php
namespace App\Entity;
use App\Repository\LocationRepository;
use App\Service\UploaderHelper;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity(repositoryClass=LocationRepository::class)
*/
class Location extends ChangeableEntity
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups("main")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups("main")
*/
private $name;
/**
* @ORM\Column(type="decimal", precision=9, scale=6, nullable=true)
* @Groups("main")
*/
private $lat;
/**
* @ORM\Column(type="decimal", precision=9, scale=6, nullable=true)
* @Groups("main")
*/
private $lon;
/**
* @ORM\OneToMany(targetEntity=Event::class, mappedBy="location")
*/
private $events;
/**
* @ORM\Column(type="boolean")
*/
protected $approved;
/**
* @ORM\Column(type="string", length=255)
*/
private $type;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\OneToMany(targetEntity=LocationReference::class, mappedBy="location", orphanRemoval=true)
*/
private $locationReferences;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $image;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="locations")
*/
private $owner;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $todayKnownAs;
/**
* @ORM\ManyToOne(targetEntity=Location::class, inversedBy="updates")
*/
protected $updateOf;
/**
* @ORM\OneToMany(targetEntity=Location::class, mappedBy="updateOf")
*/
protected $updates;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="taggedDeleteLocations")
*/
protected $taggedForDeleteBy;
/**
* @ORM\OneToMany(targetEntity=TravelPath::class, mappedBy="locationFrom", orphanRemoval=true)
*/
private $travelPaths;
/**
* @ORM\OneToMany(targetEntity=TravelPath::class, mappedBy="locationTo", orphanRemoval=true)
*/
private $travelPathsTo;
public function __construct()
{
$this->events = new ArrayCollection();
$this->locationReferences = new ArrayCollection();
//$this->changes = new ArrayCollection();
$this->travelPaths = new ArrayCollection();
$this->travelPathsTo = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getLat(): ?string
{
return $this->lat;
}
public function setLat(?string $lat): self
{
$this->lat = $lat;
return $this;
}
public function getLon(): ?string
{
return $this->lon;
}
public function setLon(?string $lon): self
{
$this->lon = $lon;
return $this;
}
/**
* @return Collection|Event[]
*/
public function getEvents(): Collection
{
return $this->events;
}
public function addEvent(Event $event): self
{
if (!$this->events->contains($event)) {
$this->events[] = $event;
$event->setLocation($this);
}
return $this;
}
public function removeEvent(Event $event): self
{
if ($this->events->contains($event)) {
$this->events->removeElement($event);
// set the owning side to null (unless already changed)
if ($event->getLocation() === $this) {
$event->setLocation(null);
}
}
return $this;
}
public function __toString()
{
return $this->name;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
/**
* @return Collection|LocationReference[]
*/
public function getLocationReferences(): Collection
{
return $this->locationReferences;
}
public function addLocationReference(LocationReference $locationReference): self
{
if (!$this->locationReferences->contains($locationReference)) {
$this->locationReferences[] = $locationReference;
$locationReference->setLocation($this);
}
return $this;
}
public function removeLocationReference(LocationReference $locationReference): self
{
if ($this->locationReferences->contains($locationReference)) {
$this->locationReferences->removeElement($locationReference);
// set the owning side to null (unless already changed)
if ($locationReference->getLocation() === $this) {
$locationReference->setLocation(null);
}
}
return $this;
}
public function getReferenceList($type = null): array
{
$list = [];
;
foreach($this->getLocationReferences() as $locationReference) {
//$ref = $locationReference->getReference();
if(!$type || $locationReference->getType() == $type){
$list[] = $locationReference;
}
}
/** @var Reference $ref*/
return $list;
}
public function getImage(): ?string
{
if($this->image !== null) {
return UploaderHelper::LOCATION_IMAGE.'/'. $this->image;
} else {
$defaultFileName = UploaderHelper::LOCATION_IMAGE.'/'. $this->type .'.png';
/* if(!file_exists($defaultFileName)) {
dd($defaultFileName);
$defaultFileName = UploaderHelper::LOCATION_IMAGE.'/'. 'default.png';
}*/
return $defaultFileName;
}
return null;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
public function getOwner(): ?User
{
return $this->owner;
}
public function setOwner(?User $owner): self
{
$this->owner = $owner;
return $this;
}
public function getTodayKnownAs(): ?string
{
return $this->todayKnownAs;
}
public function setTodayKnownAs(?string $todayKnownAs): self
{
$this->todayKnownAs = $todayKnownAs;
return $this;
}
/*
public function getUpdateOf(): ?self
{
return $this->updateOf;
}
public function getUpdate(?User $user) : self{
if ($user && !in_array('ROLE_ACCEPT_CHANGES', $user->getRoles())) {
$arr = $this->updates->toArray();
$arr = array_filter($arr, function(Location $location) use ($user){
return ($location->getOwner() == $user);
});
if(!empty($arr)) {
return array_slice($arr, 0, 1)[0];;
}
}
return $this;
}
public function setUpdateOf(?self $updateOf): self
{
$this->updateOf = $updateOf;
return $this;
}
/**
* @return Collection|self[]
* /
public function getUpdates(): Collection
{
return $this->updates;
}
public function addUpdate(self $update): self
{
if (!$this->updates->contains($update)) {
$this->updates[] = $update;
$update->setUpdateOf($this);
}
return $this;
}
public function removeUpdate(self $update): self
{
if ($this->updates->removeElement($update)) {
// set the owning side to null (unless already changed)
if ($update->getUpdateOf() === $this) {
$update->setUpdateOf(null);
}F
}
return $this;
}*/
public function prepareDelete(User $user, EntityManagerInterface $em)
{
foreach ($this->getLocationReferences() as $locationReference) {
$this->removeLocationReference($locationReference);
$em->remove($locationReference);
}
}
public function getChangesAsString(?self $otherLocation) : string {
$s = "";
if($otherLocation)
{
if($this->getName() != $otherLocation->getName()) {
$s .= '<br>Name: <s>'. $this->getName() .'</s><i class="fa fa-arrow-circle-right"></i>'. $otherLocation->getName();
}
if($this->getLat() != $otherLocation->getLat() || $this->getLon() != $otherLocation->getLon()) {
$s .= '<br>Lat/Lon: <s>'. $this->getLat() .'/'. $this->getLon()
.'</s><i class="fa fa-arrow-circle-right"></i>'. $otherLocation->getLat() .'/'. $otherLocation->getLon();
}
if($this->getTodayKnownAs() != $otherLocation->getTodayKnownAs()) {
$s.='<br>Today known as: <s>'. $this->getTodayKnownAs() .'</s><i class="fa fa-arrow-circle-right"></i>'. $otherLocation->getTodayKnownAs();
}
} else {
$s.= '<br>Name: '. $this->getName();
$s.= '<br>Lat/Lon: '. $this->getLat() .'/'. $this->getLon();
$s.='<br>Today known as: '. $this->getTodayKnownAs();
}
return $s;
}
/**
* @return Collection|TravelPath[]
*/
public function getTravelPaths(): Collection
{
return $this->travelPaths;
}
public function addTravelPath(TravelPath $travelPath): self
{
if (!$this->travelPaths->contains($travelPath)) {
$this->travelPaths[] = $travelPath;
$travelPath->setLocationFrom($this);
}
return $this;
}
public function removeTravelPath(TravelPath $travelPath): self
{
if ($this->travelPaths->removeElement($travelPath)) {
// set the owning side to null (unless already changed)
if ($travelPath->getLocationFrom() === $this) {
$travelPath->setLocationFrom(null);
}
}
return $this;
}
/**
* @return Collection|TravelPath[]
*/
public function getTravelPathsTo(): Collection
{
return $this->travelPathsTo;
}
public function addTravelPathsTo(TravelPath $travelPathsTo): self
{
if (!$this->travelPathsTo->contains($travelPathsTo)) {
$this->travelPathsTo[] = $travelPathsTo;
$travelPathsTo->setLocationTo($this);
}
return $this;
}
public function removeTravelPathsTo(TravelPath $travelPathsTo): self
{
if ($this->travelPathsTo->removeElement($travelPathsTo)) {
// set the owning side to null (unless already changed)
if ($travelPathsTo->getLocationTo() === $this) {
$travelPathsTo->setLocationTo(null);
}
}
return $this;
}
}