src/Aqarmap/Bundle/ListingBundle/Entity/CompoundLocation.php line 27

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\Entity;
  3. use Aqarmap\Bundle\ListingBundle\Constant\CompoundLocationStatus;
  4. use Aqarmap\Bundle\ListingBundle\Constant\CompoundType;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Gedmo\Mapping\Annotation as Gedmo;
  8. use Gedmo\Translatable\Translatable;
  9. use JMS\Serializer\Annotation as Serializer;
  10. /**
  11.  * Compound Location.
  12.  *
  13.  * @ORM\Entity(repositoryClass="Aqarmap\Bundle\ListingBundle\Repository\CompoundLocationRepository")
  14.  *
  15.  * @ORM\Table(name="compound_locations")
  16.  *
  17.  * @Gedmo\TranslationEntity(class="Aqarmap\Bundle\ListingBundle\Entity\CompoundLocationTranslation")
  18.  *
  19.  * @Gedmo\Tree(type="nested")
  20.  *
  21.  * @Serializer\ExclusionPolicy("all")
  22.  * Class CompoundLocation
  23.  */
  24. class CompoundLocation implements Translatable
  25. {
  26.     public function __construct()
  27.     {
  28.         $this->children = new ArrayCollection();
  29.         $this->translations = new ArrayCollection();
  30.         $this->listings = new ArrayCollection();
  31.         $this->type CompoundType::COMPOUND;
  32.         $this->location null;
  33.     }
  34.     /**
  35.      * @ORM\Column(name="id", type="integer")
  36.      *
  37.      * @ORM\Id
  38.      *
  39.      * @ORM\GeneratedValue(strategy="AUTO")
  40.      *
  41.      * @Serializer\Groups({"Default", "ProjectLocation", "ProjectSearchV4"})
  42.      *
  43.      * @Serializer\Expose
  44.      */
  45.     private $id;
  46.     /**
  47.      * @ORM\Column(name="title", type="string", length=50)
  48.      *
  49.      * @Gedmo\Translatable
  50.      *
  51.      * @Serializer\Expose
  52.      */
  53.     private $title;
  54.     /**
  55.      * @Serializer\Expose
  56.      *
  57.      * @ORM\Column(name="coordinates", type="string", length=50, nullable=true)
  58.      */
  59.     private $coordinates;
  60.     /**
  61.      * @Gedmo\Slug(handlers={
  62.      *
  63.      *      @Gedmo\SlugHandler(class="Gedmo\Sluggable\Handler\TreeSlugHandler", options={
  64.      *
  65.      *          @Gedmo\SlugHandlerOption(name="parentRelationField", value="parent"),
  66.      *          @Gedmo\SlugHandlerOption(name="separator", value="/")
  67.      *      })
  68.      * }, fields={"title"})
  69.      *
  70.      * @ORM\Column(length=128, unique=true)
  71.      *
  72.      * @Serializer\Expose
  73.      */
  74.     private $slug;
  75.     /**
  76.      * @Gedmo\TreeLeft
  77.      *
  78.      * @ORM\Column(name="_left", type="integer")
  79.      */
  80.     private $left;
  81.     /**
  82.      * @Gedmo\TreeLevel
  83.      *
  84.      * @ORM\Column(type="integer")
  85.      *
  86.      * @Serializer\Expose
  87.      */
  88.     private $level;
  89.     /**
  90.      * @Gedmo\TreeRight
  91.      *
  92.      * @ORM\Column(name="_right",type="integer")
  93.      */
  94.     private $right;
  95.     /**
  96.      * @Gedmo\TreeRoot
  97.      *
  98.      * @ORM\Column(name="root", type="integer", nullable=true)
  99.      */
  100.     private $root;
  101.     /**
  102.      * @Gedmo\TreeParent
  103.      *
  104.      * @ORM\ManyToOne(targetEntity="CompoundLocation", inversedBy="children")
  105.      *
  106.      * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
  107.      *
  108.      * @Serializer\Expose
  109.      */
  110.     private $parent;
  111.     /**
  112.      * @ORM\OneToMany(targetEntity="CompoundLocation", mappedBy="parent")
  113.      *
  114.      * @ORM\OrderBy({"left" = "ASC"})
  115.      *
  116.      * @Serializer\MaxDepth(2)
  117.      *
  118.      * @Serializer\Expose
  119.      */
  120.     private $children;
  121.     /**
  122.      * @ORM\OneToMany(
  123.      *   targetEntity="CompoundLocationTranslation",
  124.      *   mappedBy="object",
  125.      *   cascade={"persist", "remove"}
  126.      * )
  127.      */
  128.     private $translations;
  129.     /**
  130.      * @Gedmo\Locale
  131.      */
  132.     private $locale;
  133.     /**
  134.      * @ORM\Column(name="activity", type="smallint")
  135.      *
  136.      * @Serializer\Expose
  137.      */
  138.     private $activity false;
  139.     /**
  140.      * @ORM\Column(name="is_compound_location", type="boolean")
  141.      *
  142.      * @Serializer\Expose
  143.      */
  144.     private $isCompoundLocation false;
  145.     /**
  146.      * @ORM\ManyToOne(targetEntity="Listing", cascade={"persist", "remove"})
  147.      *
  148.      * @ORM\JoinColumn(name="listing_id", referencedColumnName="id", nullable=true)
  149.      */
  150.     protected $listing;
  151.     /**
  152.      * @ORM\OneToMany(targetEntity="Listing", mappedBy="compoundLocation")
  153.      */
  154.     protected $listings;
  155.     /**
  156.      * @ORM\Column(name="type", type="integer", options={"default" : 1})
  157.      *
  158.      * @Serializer\Expose
  159.      */
  160.     protected $type;
  161.     /**
  162.      * @ORM\ManyToOne(targetEntity="Location", cascade={"persist", "remove"})
  163.      *
  164.      * @ORM\JoinColumn(name="location_id", referencedColumnName="id", nullable=true)
  165.      */
  166.     protected $location;
  167.     /**
  168.      * @var array
  169.      *
  170.      * @Serializer\Groups({"ProjectLocation", "ProjectSearchV4"})
  171.      *
  172.      * @Serializer\SerializedName("title")
  173.      *
  174.      * @Serializer\Expose
  175.      */
  176.     protected $v4Translations = [];
  177.     /**
  178.      * @return int
  179.      */
  180.     public function getType()
  181.     {
  182.         return $this->type;
  183.     }
  184.     /**
  185.      * @param int $type
  186.      */
  187.     public function setType($type): void
  188.     {
  189.         $this->type $type;
  190.     }
  191.     public function getId()
  192.     {
  193.         return $this->id;
  194.     }
  195.     /**
  196.      * @return $this
  197.      */
  198.     public function setTitle($title)
  199.     {
  200.         $this->title $title;
  201.         return $this;
  202.     }
  203.     public function getTitle()
  204.     {
  205.         return $this->title;
  206.     }
  207.     /**
  208.      * @return $this
  209.      */
  210.     public function setSlug($slug)
  211.     {
  212.         $this->slug $slug;
  213.         return $this;
  214.     }
  215.     public function getSlug()
  216.     {
  217.         return $this->slug;
  218.     }
  219.     public function getLastSlug()
  220.     {
  221.         $segments explode('/'$this->slug);
  222.         return end($segments);
  223.     }
  224.     /**
  225.      * @return $this
  226.      */
  227.     public function setLevel($level)
  228.     {
  229.         $this->level $level;
  230.         return $this;
  231.     }
  232.     public function getLevel()
  233.     {
  234.         return $this->level;
  235.     }
  236.     /**
  237.      * @return $this
  238.      */
  239.     public function setLeft($left)
  240.     {
  241.         $this->left $left;
  242.         return $this;
  243.     }
  244.     public function getLeft()
  245.     {
  246.         return $this->left;
  247.     }
  248.     /**
  249.      * @return $this
  250.      */
  251.     public function setRight($right)
  252.     {
  253.         $this->right $right;
  254.         return $this;
  255.     }
  256.     public function getRight()
  257.     {
  258.         return $this->right;
  259.     }
  260.     /**
  261.      * @return $this
  262.      */
  263.     public function setRoot($root)
  264.     {
  265.         $this->root $root;
  266.         return $this;
  267.     }
  268.     public function getRoot()
  269.     {
  270.         return $this->root;
  271.     }
  272.     /**
  273.      * @return $this
  274.      */
  275.     public function setParent(?self $parent null)
  276.     {
  277.         $this->parent $parent;
  278.         return $this;
  279.     }
  280.     public function getParent(): ?self
  281.     {
  282.         return $this->parent;
  283.     }
  284.     /**
  285.      * Get Nearest Location Custom Level .
  286.      *
  287.      * @return CompoundLocation
  288.      */
  289.     public function getFirstParent()
  290.     {
  291.         $location $this;
  292.         /** @var CompoundLocation $location */
  293.         while ($location $location->getParent()) {
  294.             if (!$location->getParent()) {
  295.                 return $location;
  296.             }
  297.         }
  298.         return $location;
  299.     }
  300.     public function getEnabledParents(): array
  301.     {
  302.         $location $this->getParent();
  303.         $parents = [];
  304.         /** @var CompoundLocation $location */
  305.         while (
  306.             $location instanceof self
  307.         ) {
  308.             if (CompoundLocationStatus::ENABLED == $location->getActivity()) {
  309.                 $parents[] = $location;
  310.             }
  311.             if (!$location->getParent()) {
  312.                 break;
  313.             }
  314.             $location $location->getParent();
  315.         }
  316.         return $parents;
  317.     }
  318.     /**
  319.      * @return $this
  320.      */
  321.     public function setChildren(self $child)
  322.     {
  323.         $this->children[] = $child;
  324.         return $this;
  325.     }
  326.     public function setTranslatableLocale($locale): void
  327.     {
  328.         $this->locale $locale;
  329.     }
  330.     /**
  331.      * Check if location has children.
  332.      *
  333.      * @Serializer\VirtualProperty
  334.      *
  335.      * @Serializer\Groups({"ProjectLocation"})
  336.      *
  337.      * @Serializer\SerializedName("hasChildren")
  338.      *
  339.      * @Serializer\Expose
  340.      *
  341.      * @return bool
  342.      */
  343.     public function hasChildren()
  344.     {
  345.         return !$this->children->isEmpty();
  346.     }
  347.     /**
  348.      * @return $this
  349.      */
  350.     public function addTranslation(CompoundLocationTranslation $translation)
  351.     {
  352.         if (!$this->translations->contains($translation)) {
  353.             $this->translations[] = $translation;
  354.             $translation->setObject($this);
  355.         }
  356.         return $this;
  357.     }
  358.     public function getTranslations()
  359.     {
  360.         return $this->translations;
  361.     }
  362.     /**
  363.      * @return $this
  364.      */
  365.     public function setActivity($activity)
  366.     {
  367.         $this->activity $activity;
  368.         return $this;
  369.     }
  370.     /**
  371.      * @return bool
  372.      */
  373.     public function getActivity()
  374.     {
  375.         return $this->activity;
  376.     }
  377.     /**
  378.      * @return $this
  379.      */
  380.     public function setListing(?Listing $listing null)
  381.     {
  382.         $this->listing $listing;
  383.         return $this;
  384.     }
  385.     /**
  386.      * @return Listing
  387.      */
  388.     public function getListing()
  389.     {
  390.         return $this->listing;
  391.     }
  392.     /**
  393.      * @return string|null
  394.      */
  395.     public function getCompoundLocationParentLabel()
  396.     {
  397.         $parent $this->getParent() ? $this->getParent()->getTitle().' / ' null;
  398.         return $parent.$this->getTitle();
  399.     }
  400.     public function getLocation(): ?Location
  401.     {
  402.         return $this->location;
  403.     }
  404.     /**
  405.      * @return CompoundLocation
  406.      */
  407.     public function setLocation(?Location $location)
  408.     {
  409.         $this->location $location;
  410.         return $this;
  411.     }
  412.     public function __toString()
  413.     {
  414.         return $this->title;
  415.     }
  416.     public function getCoordinates()
  417.     {
  418.         return $this->coordinates;
  419.     }
  420.     public function setCoordinates($coordinates): void
  421.     {
  422.         $this->coordinates $coordinates;
  423.     }
  424.     /**
  425.      * @param array $translations
  426.      *
  427.      * @return self
  428.      */
  429.     public function setV4Translations($translations = [])
  430.     {
  431.         $this->v4Translations $translations;
  432.         return $this;
  433.     }
  434.     /**
  435.      * Get translations.
  436.      *
  437.      * @return array
  438.      */
  439.     public function getV4Translations()
  440.     {
  441.         return $this->v4Translations;
  442.     }
  443.     /**
  444.      * Set isCompoundLocation.
  445.      *
  446.      * @param bool $isCompoundLocation
  447.      *
  448.      * @return CompoundLocation
  449.      */
  450.     public function setIsCompoundLocation($isCompoundLocation)
  451.     {
  452.         $this->isCompoundLocation $isCompoundLocation;
  453.         return $this;
  454.     }
  455.     /**
  456.      * Get isCompoundLocation.
  457.      *
  458.      * @return bool
  459.      */
  460.     public function getIsCompoundLocation()
  461.     {
  462.         return $this->isCompoundLocation;
  463.     }
  464. }