vendor/roothirsch/dam-bundle/Entity/Category.php line 35

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\DamBundle\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use Roothirsch\DamBundle\API\UpdateSorting;
  5. use Roothirsch\DamBundle\Repository\CategoryRepository;
  6. use Roothirsch\CoreBundle\Entity\Group;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use \Roothirsch\DamBundle\Entity\Asset;
  11. use Gedmo\Mapping\Annotation as Gedmo;
  12. use Symfony\Component\String\Slugger\AsciiSlugger;
  13. /**
  14. * @ORM\Entity(repositoryClass=CategoryRepository::class)
  15. * @ORM\Table(name="dam_category")
  16. * @Gedmo\Tree(type="nested")
  17. * @ApiResource(
  18. * shortName="Dam/Category",
  19. * itemOperations={
  20. * "get",
  21. * "put",
  22. * "delete",
  23. * "update_sorting"={
  24. * "method"="PUT",
  25. * "path"="/dam/categories/{id}/update-sorting",
  26. * "controller"=UpdateSorting::class
  27. * },
  28. * }
  29. * )
  30. * @ORM\HasLifecycleCallbacks()
  31. */
  32. class Category
  33. {
  34. /**
  35. * @ORM\Id
  36. * @ORM\GeneratedValue
  37. * @ORM\Column(type="integer")
  38. */
  39. private $id;
  40. /**
  41. * @ORM\Column(type="string", length=255)
  42. * @Gedmo\Translatable
  43. */
  44. private $title;
  45. /**
  46. * @ORM\Column(type="string", length=255)
  47. */
  48. private $path;
  49. /**
  50. * @ORM\ManyToMany(targetEntity=Asset::class, inversedBy="categories")
  51. * @ORM\JoinTable(name="dam_category_asset")
  52. */
  53. private $assets;
  54. /**
  55. * @ORM\Column(type="text", nullable=true)
  56. * @Gedmo\Translatable
  57. */
  58. private $description;
  59. /**
  60. * @ORM\Column(type="text", nullable=true)
  61. */
  62. private $image;
  63. /**
  64. * @Gedmo\TreeLeft
  65. * @ORM\Column(name="lft", type="integer")
  66. */
  67. private $lft;
  68. /**
  69. * @Gedmo\TreeLevel
  70. * @ORM\Column(name="lvl", type="integer")
  71. */
  72. private $lvl;
  73. /**
  74. * @Gedmo\TreeRight
  75. * @ORM\Column(name="rgt", type="integer")
  76. */
  77. private $rgt;
  78. /**
  79. * @Gedmo\TreeRoot
  80. * @ORM\ManyToOne(targetEntity="Category")
  81. * @ORM\JoinColumn(name="tree_root", referencedColumnName="id", onDelete="CASCADE")
  82. */
  83. private $root;
  84. /**
  85. * @Gedmo\TreeParent
  86. * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
  87. * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
  88. */
  89. private $parent;
  90. /**
  91. * @ORM\OneToMany(targetEntity="Category", mappedBy="parent_id")
  92. * @ORM\OrderBy({"lft" = "ASC"})
  93. */
  94. private $children;
  95. /**
  96. * @ORM\ManyToMany(targetEntity=Group::class)
  97. * @ORM\JoinTable(name="dam_category_groups")
  98. */
  99. private $groups;
  100. public function __construct($title = null)
  101. {
  102. $this->assets = new ArrayCollection();
  103. $this->groups = new ArrayCollection();
  104. $this->title = $title;
  105. }
  106. public function __toString()
  107. {
  108. return ($this->parent != null && $this->parent->getLvl() !== 0) ? $this->parent->__toString() . ' / ' . $this->title : $this->title;
  109. }
  110. public function getBreadCrumbs() {
  111. if ($this->parent != null && $this->parent->getLvl() !== 0) {
  112. $breadcrumbs = $this->getParent()->getBreadCrumbs();
  113. } else {
  114. $breadcrumbs = [];
  115. }
  116. $breadcrumbs[] = $this->title;
  117. return $breadcrumbs;
  118. }
  119. /**
  120. * @ORM\PrePersist
  121. * @ORM\PreUpdate
  122. */
  123. public function updatePath()
  124. {
  125. if ($this->path === null) {
  126. $slugger = new AsciiSlugger('de');
  127. $category = $this;
  128. $pieces = [];
  129. while($category instanceof Category) {
  130. if ($category->getLvl() > 0) {
  131. $pieces[] = $category->getTitle();
  132. }
  133. $category = $category->getParent();
  134. }
  135. $pieces = array_reverse($pieces);
  136. $this->path = implode('/', $pieces);
  137. }
  138. }
  139. public function getId(): ?int
  140. {
  141. return $this->id;
  142. }
  143. public function getTitle(): ?string
  144. {
  145. return $this->title;
  146. }
  147. public function setTitle(string $title): self
  148. {
  149. $this->title = $title;
  150. return $this;
  151. }
  152. /**
  153. * @return Collection|Asset[]
  154. */
  155. public function getAssets(): Collection
  156. {
  157. return $this->assets;
  158. }
  159. public function addAsset(Asset $asset): self
  160. {
  161. if (!$this->assets->contains($asset)) {
  162. $this->assets[] = $asset;
  163. }
  164. return $this;
  165. }
  166. public function removeAsset(Asset $asset): self
  167. {
  168. $this->assets->removeElement($asset);
  169. return $this;
  170. }
  171. public function getDescription(): ?string
  172. {
  173. return $this->description;
  174. }
  175. public function setDescription(?string $description): self
  176. {
  177. $this->description = $description;
  178. return $this;
  179. }
  180. public function getImage(): ?string
  181. {
  182. return $this->image;
  183. }
  184. public function setImage(?string $image): self
  185. {
  186. $this->image = $image;
  187. return $this;
  188. }
  189. public function getRoot()
  190. {
  191. return $this->root;
  192. }
  193. public function setParent($parent = null)
  194. {
  195. $this->parent = $parent;
  196. }
  197. public function getParent()
  198. {
  199. return $this->parent;
  200. }
  201. /**
  202. * @return mixed
  203. */
  204. public function getPath()
  205. {
  206. return $this->path;
  207. }
  208. /**
  209. * @param mixed $path
  210. */
  211. public function setPath($path): void
  212. {
  213. $this->path = $path;
  214. }
  215. public function getAssetByFilename(string $filename)
  216. {
  217. foreach ($this->getAssets() as $asset) {
  218. if ($asset->getTitle() == $filename) {
  219. return $asset;
  220. }
  221. }
  222. }
  223. /**
  224. * @return mixed
  225. */
  226. public function getLft()
  227. {
  228. return $this->lft;
  229. }
  230. /**
  231. * @return mixed
  232. */
  233. public function getLvl()
  234. {
  235. return $this->lvl;
  236. }
  237. /**
  238. * @return mixed
  239. */
  240. public function getRgt()
  241. {
  242. return $this->rgt;
  243. }
  244. /**
  245. * @return ArrayCollection
  246. */
  247. public function getGroups()
  248. {
  249. return $this->groups;
  250. }
  251. public function hasGroup($groupName)
  252. {
  253. return array_search($groupName, $this->getGroups()) !== false;
  254. }
  255. public function addGroup(Group $group): self
  256. {
  257. if (!$this->groups->contains($group)) {
  258. $this->groups[] = $group;
  259. }
  260. return $this;
  261. }
  262. public function removeGroup(Group $group): self
  263. {
  264. $this->groups->removeElement($group);
  265. return $this;
  266. }
  267. }