src/Entity/Category.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use App\Entity\Behaviors\TranslatableTrait;
  7. use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
  8. use Symfony\Component\HttpFoundation\File\File;
  9. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. /**
  12. * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
  13. * @ORM\EntityListeners({"App\Listener\EntityListener"})
  14. * @Vich\Uploadable()
  15. */
  16. class Category implements TranslatableInterface
  17. {
  18. use TranslatableTrait;
  19. /**
  20. * @ORM\Id()
  21. * @ORM\GeneratedValue()
  22. * @ORM\Column(type="integer")
  23. */
  24. private $id;
  25. /**
  26. * @ORM\Column(type="boolean")
  27. */
  28. private $isActive = true;
  29. /**
  30. * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="subCategories")
  31. */
  32. private $parent;
  33. /**
  34. * @ORM\OneToMany(targetEntity="App\Entity\Category", mappedBy="parent", cascade={"persist"})
  35. */
  36. private $subCategories;
  37. /**
  38. * @ORM\Column(type="datetime")
  39. */
  40. private $createts;
  41. /**
  42. * @ORM\ManyToOne(targetEntity="App\Entity\User")
  43. * @ORM\JoinColumn(nullable=true)
  44. */
  45. private $createUser;
  46. /**
  47. * @Vich\UploadableField(mapping="category_image", fileNameProperty="image")
  48. * @Assert\Image(mimeTypes={"image/png", "image/jpeg", "image/jpg", "image/gif"})
  49. * @var File|null
  50. */
  51. private $imageFile;
  52. /**
  53. * @ORM\Column(type="string", length=199, nullable=true)
  54. */
  55. private $image;
  56. /**
  57. * @ORM\Column(type="datetime", nullable=true)
  58. */
  59. private $editts;
  60. /**
  61. * @ORM\ManyToOne(targetEntity="App\Entity\User")
  62. */
  63. private $editUser;
  64. /**
  65. * @ORM\Column(type="boolean")
  66. */
  67. private $showProducts = true;
  68. /**
  69. * @ORM\Column(type="boolean")
  70. */
  71. private $onlyForAdults = false;
  72. /**
  73. * @ORM\Column(type="boolean")
  74. */
  75. private $showSize = true;
  76. /**
  77. * @ORM\Column(type="integer", nullable=true)
  78. */
  79. private $orderBy;
  80. /**
  81. * @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="category", cascade={"persist"})
  82. */
  83. private $products;
  84. public function __construct()
  85. {
  86. $this->subCategories = new ArrayCollection();
  87. $this->products = new ArrayCollection();
  88. }
  89. public function getId(): ?int
  90. {
  91. return $this->id;
  92. }
  93. public function getIsActive(): ?bool
  94. {
  95. return $this->isActive;
  96. }
  97. public function setIsActive(bool $isActive): self
  98. {
  99. $this->isActive = $isActive;
  100. return $this;
  101. }
  102. public function getParent(): ?self
  103. {
  104. return $this->parent;
  105. }
  106. public function setParent(?self $parent): self
  107. {
  108. $this->parent = $parent;
  109. return $this;
  110. }
  111. /**
  112. * @return Collection|self[]
  113. */
  114. public function getSubCategories(): Collection
  115. {
  116. return $this->subCategories;
  117. }
  118. public function addSubCategory(self $subCategory): self
  119. {
  120. if (!$this->subCategories->contains($subCategory)) {
  121. $this->subCategories[] = $subCategory;
  122. $subCategory->setParent($this);
  123. }
  124. return $this;
  125. }
  126. public function removeSubCategory(self $subCategory): self
  127. {
  128. if ($this->subCategories->contains($subCategory)) {
  129. $this->subCategories->removeElement($subCategory);
  130. // set the owning side to null (unless already changed)
  131. if ($subCategory->getParent() === $this) {
  132. $subCategory->setParent(null);
  133. }
  134. }
  135. return $this;
  136. }
  137. public function getCreatets(): ?\DateTimeInterface
  138. {
  139. return $this->createts;
  140. }
  141. public function setCreatets(\DateTimeInterface $createts): self
  142. {
  143. $this->createts = $createts;
  144. return $this;
  145. }
  146. public function getCreateUser(): ?User
  147. {
  148. return $this->createUser;
  149. }
  150. public function setCreateUser(?User $createUser): self
  151. {
  152. $this->createUser = $createUser;
  153. return $this;
  154. }
  155. public function getEditts(): ?\DateTimeInterface
  156. {
  157. return $this->editts;
  158. }
  159. public function setEditts(?\DateTimeInterface $editts): self
  160. {
  161. $this->editts = $editts;
  162. return $this;
  163. }
  164. public function getEditUser(): ?User
  165. {
  166. return $this->editUser;
  167. }
  168. public function setEditUser(?User $editUser): self
  169. {
  170. $this->editUser = $editUser;
  171. return $this;
  172. }
  173. public function getOrderBy(): ?int
  174. {
  175. return $this->orderBy;
  176. }
  177. public function setOrderBy(?int $orderBy): self
  178. {
  179. $this->orderBy = $orderBy;
  180. return $this;
  181. }
  182. public function getImage(): ?string
  183. {
  184. return $this->image;
  185. }
  186. public function setImage(?string $image): self
  187. {
  188. $this->image = $image;
  189. return $this;
  190. }
  191. public function __call($method, $arguments)
  192. {
  193. $ret = $this->proxyCurrentLocaleTranslation($method, $arguments);
  194. if(empty($ret)) {
  195. $ret = call_user_func_array( [ $this->translate( \Locale::getDefault() ), $method ], $arguments );
  196. }
  197. return $ret;
  198. }
  199. /**
  200. * Get ImageFile
  201. *
  202. * @return null|File
  203. */
  204. public function getImageFile()
  205. {
  206. return $this->imageFile;
  207. }
  208. /**
  209. * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
  210. */
  211. public function setImageFile(?File $imageFile = null): void
  212. {
  213. $this->imageFile = $imageFile;
  214. if (null !== $imageFile) {
  215. $this->editts = new \DateTime();
  216. }
  217. }
  218. /**
  219. * @return Collection|Product[]
  220. */
  221. public function getProducts(): Collection
  222. {
  223. return $this->products;
  224. }
  225. public function addProduct(Product $product): self
  226. {
  227. if (!$this->products->contains($product)) {
  228. $this->products[] = $product;
  229. $product->setCategory($this);
  230. }
  231. return $this;
  232. }
  233. public function removeProduct(Product $product): self
  234. {
  235. if ($this->products->contains($product)) {
  236. $this->products->removeElement($product);
  237. // set the owning side to null (unless already changed)
  238. if ($product->getCategory() === $this) {
  239. $product->setCategory(null);
  240. }
  241. }
  242. return $this;
  243. }
  244. public function getFullName()
  245. {
  246. $name=$this->getName();
  247. $ex = $this;
  248. while($ex->getParent() !== null){
  249. $ex = $ex->getParent();
  250. $name = $ex->getName().' -> '.$name;
  251. }
  252. return $name;
  253. }
  254. /**
  255. * Get ShowProducts
  256. *
  257. * @return mixed
  258. */
  259. public function getShowProducts()
  260. {
  261. return $this->showProducts;
  262. }
  263. /**
  264. * Set ShowProducts
  265. *
  266. * @param mixed $showProducts
  267. */
  268. public function setShowProducts( $showProducts )
  269. {
  270. $this->showProducts = $showProducts;
  271. }
  272. /**
  273. * Get ShowSize
  274. *
  275. * @return mixed
  276. */
  277. public function getShowSize()
  278. {
  279. return $this->showSize;
  280. }
  281. /**
  282. * Set ShowSize
  283. *
  284. * @param mixed $showSize
  285. */
  286. public function setShowSize( $showSize )
  287. {
  288. $this->showSize = $showSize;
  289. }
  290. /**
  291. * Get OnlyForAdults
  292. *
  293. * @return mixed
  294. */
  295. public function getOnlyForAdults()
  296. {
  297. return $this->onlyForAdults;
  298. }
  299. /**
  300. * Set OnlyForAdults
  301. *
  302. * @param mixed $onlyForAdults
  303. */
  304. public function setOnlyForAdults( $onlyForAdults )
  305. {
  306. $this->onlyForAdults = $onlyForAdults;
  307. }
  308. }