<?php
namespace Roothirsch\DamBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Roothirsch\DamBundle\API\UpdateSorting;
use Roothirsch\DamBundle\Repository\CategoryRepository;
use Roothirsch\CoreBundle\Entity\Group;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use \Roothirsch\DamBundle\Entity\Asset;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\String\Slugger\AsciiSlugger;
/**
* @ORM\Entity(repositoryClass=CategoryRepository::class)
* @ORM\Table(name="dam_category")
* @Gedmo\Tree(type="nested")
* @ApiResource(
* shortName="Dam/Category",
* itemOperations={
* "get",
* "put",
* "delete",
* "update_sorting"={
* "method"="PUT",
* "path"="/dam/categories/{id}/update-sorting",
* "controller"=UpdateSorting::class
* },
* }
* )
* @ORM\HasLifecycleCallbacks()
*/
class Category
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Gedmo\Translatable
*/
private $title;
/**
* @ORM\Column(type="string", length=255)
*/
private $path;
/**
* @ORM\ManyToMany(targetEntity=Asset::class, inversedBy="categories")
* @ORM\JoinTable(name="dam_category_asset")
*/
private $assets;
/**
* @ORM\Column(type="text", nullable=true)
* @Gedmo\Translatable
*/
private $description;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $image;
/**
* @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=Group::class)
* @ORM\JoinTable(name="dam_category_groups")
*/
private $groups;
public function __construct($title = null)
{
$this->assets = new ArrayCollection();
$this->groups = new ArrayCollection();
$this->title = $title;
}
public function __toString()
{
return ($this->parent != null && $this->parent->getLvl() !== 0) ? $this->parent->__toString() . ' / ' . $this->title : $this->title;
}
public function getBreadCrumbs() {
if ($this->parent != null && $this->parent->getLvl() !== 0) {
$breadcrumbs = $this->getParent()->getBreadCrumbs();
} else {
$breadcrumbs = [];
}
$breadcrumbs[] = $this->title;
return $breadcrumbs;
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatePath()
{
if ($this->path === null) {
$slugger = new AsciiSlugger('de');
$category = $this;
$pieces = [];
while($category instanceof Category) {
if ($category->getLvl() > 0) {
$pieces[] = $category->getTitle();
}
$category = $category->getParent();
}
$pieces = array_reverse($pieces);
$this->path = implode('/', $pieces);
}
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
/**
* @return Collection|Asset[]
*/
public function getAssets(): Collection
{
return $this->assets;
}
public function addAsset(Asset $asset): self
{
if (!$this->assets->contains($asset)) {
$this->assets[] = $asset;
}
return $this;
}
public function removeAsset(Asset $asset): self
{
$this->assets->removeElement($asset);
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
public function getRoot()
{
return $this->root;
}
public function setParent($parent = null)
{
$this->parent = $parent;
}
public function getParent()
{
return $this->parent;
}
/**
* @return mixed
*/
public function getPath()
{
return $this->path;
}
/**
* @param mixed $path
*/
public function setPath($path): void
{
$this->path = $path;
}
public function getAssetByFilename(string $filename)
{
foreach ($this->getAssets() as $asset) {
if ($asset->getTitle() == $filename) {
return $asset;
}
}
}
/**
* @return mixed
*/
public function getLft()
{
return $this->lft;
}
/**
* @return mixed
*/
public function getLvl()
{
return $this->lvl;
}
/**
* @return mixed
*/
public function getRgt()
{
return $this->rgt;
}
/**
* @return ArrayCollection
*/
public function getGroups()
{
return $this->groups;
}
public function hasGroup($groupName)
{
return array_search($groupName, $this->getGroups()) !== false;
}
public function addGroup(Group $group): self
{
if (!$this->groups->contains($group)) {
$this->groups[] = $group;
}
return $this;
}
public function removeGroup(Group $group): self
{
$this->groups->removeElement($group);
return $this;
}
}