<?php
namespace Aqarmap\Bundle\DiscussionBundle\Entity;
use Aqarmap\Bundle\DiscussionBundle\Constant\CommentStatus;
use Aqarmap\Bundle\DiscussionBundle\Constant\DiscussionStatus;
use Aqarmap\Bundle\ListingBundle\Entity\Location;
use Aqarmap\Bundle\UserBundle\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use JMS\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Discussion.
*
* @ORM\Entity(repositoryClass="Aqarmap\Bundle\DiscussionBundle\Repository\DiscussionRepository")
*
* @ORM\Table(name="discussions")
*
* @ORM\HasLifecycleCallbacks
*
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
*
* @Serializer\ExclusionPolicy("all")
*/
class Discussion
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
*
* @ORM\Id
*
* @ORM\GeneratedValue(strategy="AUTO")
*
* @Serializer\Groups({"Discussion"})
*
* @Serializer\Expose
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="content", type="text", length=512, nullable=true)
*
* @Assert\NotBlank()
*
* @Serializer\Groups({"Discussion"})
*
* @Serializer\Expose
*/
private $content;
/**
* @var int
*
* @ORM\Column(name="status", type="smallint", nullable=true, options={"default" = 1})
*/
private $status = DiscussionStatus::PENDING;
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime", nullable=true)
*
* @Serializer\Groups({"Discussion"})
*
* @Serializer\Expose
*/
private $createdAt;
/**
* @var \DateTime
*
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*
* @Serializer\Groups({"Discussion"})
*
* @Serializer\Expose
*/
private $updatedAt;
/**
* @ORM\ManyToOne(targetEntity="\Aqarmap\Bundle\UserBundle\Entity\User", inversedBy="discussions")
*
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*
* @Serializer\Groups({"Discussion"})
*
* @Serializer\Expose
*/
protected $user;
/**
* @ORM\ManyToMany(targetEntity="\Aqarmap\Bundle\UserBundle\Entity\User")
*
* @ORM\JoinTable(name="discussions_subscribers",
* joinColumns={@ORM\JoinColumn(name="discussion_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="subscriber_id", referencedColumnName="id")}
* )
*/
protected $subscribers;
/**
* @ORM\ManyToOne(targetEntity="\Aqarmap\Bundle\ListingBundle\Entity\Location", inversedBy="discussions")
*
* @ORM\JoinColumn(name="location_id", referencedColumnName="id", nullable=true)
*
* @Serializer\Groups({"Discussion"})
*
* @Serializer\Expose
*/
protected $location;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="discussions")
*
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*
* @Serializer\Groups({"Discussion"})
*
* @Serializer\Expose
*/
protected $category;
/**
* @ORM\OneToMany(targetEntity="Comment",
* mappedBy="discussion", fetch="EXTRA_LAZY")
*
* @Serializer\Groups({"Comment"})
*
* @Serializer\Expose
*/
protected $comments;
/**
* @var int
*
* @ORM\Column(name="comments_counter", type="integer", options={"default" = 0})
*
* @Serializer\Groups({"Discussion", "Comment"})
*
* @Serializer\Expose
*/
private $commentsCounter = 0;
/**
* @ORM\Column(name="deleted_at", type="datetime", nullable=true)
*/
private $deletedAt;
/**
* @var int
*
* @ORM\Column(name="severity", type="smallint", nullable=true, options={"default" = 0})
*/
private $severity;
/**
* @ORM\Column(name="subscribers_notified",type="boolean",nullable=true, options={"default" = 0})
*/
private $subscribersNotified;
/**
* @ORM\Column(name="reach_count",type="integer", nullable=true, options={"default" = 0})
*/
private $reachCount = 0;
/**
* Constructor.
*/
public function __construct()
{
$this->severity = 0;
$this->comments = new ArrayCollection();
}
/**
* @return self
*/
public function setId(int $id)
{
$this->id = $id;
return $this;
}
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @ORM\PrePersist
*/
public function onPrePersist(): void
{
if (!$this->getCreatedAt()) {
$this->setCreatedAt(new \DateTime());
} else {
$this->setUpdatedAt(new \DateTime());
}
}
/**
* Set createdAt.
*
* @param \DateTime $createdAt
*
* @return Discussion
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt.
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set user.
*
* @return Discussion
*/
public function setUser(?User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user.
*
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* Set content.
*
* @param string $content
*
* @return Discussion
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content.
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set status.
*
* @param int $status
*
* @return Discussion
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status.
*
* @return int
*/
public function getStatus()
{
return $this->status;
}
/**
* Set location.
*
* @return Discussion
*/
public function setLocation(?Location $location = null)
{
$this->location = $location;
return $this;
}
/**
* Get location.
*
* @return Location
*/
public function getLocation()
{
return $this->location;
}
public function getStatusLabel()
{
return DiscussionStatus::getLabel($this->getStatus());
}
/**
* Add comment.
*
* @return Discussion
*/
public function addComment(Comment $comment)
{
$this->comments[] = $comment;
return $this;
}
/**
* Remove comment.
*/
public function removeComment(Comment $comment): void
{
$this->comments->removeElement($comment);
}
/**
* Get comments.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getComments()
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq('status', CommentStatus::APPROVED))
;
return $this->comments->matching($criteria);
}
/**
* Get all comments.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAllComments()
{
return $this->comments;
}
/**
* Add subscriber.
*
* @return Discussion
*/
public function addSubscriber(User $subscriber)
{
$this->subscribers[] = $subscriber;
return $this;
}
/**
* Remove subscriber.
*/
public function removeSubscriber(User $subscriber): void
{
$this->subscribers->removeElement($subscriber);
}
/**
* Get subscribers.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getSubscribers()
{
return $this->subscribers;
}
/**
* Set commentsCounter.
*
* @param int $commentsCounter
*
* @return Discussion
*/
public function setCommentsCounter($commentsCounter)
{
$this->commentsCounter = $commentsCounter;
return $this;
}
/**
* Get commentsCounter.
*
* @return int
*/
public function getCommentsCounter()
{
return $this->commentsCounter;
}
public function getSubscribersNotified()
{
return $this->subscribersNotified;
}
public function setSubscribersNotified($subscribersNotified): void
{
$this->subscribersNotified = $subscribersNotified;
}
public function getCategory()
{
return $this->category;
}
public function setCategory($category): void
{
$this->category = $category;
}
public function getUpdatedAt()
{
return $this->updatedAt;
}
public function setUpdatedAt($updatedAt): void
{
$this->updatedAt = $updatedAt;
}
public function getDeletedAt()
{
return $this->deletedAt;
}
public function setDeletedAt($deletedAt): void
{
$this->deletedAt = $deletedAt;
}
/**
* @return int
*/
public function getSeverity()
{
return $this->severity;
}
public function setSeverity(int $severity): void
{
$this->severity = $severity;
}
public function getReachCount()
{
return $this->reachCount;
}
/**
* @param mixed $reachCount
*/
public function setReachCount(int $reachCount = 0): void
{
$this->reachCount = $reachCount;
}
}