<?php
namespace Roothirsch\PimBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Roothirsch\CoreBundle\Entity\Traits\TimetrackedTrait;
use Roothirsch\PimBundle\API\UpdateSorting;
use Roothirsch\PimBundle\Repository\CategoryRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use \Roothirsch\PimBundle\Entity\Article;
use Gedmo\Mapping\Annotation as Gedmo;
use \Roothirsch\PimBundle\Entity\AttributeGroup;
use \Roothirsch\PimBundle\Entity\DataSource;
/**
* @Gedmo\Tree(type="nested")
* @ApiResource(
* shortName="Pim/Category",
* itemOperations={
* "get",
* "put",
* "delete",
* "update_sorting"={
* "method"="PUT",
* "path"="/pim/categories/{id}/update-sorting",
* "controller"=UpdateSorting::class
* },
* }
* )
* @ORM\Entity(repositoryClass=CategoryRepository::class)
* @ORM\Table(name="pim_category")
*/
class Category
{
use TimetrackedTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @Gedmo\TreeLeft
* @ORM\Column(name="lft", type="integer")
*/
private $lft;
/**
* @Gedmo\TreeLevel
* @ORM\Column(name="lvl", type="integer")
*/
private $lvl;
/**
* @Gedmo\TreeRight
* @ORM\Column(name="rgt", type="integer")
*/
private $rgt;
/**
* @Gedmo\TreeRoot
* @ORM\ManyToOne(targetEntity="Category")
* @ORM\JoinColumn(name="tree_root", referencedColumnName="id", onDelete="CASCADE")
*/
private $root;
/**
* @Gedmo\TreeParent
* @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent_id")
* @ORM\OrderBy({"lft" = "ASC"})
*/
private $children;
/**
* @ORM\ManyToMany(targetEntity=Product::class, mappedBy="categories")
* @ORM\JoinTable(name="pim_category_products")
*/
private $products;
/**
* @ORM\ManyToMany(targetEntity=AttributeGroup::class, inversedBy="categories")
* @ORM\JoinTable(name="pim_category_product_attribute_groups")
*/
private $productAttributeGroups;
/**
* @ORM\ManyToMany(targetEntity=AttributeGroup::class, inversedBy="categories")
* @ORM\JoinTable(name="pim_category_article_attribute_groups")
*/
private $articleAttributeGroups;
/**
* @ORM\OneToMany(targetEntity=DataSource::class, mappedBy="category", orphanRemoval=true, cascade={"persist"})
*/
private $dataSources;
public function __construct()
{
$this->articles = new ArrayCollection();
$this->children = new ArrayCollection();
$this->products = new ArrayCollection();
$this->productAttributeGroups = new ArrayCollection();
$this->articleAttributeGroups = new ArrayCollection();
$this->dataSources = new ArrayCollection();
}
public function __toString()
{
return $this->title;
}
public function getId(): ?int
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id): void
{
$this->id = $id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getRoot()
{
return $this->root;
}
public function setParent($parent = null)
{
$this->parent = $parent;
}
public function getParent()
{
return $this->parent;
}
/**
* @return Collection|Product[]
*/
public function getProducts(): Collection
{
return $this->products;
}
public function addProduct(Product $product): self
{
if (!$this->products->contains($product)) {
$this->products[] = $product;
$product->addCategory($this);
}
return $this;
}
public function removeProduct(Product $product): self
{
if ($this->products->removeElement($product)) {
$product->removeCategory($this);
}
return $this;
}
/**
* @return Collection|AttributeGroup[]
*/
public function getProductAttributeGroups(): Collection
{
return $this->productAttributeGroups;
}
public function addProductAttributeGroup(AttributeGroup $productAttributeGroup): self
{
if (!$this->productAttributeGroups->contains($productAttributeGroup)) {
$this->productAttributeGroups[] = $productAttributeGroup;
}
return $this;
}
public function removeProductAttributeGroup(AttributeGroup $productAttributeGroup): self
{
$this->productAttributeGroups->removeElement($productAttributeGroup);
return $this;
}
/**
* @return Collection|AttributeGroup[]
*/
public function getArticleAttributeGroups(): Collection
{
return $this->articleAttributeGroups;
}
public function addArticleAttributeGroup(AttributeGroup $articleAttributeGroup): self
{
if (!$this->articleAttributeGroups->contains($articleAttributeGroup)) {
$this->articleAttributeGroups[] = $articleAttributeGroup;
}
return $this;
}
public function removeArticleAttributeGroup(AttributeGroup $articleAttributeGroup): self
{
$this->articleAttributeGroups->removeElement($articleAttributeGroup);
return $this;
}
/**
* @return Collection|DataSource[]
*/
public function getDataSources(): Collection
{
return $this->dataSources;
}
public function addDataSource(DataSource $dataSource): self
{
if (!$this->dataSources->contains($dataSource)) {
$this->dataSources[] = $dataSource;
$dataSource->setCategory($this);
}
return $this;
}
public function removeDataSource(DataSource $dataSource): self
{
if ($this->dataSources->removeElement($dataSource)) {
// set the owning side to null (unless already changed)
if ($dataSource->getCategory() === $this) {
$dataSource->setCategory(null);
}
}
return $this;
}
public function getProduct($name) {
foreach($this->products as $product) {
if (strtolower($product->getName()) == strtolower($name)) {
return $product;
}
}
return null;
}
public function getArticle($name) {
foreach($this->products as $product) {
foreach($product->getArticles() as $article) {
if (strtolower($article->getName()) == strtolower($name)) {
return $article;
}
}
}
return null;
}
}