<?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;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $startDate;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $studentLevels;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $studentStatuses;
/**
* @ORM\ManyToMany(targetEntity=CalendarEvent::class)
* @ORM\JoinTable(name="telegram_campaign__calendar_event_deny_participations",
* joinColumns={@ORM\JoinColumn(name="calendar_event_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="deny_calendar_event_id", referencedColumnName="id")}
* )
*/
private $denyCalendarEventsParticipations;
// /**
// * @ORM\ManyToMany(targetEntity=CalendarEvent::class)
// * @ORM\JoinTable(name="telegram_campaign__calendar_events",
// * joinColumns={@ORM\JoinColumn(name="calendar_event_id", referencedColumnName="id")},
// * inverseJoinColumns={@ORM\JoinColumn(name="calendar_event_id", referencedColumnName="id")}
// * )
// */
// private $calendarEvents;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $candidatesStatuses;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $payedOnly;
public function __construct()
{
$this->participants = new ArrayCollection();
$this->messages = new ArrayCollection();
$this->denyCalendarEventsParticipations = new ArrayCollection();
// $this->calendarEvents = new ArrayCollection();
}
public function __toString()
{
return $this->getTitleText() ?? "Рассылка #{$this->getId()}";
}
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 isInformationalType(): bool
{
return $this->getType() === null || $this->getType() == Type::TYPE_INFORMATIONAL;
}
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 getTitleText(): ?string
{
if ($this->getType() && $this->getCalendarEvent()) {
switch ($this->getType()) {
case Type::TYPE_APPROVED_CANDIDATES:
return "✔️ Прошедшие отбор на " . $this->getCalendarEvent()->getOnNameText();
case Type::TYPE_DECLINED_CANDIDATES:
return "❎ Не прошедшие отбор на " . $this->getCalendarEvent()->getOnNameText();
case Type::TYPE_REGISTERED_CANDIDATES:
return "✍️ Зарегистрированные на " . $this->getCalendarEvent()->getOnNameText();
case Type::TYPE_CALENDAR_EVENT_TELEGRAM_CHANNEL_LINK:
return "🔗 Ссылка на закрытый Telegram-канал на " . $this->getCalendarEvent()->getOnNameText();
case Type::TYPE_ZOOM:
return "🔵 Приглашение в Zoom для события: " . $this->getCalendarEvent()->getText();
}
} elseif ($this->getType() === null) {
return "ℹ️ Информационная рассылка «"
. ($this->getCalendarEvent() ? $this->getCalendarEvent()->getText() : $this->getTitle()) . "»"
. ($this->getCalendarEvent() && $this->getTitle() ? ": " . $this->getTitle() : "");
}
return $this->getTitle();
}
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->getPerson() && $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;
}
public function hasPaymentRequisiteMessages(): bool
{
/**@var TelegramCampaignMessage $message */
foreach ($this->messages as $message) {
if ($message->isSendPaymentRequisites()) {
return true;
}
}
return false;
}
public function getStartDate(): ?\DateTimeInterface
{
return $this->startDate;
}
public function setStartDate(?\DateTimeInterface $startDate): self
{
$this->startDate = $startDate;
return $this;
}
public function getStudentLevels($asInt = false): ?array
{
$result = $this->studentLevels ? json_decode($this->studentLevels, true) : null;
if ($asInt && $result) {
return array_map(function ($item) {
return (int)$item;
}, $result);
}
return $result;
}
public function setStudentLevels(?array $studentLevels): self
{
if ($studentLevels === null) {
$this->studentLevels = null;
return $this;
}
sort($studentLevels);
$this->studentLevels = json_encode($studentLevels,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return $this;
}
public function getFromStudentLevel(): ?int
{
$levels = $this->getStudentLevels(true);
if ($levels) {
return min($levels);
}
return null;
}
public function getToStudentLevel(): ?int
{
$levels = $this->getStudentLevels(true);
if ($levels) {
return max($levels);
}
return null;
}
public function getStudentStatuses(): ?array
{
return $this->studentStatuses ? json_decode($this->studentStatuses, true) : null;
}
public function setStudentStatuses(?array $studentStatuses): self
{
if ($studentStatuses === null) {
$this->studentStatuses = null;
return $this;
}
sort($studentStatuses);
$this->studentStatuses = json_encode($studentStatuses,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return $this;
}
public function getCandidatesStatuses(): ?array
{
return $this->candidatesStatuses ? json_decode($this->candidatesStatuses, true) : null;
}
public function setCandidatesStatuses(?array $candidatesStatuses): self
{
if ($candidatesStatuses === null) {
$this->candidatesStatuses = null;
return $this;
}
sort($candidatesStatuses);
$this->candidatesStatuses = json_encode($candidatesStatuses,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return $this;
}
/**
* @return Collection<int, self>
*/
public function getDenyCalendarEventsParticipations(): Collection
{
return $this->denyCalendarEventsParticipations;
}
public function addDenyCalendarEventsParticipation(self $denyCalendarEventsParticipation): self
{
if (!$this->denyCalendarEventsParticipations->contains($denyCalendarEventsParticipation)) {
$this->denyCalendarEventsParticipations[] = $denyCalendarEventsParticipation;
}
return $this;
}
public function removeDenyCalendarEventsParticipation(self $denyCalendarEventsParticipation): self
{
$this->denyCalendarEventsParticipations->removeElement($denyCalendarEventsParticipation);
return $this;
}
public function getDenyCalendarEventsParticipationsText(): string
{
$texts = [];
foreach ($this->getDenyCalendarEventsParticipations() as $event) {
$texts[] = mb_lcfirst($event->getNameText(), 'UTF-8');
}
return implode(", ", $texts);
}
// /**
// * @return Collection<int, CalendarEvent>
// */
// public function getCalendarEvents(): Collection
// {
// return $this->calendarEvents;
// }
//
// public function addCalendarEvent(CalendarEvent $calendarEvent): self
// {
// if (!$this->calendarEvents->contains($calendarEvent)) {
// $this->calendarEvents[] = $calendarEvent;
// }
//
// return $this;
// }
//
// public function removeCalendarEvent(CalendarEvent $calendarEvent): self
// {
// $this->calendarEvents->removeElement($calendarEvent);
//
// return $this;
// }
public function isPayedOnly(): ?bool
{
return $this->payedOnly;
}
public function setPayedOnly(?bool $payedOnly): self
{
$this->payedOnly = $payedOnly;
return $this;
}
public function isForParticipantsUpdateType(): bool
{
return Type::isForParticipantsUpdateType($this->getType());
}
public function isTypeDeclinedCandidates(): bool
{
return $this->getType() === Type::TYPE_DECLINED_CANDIDATES;
}
public function isTypeApprovedCandidates(): bool
{
return $this->getType() === Type::TYPE_APPROVED_CANDIDATES;
}
public function isTypeRegisteredCandidates(): bool
{
return $this->getType() === Type::TYPE_REGISTERED_CANDIDATES;
}
public function isTypeCalendarEventTelegramChannelLink(): bool
{
return $this->getType() === Type::TYPE_CALENDAR_EVENT_TELEGRAM_CHANNEL_LINK;
}
public function isTypeZoom(): bool
{
return $this->getType() === Type::TYPE_ZOOM;
}
public function isTypeInformational(): bool
{
return $this->getType() === Type::TYPE_INFORMATIONAL;
}
}