src/Aqarmap/Bundle/DiscussionBundle/Entity/Discussion.php line 29

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\DiscussionBundle\Entity;
  3. use Aqarmap\Bundle\DiscussionBundle\Constant\CommentStatus;
  4. use Aqarmap\Bundle\DiscussionBundle\Constant\DiscussionStatus;
  5. use Aqarmap\Bundle\ListingBundle\Entity\Location;
  6. use Aqarmap\Bundle\UserBundle\Entity\User;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Criteria;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Gedmo\Mapping\Annotation as Gedmo;
  11. use JMS\Serializer\Annotation as Serializer;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. /**
  14.  * Discussion.
  15.  *
  16.  * @ORM\Entity(repositoryClass="Aqarmap\Bundle\DiscussionBundle\Repository\DiscussionRepository")
  17.  *
  18.  * @ORM\Table(name="discussions")
  19.  *
  20.  * @ORM\HasLifecycleCallbacks
  21.  *
  22.  * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
  23.  *
  24.  * @Serializer\ExclusionPolicy("all")
  25.  */
  26. class Discussion
  27. {
  28.     /**
  29.      * @var int
  30.      *
  31.      * @ORM\Column(name="id", type="integer")
  32.      *
  33.      * @ORM\Id
  34.      *
  35.      * @ORM\GeneratedValue(strategy="AUTO")
  36.      *
  37.      * @Serializer\Groups({"Discussion"})
  38.      *
  39.      * @Serializer\Expose
  40.      */
  41.     private $id;
  42.     /**
  43.      * @var string
  44.      *
  45.      * @ORM\Column(name="content", type="text", length=512, nullable=true)
  46.      *
  47.      * @Assert\NotBlank()
  48.      *
  49.      * @Serializer\Groups({"Discussion"})
  50.      *
  51.      * @Serializer\Expose
  52.      */
  53.     private $content;
  54.     /**
  55.      * @var int
  56.      *
  57.      * @ORM\Column(name="status", type="smallint", nullable=true, options={"default" = 1})
  58.      */
  59.     private $status DiscussionStatus::PENDING;
  60.     /**
  61.      * @var \DateTime
  62.      *
  63.      * @ORM\Column(name="created_at", type="datetime", nullable=true)
  64.      *
  65.      * @Serializer\Groups({"Discussion"})
  66.      *
  67.      * @Serializer\Expose
  68.      */
  69.     private $createdAt;
  70.     /**
  71.      * @var \DateTime
  72.      *
  73.      * @ORM\Column(name="updated_at", type="datetime", nullable=true)
  74.      *
  75.      * @Serializer\Groups({"Discussion"})
  76.      *
  77.      * @Serializer\Expose
  78.      */
  79.     private $updatedAt;
  80.     /**
  81.      * @ORM\ManyToOne(targetEntity="\Aqarmap\Bundle\UserBundle\Entity\User", inversedBy="discussions")
  82.      *
  83.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  84.      *
  85.      * @Serializer\Groups({"Discussion"})
  86.      *
  87.      * @Serializer\Expose
  88.      */
  89.     protected $user;
  90.     /**
  91.      * @ORM\ManyToMany(targetEntity="\Aqarmap\Bundle\UserBundle\Entity\User")
  92.      *
  93.      * @ORM\JoinTable(name="discussions_subscribers",
  94.      *     joinColumns={@ORM\JoinColumn(name="discussion_id", referencedColumnName="id")},
  95.      *     inverseJoinColumns={@ORM\JoinColumn(name="subscriber_id", referencedColumnName="id")}
  96.      * )
  97.      */
  98.     protected $subscribers;
  99.     /**
  100.      * @ORM\ManyToOne(targetEntity="\Aqarmap\Bundle\ListingBundle\Entity\Location", inversedBy="discussions")
  101.      *
  102.      * @ORM\JoinColumn(name="location_id", referencedColumnName="id", nullable=true)
  103.      *
  104.      * @Serializer\Groups({"Discussion"})
  105.      *
  106.      * @Serializer\Expose
  107.      */
  108.     protected $location;
  109.     /**
  110.      * @ORM\ManyToOne(targetEntity="Category", inversedBy="discussions")
  111.      *
  112.      * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
  113.      *
  114.      * @Serializer\Groups({"Discussion"})
  115.      *
  116.      * @Serializer\Expose
  117.      */
  118.     protected $category;
  119.     /**
  120.      * @ORM\OneToMany(targetEntity="Comment",
  121.      * mappedBy="discussion", fetch="EXTRA_LAZY")
  122.      *
  123.      * @Serializer\Groups({"Comment"})
  124.      *
  125.      * @Serializer\Expose
  126.      */
  127.     protected $comments;
  128.     /**
  129.      * @var int
  130.      *
  131.      * @ORM\Column(name="comments_counter", type="integer", options={"default" = 0})
  132.      *
  133.      * @Serializer\Groups({"Discussion", "Comment"})
  134.      *
  135.      * @Serializer\Expose
  136.      */
  137.     private $commentsCounter 0;
  138.     /**
  139.      * @ORM\Column(name="deleted_at", type="datetime", nullable=true)
  140.      */
  141.     private $deletedAt;
  142.     /**
  143.      * @var int
  144.      *
  145.      * @ORM\Column(name="severity", type="smallint", nullable=true, options={"default" = 0})
  146.      */
  147.     private $severity;
  148.     /**
  149.      * @ORM\Column(name="subscribers_notified",type="boolean",nullable=true, options={"default" = 0})
  150.      */
  151.     private $subscribersNotified;
  152.     /**
  153.      * @ORM\Column(name="reach_count",type="integer", nullable=true, options={"default" = 0})
  154.      */
  155.     private $reachCount 0;
  156.     /**
  157.      * Constructor.
  158.      */
  159.     public function __construct()
  160.     {
  161.         $this->severity 0;
  162.         $this->comments = new ArrayCollection();
  163.     }
  164.     /**
  165.      * @return self
  166.      */
  167.     public function setId(int $id)
  168.     {
  169.         $this->id $id;
  170.         return $this;
  171.     }
  172.     /**
  173.      * Get id.
  174.      *
  175.      * @return int
  176.      */
  177.     public function getId()
  178.     {
  179.         return $this->id;
  180.     }
  181.     /**
  182.      * @ORM\PrePersist
  183.      */
  184.     public function onPrePersist(): void
  185.     {
  186.         if (!$this->getCreatedAt()) {
  187.             $this->setCreatedAt(new \DateTime());
  188.         } else {
  189.             $this->setUpdatedAt(new \DateTime());
  190.         }
  191.     }
  192.     /**
  193.      * Set createdAt.
  194.      *
  195.      * @param \DateTime $createdAt
  196.      *
  197.      * @return Discussion
  198.      */
  199.     public function setCreatedAt($createdAt)
  200.     {
  201.         $this->createdAt $createdAt;
  202.         return $this;
  203.     }
  204.     /**
  205.      * Get createdAt.
  206.      *
  207.      * @return \DateTime
  208.      */
  209.     public function getCreatedAt()
  210.     {
  211.         return $this->createdAt;
  212.     }
  213.     /**
  214.      * Set user.
  215.      *
  216.      * @return Discussion
  217.      */
  218.     public function setUser(?User $user null)
  219.     {
  220.         $this->user $user;
  221.         return $this;
  222.     }
  223.     /**
  224.      * Get user.
  225.      *
  226.      * @return User
  227.      */
  228.     public function getUser()
  229.     {
  230.         return $this->user;
  231.     }
  232.     /**
  233.      * Set content.
  234.      *
  235.      * @param string $content
  236.      *
  237.      * @return Discussion
  238.      */
  239.     public function setContent($content)
  240.     {
  241.         $this->content $content;
  242.         return $this;
  243.     }
  244.     /**
  245.      * Get content.
  246.      *
  247.      * @return string
  248.      */
  249.     public function getContent()
  250.     {
  251.         return $this->content;
  252.     }
  253.     /**
  254.      * Set status.
  255.      *
  256.      * @param int $status
  257.      *
  258.      * @return Discussion
  259.      */
  260.     public function setStatus($status)
  261.     {
  262.         $this->status $status;
  263.         return $this;
  264.     }
  265.     /**
  266.      * Get status.
  267.      *
  268.      * @return int
  269.      */
  270.     public function getStatus()
  271.     {
  272.         return $this->status;
  273.     }
  274.     /**
  275.      * Set location.
  276.      *
  277.      * @return Discussion
  278.      */
  279.     public function setLocation(?Location $location null)
  280.     {
  281.         $this->location $location;
  282.         return $this;
  283.     }
  284.     /**
  285.      * Get location.
  286.      *
  287.      * @return Location
  288.      */
  289.     public function getLocation()
  290.     {
  291.         return $this->location;
  292.     }
  293.     public function getStatusLabel()
  294.     {
  295.         return DiscussionStatus::getLabel($this->getStatus());
  296.     }
  297.     /**
  298.      * Add comment.
  299.      *
  300.      * @return Discussion
  301.      */
  302.     public function addComment(Comment $comment)
  303.     {
  304.         $this->comments[] = $comment;
  305.         return $this;
  306.     }
  307.     /**
  308.      * Remove comment.
  309.      */
  310.     public function removeComment(Comment $comment): void
  311.     {
  312.         $this->comments->removeElement($comment);
  313.     }
  314.     /**
  315.      * Get comments.
  316.      *
  317.      * @return \Doctrine\Common\Collections\Collection
  318.      */
  319.     public function getComments()
  320.     {
  321.         $criteria Criteria::create()
  322.             ->where(Criteria::expr()->eq('status'CommentStatus::APPROVED))
  323.         ;
  324.         return $this->comments->matching($criteria);
  325.     }
  326.     /**
  327.      * Get all comments.
  328.      *
  329.      * @return \Doctrine\Common\Collections\Collection
  330.      */
  331.     public function getAllComments()
  332.     {
  333.         return $this->comments;
  334.     }
  335.     /**
  336.      * Add subscriber.
  337.      *
  338.      * @return Discussion
  339.      */
  340.     public function addSubscriber(User $subscriber)
  341.     {
  342.         $this->subscribers[] = $subscriber;
  343.         return $this;
  344.     }
  345.     /**
  346.      * Remove subscriber.
  347.      */
  348.     public function removeSubscriber(User $subscriber): void
  349.     {
  350.         $this->subscribers->removeElement($subscriber);
  351.     }
  352.     /**
  353.      * Get subscribers.
  354.      *
  355.      * @return \Doctrine\Common\Collections\Collection
  356.      */
  357.     public function getSubscribers()
  358.     {
  359.         return $this->subscribers;
  360.     }
  361.     /**
  362.      * Set commentsCounter.
  363.      *
  364.      * @param int $commentsCounter
  365.      *
  366.      * @return Discussion
  367.      */
  368.     public function setCommentsCounter($commentsCounter)
  369.     {
  370.         $this->commentsCounter $commentsCounter;
  371.         return $this;
  372.     }
  373.     /**
  374.      * Get commentsCounter.
  375.      *
  376.      * @return int
  377.      */
  378.     public function getCommentsCounter()
  379.     {
  380.         return $this->commentsCounter;
  381.     }
  382.     public function getSubscribersNotified()
  383.     {
  384.         return $this->subscribersNotified;
  385.     }
  386.     public function setSubscribersNotified($subscribersNotified): void
  387.     {
  388.         $this->subscribersNotified $subscribersNotified;
  389.     }
  390.     public function getCategory()
  391.     {
  392.         return $this->category;
  393.     }
  394.     public function setCategory($category): void
  395.     {
  396.         $this->category $category;
  397.     }
  398.     public function getUpdatedAt()
  399.     {
  400.         return $this->updatedAt;
  401.     }
  402.     public function setUpdatedAt($updatedAt): void
  403.     {
  404.         $this->updatedAt $updatedAt;
  405.     }
  406.     public function getDeletedAt()
  407.     {
  408.         return $this->deletedAt;
  409.     }
  410.     public function setDeletedAt($deletedAt): void
  411.     {
  412.         $this->deletedAt $deletedAt;
  413.     }
  414.     /**
  415.      * @return int
  416.      */
  417.     public function getSeverity()
  418.     {
  419.         return $this->severity;
  420.     }
  421.     public function setSeverity(int $severity): void
  422.     {
  423.         $this->severity $severity;
  424.     }
  425.     public function getReachCount()
  426.     {
  427.         return $this->reachCount;
  428.     }
  429.     /**
  430.      * @param mixed $reachCount
  431.      */
  432.     public function setReachCount(int $reachCount 0): void
  433.     {
  434.         $this->reachCount $reachCount;
  435.     }
  436. }