<?php
namespace App\Entity;
use App\Enum\TelegramCampaign\Status;
use App\Enum\TelegramCampaign\Type;
use App\Enum\TelegramCampaignMessage\Insert;
use App\Repository\TelegramCampaignRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=TelegramCampaignRepository::class)
*/
class TelegramCampaign
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="enum", nullable=true, options={"values": "telegram_campaign_enum"})
*/
private $type;
/**
* @ORM\ManyToOne(targetEntity=CalendarEvent::class)
*/
private $calendarEvent;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $title;
/**
* @ORM\OneToMany(targetEntity=TelegramCampaignParticipant::class, mappedBy="telegramCampaign")
*/
private $participants;
/**
* @ORM\Column(type="enum", options={"values"="telegram_campaign_enum", "default"="active"})
*/
private $status = Status::STATUS_ACTIVE;
/**
* @ORM\OneToMany(targetEntity=TelegramCampaignMessage::class, mappedBy="telegramCampaign")
*/
private $messages;
/**
* Должен быть явно указан.
* @ORM\Column(type="boolean")
*/
private $isAutoCampaignMessages;
public function __construct()
{
$this->participants = new ArrayCollection();
$this->messages = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getType()
{
return $this->type;
}
public function setType($type): self
{
$this->type = $type;
return $this;
}
public function getTypeText(): string
{
if ($this->type) {
return Type::getText($this->type);
}
return 'Информационная рассылка';
}
public function getCalendarEvent(): ?CalendarEvent
{
return $this->calendarEvent;
}
public function setCalendarEvent(?CalendarEvent $calendarEvent): self
{
$this->calendarEvent = $calendarEvent;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
/**
* @return Collection<int, TelegramCampaignParticipant>
*/
public function getParticipants(): Collection
{
return $this->participants;
}
public function addParticipant(TelegramCampaignParticipant $participant): self
{
if (!$this->participants->contains($participant)) {
$this->participants[] = $participant;
$participant->setTelegramCampaign($this);
}
return $this;
}
public function removeParticipant(TelegramCampaignParticipant $participant): self
{
if ($this->participants->removeElement($participant)) {
// set the owning side to null (unless already changed)
if ($participant->getTelegramCampaign() === $this) {
$participant->setTelegramCampaign(null);
}
}
return $this;
}
public function hasParticipantCandidate(Candidate $candidate): bool
{
foreach ($this->getParticipants() as $participant) {
if ($participant->getCandidate()->getId() === $candidate->getId()) {
return true;
}
}
return false;
}
public function hasParticipantPerson(Person $person): bool
{
foreach ($this->getParticipants() as $participant) {
if (($participant->getCandidate() !== null && $participant->getCandidate()->getPerson()->getId() == $person->getId())
|| $participant->getPerson()->getId() == $person->getId()) {
return true;
}
}
return false;
}
public function getStatus()
{
return $this->status;
}
public function setStatus($status): self
{
$this->status = $status;
return $this;
}
public function isActive(): bool
{
return $this->getStatus() === Status::STATUS_ACTIVE;
}
public function isPaused(): bool
{
return $this->getStatus() === Status::STATUS_PAUSE;
}
public function isDeleted(): bool
{
return $this->getStatus() === Status::STATUS_DELETED;
}
/**
* @return Collection<int, TelegramCampaignMessage>
*/
public function getMessages(): Collection
{
return $this->messages;
}
public function addMessage(TelegramCampaignMessage $message): self
{
if (!$this->messages->contains($message)) {
$this->messages[] = $message;
$message->setTelegramCampaign($this);
}
return $this;
}
public function removeMessage(TelegramCampaignMessage $message): self
{
if ($this->messages->removeElement($message)) {
// set the owning side to null (unless already changed)
if ($message->getTelegramCampaign() === $this) {
$message->setTelegramCampaign(null);
}
}
return $this;
}
public function hasMessageWithPaymentRequisites(): bool
{
/**@var TelegramCampaignMessage $message */
foreach ($this->messages as $message) {
if ($message->isSendPaymentRequisites()) {
return true;
}
}
return false;
}
public function hasMessageWithCalendarEventPriceInsert(): bool
{
/**@var TelegramCampaignMessage $message */
foreach ($this->messages as $message) {
if (str_contains($message->getText(), "[" . Insert::INSERT_CALENDAR_EVENT_PRICE . "]")) {
return true;
}
}
return false;
}
public function isIsAutoCampaignMessages(): ?bool
{
return $this->isAutoCampaignMessages;
}
public function setIsAutoCampaignMessages(?bool $isAutoCampaignMessages): self
{
$this->isAutoCampaignMessages = $isAutoCampaignMessages;
return $this;
}
}