<?php
namespace Aqarmap\Bundle\ListingBundle\Entity;
use Aqarmap\Bundle\ListingBundle\Constant\CompoundLocationStatus;
use Aqarmap\Bundle\ListingBundle\Constant\CompoundType;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Translatable\Translatable;
use JMS\Serializer\Annotation as Serializer;
/**
* Compound Location.
*
* @ORM\Entity(repositoryClass="Aqarmap\Bundle\ListingBundle\Repository\CompoundLocationRepository")
*
* @ORM\Table(name="compound_locations")
*
* @Gedmo\TranslationEntity(class="Aqarmap\Bundle\ListingBundle\Entity\CompoundLocationTranslation")
*
* @Gedmo\Tree(type="nested")
*
* @Serializer\ExclusionPolicy("all")
* Class CompoundLocation
*/
class CompoundLocation implements Translatable
{
public function __construct()
{
$this->children = new ArrayCollection();
$this->translations = new ArrayCollection();
$this->listings = new ArrayCollection();
$this->type = CompoundType::COMPOUND;
$this->location = null;
}
/**
* @ORM\Column(name="id", type="integer")
*
* @ORM\Id
*
* @ORM\GeneratedValue(strategy="AUTO")
*
* @Serializer\Groups({"Default", "ProjectLocation", "ProjectSearchV4"})
*
* @Serializer\Expose
*/
private $id;
/**
* @ORM\Column(name="title", type="string", length=50)
*
* @Gedmo\Translatable
*
* @Serializer\Expose
*/
private $title;
/**
* @Serializer\Expose
*
* @ORM\Column(name="coordinates", type="string", length=50, nullable=true)
*/
private $coordinates;
/**
* @Gedmo\Slug(handlers={
*
* @Gedmo\SlugHandler(class="Gedmo\Sluggable\Handler\TreeSlugHandler", options={
*
* @Gedmo\SlugHandlerOption(name="parentRelationField", value="parent"),
* @Gedmo\SlugHandlerOption(name="separator", value="/")
* })
* }, fields={"title"})
*
* @ORM\Column(length=128, unique=true)
*
* @Serializer\Expose
*/
private $slug;
/**
* @Gedmo\TreeLeft
*
* @ORM\Column(name="_left", type="integer")
*/
private $left;
/**
* @Gedmo\TreeLevel
*
* @ORM\Column(type="integer")
*
* @Serializer\Expose
*/
private $level;
/**
* @Gedmo\TreeRight
*
* @ORM\Column(name="_right",type="integer")
*/
private $right;
/**
* @Gedmo\TreeRoot
*
* @ORM\Column(name="root", type="integer", nullable=true)
*/
private $root;
/**
* @Gedmo\TreeParent
*
* @ORM\ManyToOne(targetEntity="CompoundLocation", inversedBy="children")
*
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*
* @Serializer\Expose
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="CompoundLocation", mappedBy="parent")
*
* @ORM\OrderBy({"left" = "ASC"})
*
* @Serializer\MaxDepth(2)
*
* @Serializer\Expose
*/
private $children;
/**
* @ORM\OneToMany(
* targetEntity="CompoundLocationTranslation",
* mappedBy="object",
* cascade={"persist", "remove"}
* )
*/
private $translations;
/**
* @Gedmo\Locale
*/
private $locale;
/**
* @ORM\Column(name="activity", type="smallint")
*
* @Serializer\Expose
*/
private $activity = false;
/**
* @ORM\Column(name="is_compound_location", type="boolean")
*
* @Serializer\Expose
*/
private $isCompoundLocation = false;
/**
* @ORM\ManyToOne(targetEntity="Listing", cascade={"persist", "remove"})
*
* @ORM\JoinColumn(name="listing_id", referencedColumnName="id", nullable=true)
*/
protected $listing;
/**
* @ORM\OneToMany(targetEntity="Listing", mappedBy="compoundLocation")
*/
protected $listings;
/**
* @ORM\Column(name="type", type="integer", options={"default" : 1})
*
* @Serializer\Expose
*/
protected $type;
/**
* @ORM\ManyToOne(targetEntity="Location", cascade={"persist", "remove"})
*
* @ORM\JoinColumn(name="location_id", referencedColumnName="id", nullable=true)
*/
protected $location;
/**
* @var array
*
* @Serializer\Groups({"ProjectLocation", "ProjectSearchV4"})
*
* @Serializer\SerializedName("title")
*
* @Serializer\Expose
*/
protected $v4Translations = [];
/**
* @return int
*/
public function getType()
{
return $this->type;
}
/**
* @param int $type
*/
public function setType($type): void
{
$this->type = $type;
}
public function getId()
{
return $this->id;
}
/**
* @return $this
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function getTitle()
{
return $this->title;
}
/**
* @return $this
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
public function getSlug()
{
return $this->slug;
}
public function getLastSlug()
{
$segments = explode('/', $this->slug);
return end($segments);
}
/**
* @return $this
*/
public function setLevel($level)
{
$this->level = $level;
return $this;
}
public function getLevel()
{
return $this->level;
}
/**
* @return $this
*/
public function setLeft($left)
{
$this->left = $left;
return $this;
}
public function getLeft()
{
return $this->left;
}
/**
* @return $this
*/
public function setRight($right)
{
$this->right = $right;
return $this;
}
public function getRight()
{
return $this->right;
}
/**
* @return $this
*/
public function setRoot($root)
{
$this->root = $root;
return $this;
}
public function getRoot()
{
return $this->root;
}
/**
* @return $this
*/
public function setParent(?self $parent = null)
{
$this->parent = $parent;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
/**
* Get Nearest Location Custom Level .
*
* @return CompoundLocation
*/
public function getFirstParent()
{
$location = $this;
/** @var CompoundLocation $location */
while ($location = $location->getParent()) {
if (!$location->getParent()) {
return $location;
}
}
return $location;
}
public function getEnabledParents(): array
{
$location = $this->getParent();
$parents = [];
/** @var CompoundLocation $location */
while (
$location instanceof self
) {
if (CompoundLocationStatus::ENABLED == $location->getActivity()) {
$parents[] = $location;
}
if (!$location->getParent()) {
break;
}
$location = $location->getParent();
}
return $parents;
}
/**
* @return $this
*/
public function setChildren(self $child)
{
$this->children[] = $child;
return $this;
}
public function setTranslatableLocale($locale): void
{
$this->locale = $locale;
}
/**
* Check if location has children.
*
* @Serializer\VirtualProperty
*
* @Serializer\Groups({"ProjectLocation"})
*
* @Serializer\SerializedName("hasChildren")
*
* @Serializer\Expose
*
* @return bool
*/
public function hasChildren()
{
return !$this->children->isEmpty();
}
/**
* @return $this
*/
public function addTranslation(CompoundLocationTranslation $translation)
{
if (!$this->translations->contains($translation)) {
$this->translations[] = $translation;
$translation->setObject($this);
}
return $this;
}
public function getTranslations()
{
return $this->translations;
}
/**
* @return $this
*/
public function setActivity($activity)
{
$this->activity = $activity;
return $this;
}
/**
* @return bool
*/
public function getActivity()
{
return $this->activity;
}
/**
* @return $this
*/
public function setListing(?Listing $listing = null)
{
$this->listing = $listing;
return $this;
}
/**
* @return Listing
*/
public function getListing()
{
return $this->listing;
}
/**
* @return string|null
*/
public function getCompoundLocationParentLabel()
{
$parent = $this->getParent() ? $this->getParent()->getTitle().' / ' : null;
return $parent.$this->getTitle();
}
public function getLocation(): ?Location
{
return $this->location;
}
/**
* @return CompoundLocation
*/
public function setLocation(?Location $location)
{
$this->location = $location;
return $this;
}
public function __toString()
{
return $this->title;
}
public function getCoordinates()
{
return $this->coordinates;
}
public function setCoordinates($coordinates): void
{
$this->coordinates = $coordinates;
}
/**
* @param array $translations
*
* @return self
*/
public function setV4Translations($translations = [])
{
$this->v4Translations = $translations;
return $this;
}
/**
* Get translations.
*
* @return array
*/
public function getV4Translations()
{
return $this->v4Translations;
}
/**
* Set isCompoundLocation.
*
* @param bool $isCompoundLocation
*
* @return CompoundLocation
*/
public function setIsCompoundLocation($isCompoundLocation)
{
$this->isCompoundLocation = $isCompoundLocation;
return $this;
}
/**
* Get isCompoundLocation.
*
* @return bool
*/
public function getIsCompoundLocation()
{
return $this->isCompoundLocation;
}
}