<?php
namespace App\Entity\Feed;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Filesystem\Filesystem;
//use Symfony\Component\Intl\Intl;
use Symfony\Component\Intl\Countries;
use Doctrine\Common\Collections\ArrayCollection;
/**
* FeedElement
*
* @ORM\Table(name="tam_feed_element")
* @ORM\Entity(repositoryClass="App\Repository\Feed\FeedElementRepository")
* @ORM\HasLifecycleCallbacks
*/
class FeedElement
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \App\Entity\User
*
* @ORM\ManyToOne(targetEntity="\App\Entity\User", fetch="EAGER", inversedBy="feedElements")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="tam_user", referencedColumnName="id", onDelete="SET NULL")
* })
*/
private $user;
/**
* @var \App\Entity\Feed\FeedElementType
*
* @ORM\ManyToOne(targetEntity="\App\Entity\Feed\FeedElementType", fetch="EAGER", inversedBy="feedElements")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="feed_element_type", referencedColumnName="id", onDelete="SET NULL")
* })
*/
private $feedElementType;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255, nullable=true)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="description", type="text", nullable=true)
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="place_name", type="string", length=255, nullable=true)
*/
private $placeName;
/**
* @var string
*
* @ORM\Column(name="place_address", type="string", length=255, nullable=true)
*/
private $placeAddress;
/**
* @var string
*
* @ORM\Column(name="place_zipcode", type="string", length=255, nullable=true)
*/
private $placeZipcode;
/**
* @var string
*
* @ORM\Column(name="place_city", type="string", length=255, nullable=true)
*/
private $placeCity;
/**
* @var string
*
* @ORM\Column(name="place_country", type="string", length=255, nullable=true)
*/
private $placeCountry;
/**
* @var string
*
* @ORM\Column(name="lat", type="decimal", precision=14, scale=10, nullable=true)
*/
private $lat;
/**
* @var string
*
* @ORM\Column(name="lng", type="decimal", precision=14, scale=10, nullable=true)
*/
private $lng;
/**
* @var geography
*
* @ORM\Column(name="coordinates", type="geography", nullable=true, options={"geometry_type"="POINT", "srid"=4326})
*/
private $coordinates;
/**
* @var \DateTime
*
* @ORM\Column(name="date_created", type="datetime", nullable=false)
*/
private $dateCreated;
/**
* @var \DateTime
*
* @ORM\Column(name="date_updated", type="datetime", nullable=true)
*/
private $dateUpdated;
/**
* @var boolean
*
* @ORM\Column(name="active", type="boolean", options={"default": FALSE})
*/
private $active;
/**
* @ORM\OneToOne(targetEntity="\App\Entity\Tam\Tam", mappedBy="feedElement", cascade={"persist", "remove"})
*/
private $tam;
/**
* @ORM\OneToOne(targetEntity="\App\Entity\Feed\Advert", mappedBy="feedElement", cascade={"persist", "remove"})
*/
private $advert;
/**
* @ORM\OneToOne(targetEntity="\App\Entity\Feed\FeedTransaction", mappedBy="feedElement", cascade={"persist", "remove"})
*/
private $transaction;
/**
* @var string
*
* @ORM\Column(name="illustration", type="string", length=255, nullable=true)
*/
private $illustration;
/**
* Image illustrationFile
*
* @var File
*
* @Assert\File(
* maxSize = "5M",
* mimeTypes = {"image/jpeg", "image/gif", "image/png", "image/tiff"},
* maxSizeMessage = "The maxmimum allowed file size is 5MB.",
* mimeTypesMessage = "Only the filetypes image are allowed."
* )
*/
protected $illustrationFile;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="\App\Entity\Feed\FeedMessage", mappedBy="feedElement", cascade={"persist", "remove"})
*/
private $messages;
/**
* @var float
*
*/
private $distance;
public function __construct()
{
$this->active = true;
$this->dateCreated = new \DateTime();
$this->dateUpdated = new \DateTime();
$this->illustrationFile = null;
$this->messages = new ArrayCollection();
$this->distance = 0;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set user
*
* @param User $user
*
* @return FeedElement
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* Set feedElementType
*
* @param FeedElementType $feedElementType
*
* @return FeedElement
*/
public function setFeedElementType($feedElementType)
{
$this->feedElementType = $feedElementType;
return $this;
}
/**
* Get feedElementType
*
* @return FeedElementType
*/
public function getFeedElementType()
{
return $this->feedElementType;
}
/**
* Set title
*
* @param string $title
*
* @return FeedElement
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* @param string $description
*
* @return FeedElement
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set placeName
*
* @param string $placeName
*
* @return FeedElement
*/
public function setPlaceName($placeName)
{
$this->placeName = $placeName;
return $this;
}
/**
* Get placeName
*
* @return string
*/
public function getPlaceName()
{
return $this->placeName;
}
/**
* Set placeAddress
*
* @param string $placeAddress
*
* @return FeedElement
*/
public function setPlaceAddress($placeAddress)
{
$this->placeAddress = $placeAddress;
return $this;
}
/**
* Get placeAddress
*
* @return string
*/
public function getPlaceAddress()
{
return $this->placeAddress;
}
/**
* Set placeZipcode
*
* @param string $placeZipcode
*
* @return FeedElement
*/
public function setPlaceZipcode($placeZipcode)
{
$this->placeZipcode = $placeZipcode;
return $this;
}
/**
* Get placeZipcode
*
* @return string
*/
public function getPlaceZipcode()
{
return $this->placeZipcode;
}
/**
* Set placeCity
*
* @param string $placeCity
*
* @return FeedElement
*/
public function setPlaceCity($placeCity)
{
$this->placeCity = $placeCity;
return $this;
}
/**
* Get placeCity
*
* @return string
*/
public function getPlaceCity()
{
return $this->placeCity;
}
/**
* Set placeCountry
*
* @param string $placeCountry
*
* @return FeedElement
*/
public function setPlaceCountry($placeCountry)
{
$this->placeCountry = $placeCountry;
return $this;
}
/**
* Get placeCountry
*
* @return string
*/
public function getPlaceCountry()
{
return $this->placeCountry;
}
/**
* Set lat
*
* @param string $lat
* @return FeedElement
*/
public function setLat($lat)
{
$this->lat = $lat;
return $this;
}
/**
* Get lat
*
* @return string
*/
public function getLat()
{
return $this->lat;
}
/**
* Set lng
*
* @param string $lng
* @return FeedElement
*/
public function setLng($lng)
{
$this->lng = $lng;
return $this;
}
/**
* Get lng
*
* @return string
*/
public function getLng()
{
return $this->lng;
}
/**
* Set coordinates
*
* @param string $coordinates
* @return FeedElement
*/
public function setCoordinates($coordinates)
{
$this->coordinates = $coordinates;
return $this;
}
/**
* Get coordinates
*
* @return \geography
*/
public function getCoordinates()
{
return $this->coordinates;
}
/**
* Set dateCreated
*
* @param \DateTime $dateCreated
*
* @return FeedElement
*/
public function setDateCreated($dateCreated)
{
$this->dateCreated = $dateCreated;
return $this;
}
/**
* Get dateCreated
*
* @return \DateTime
*/
public function getDateCreated()
{
return $this->dateCreated;
}
/**
* Set dateUpdated
*
* @param \DateTime $dateUpdated
*
* @return FeedElement
*/
public function setDateUpdated($dateUpdated)
{
$this->dateUpdated = $dateUpdated;
return $this;
}
/**
* Get dateUpdated
*
* @return \DateTime
*/
public function getDateUpdated()
{
return $this->dateUpdated;
}
/**
* Set active
*
* @param boolean $active
*
* @return FeedElement
*/
public function setActive($active)
{
$this->active = $active;
return $this;
}
/**
* Get active
*
* @return boolean
*/
public function getActive()
{
return $this->active;
}
/**
* Set Tam
*
* @param \App\Entity\Tam\Tam $tam
*
* @return FeedElement
*/
public function setTam(\App\Entity\Tam\Tam $tam)
{
$this->tam = $tam;
return $this;
}
/**
* Get tam
*
* @return \App\Entity\Tam\Tam
*/
public function getTam()
{
return $this->tam;
}
/**
* Set Advert
*
* @param \App\Entity\Feed\Advert $advert
*
* @return FeedElement
*/
public function setAdvert(\App\Entity\Feed\Advert $advert)
{
$this->advert = $advert;
return $this;
}
/**
* Get advert
*
* @return \App\Entity\Feed\Advert
*/
public function getAdvert()
{
return $this->advert;
}
/**
* Set transaction
*
* @param \App\Entity\Feed\FeedTransaction $transaction
*
* @return FeedElement
*/
public function setTransaction(\App\Entity\Feed\FeedTransaction $transaction)
{
$this->transaction = $transaction;
return $this;
}
/**
* Get transaction
*
* @return \App\Entity\Feed\FeedTransaction
*/
public function getTransaction()
{
return $this->transaction;
}
/**
* Set illustration
*
* @param string $illustration
*
* @return FeedElement
*/
public function setIllustration($illustration)
{
$this->illustration = $illustration;
return $this;
}
/**
* Get illustration
*
* @return string
*/
public function getIllustration()
{
return $this->illustration;
}
public function getLinkIllustration()
{
if($this->getIllustration()){
if(file_exists($this->getUploadIllustrationRootDir().$this->getIllustration()))
return $this->getIllustrationDir().$this->getIllustration();
else
return '/assets/images/common/feed_default.png';
}else{
return '/assets/images/common/feed_default.png';
}
}
public function getAbsolutePathIllustration()
{
if($this->getIllustration()){
if(file_exists($this->getUploadIllustrationRootDir().$this->getIllustration()))
return $this->getUploadIllustrationRootDir().$this->getIllustrationPath();
else
return null;
}else{
return null;
}
}
/**
* Sets avatarFile.
*
* @param UploadedFile $file
*/
public function setIllustrationFile(UploadedFile $file = null)
{
$this->illustrationFile = $file;
}
/**
* Get illustrationFile.
*
* @return UploadedFile
*/
public function getIllustrationFile()
{
return $this->illustrationFile;
}
/**
* Add message
*
* @param \App\Entity\Feed\FeedMessage $message
*
* @return FeedMessage
*/
public function addMessage(\App\Entity\Feed\FeedMessage $message)
{
if (!$this->messages->contains($message)) {
$this->messages[] = $message;
$message->setFeedElement($this);
}
return $this;
}
/**
* Remove message
*
* @param \App\Entity\Feed\FeedMessage $message
*/
public function removeMessage(\App\Entity\Feed\FeedMessage $message)
{
if ($this->messages->contains($message)) {
$this->messages->removeElement($message);
}
return $this;
}
/**
* Get messages
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getMessages()
{
return $this->messages;
}
/**
* Set distance
*
* @param float $distance
*
* @return User
*/
public function setDistance($distance)
{
$this->distance = str_replace(",", ".", $distance);
return $this;
}
/**
* Get distance
*
* @return float
*/
public function getDistance()
{
return $this->distance;
}
/**
* Get stringDistance
*
* @return string
*/
public function getStringDistance()
{
if($this->distance > 1000){
return sprintf("%0.2f km", $this->distance/1000);
}else{
return sprintf("%0.2f m", $this->distance);
}
}
/***********************
* Lifecycle Callbacks
***********************/
/**
*
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpdate()
{
$this->setDateUpdated(new \DateTime());
if($this->getLng() && $this->getLat())
$this->setCoordinates('SRID=4326;POINT(' . $this->getLng() . ' ' . $this->getLat() . ')');
}
/**
* Called before saving the entity
*
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->illustrationFile) {
// do whatever you want to generate a unique name
$filename = sha1(uniqid(mt_rand(), true));
$this->illustration = $filename.'.'.$this->illustrationFile->guessExtension();
$this->setDateUpdated(new \DateTime());
}
}
/**
* Called before entity removal
*
* @ORM\PreRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePathAvatar()) {
unlink($file);
}
}
/**
* Called after entity persistence
*
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
// The file property can be empty if the field is not required
if (null === $this->illustrationFile) {
return;
}
// Use the original file name here but you should
// sanitize it at least to avoid any security issues
// move takes the target directory and then the
// target filename to move to
$this->illustrationFile->move(
$this->getUploadIllustrationRootDir(),
$this->illustration
);
// Set the path property to the filename where you've saved the file
//$this->path = $this->file->getClientOriginalName();
// Clean up the file property as you won't need it anymore
$this->illustrationFile = null;
}
/***********************
* OTHER FUNCTION
***********************/
public function isTam(){
if($this->getFeedElementType()->getCodeTranslate() == "FEED_ELEMENT_TAM")
return true;
return false;
}
public function isAdvert(){
if($this->getFeedElementType()->getCodeTranslate() == "FEED_ELEMENT_ADVERT")
return true;
return false;
}
public function isAdvertOccasion(){
if($this->getFeedElementType()->getCodeTranslate() == "FEED_ELEMENT_ADVERT_OCCASION")
return true;
return false;
}
public function isTournament(){
if($this->getFeedElementType()->getCodeTranslate() == "FEED_ELEMENT_TOURNAMENT")
return true;
return false;
}
public function getCountryName(){
//return Intl::getRegionBundle()->getCountryName($this->getPlaceCountry());
if($this->getPlaceCountry())
return Countries::getName($this->getPlaceCountry());
return "";
}
public function getUploadIllustrationRootDir(){
$logoDir = __DIR__ . '/../../../public/assets/feed/'.$this->getUser()->getId().'/';
$fs = new Filesystem();
if ($fs->exists($logoDir) == false) {
$fs->mkdir($logoDir);
}
return $logoDir;
}
public function getIllustrationDir()
{
return '/assets/feed/'.$this->getUser()->getId().'/';
}
public function getPlaceCountryName(){
//return Intl::getRegionBundle()->getCountryName($this->getPlaceCountry());
if($this->getPlaceCountry())
return Countries::getName($this->getPlaceCountry());
return "";
}
public function updateFromJson($jsonObject){
if(isset($jsonObject->title)){
$this->setTitle($jsonObject->title);
}
if(isset($jsonObject->description)){
$this->setDescription($jsonObject->description);
}
if(isset($jsonObject->placeAddress)){
$this->setPlaceAddress($jsonObject->placeAddress);
}
if(isset($jsonObject->placeCity)){
$this->setPlaceCity($jsonObject->placeCity);
}
if(isset($jsonObject->placeCountry)){
$this->setPlaceCountry($jsonObject->placeCountry);
}
}
public function getArrayForJson($schemeAndBaseURL){
$array = array();
$array['id'] = $this->getId();
$array['user'] = $this->getUser()->getSimpleArrayForJson($schemeAndBaseURL);
$array['typeFeed'] = $this->getFeedElementType()->getArrayForJson();
$array['title'] = $this->getTitle();
$array['description'] = $this->getDescription() ? $this->getDescription() : "";
$array['placeName'] = $this->getPlaceName() ? $this->getDescription() : "";
$array['placeAddress'] = $this->getPlaceAddress() ? $this->getPlaceAddress() : "";
$array['placeZipcode'] = $this->getPlaceZipcode() ? $this->getPlaceZipcode() : "";
$array['placeCity'] = $this->getPlaceCity() ? $this->getPlaceCity() : "";
$array['placeCountry'] = $this->getPlaceCountry() ? $this->getPlaceCountry() : "";
$array['placeCountryName'] = $this->getPlaceCountryName() ? $this->getPlaceCountryName() : $this->getUser()->getCountryName();
$array['lat'] = $this->getLat();
$array['lng'] = $this->getLng();
$array['distance'] = $this->getDistance();
$array['distanceLabel'] = $this->getStringDistance();
if($this->getDateCreated()){
$array['dateCreated'] = $this->getDateCreated()->format("Y-m-d\TH:i:sP");
}
if($this->getIllustration()){
$array['illustrationURL'] = $schemeAndBaseURL.$this->getLinkIllustration();
}
if($this->getTam())
$array['tam'] = $this->getTam()->getArrayForJson($schemeAndBaseURL);
if($this->getAdvert())
$array['advert'] = $this->getAdvert()->getArrayForJson($schemeAndBaseURL);
if($this->getMessages()){
foreach($this->getMessages() as $message){
$array['messages'][] = $message->getArrayForJson($schemeAndBaseURL);
}
}
return $array;
}
}