src/Entity/Content.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Dictionary\ContentStatus;
  4. use App\Dictionary\ContentType;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Doctrine\ORM\Mapping\JoinColumn;
  9. use Doctrine\ORM\Mapping\ManyToOne;
  10. use Doctrine\ORM\Mapping\OneToOne;
  11. use Doctrine\ORM\PersistentCollection;
  12. use JMS\Serializer\Annotation\Groups;
  13. /**
  14.  * @ORM\Table(name="Content", indexes={
  15.  *
  16.  *   @ORM\Index(name="publish_date_idx", columns={"publish_date"}),
  17.  *   @ORM\Index(name="publish_date_status_idx", columns={"publish_date", "status"}),
  18.  *   @ORM\Index(name="id_status_idx", columns={"id", "status"}),
  19.  *   @ORM\Index(name="status_idx", columns={"status"})
  20.  * })
  21.  *
  22.  * @ORM\Entity(repositoryClass="App\Repository\ContentRepository")
  23.  *
  24.  * @ORM\HasLifecycleCallbacks()
  25.  */
  26. class Content
  27. {
  28.     /**
  29.      * @Groups({"contentlist", "waitrating", "conversationmessageindex"})
  30.      *
  31.      * @ORM\Id()
  32.      *
  33.      * @ORM\GeneratedValue()
  34.      *
  35.      * @ORM\Column(type="integer")
  36.      */
  37.     private $id;
  38.     /**
  39.      * @ORM\OneToMany(targetEntity="ContentComment", mappedBy="content", cascade={"persist"})
  40.      */
  41.     private $comments;
  42.     /**
  43.      * @Groups({"waitrating", "contentlist", "conversationmessageindex"})
  44.      *
  45.      * @OneToOne(targetEntity="ContentVideo",cascade={"persist"}, inversedBy="content")
  46.      *
  47.      * @JoinColumn(name="video_id", referencedColumnName="id", onDelete="SET NULL")
  48.      */
  49.     private $video;
  50.     /**
  51.      * @Groups({"waitrating", "contentlist", "video_search", "imageset_search"})
  52.      *
  53.      * @ManyToOne(targetEntity="Member",cascade={"persist"}, fetch="EAGER")
  54.      *
  55.      * @JoinColumn(name="member_id", referencedColumnName="id", onDelete="SET NULL")
  56.      */
  57.     private $member;
  58.     /**
  59.      * @Groups({"waitrating", "contentlist", "conversationmessageindex"})
  60.      *
  61.      * @OneToOne(targetEntity="ContentImageset",cascade={"persist"}, inversedBy="content")
  62.      *
  63.      * @JoinColumn(name="imageset_id", referencedColumnName="id", onDelete="SET NULL")
  64.      */
  65.     private $imageset;
  66.     /**
  67.      * @Groups({"contentlist", "video_search", "imageset_search"})
  68.      *
  69.      * @ORM\Column(type="decimal", precision=10, scale=0, nullable=true)
  70.      */
  71.     private $price;
  72.     /**
  73.      * @Groups({"contentlist", "adminindex", "video_search", "imageset_search"})
  74.      *
  75.      * @ORM\Column(type="datetime", nullable=true)
  76.      */
  77.     private $publish_date;
  78.     /**
  79.      * @Groups({"contentdetail", "adminindex", "contentstatus"})
  80.      *
  81.      * @ORM\Column(type="smallint")
  82.      */
  83.     private $status ContentStatus::NONE;
  84.     /**
  85.      * @Groups({"waitrating", "adminindex", "contentdetail", "video_search", "imageset_search"})
  86.      *
  87.      * @ORM\Column(type="datetime")
  88.      */
  89.     private $created_at;
  90.     /**
  91.      * @ORM\Column(type="datetime")
  92.      */
  93.     private $updated_at;
  94.     /**
  95.      * @Groups({"contentlist", "conversationmessageindex"})
  96.      *
  97.      * @ORM\Column(type="smallint")
  98.      */
  99.     private $type;
  100.     /**
  101.      * @Groups({"conversationmessageindex", "contentlist", "video_search", "imageset_search"})
  102.      *
  103.      * @ORM\Column(type="smallint", nullable=true)
  104.      */
  105.     protected ?int $preview_image 1;
  106.     /**
  107.      * @Groups({"contentlist", "video_search", "imageset_search"})
  108.      *
  109.      * @OneToOne(targetEntity="ContentRanking", mappedBy="content")
  110.      */
  111.     private $ranking;
  112.     /**
  113.      * @Groups({"contentdetail", "video_search", "imageset_search"})
  114.      *
  115.      * @ORM\Column(type="boolean")
  116.      */
  117.     protected ?bool $is_hardcore false;
  118.     /**
  119.      * @Groups({"contentdetail", "video_search", "imageset_search"})
  120.      *
  121.      * @ORM\Column(type="string", nullable=true)
  122.      */
  123.     protected $meta_keywords;
  124.     /**
  125.      * @Groups({"contentdetail", "video_search", "imageset_search"})
  126.      *
  127.      * @ORM\Column(type="string", nullable=true)
  128.      */
  129.     protected $meta_description;
  130.     /**
  131.      * @Groups({"actorlist"})
  132.      *
  133.      * @ORM\ManyToMany(targetEntity="Actor", cascade={"persist"})
  134.      *
  135.      * @ORM\JoinTable(name="ContentActor",
  136.      *     joinColumns={@ORM\JoinColumn(name="content_id", referencedColumnName="id", onDelete="RESTRICT")},
  137.      *     inverseJoinColumns={@ORM\JoinColumn(name="actor_id", referencedColumnName="id", onDelete="RESTRICT")}
  138.      * )
  139.      */
  140.     private $actors;
  141.     /**
  142.      * @Groups({"contentdetail", "video_search", "imageset_search"})
  143.      *
  144.      * @ORM\OneToMany(targetEntity="App\Entity\ContentCategory", mappedBy="content", cascade={"persist"})
  145.      */
  146.     private $categories;
  147.     /**
  148.      * @Groups({"adminindex"})
  149.      */
  150.     protected int $nfsSize 0;
  151.     /**
  152.      * @Groups({"adminindex"})
  153.      *
  154.      * @ORM\OneToMany(targetEntity="App\Entity\MemberContent", mappedBy="content", cascade={"persist"})
  155.      */
  156.     private $memberContents;
  157.     public function __construct()
  158.     {
  159.         $this->actors = new ArrayCollection();
  160.         $this->categories = new ArrayCollection();
  161.         $this->comments = new ArrayCollection();
  162.         $this->memberContents = new ArrayCollection();
  163.     }
  164.     /**
  165.      * @return $this
  166.      */
  167.     public function setNfsSize(int $size): Content
  168.     {
  169.         $this->nfsSize $size;
  170.         return $this;
  171.     }
  172.     /**
  173.      * @ORM\PrePersist()
  174.      */
  175.     public function prePersist()
  176.     {
  177.         if (!$this->getCreatedAt()) {
  178.             $this->setCreatedAt(new \DateTime());
  179.         }
  180.         if (!$this->getUpdatedAt()) {
  181.             $this->setUpdatedAt(new \DateTime());
  182.         }
  183.     }
  184.     public function getContent(): ?ContentTypeAbstract
  185.     {
  186.         if (ContentType::VIDEO == $this->type) {
  187.             return $this->getVideo();
  188.         } elseif (ContentType::IMAGESET == $this->type) {
  189.             return $this->getImageset();
  190.         }
  191.         return null;
  192.     }
  193.     public function getId()
  194.     {
  195.         return $this->id;
  196.     }
  197.     public function setRanking(?ContentRanking $ranking): self
  198.     {
  199.         $this->ranking $ranking;
  200.         return $this;
  201.     }
  202.     public function getRanking(): ?ContentRanking
  203.     {
  204.         return $this->ranking;
  205.     }
  206.     public function getComments(): Collection
  207.     {
  208.         return $this->comments;
  209.     }
  210.     /**
  211.      * @return PersistentCollection
  212.      */
  213.     public function getActors(): Collection
  214.     {
  215.         return $this->actors;
  216.     }
  217.     /**
  218.      * @return PersistentCollection
  219.      */
  220.     public function getMemberContents(): Collection
  221.     {
  222.         return $this->memberContents;
  223.     }
  224.     public function getImageset(): ?ContentImageset
  225.     {
  226.         return $this->imageset;
  227.     }
  228.     public function setImageset(?ContentImageset $imageset): self
  229.     {
  230.         $this->imageset $imageset;
  231.         return $this;
  232.     }
  233.     public function getPrice(): ?float
  234.     {
  235.         return $this->price;
  236.     }
  237.     public function setPrice(?float $price): self
  238.     {
  239.         $this->price $price;
  240.         return $this;
  241.     }
  242.     public function getPublishDate(): ?\DateTimeInterface
  243.     {
  244.         return $this->publish_date;
  245.     }
  246.     public function isPublished(string $when 'now'): bool
  247.     {
  248.         if (!$this->publish_date instanceof \DateTimeInterface) {
  249.             return true;
  250.         }
  251.         return $this->publish_date->format('U') < (new \DateTime($when))->format('U');
  252.     }
  253.     public function setPublishDate(?\DateTimeInterface $publish_date): self
  254.     {
  255.         $this->publish_date $publish_date;
  256.         return $this;
  257.     }
  258.     public function getStatus(): ?int
  259.     {
  260.         return $this->status;
  261.     }
  262.     public function setStatus(int $status): self
  263.     {
  264.         $this->status $status;
  265.         return $this;
  266.     }
  267.     public function getCreatedAt(): ?\DateTime
  268.     {
  269.         return $this->created_at;
  270.     }
  271.     public function setCreatedAt(\DateTime $created_at): self
  272.     {
  273.         $this->created_at $created_at;
  274.         return $this;
  275.     }
  276.     public function getUpdatedAt(): ?\DateTime
  277.     {
  278.         return $this->updated_at;
  279.     }
  280.     public function setUpdatedAt(\DateTime $updated_at): self
  281.     {
  282.         $this->updated_at $updated_at;
  283.         return $this;
  284.     }
  285.     public function getType(): ?int
  286.     {
  287.         return $this->type;
  288.     }
  289.     public function getTypeWord(): ?string
  290.     {
  291.         if (ContentType::VIDEO === $this->type) {
  292.             return 'video';
  293.         }
  294.         if (ContentType::IMAGESET === $this->type) {
  295.             return 'imageset';
  296.         }
  297.         return null;
  298.     }
  299.     public function setType(int $type): self
  300.     {
  301.         $this->type $type;
  302.         return $this;
  303.     }
  304.     public function getTitle(): ?string
  305.     {
  306.         if ($this->isImageset()) {
  307.             return $this->getImageset()->getTitle();
  308.         }
  309.         if ($this->isVideo()) {
  310.             return $this->getVideo()->getTitle();
  311.         }
  312.         return null;
  313.     }
  314.     public function getSlug(): ?string
  315.     {
  316.         if ($this->isImageset()) {
  317.             return $this->getImageset()?->getSlug();
  318.         }
  319.         if ($this->isVideo()) {
  320.             return $this->getVideo()?->getSlug();
  321.         }
  322.         return null;
  323.     }
  324.     public function isSlugEmpty(): bool
  325.     {
  326.         return empty($this->getSlug());
  327.     }
  328.     public function getVideo(): ?ContentVideo
  329.     {
  330.         return $this->video;
  331.     }
  332.     public function setVideo(?ContentVideo $video): self
  333.     {
  334.         $this->video $video;
  335.         if ($video) {
  336.             $this->setType(ContentType::VIDEO);
  337.         }
  338.         return $this;
  339.     }
  340.     public function getMember(): ?Member
  341.     {
  342.         return $this->member;
  343.     }
  344.     public function setMember(?Member $member): self
  345.     {
  346.         $this->member $member;
  347.         return $this;
  348.     }
  349.     public function getPreviewImage(): ?int
  350.     {
  351.         return $this->preview_image;
  352.     }
  353.     public function setPreviewImage(?int $index): self
  354.     {
  355.         $this->preview_image $index;
  356.         return $this;
  357.     }
  358.     public function setIsHardcore(bool $is_hardcore): self
  359.     {
  360.         $this->is_hardcore $is_hardcore;
  361.         return $this;
  362.     }
  363.     public function getIsHardcore(): ?bool
  364.     {
  365.         return $this->is_hardcore;
  366.     }
  367.     public function setMetaDescription(?string $meta_description): self
  368.     {
  369.         $this->meta_description $meta_description;
  370.         return $this;
  371.     }
  372.     public function getMetaDescription(): ?string
  373.     {
  374.         return $this->meta_description;
  375.     }
  376.     public function setMetaKeywords(?string $meta_keywords): self
  377.     {
  378.         $this->meta_keywords $meta_keywords;
  379.         return $this;
  380.     }
  381.     public function getMetaKeywords(): ?string
  382.     {
  383.         return $this->meta_keywords;
  384.     }
  385.     /**
  386.      * @return Collection|ContentCategory[]
  387.      */
  388.     public function getCategories(): Collection
  389.     {
  390.         return $this->categories;
  391.     }
  392.     public function addCategory(ContentCategory $category): self
  393.     {
  394.         if (!$this->categories->contains($category)) {
  395.             $this->categories[] = $category;
  396.             $category->setContent($this);
  397.         }
  398.         return $this;
  399.     }
  400.     public function removeCategory(ContentCategory $category): self
  401.     {
  402.         if ($this->categories->contains($category)) {
  403.             $this->categories->removeElement($category);
  404.             // set the owning side to null (unless already changed)
  405.             if ($category->getContent() === $this) {
  406.                 $category->setContent(null);
  407.             }
  408.         }
  409.         return $this;
  410.     }
  411.     /**
  412.      * @param int|ContentCategory $category
  413.      */
  414.     public function hasCategory($category): bool
  415.     {
  416.         if ($category instanceof ContentCategory) {
  417.             $id $category->getId();
  418.         } else {
  419.             $id $category;
  420.         }
  421.         foreach ($this->getCategories() as $cat) {
  422.             if ($cat->getId() === $id) {
  423.                 return true;
  424.             }
  425.         }
  426.         return false;
  427.     }
  428.     public function isStatusActive(): bool
  429.     {
  430.         return ContentStatus::ACTIVE === $this->status;
  431.     }
  432.     public function isImageset(): bool
  433.     {
  434.         return ContentType::IMAGESET === $this->type;
  435.     }
  436.     public function isVideo(): bool
  437.     {
  438.         return ContentType::VIDEO === $this->type;
  439.     }
  440. }