src/Entity/Forum/Post.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Forum;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use App\Traits\CreatedUpdatedTrait;
  6. use Gedmo\Mapping\Annotation as Gedmo;
  7. use JMS\Serializer\Annotation\Exclude;
  8. use JMS\Serializer\Annotation as JMS;
  9. use JMS\Serializer\Annotation as Serializer;
  10. use Symfony\Component\HttpFoundation\File\File;
  11. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  12. /**
  13.  * @ORM\Table(name="v_forum_post")
  14.  * @ORM\Entity(repositoryClass="App\Repository\Forum\PostRepository")
  15.  * @Vich\Uploadable
  16.  * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
  17.  */
  18. class Post
  19. {
  20.     /**
  21.      * @ORM\Id()
  22.      * @ORM\GeneratedValue()
  23.      * @ORM\Column(type="integer")
  24.      */
  25.     private $id;
  26.     /**
  27.      * @ORM\Column(type="string", length=55)
  28.      */
  29.     private $title;
  30.     /**
  31.      * @var string
  32.      * @Gedmo\Slug(fields={"title"}, updatable=false)
  33.      * @ORM\Column(type="string", length=255)
  34.      */
  35.     private $slug;
  36.     /**
  37.      * @ORM\Column(type="text", nullable=true)
  38.      */
  39.     private $content;
  40.     /**
  41.      * @ORM\Column(type="boolean")
  42.      */
  43.     private $approved true;
  44.     /**
  45.      * @ORM\Column(type="boolean")
  46.      */
  47.     private $anonymous false;
  48.     /**
  49.      * @ORM\Column(type="boolean")
  50.      */
  51.     private $recommended false;
  52.     /**
  53.      * @ORM\ManyToOne(targetEntity="App\Entity\Forum\Category")
  54.      * @Exclude
  55.      */
  56.     private $category;
  57.     /**
  58.      * @ORM\ManyToOne(targetEntity="App\Entity\User")
  59.      * @Exclude
  60.      */
  61.     private $user;
  62.     /**
  63.      * @ORM\ManyToOne(targetEntity="App\Entity\Admin")
  64.      * @Exclude
  65.      */
  66.     private $admin;
  67.     /**
  68.      * @ORM\ManyToOne(targetEntity="App\Entity\Forum\Group")
  69.      * @Exclude
  70.      */
  71.     private $group;
  72.     /**
  73.      * @ORM\Column(name="deleted_at", type="datetime", nullable=true)
  74.      */
  75.     private $deletedAt;
  76.     /**
  77.      * @ORM\OneToMany(targetEntity="App\Entity\Forum\PostReply", mappedBy="post")
  78.      * @Exclude
  79.      * @ORM\OrderBy({"id": "DESC"})
  80.      */
  81.     private $replies;
  82.     /**
  83.      * @ORM\OneToMany(targetEntity="App\Entity\Forum\PostLike", mappedBy="post")
  84.      * @Exclude
  85.      **/
  86.     private $likes;
  87.     /**
  88.      * @ORM\OneToMany(targetEntity="App\Entity\Forum\PostSave", mappedBy="post")
  89.      * @Exclude
  90.      **/
  91.     private $saves;
  92.     /**
  93.      * NOTE: This is not a mapped field of entity metadata, just a simple property.
  94.      *
  95.      * @Vich\UploadableField(mapping="forum_files", fileNameProperty="image")
  96.      *
  97.      * @var File
  98.      * @Exclude
  99.      */
  100.     private $imageFile;
  101.     /**
  102.      * @ORM\Column(name="image", type="string", length=255, nullable=true)
  103.      *
  104.      * @var string
  105.      */
  106.     private $image;
  107.     /**
  108.      * @var string
  109.      *
  110.      * @ORM\Column(name="view_count", type="integer", nullable=true)
  111.      */
  112.     private $viewCount 0;
  113.     /**
  114.      * @ORM\Column(type="boolean")
  115.      */
  116.     private $banned false;
  117.     private $liked false;
  118.     private $saved false;
  119.     /**
  120.      * @ORM\Column(name="posted_by_name", type="string", length=255, nullable=true)
  121.      */
  122.     private $postedByName;
  123.     /**
  124.      * @ORM\Column(name="posted_by_image", type="string", length=255, nullable=true)
  125.      */
  126.     private $postedByImage;
  127.     /**
  128.      * @ORM\Column(type="boolean")
  129.      */
  130.     private $archived false;
  131.     /**
  132.      * @return mixed
  133.      */
  134.     public function getLikes()
  135.     {
  136.         return $this->likes;
  137.     }
  138.     use CreatedUpdatedTrait;
  139.     public function __construct(){
  140.         $this->replies = new ArrayCollection();
  141.     }
  142.     public function __toString()
  143.     {
  144.         return (string) $this->title;
  145.     }
  146.     public function getId(): ?int
  147.     {
  148.         return $this->id;
  149.     }
  150.     public function getTitle(): ?string
  151.     {
  152.         return $this->title;
  153.     }
  154.     public function setTitle(string $title): self
  155.     {
  156.         $this->title $title;
  157.         return $this;
  158.     }
  159.     public function getSlug(): ?string
  160.     {
  161.         return $this->slug;
  162.     }
  163.     public function setSlug(?string $slug): self
  164.     {
  165.         $this->slug $slug;
  166.         return $this;
  167.     }
  168.     public function getContent(): ?string
  169.     {
  170.         return $this->content;
  171.     }
  172.     public function setContent(?string $content): self
  173.     {
  174.         $this->content $content;
  175.         return $this;
  176.     }
  177.     /**
  178.      * @return bool
  179.      */
  180.     public function isApproved(): bool
  181.     {
  182.         return $this->approved;
  183.     }
  184.     /**
  185.      * @param bool $approved
  186.      */
  187.     public function setApproved(?bool $approved): void
  188.     {
  189.         $this->approved $approved;
  190.     }
  191.     /**
  192.      * @return bool
  193.      */
  194.     public function isAnonymous(): ?bool
  195.     {
  196.         return $this->anonymous;
  197.     }
  198.     /**
  199.      * @param bool $anonymous
  200.      */
  201.     public function setAnonymous(?bool $anonymous): void
  202.     {
  203.         $this->anonymous $anonymous;
  204.     }
  205.     /**
  206.      * @return bool
  207.      */
  208.     public function isRecommended(): ?bool
  209.     {
  210.         return $this->recommended;
  211.     }
  212.     /**
  213.      * @param bool $recommended
  214.      */
  215.     public function setRecommended(?bool $recommended): void
  216.     {
  217.         $this->recommended $recommended;
  218.     }
  219.     public function getCategory(): ?Category
  220.     {
  221.         return $this->category;
  222.     }
  223.     public function setCategory(?Category $category): self
  224.     {
  225.         $this->category $category;
  226.         return $this;
  227.     }
  228.     public function getGroup(): ?Group
  229.     {
  230.         return $this->group;
  231.     }
  232.     public function setGroup(?Group $group): self
  233.     {
  234.         $this->group $group;
  235.         return $this;
  236.     }
  237.     /**
  238.      * @return mixed
  239.      */
  240.     public function getDeletedAt()
  241.     {
  242.         return $this->deletedAt;
  243.     }
  244.     /**
  245.      * @param mixed $deletedAt
  246.      */
  247.     public function setDeletedAt($deletedAt): void
  248.     {
  249.         $this->deletedAt $deletedAt;
  250.     }
  251.     /**
  252.      * @return mixed
  253.      */
  254.     public function getUser()
  255.     {
  256.         return $this->user;
  257.     }
  258.     /**
  259.      * @param mixed $user
  260.      */
  261.     public function setUser($user): void
  262.     {
  263.         $this->user $user;
  264.         $this->postedByName $user->getName();
  265.     }
  266.     /**
  267.      * @return mixed
  268.      */
  269.     public function getAdmin()
  270.     {
  271.         return $this->admin;
  272.     }
  273.     /**
  274.      * @param mixed $admin
  275.      */
  276.     public function setAdmin($admin): void
  277.     {
  278.         $this->admin $admin;
  279.         $this->postedByName $admin->getName();
  280.     }
  281.     /**
  282.      * @JMS\VirtualProperty
  283.      * @JMS\SerializedName("user_id")
  284.      *
  285.      * @return string
  286.      */
  287.     public function getUserId()
  288.     {
  289.         return $this->getUser() ? $this->getUser()->getId() : $this->getAdmin()->getId();
  290.     }
  291.     /**
  292.      * @JMS\VirtualProperty
  293.      * @JMS\SerializedName("user_name")
  294.      *
  295.      * @return string
  296.      */
  297.     public function getUserName()
  298.     {
  299.         if(!is_null($this->deletedAt)){
  300.             return $this->getPostedByName() ?: 'Anonymous';
  301.         }
  302.         if($this->getAdmin()){
  303.             return $this->getAdmin() ? $this->getAdmin()->getName() : 'Anonymous';
  304.         }
  305.         return $this->getUser() ? $this->getUser()->getName() : 'Anonymous';
  306.     }
  307.     /**
  308.      * @JMS\VirtualProperty
  309.      * @JMS\SerializedName("is_admin_post")
  310.      *
  311.      * @return string
  312.      */
  313.     public function isAdminPost()
  314.     {
  315.         return $this->getAdmin() ? true false;
  316.     }
  317.     /**
  318.      * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
  319.      * of 'UploadedFile' is injected into this setter to trigger the  update. If this
  320.      * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
  321.      * must be able to accept an instance of 'File' as the bundle will inject one here
  322.      * during Doctrine hydration.
  323.      *
  324.      * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $file
  325.      */
  326.     public function setImageFile(File $file null)
  327.     {
  328.         $this->imageFile $file;
  329.         if ($file) {
  330.             // It is required that at least one field changes if you are using doctrine
  331.             // otherwise the event listeners won't be called and the file is lost
  332.             $this->updated = new \DateTime('now');
  333.         }
  334.     }
  335.     /**
  336.      * @return File
  337.      */
  338.     public function getImageFile()
  339.     {
  340.         return $this->imageFile;
  341.     }
  342.     /**
  343.      * @param string $image
  344.      */
  345.     public function setImage($image)
  346.     {
  347.         $this->image $image;
  348.     }
  349.     /**
  350.      * @return string
  351.      */
  352.     public function getImage()
  353.     {
  354.         return $this->image;
  355.     }
  356.     /**
  357.      * @return mixed
  358.      */
  359.     public function getReplies()
  360.     {
  361.         return $this->replies;
  362.     }
  363.     /**
  364.      * @Serializer\VirtualProperty
  365.      * @Serializer\SerializedName("replies")
  366.      * @Serializer\Expose
  367.      */
  368.     public function getFilteredReplies()
  369.     {
  370.         $filteredReplies = [];
  371.         foreach ($this->replies as $reply){
  372.             if(is_null($reply->getDeletedAt())) {
  373.                 $filteredReplies[] = $reply;
  374.             }
  375.         }
  376.         return $filteredReplies;
  377.     }
  378.     /**
  379.      * @return mixed
  380.      */
  381.     public function getSaves()
  382.     {
  383.         return $this->saves;
  384.     }
  385.     /**
  386.      * @return integer
  387.      */
  388.     public function getViewCount()
  389.     {
  390.         return $this->viewCount;
  391.     }
  392.     /**
  393.      * @param integer $viewCount
  394.      */
  395.     public function setViewCount($viewCount)
  396.     {
  397.         $this->viewCount $viewCount;
  398.     }
  399.     public function incrementViewCount()
  400.     {
  401.         $this->viewCount++;
  402.     }
  403.     /**
  404.      * @return bool
  405.      */
  406.     public function isBanned(): ?bool
  407.     {
  408.         return $this->banned;
  409.     }
  410.     /**
  411.      * @param bool $banned
  412.      */
  413.     public function setBanned(?bool $banned): void
  414.     {
  415.         $this->banned $banned;
  416.     }
  417.     /**
  418.      * @return mixed
  419.      */
  420.     public function getPostedByName()
  421.     {
  422.         return $this->postedByName;
  423.     }
  424.     /**
  425.      * @param mixed $postedByName
  426.      */
  427.     public function setPostedByName($postedByName): void
  428.     {
  429.         $this->postedByName $postedByName;
  430.     }
  431.     /**
  432.      * @return mixed
  433.      */
  434.     public function getPostedByImage()
  435.     {
  436.         return $this->postedByImage;
  437.     }
  438.     /**
  439.      * @param mixed $postedByImage
  440.      */
  441.     public function setPostedByImage($postedByImage): void
  442.     {
  443.         $this->postedByImage $postedByImage;
  444.     }
  445.     /**
  446.      * @return bool
  447.      */
  448.     public function isArchived(): ?bool
  449.     {
  450.         return $this->archived;
  451.     }
  452.     /**
  453.      * @param bool $archived
  454.      */
  455.     public function setArchived(?bool $archived): void
  456.     {
  457.         $this->archived $archived;
  458.     }
  459. }