<?php
namespace App\Entity;
use App\Traits\PictureTrait;
use App\Traits\SortTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\SoftDeletableInterface;
use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletableTrait;
use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
/**
* Category
*
* @ORM\Table(name="catalog_category")
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
* @ORM\HasLifecycleCallbacks()
* @ORM\AssociationOverrides({
* @ORM\AssociationOverride(name="pictures",
* joinTable=@ORM\JoinTable(
* name="catalog_category_pictures"
* ),
* joinColumns=@ORM\JoinColumn(
* name="category_id", referencedColumnName="id"
* )
* )
* })
*/
class Category implements TimestampableInterface, SoftDeletableInterface, TranslatableInterface
{
use TimestampableTrait,
TranslatableTrait,
SoftDeletableTrait,
SortTrait,
PictureTrait;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="Product", mappedBy="categories")
* @ORM\OrderBy({"sort" = "ASC"})
**/
private $products;
/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent", cascade={"remove", "persist"})
*/
private $childrens;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="childrens")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
private $parent;
/**
*
* @ORM\Column(name="deletedAt", type="datetime", nullable=true)
*/
protected $deletedAt;
/**
* @ORM\OneToMany(targetEntity=Activite::class, mappedBy="category")
*/
private $activites;
/**
* @ORM\OneToMany(targetEntity=CategoryParagraphe::class, mappedBy="category", cascade={"remove", "persist"})
*/
private $categoryParagraphes;
public function __toString()
{
$result = $this->translate()->getTitle();
if ($this->parent != null) {
$result = $this->parent . " / " . $result;
}
return $result;
}
public function __construct()
{
$this->products = new ArrayCollection();
$this->childrens = new ArrayCollection();
$this->activites = new ArrayCollection();
$this->categoryParagraphes = new ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Add Product
*
* @param Product $product
* @return Category
*/
public function addProduct(Product $product)
{
$this->products[] = $product;
return $this;
}
/**
* Remove Product
*
* @param Product $product
* @return Category
*/
public function removeProduct(Product $product)
{
$this->products->removeElement($product);
return $this;
}
/**
* Get Products
*
* @return ArrayCollection
*/
public function getProducts()
{
return $this->products;
}
/**
* Add Children
*
* @param Category $children
* @return Category
*/
public function addChildren(Category $children)
{
$this->childrens[] = $children;
return $this;
}
/**
* Remove Children
*
* @param Category $children
* @return Category
*/
public function removeChildren(Category $children)
{
$this->childrens->removeElement($children);
return $this;
}
/**
* Get Childrens
*
* @return ArrayCollection
*
*/
public function getChildrens($showDeleted = true, $type = "all")
{
$criteria = Criteria::create();
if ($showDeleted !== true) {
$criteria->where(Criteria::expr()->isNull('deletedAt'));
}
$results = $this->childrens->matching($criteria);
$final = [];
if ($type === "onlyWithChildrens") {
foreach ($results as $result) {
if (count($result->getChildrens()) > 0) {
$final[] = $result;
}
}
} elseif ($type === "onlyWithoutChildrens") {
foreach ($results as $result) {
if (count($result->getChildrens()) == 0) {
$final[] = $result;
}
}
} else {
$final = $results;
}
return $final;
}
public function getAllChildrens($array = [])
{
if (!in_array($this, $array)) {
$array[] = $this;
}
foreach ($this->childrens as $children) {
if ($children->isDeleted() == false) {
$array = array_merge($array, $children->getAllChildrens($array));
}
}
return array_unique($array);
}
/**
* @return Category
*/
public function getParent()
{
return $this->parent;
}
/**
* @param Category $parent
*/
public function setParent($parent)
{
$this->parent = $parent;
}
/**
* @return CategoryTranslation
*/
public function createTrans($locale = 'fr', $title, $shortContent = '', $content = '')
{
$trans = new CategoryTranslation();
$trans->setLocale($locale);
$trans->setTitle($title);
$trans->setContent($content);
$trans->setShortContent($shortContent);
$this->addTranslation($trans);
return $trans;
}
/**
* @ORM\PreRemove()
*/
public function preRemove()
{
foreach ($this->products as $product) {
$product->removeCategory($this);
}
}
public function getLevel()
{
if ($this->parent === null) {
return 1;
} else {
return $this->parent->getLevel() + 1;
}
}
/**
* @return Collection<int, Activite>
*/
public function getActivites(): Collection
{
return $this->activites;
}
public function addActivite(Activite $activite): self
{
if (!$this->activites->contains($activite)) {
$this->activites[] = $activite;
$activite->setCategory($this);
}
return $this;
}
public function removeActivite(Activite $activite): self
{
if ($this->activites->removeElement($activite)) {
// set the owning side to null (unless already changed)
if ($activite->getCategory() === $this) {
$activite->setCategory(null);
}
}
return $this;
}
/**
* @return Collection<int, CategoryParagraphe>
*/
public function getCategoryParagraphes(): Collection
{
return $this->categoryParagraphes;
}
public function addCategoryParagraphe(CategoryParagraphe $categoryParagraphe): self
{
if (!$this->categoryParagraphes->contains($categoryParagraphe)) {
$this->categoryParagraphes[] = $categoryParagraphe;
$categoryParagraphe->setCategory($this);
}
return $this;
}
public function removeCategoryParagraphe(CategoryParagraphe $categoryParagraphe): self
{
if ($this->categoryParagraphes->removeElement($categoryParagraphe)) {
// set the owning side to null (unless already changed)
if ($categoryParagraphe->getCategory() === $this) {
$categoryParagraphe->setCategory(null);
}
}
return $this;
}
}