<?php
namespace Rawafed\ContactFormsBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Rawafed\CommonsBundle\Entity\Country;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Rawafed\EnhancedUploaderBundle\Mapping\Annotation as Uploader;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use JMS\Serializer\Annotation as Serializer;
/**
* @ORM\Entity(repositoryClass="Rawafed\ContactFormsBundle\Entity\Repository\ContactUsMessageRepository")
* @Uploader\Uploadable
*/
class ContactUsMessage
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
* @Serializer\Groups({"post"})
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
* @Assert\Email(message="contactforms.messages.bad_email", checkMX=true)
* @Serializer\Groups({"post"})
*/
private $email;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Serializer\Groups({"post"})
*/
private $mobile;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
* @Serializer\Groups({"post"})
*/
private $title;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
* @Serializer\Groups({"post"})
*/
private $message;
/**
* @ORM\Column(name="date_time", type="datetime")
* @Gedmo\Timestampable(on="create")
*/
private $dateTime;
/**
* @ORM\Column(type="EnumMessageStatus", nullable=true, options={"default" : "new"})
*/
protected $status = MessageStatus::NEW;
/**
* @ORM\OneToMany(targetEntity="ContactUsMessageReply", mappedBy="message", cascade={"persist", "remove"})
*/
protected $replies;
/**
* @var ContactUsCategory
*
* @ORM\ManyToOne(targetEntity="ContactUsCategory", inversedBy="messages")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
* })
* @Assert\NotBlank()
*/
private $category;
/**
* @ORM\ManyToOne(targetEntity="Rawafed\CommonsBundle\Entity\Country")
*/
private $country;
/**
* @Uploader\UploadableField(mapping="rawafed_upload", fileNameProperty="attachment", uploadsDirName="contactforms")
*
* @var File $attachmentFile
*/
protected $attachmentFile;
/**
* @var string
*
* @ORM\Column(name="attachment", type="string", nullable=true)
*/
private $attachment;
public function __construct()
{
$this->replies = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return ContactUsMessage
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set email
*
* @param string $email
* @return ContactUsMessage
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set mobile
*
* @param string $mobile
* @return ContactUsMessage
*/
public function setMobile($mobile)
{
$this->mobile = $mobile;
return $this;
}
/**
* Get mobile
*
* @return string
*/
public function getMobile()
{
return $this->mobile;
}
/**
* Set title
*
* @param string $title
* @return ContactUsMessage
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set message
*
* @param string $message
* @return ContactUsMessage
*/
public function setMessage($message)
{
$this->message = $message;
return $this;
}
/**
* Get message
*
* @return string
*/
public function getMessage()
{
return $this->message;
}
/**
* Set dateTime
*
* @param \DateTime $dateTime
* @return ContactUsMessage
*/
public function setDateTime($dateTime)
{
$this->dateTime = $dateTime;
return $this;
}
/**
* Get dateTime
*
* @return \DateTime
*/
public function getDateTime()
{
return $this->dateTime;
}
/**
* Set status
*
* @param string $status
* @return Account
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* @return string
*/
public function getStatus()
{
return $this->status;
}
/**
* Set category
*
* @param ContactUsCategory $category
* @return ContactUsMessage
*/
public function setCategory(ContactUsCategory $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* @return ContactUsCategory
*/
public function getCategory()
{
return $this->category;
}
/**
* Set country
*
* @param Country $country
* @return ContactUsMessage
*/
public function setCountry(Country $country = null)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* @return Country
*/
public function getCountry()
{
return $this->country;
}
/**
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $attachmentFile
* @return ContactUsMessage
*/
public function setAttachmentFile(File $attachmentFile = null)
{
$this->attachmentFile = $attachmentFile;
return $this;
}
/**
* @return \Symfony\Component\HttpFoundation\File\File
*/
public function getAttachmentFile()
{
return $this->attachmentFile;
}
/**
* @param string $attachment
* @return ContactUsMessage
*/
public function setAttachment($attachment)
{
$this->attachment = $attachment;
return $this;
}
/**
* @return string
*/
public function getAttachment()
{
return $this->attachment;
}
public function __toString()
{
return $this->getTitle();
}
/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getReplies()
{
return $this->replies;
}
/**
* Add reply
*
* @param ContactUsMessageReply $contactUsMessageReply
* @return ContactUsMessage
*/
public function addReply($messageReply)
{
$contactUsMessageReply = new ContactUsMessageReply();
$contactUsMessageReply->setReplyMessage($messageReply);
$contactUsMessageReply->setMessage($this);
if (!$this->replies->contains($contactUsMessageReply)) {
$this->replies->add($contactUsMessageReply);
// dd('dd');
}
return $this;
}
/**
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context)
{
if($this->getAttachmentFile() && !$this->getCategory()->hasAttachments()) {
$context->buildViolation('This category does not allow attachments')
->atPath('attachment')
->addViolation();
}
}
}