src/Entity/Category.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Traits\PictureTrait;
  4. use App\Traits\SortTrait;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\Common\Collections\Criteria;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Knp\DoctrineBehaviors\Contract\Entity\SoftDeletableInterface;
  10. use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
  11. use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
  12. use Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletableTrait;
  13. use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
  14. use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
  15. /**
  16.  * Category
  17.  *
  18.  * @ORM\Table(name="catalog_category")
  19.  * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
  20.  * @ORM\HasLifecycleCallbacks()
  21.  * @ORM\AssociationOverrides({
  22.  *      @ORM\AssociationOverride(name="pictures",
  23.  *          joinTable=@ORM\JoinTable(
  24.  *              name="catalog_category_pictures"
  25.  *          ),
  26.  *          joinColumns=@ORM\JoinColumn(
  27.  *              name="category_id", referencedColumnName="id"
  28.  *          )
  29.  *      )
  30.  * })
  31.  */
  32. class Category implements TimestampableInterfaceSoftDeletableInterfaceTranslatableInterface
  33. {
  34.     use TimestampableTrait,
  35.         TranslatableTrait,
  36.         SoftDeletableTrait,
  37.         SortTrait,
  38.         PictureTrait;
  39.     /**
  40.      * @var int
  41.      *
  42.      * @ORM\Column(name="id", type="integer")
  43.      * @ORM\Id
  44.      * @ORM\GeneratedValue(strategy="AUTO")
  45.      */
  46.     private $id;
  47.     /**
  48.      * @ORM\ManyToMany(targetEntity="Product", mappedBy="categories")
  49.      * @ORM\OrderBy({"sort" = "ASC"})
  50.      **/
  51.     private $products;
  52.     /**
  53.      * @ORM\OneToMany(targetEntity="Category", mappedBy="parent", cascade={"remove", "persist"})
  54.      */
  55.     private $childrens;
  56.     /**
  57.      * @ORM\ManyToOne(targetEntity="Category", inversedBy="childrens")
  58.      * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
  59.      */
  60.     private $parent;
  61.     
  62.     /**
  63.     *
  64.     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
  65.     */
  66.     protected $deletedAt;
  67.     /**
  68.      * @ORM\OneToMany(targetEntity=Activite::class, mappedBy="category")
  69.      */
  70.     private $activites;
  71.     /**
  72.      * @ORM\OneToMany(targetEntity=CategoryParagraphe::class, mappedBy="category", cascade={"remove", "persist"})
  73.      */
  74.     private $categoryParagraphes;
  75.     
  76.     
  77.     public function __toString()
  78.     {
  79.         $result $this->translate()->getTitle();
  80.         if ($this->parent != null) {
  81.             $result $this->parent " / " $result;
  82.         }
  83.         return $result;
  84.     }
  85.     public function __construct()
  86.     {
  87.         $this->products = new ArrayCollection();
  88.         $this->childrens = new ArrayCollection();
  89.         $this->activites = new ArrayCollection();
  90.         $this->categoryParagraphes = new ArrayCollection();
  91.     }
  92.     /**
  93.      * Get id
  94.      *
  95.      * @return int
  96.      */
  97.     public function getId()
  98.     {
  99.         return $this->id;
  100.     }
  101.     /**
  102.      * Add Product
  103.      *
  104.      * @param Product $product
  105.      * @return Category
  106.      */
  107.     public function addProduct(Product $product)
  108.     {
  109.         $this->products[] = $product;
  110.         return $this;
  111.     }
  112.     /**
  113.      * Remove Product
  114.      *
  115.      * @param Product $product
  116.      * @return Category
  117.      */
  118.     public function removeProduct(Product $product)
  119.     {
  120.         $this->products->removeElement($product);
  121.         return $this;
  122.     }
  123.     /**
  124.      * Get Products
  125.      *
  126.      * @return ArrayCollection
  127.      */
  128.     public function getProducts()
  129.     {
  130.         return $this->products;
  131.     }
  132.     /**
  133.      * Add Children
  134.      *
  135.      * @param Category $children
  136.      * @return Category
  137.      */
  138.     public function addChildren(Category $children)
  139.     {
  140.         $this->childrens[] = $children;
  141.         return $this;
  142.     }
  143.     /**
  144.      * Remove Children
  145.      *
  146.      * @param Category $children
  147.      * @return Category
  148.      */
  149.     public function removeChildren(Category $children)
  150.     {
  151.         $this->childrens->removeElement($children);
  152.         return $this;
  153.     }
  154.     /**
  155.      * Get Childrens
  156.      *
  157.      * @return ArrayCollection
  158.      *
  159.      */
  160.     public function getChildrens($showDeleted true$type "all")
  161.     {
  162.         $criteria Criteria::create();
  163.         if ($showDeleted !== true) {
  164.             $criteria->where(Criteria::expr()->isNull('deletedAt'));
  165.         }
  166.         $results $this->childrens->matching($criteria);
  167.         $final = [];
  168.         if ($type === "onlyWithChildrens") {
  169.             foreach ($results as $result) {
  170.                 if (count($result->getChildrens()) > 0) {
  171.                     $final[] = $result;
  172.                 }
  173.             }
  174.         } elseif ($type === "onlyWithoutChildrens") {
  175.             foreach ($results as $result) {
  176.                 if (count($result->getChildrens()) == 0) {
  177.                     $final[] = $result;
  178.                 }
  179.             }
  180.         } else {
  181.             $final $results;
  182.         }
  183.         return $final;
  184.     }
  185.     public function getAllChildrens($array = [])
  186.     {
  187.         if (!in_array($this$array)) {
  188.             $array[] = $this;
  189.         }
  190.         foreach ($this->childrens as $children) {
  191.             if ($children->isDeleted() == false) {
  192.                 $array array_merge($array$children->getAllChildrens($array));
  193.             }
  194.         }
  195.         return array_unique($array);
  196.     }
  197.     /**
  198.      * @return Category
  199.      */
  200.     public function getParent()
  201.     {
  202.         return $this->parent;
  203.     }
  204.     /**
  205.      * @param Category $parent
  206.      */
  207.     public function setParent($parent)
  208.     {
  209.         $this->parent $parent;
  210.     }
  211.     /**
  212.      * @return CategoryTranslation
  213.      */
  214.     public function createTrans($locale 'fr'$title$shortContent ''$content '')
  215.     {
  216.         $trans = new CategoryTranslation();
  217.         $trans->setLocale($locale);
  218.         $trans->setTitle($title);
  219.         $trans->setContent($content);
  220.         $trans->setShortContent($shortContent);
  221.         $this->addTranslation($trans);
  222.         return $trans;
  223.     }
  224.     /**
  225.      * @ORM\PreRemove()
  226.      */
  227.     public function preRemove()
  228.     {
  229.         foreach ($this->products as $product) {
  230.             $product->removeCategory($this);
  231.         }
  232.     }
  233.     public function getLevel()
  234.     {
  235.         if ($this->parent === null) {
  236.             return 1;
  237.         } else {
  238.             return $this->parent->getLevel() + 1;
  239.         }
  240.     }
  241.     /**
  242.      * @return Collection<int, Activite>
  243.      */
  244.     public function getActivites(): Collection
  245.     {
  246.         return $this->activites;
  247.     }
  248.     public function addActivite(Activite $activite): self
  249.     {
  250.         if (!$this->activites->contains($activite)) {
  251.             $this->activites[] = $activite;
  252.             $activite->setCategory($this);
  253.         }
  254.         return $this;
  255.     }
  256.     public function removeActivite(Activite $activite): self
  257.     {
  258.         if ($this->activites->removeElement($activite)) {
  259.             // set the owning side to null (unless already changed)
  260.             if ($activite->getCategory() === $this) {
  261.                 $activite->setCategory(null);
  262.             }
  263.         }
  264.         return $this;
  265.     }
  266.     /**
  267.      * @return Collection<int, CategoryParagraphe>
  268.      */
  269.     public function getCategoryParagraphes(): Collection
  270.     {
  271.         return $this->categoryParagraphes;
  272.     }
  273.     public function addCategoryParagraphe(CategoryParagraphe $categoryParagraphe): self
  274.     {
  275.         if (!$this->categoryParagraphes->contains($categoryParagraphe)) {
  276.             $this->categoryParagraphes[] = $categoryParagraphe;
  277.             $categoryParagraphe->setCategory($this);
  278.         }
  279.         return $this;
  280.     }
  281.     public function removeCategoryParagraphe(CategoryParagraphe $categoryParagraphe): self
  282.     {
  283.         if ($this->categoryParagraphes->removeElement($categoryParagraphe)) {
  284.             // set the owning side to null (unless already changed)
  285.             if ($categoryParagraphe->getCategory() === $this) {
  286.                 $categoryParagraphe->setCategory(null);
  287.             }
  288.         }
  289.         return $this;
  290.     }
  291. }