src/Entity/Location.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\LocationRepository;
  4. use App\Service\UploaderHelper;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Gedmo\Timestampable\Traits\TimestampableEntity;
  10. use Symfony\Component\Serializer\Annotation\Groups;
  11. /**
  12.  * @ORM\Entity(repositoryClass=LocationRepository::class)
  13.  */
  14. class Location extends ChangeableEntity
  15. {
  16.     use TimestampableEntity;
  17.     /**
  18.      * @ORM\Id()
  19.      * @ORM\GeneratedValue()
  20.      * @ORM\Column(type="integer")
  21.      * @Groups("main")
  22.      */
  23.     protected $id;
  24.     /**
  25.      * @ORM\Column(type="string", length=255)
  26.      * @Groups("main")
  27.      */
  28.     private $name;
  29.     /**
  30.      * @ORM\Column(type="decimal", precision=9, scale=6, nullable=true)
  31.      * @Groups("main")
  32.      */
  33.     private $lat;
  34.     /**
  35.      * @ORM\Column(type="decimal", precision=9, scale=6, nullable=true)
  36.      * @Groups("main")
  37.      */
  38.     private $lon;
  39.     /**
  40.      * @ORM\OneToMany(targetEntity=Event::class, mappedBy="location")
  41.      */
  42.     private $events;
  43.     /**
  44.      * @ORM\Column(type="boolean")
  45.      */
  46.     protected $approved;
  47.     /**
  48.      * @ORM\Column(type="string", length=255)
  49.      */
  50.     private $type;
  51.     /**
  52.      * @ORM\Column(type="text", nullable=true)
  53.      */
  54.     private $description;
  55.     /**
  56.      * @ORM\OneToMany(targetEntity=LocationReference::class, mappedBy="location", orphanRemoval=true)
  57.      */
  58.     private $locationReferences;
  59.     /**
  60.      * @ORM\Column(type="string", length=255, nullable=true)
  61.      */
  62.     private $image;
  63.     /**
  64.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="locations")
  65.      */
  66.     private $owner;
  67.     /**
  68.      * @ORM\Column(type="string", length=255, nullable=true)
  69.      */
  70.     private $todayKnownAs;
  71.     /**
  72.      * @ORM\ManyToOne(targetEntity=Location::class, inversedBy="updates")
  73.      */
  74.     protected $updateOf;
  75.     /**
  76.      * @ORM\OneToMany(targetEntity=Location::class, mappedBy="updateOf")
  77.      */
  78.     protected $updates;
  79.     /**
  80.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="taggedDeleteLocations")
  81.      */
  82.     protected $taggedForDeleteBy;
  83.     /**
  84.      * @ORM\OneToMany(targetEntity=TravelPath::class, mappedBy="locationFrom", orphanRemoval=true)
  85.      */
  86.     private $travelPaths;
  87.     /**
  88.      * @ORM\OneToMany(targetEntity=TravelPath::class, mappedBy="locationTo", orphanRemoval=true)
  89.      */
  90.     private $travelPathsTo;
  91.     public function __construct()
  92.     {
  93.         $this->events = new ArrayCollection();
  94.         $this->locationReferences = new ArrayCollection();
  95.         //$this->changes = new ArrayCollection();
  96.         $this->travelPaths = new ArrayCollection();
  97.         $this->travelPathsTo = new ArrayCollection();
  98.     }
  99.     public function getId(): ?int
  100.     {
  101.         return $this->id;
  102.     }
  103.     public function getName(): ?string
  104.     {
  105.         return $this->name;
  106.     }
  107.     public function setName(string $name): self
  108.     {
  109.         $this->name $name;
  110.         return $this;
  111.     }
  112.     public function getLat(): ?string
  113.     {
  114.         return $this->lat;
  115.     }
  116.     public function setLat(?string $lat): self
  117.     {
  118.         $this->lat $lat;
  119.         return $this;
  120.     }
  121.     public function getLon(): ?string
  122.     {
  123.         return $this->lon;
  124.     }
  125.     public function setLon(?string $lon): self
  126.     {
  127.         $this->lon $lon;
  128.         return $this;
  129.     }
  130.     /**
  131.      * @return Collection|Event[]
  132.      */
  133.     public function getEvents(): Collection
  134.     {
  135.         return $this->events;
  136.     }
  137.     public function addEvent(Event $event): self
  138.     {
  139.         if (!$this->events->contains($event)) {
  140.             $this->events[] = $event;
  141.             $event->setLocation($this);
  142.         }
  143.         return $this;
  144.     }
  145.     public function removeEvent(Event $event): self
  146.     {
  147.         if ($this->events->contains($event)) {
  148.             $this->events->removeElement($event);
  149.             // set the owning side to null (unless already changed)
  150.             if ($event->getLocation() === $this) {
  151.                 $event->setLocation(null);
  152.             }
  153.         }
  154.         return $this;
  155.     }
  156.     public function __toString()
  157.     {
  158.         return $this->name;
  159.     }
  160.     public function getType(): ?string
  161.     {
  162.         return $this->type;
  163.     }
  164.     public function setType(string $type): self
  165.     {
  166.         $this->type $type;
  167.         return $this;
  168.     }
  169.     public function getDescription(): ?string
  170.     {
  171.         return $this->description;
  172.     }
  173.     public function setDescription(?string $description): self
  174.     {
  175.         $this->description $description;
  176.         return $this;
  177.     }
  178.     /**
  179.      * @return Collection|LocationReference[]
  180.      */
  181.     public function getLocationReferences(): Collection
  182.     {
  183.         return $this->locationReferences;
  184.     }
  185.     public function addLocationReference(LocationReference $locationReference): self
  186.     {
  187.         if (!$this->locationReferences->contains($locationReference)) {
  188.             $this->locationReferences[] = $locationReference;
  189.             $locationReference->setLocation($this);
  190.         }
  191.         return $this;
  192.     }
  193.     public function removeLocationReference(LocationReference $locationReference): self
  194.     {
  195.         if ($this->locationReferences->contains($locationReference)) {
  196.             $this->locationReferences->removeElement($locationReference);
  197.             // set the owning side to null (unless already changed)
  198.             if ($locationReference->getLocation() === $this) {
  199.                 $locationReference->setLocation(null);
  200.             }
  201.         }
  202.         return $this;
  203.     }
  204.     public function getReferenceList($type null): array
  205.     {
  206.         $list = [];
  207.         ;
  208.         foreach($this->getLocationReferences() as $locationReference) {
  209.             //$ref = $locationReference->getReference();
  210.             if(!$type || $locationReference->getType() == $type){
  211.                 $list[] = $locationReference;
  212.             }
  213.         }
  214.         /** @var Reference $ref*/
  215.         return $list;
  216.     }
  217.     public function getImage(): ?string
  218.     {
  219.         if($this->image !== null) {
  220.             return UploaderHelper::LOCATION_IMAGE.'/'$this->image;
  221.         } else {
  222.             $defaultFileName UploaderHelper::LOCATION_IMAGE.'/'$this->type .'.png';
  223.            /* if(!file_exists($defaultFileName)) {
  224.                 dd($defaultFileName);
  225.                 $defaultFileName = UploaderHelper::LOCATION_IMAGE.'/'. 'default.png';
  226.             }*/
  227.             return $defaultFileName;
  228.         }
  229.         return null;
  230.     }
  231.     public function setImage(?string $image): self
  232.     {
  233.         $this->image $image;
  234.         return $this;
  235.     }
  236.     public function getOwner(): ?User
  237.     {
  238.         return $this->owner;
  239.     }
  240.     public function setOwner(?User $owner): self
  241.     {
  242.         $this->owner $owner;
  243.         return $this;
  244.     }
  245.     public function getTodayKnownAs(): ?string
  246.     {
  247.         return $this->todayKnownAs;
  248.     }
  249.     public function setTodayKnownAs(?string $todayKnownAs): self
  250.     {
  251.         $this->todayKnownAs $todayKnownAs;
  252.         return $this;
  253.     }
  254. /*
  255.     public function getUpdateOf(): ?self
  256.     {
  257.         return $this->updateOf;
  258.     }
  259.     public function getUpdate(?User $user) : self{
  260.         if ($user && !in_array('ROLE_ACCEPT_CHANGES', $user->getRoles())) {
  261.             $arr = $this->updates->toArray();
  262.             $arr = array_filter($arr, function(Location $location) use ($user){
  263.                 return ($location->getOwner() == $user);
  264.             });
  265.             if(!empty($arr)) {
  266.                 return array_slice($arr, 0, 1)[0];;
  267.             }
  268.         }
  269.         return $this;
  270.     }
  271.     public function setUpdateOf(?self $updateOf): self
  272.     {
  273.         $this->updateOf = $updateOf;
  274.         return $this;
  275.     }
  276.     /**
  277.      * @return Collection|self[]
  278.      * /
  279.     public function getUpdates(): Collection
  280.     {
  281.         return $this->updates;
  282.     }
  283.     public function addUpdate(self $update): self
  284.     {
  285.         if (!$this->updates->contains($update)) {
  286.             $this->updates[] = $update;
  287.             $update->setUpdateOf($this);
  288.         }
  289.         return $this;
  290.     }
  291.     public function removeUpdate(self $update): self
  292.     {
  293.         if ($this->updates->removeElement($update)) {
  294.             // set the owning side to null (unless already changed)
  295.             if ($update->getUpdateOf() === $this) {
  296.                 $update->setUpdateOf(null);
  297.             }F
  298.         }
  299.         return $this;
  300.     }*/
  301.     public function prepareDelete(User $userEntityManagerInterface $em)
  302.     {
  303.         foreach ($this->getLocationReferences() as $locationReference) {
  304.             $this->removeLocationReference($locationReference);
  305.             $em->remove($locationReference);
  306.         }
  307.     }
  308.     public function getChangesAsString(?self $otherLocation) : string {
  309.         $s "";
  310.         if($otherLocation)
  311.         {
  312.             if($this->getName() != $otherLocation->getName()) {
  313.                 $s .= '<br>Name: <s>'$this->getName() .'</s><i class="fa fa-arrow-circle-right"></i>'$otherLocation->getName();
  314.             }
  315.             if($this->getLat() != $otherLocation->getLat() || $this->getLon() != $otherLocation->getLon()) {
  316.                 $s .= '<br>Lat/Lon: <s>'$this->getLat() .'/'$this->getLon()
  317.                     .'</s><i class="fa fa-arrow-circle-right"></i>'$otherLocation->getLat() .'/'$otherLocation->getLon();
  318.             }
  319.             if($this->getTodayKnownAs() != $otherLocation->getTodayKnownAs()) {
  320.                 $s.='<br>Today known as: <s>'$this->getTodayKnownAs() .'</s><i class="fa fa-arrow-circle-right"></i>'$otherLocation->getTodayKnownAs();
  321.             }
  322.         } else {
  323.             $s.= '<br>Name: '$this->getName();
  324.             $s.= '<br>Lat/Lon: '$this->getLat() .'/'$this->getLon();
  325.             $s.='<br>Today known as: '$this->getTodayKnownAs();
  326.         }
  327.         return $s;
  328.     }
  329.     /**
  330.      * @return Collection|TravelPath[]
  331.      */
  332.     public function getTravelPaths(): Collection
  333.     {
  334.         return $this->travelPaths;
  335.     }
  336.     public function addTravelPath(TravelPath $travelPath): self
  337.     {
  338.         if (!$this->travelPaths->contains($travelPath)) {
  339.             $this->travelPaths[] = $travelPath;
  340.             $travelPath->setLocationFrom($this);
  341.         }
  342.         return $this;
  343.     }
  344.     public function removeTravelPath(TravelPath $travelPath): self
  345.     {
  346.         if ($this->travelPaths->removeElement($travelPath)) {
  347.             // set the owning side to null (unless already changed)
  348.             if ($travelPath->getLocationFrom() === $this) {
  349.                 $travelPath->setLocationFrom(null);
  350.             }
  351.         }
  352.         return $this;
  353.     }
  354.     /**
  355.      * @return Collection|TravelPath[]
  356.      */
  357.     public function getTravelPathsTo(): Collection
  358.     {
  359.         return $this->travelPathsTo;
  360.     }
  361.     public function addTravelPathsTo(TravelPath $travelPathsTo): self
  362.     {
  363.         if (!$this->travelPathsTo->contains($travelPathsTo)) {
  364.             $this->travelPathsTo[] = $travelPathsTo;
  365.             $travelPathsTo->setLocationTo($this);
  366.         }
  367.         return $this;
  368.     }
  369.     public function removeTravelPathsTo(TravelPath $travelPathsTo): self
  370.     {
  371.         if ($this->travelPathsTo->removeElement($travelPathsTo)) {
  372.             // set the owning side to null (unless already changed)
  373.             if ($travelPathsTo->getLocationTo() === $this) {
  374.                 $travelPathsTo->setLocationTo(null);
  375.             }
  376.         }
  377.         return $this;
  378.     }
  379. }