src/Entity/Tuer24/Category.php line 33

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