vendor/roothirsch/dam-bundle/Entity/Asset.php line 27

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\DamBundle\Entity;
  3. use Roothirsch\DamBundle\Repository\AssetRepository;
  4. use Roothirsch\CoreBundle\Entity\Traits\TimetrackedTrait;
  5. use Roothirsch\CoreBundle\Entity\User;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use ApiPlatform\Core\Annotation\ApiResource;
  10. use Gedmo\Mapping\Annotation as Gedmo;
  11. /**
  12. * @ORM\Entity(repositoryClass=AssetRepository::class)
  13. * @ORM\Table(name="dam_asset")
  14. * @ApiResource(
  15. * shortName="Dam/Asset",
  16. * itemOperations={
  17. * "toggle_favorite"={"method"="PUT", "path"="/dam/assets/{id}/toggle_favorite", "requirements"={"id"=".+"}},
  18. * "get",
  19. * "put",
  20. * "delete"
  21. * }
  22. * )
  23. */
  24. class Asset
  25. {
  26. use TimetrackedTrait;
  27. /**
  28. * @ORM\Id
  29. * @ORM\GeneratedValue
  30. * @ORM\Column(type="integer")
  31. */
  32. private $id;
  33. /**
  34. * @ORM\Column(type="string", length=255)
  35. * @Gedmo\Translatable
  36. */
  37. private $title;
  38. /**
  39. * @ORM\Column(type="text", nullable=true)
  40. */
  41. private $description;
  42. /**
  43. * @ORM\OneToMany(targetEntity=File::class, mappedBy="asset", orphanRemoval=true, cascade={"persist"})
  44. */
  45. private $files;
  46. /**
  47. * @ORM\ManyToMany(targetEntity=Category::class, mappedBy="assets")
  48. */
  49. private $categories;
  50. /**
  51. * @ORM\ManyToMany(targetEntity=User::class)
  52. * @ORM\JoinTable(name="dam_asset_favorites")
  53. */
  54. private $favorites;
  55. public function __construct()
  56. {
  57. $this->files = new ArrayCollection();
  58. $this->categories = new ArrayCollection();
  59. $this->favorites = new ArrayCollection();
  60. }
  61. public function getId(): ?int
  62. {
  63. return $this->id;
  64. }
  65. public function getTitle(): ?string
  66. {
  67. return $this->title;
  68. }
  69. public function setTitle(string $title): self
  70. {
  71. $this->title = $title;
  72. return $this;
  73. }
  74. public function getDescription(): ?string
  75. {
  76. return $this->description;
  77. }
  78. public function setDescription(?string $description): self
  79. {
  80. $this->description = $description;
  81. return $this;
  82. }
  83. /**
  84. * @return Collection|File[]
  85. */
  86. public function getFiles(): Collection
  87. {
  88. return $this->files;
  89. }
  90. public function addFile(File $file): self
  91. {
  92. if (!$this->files->contains($file)) {
  93. $this->files[] = $file;
  94. $file->setAsset($this);
  95. }
  96. return $this;
  97. }
  98. public function removeFile(File $file): self
  99. {
  100. if ($this->files->removeElement($file)) {
  101. // set the owning side to null (unless already changed)
  102. if ($file->getAsset() === $this) {
  103. $file->setAsset(null);
  104. }
  105. }
  106. return $this;
  107. }
  108. public function getFirstFile()
  109. {
  110. return current($this->files);
  111. }
  112. /**
  113. * @return Collection|Category[]
  114. */
  115. public function getCategories(): Collection
  116. {
  117. return $this->categories;
  118. }
  119. public function addCategory(Category $category): self
  120. {
  121. if (!$this->categories->contains($category)) {
  122. $this->categories[] = $category;
  123. $category->addAsset($this);
  124. }
  125. return $this;
  126. }
  127. public function removeCategory(Category $category): self
  128. {
  129. if ($this->categories->removeElement($category)) {
  130. $category->removeAsset($this);
  131. }
  132. return $this;
  133. }
  134. /**
  135. * @return Collection|User[]
  136. */
  137. public function getFavorites(): Collection
  138. {
  139. return $this->favorites;
  140. }
  141. public function addFavorite(User $user): self
  142. {
  143. if (!$this->favorites->contains($user)) {
  144. $this->favorites->add($user);
  145. }
  146. return $this;
  147. }
  148. public function removeFavorite(User $user): self
  149. {
  150. $this->favorites->removeElement($user);
  151. return $this;
  152. }
  153. public function hasFavorite(User $user)
  154. {
  155. return $this->favorites->contains($user);
  156. }
  157. public function toggleFavorite(User $user)
  158. {
  159. if ($this->hasFavorite($user)) {
  160. $this->removeFavorite($user);
  161. } else {
  162. $this->addFavorite($user);
  163. }
  164. }
  165. public function getBreadCrumbs() {
  166. $breadcrumbs = [];
  167. foreach($this->categories as $category) {
  168. $breadcrumbs[] = $category->getBreadCrumbs();
  169. }
  170. return $breadcrumbs;
  171. }
  172. }