src/Entity/TelegramCampaign.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Enum\TelegramCampaign\Status;
  4. use App\Enum\TelegramCampaign\Type;
  5. use App\Enum\TelegramCampaignMessage\Insert;
  6. use App\Repository\TelegramCampaignRepository;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. /**
  11.  * @ORM\Entity(repositoryClass=TelegramCampaignRepository::class)
  12.  */
  13. class TelegramCampaign
  14. {
  15.     /**
  16.      * @ORM\Id
  17.      * @ORM\GeneratedValue
  18.      * @ORM\Column(type="integer")
  19.      */
  20.     private $id;
  21.     /**
  22.      * @ORM\Column(type="enum", nullable=true, options={"values": "telegram_campaign_enum"})
  23.      */
  24.     private $type;
  25.     /**
  26.      * @ORM\ManyToOne(targetEntity=CalendarEvent::class)
  27.      */
  28.     private $calendarEvent;
  29.     /**
  30.      * @ORM\Column(type="string", length=255, nullable=true)
  31.      */
  32.     private $title;
  33.     /**
  34.      * @ORM\OneToMany(targetEntity=TelegramCampaignParticipant::class, mappedBy="telegramCampaign")
  35.      */
  36.     private $participants;
  37.     /**
  38.      * @ORM\Column(type="enum", options={"values"="telegram_campaign_enum", "default"="active"})
  39.      */
  40.     private $status Status::STATUS_ACTIVE;
  41.     /**
  42.      * @ORM\OneToMany(targetEntity=TelegramCampaignMessage::class, mappedBy="telegramCampaign")
  43.      */
  44.     private $messages;
  45.     /**
  46.      * Должен быть явно указан.
  47.      * @ORM\Column(type="boolean")
  48.      */
  49.     private $isAutoCampaignMessages;
  50.     /**
  51.      * @ORM\Column(type="datetime", nullable=true)
  52.      */
  53.     private $startDate;
  54.     /**
  55.      * @ORM\Column(type="text", nullable=true)
  56.      */
  57.     private $studentLevels;
  58.     /**
  59.      * @ORM\Column(type="text", nullable=true)
  60.      */
  61.     private $studentStatuses;
  62.     /**
  63.      * @ORM\ManyToMany(targetEntity=CalendarEvent::class)
  64.      * @ORM\JoinTable(name="telegram_campaign__calendar_event_deny_participations",
  65.      *       joinColumns={@ORM\JoinColumn(name="calendar_event_id", referencedColumnName="id")},
  66.      *       inverseJoinColumns={@ORM\JoinColumn(name="deny_calendar_event_id", referencedColumnName="id")}
  67.      *  )
  68.      */
  69.     private $denyCalendarEventsParticipations;
  70. //    /**
  71. //     * @ORM\ManyToMany(targetEntity=CalendarEvent::class)
  72. //     * @ORM\JoinTable(name="telegram_campaign__calendar_events",
  73. //     *       joinColumns={@ORM\JoinColumn(name="calendar_event_id", referencedColumnName="id")},
  74. //     *       inverseJoinColumns={@ORM\JoinColumn(name="calendar_event_id", referencedColumnName="id")}
  75. //     *  )
  76. //     */
  77. //    private $calendarEvents;
  78.     /**
  79.      * @ORM\Column(type="text", nullable=true)
  80.      */
  81.     private $candidatesStatuses;
  82.     /**
  83.      * @ORM\Column(type="boolean", nullable=true)
  84.      */
  85.     private $payedOnly;
  86.     public function __construct()
  87.     {
  88.         $this->participants = new ArrayCollection();
  89.         $this->messages = new ArrayCollection();
  90.         $this->denyCalendarEventsParticipations = new ArrayCollection();
  91. //        $this->calendarEvents = new ArrayCollection();
  92.     }
  93.     public function __toString()
  94.     {
  95.         return $this->getTitleText() ?? "Рассылка #{$this->getId()}";
  96.     }
  97.     public function getId(): ?int
  98.     {
  99.         return $this->id;
  100.     }
  101.     public function getType()
  102.     {
  103.         return $this->type;
  104.     }
  105.     public function setType($type): self
  106.     {
  107.         $this->type $type;
  108.         return $this;
  109.     }
  110.     public function isInformationalType(): bool
  111.     {
  112.         return $this->getType() === null || $this->getType() == Type::TYPE_INFORMATIONAL;
  113.     }
  114.     public function getTypeText(): string
  115.     {
  116.         if ($this->type) {
  117.             return Type::getText($this->type);
  118.         }
  119.         return 'Информационная рассылка';
  120.     }
  121.     public function getCalendarEvent(): ?CalendarEvent
  122.     {
  123.         return $this->calendarEvent;
  124.     }
  125.     public function setCalendarEvent(?CalendarEvent $calendarEvent): self
  126.     {
  127.         $this->calendarEvent $calendarEvent;
  128.         return $this;
  129.     }
  130.     public function getTitle(): ?string
  131.     {
  132.         return $this->title;
  133.     }
  134.     public function getTitleText(): ?string
  135.     {
  136.         if ($this->getType() && $this->getCalendarEvent()) {
  137.             switch ($this->getType()) {
  138.                 case Type::TYPE_APPROVED_CANDIDATES:
  139.                     return "✔️ Прошедшие отбор на " $this->getCalendarEvent()->getOnNameText();
  140.                 case Type::TYPE_DECLINED_CANDIDATES:
  141.                     return "❎ Не прошедшие отбор на " $this->getCalendarEvent()->getOnNameText();
  142.                 case Type::TYPE_REGISTERED_CANDIDATES:
  143.                     return "✍️ Зарегистрированные на " $this->getCalendarEvent()->getOnNameText();
  144.                 case Type::TYPE_CALENDAR_EVENT_TELEGRAM_CHANNEL_LINK:
  145.                     return "🔗 Ссылка на закрытый Telegram-канал на " $this->getCalendarEvent()->getOnNameText();
  146.                 case Type::TYPE_ZOOM:
  147.                     return "🔵 Приглашение в Zoom для события: " $this->getCalendarEvent()->getText();
  148.             }
  149.         } elseif ($this->getType() === null) {
  150.             return "ℹ️ Информационная рассылка «"
  151.                 . ($this->getCalendarEvent() ? $this->getCalendarEvent()->getText() : $this->getTitle()) . "»"
  152.                     . ($this->getCalendarEvent() && $this->getTitle() ? ": " $this->getTitle() : "");
  153.         }
  154.         return $this->getTitle();
  155.     }
  156.     public function setTitle(?string $title): self
  157.     {
  158.         $this->title $title;
  159.         return $this;
  160.     }
  161.     /**
  162.      * @return Collection<int, TelegramCampaignParticipant>
  163.      */
  164.     public function getParticipants(): Collection
  165.     {
  166.         return $this->participants;
  167.     }
  168.     public function addParticipant(TelegramCampaignParticipant $participant): self
  169.     {
  170.         if (!$this->participants->contains($participant)) {
  171.             $this->participants[] = $participant;
  172.             $participant->setTelegramCampaign($this);
  173.         }
  174.         return $this;
  175.     }
  176.     public function removeParticipant(TelegramCampaignParticipant $participant): self
  177.     {
  178.         if ($this->participants->removeElement($participant)) {
  179.             // set the owning side to null (unless already changed)
  180.             if ($participant->getTelegramCampaign() === $this) {
  181.                 $participant->setTelegramCampaign(null);
  182.             }
  183.         }
  184.         return $this;
  185.     }
  186.     public function hasParticipantCandidate(Candidate $candidate): bool
  187.     {
  188.         foreach ($this->getParticipants() as $participant) {
  189.             if ($participant->getCandidate()->getId() === $candidate->getId()) {
  190.                 return true;
  191.             }
  192.         }
  193.         return false;
  194.     }
  195.     public function hasParticipantPerson(Person $person): bool
  196.     {
  197.         foreach ($this->getParticipants() as $participant) {
  198.             if ($participant->getPerson() && $participant->getPerson()->getId() == $person->getId()) {
  199.                 return true;
  200.             }
  201.         }
  202.         return false;
  203.     }
  204.     public function getStatus()
  205.     {
  206.         return $this->status;
  207.     }
  208.     public function setStatus($status): self
  209.     {
  210.         $this->status $status;
  211.         return $this;
  212.     }
  213.     public function isActive(): bool
  214.     {
  215.         return $this->getStatus() === Status::STATUS_ACTIVE;
  216.     }
  217.     public function isPaused(): bool
  218.     {
  219.         return $this->getStatus() === Status::STATUS_PAUSE;
  220.     }
  221.     public function isDeleted(): bool
  222.     {
  223.         return $this->getStatus() === Status::STATUS_DELETED;
  224.     }
  225.     /**
  226.      * @return Collection<int, TelegramCampaignMessage>
  227.      */
  228.     public function getMessages(): Collection
  229.     {
  230.         return $this->messages;
  231.     }
  232.     public function addMessage(TelegramCampaignMessage $message): self
  233.     {
  234.         if (!$this->messages->contains($message)) {
  235.             $this->messages[] = $message;
  236.             $message->setTelegramCampaign($this);
  237.         }
  238.         return $this;
  239.     }
  240.     public function removeMessage(TelegramCampaignMessage $message): self
  241.     {
  242.         if ($this->messages->removeElement($message)) {
  243.             // set the owning side to null (unless already changed)
  244.             if ($message->getTelegramCampaign() === $this) {
  245.                 $message->setTelegramCampaign(null);
  246.             }
  247.         }
  248.         return $this;
  249.     }
  250.     public function hasMessageWithPaymentRequisites(): bool
  251.     {
  252.         /**@var TelegramCampaignMessage $message */
  253.         foreach ($this->messages as $message) {
  254.             if ($message->isSendPaymentRequisites()) {
  255.                 return true;
  256.             }
  257.         }
  258.         return false;
  259.     }
  260.     public function hasMessageWithCalendarEventPriceInsert(): bool
  261.     {
  262.         /**@var TelegramCampaignMessage $message */
  263.         foreach ($this->messages as $message) {
  264.             if (str_contains($message->getText(), "[" Insert::INSERT_CALENDAR_EVENT_PRICE "]")) {
  265.                 return true;
  266.             }
  267.         }
  268.         return false;
  269.     }
  270.     public function isIsAutoCampaignMessages(): ?bool
  271.     {
  272.         return $this->isAutoCampaignMessages;
  273.     }
  274.     public function setIsAutoCampaignMessages(?bool $isAutoCampaignMessages): self
  275.     {
  276.         $this->isAutoCampaignMessages $isAutoCampaignMessages;
  277.         return $this;
  278.     }
  279.     public function hasPaymentRequisiteMessages(): bool
  280.     {
  281.         /**@var TelegramCampaignMessage $message */
  282.         foreach ($this->messages as $message) {
  283.             if ($message->isSendPaymentRequisites()) {
  284.                 return true;
  285.             }
  286.         }
  287.         return false;
  288.     }
  289.     public function getStartDate(): ?\DateTimeInterface
  290.     {
  291.         return $this->startDate;
  292.     }
  293.     public function setStartDate(?\DateTimeInterface $startDate): self
  294.     {
  295.         $this->startDate $startDate;
  296.         return $this;
  297.     }
  298.     public function getStudentLevels($asInt false): ?array
  299.     {
  300.         $result $this->studentLevels json_decode($this->studentLevelstrue) : null;
  301.         if ($asInt && $result) {
  302.             return array_map(function ($item) {
  303.                 return (int)$item;
  304.             }, $result);
  305.         }
  306.         return $result;
  307.     }
  308.     public function setStudentLevels(?array $studentLevels): self
  309.     {
  310.         if ($studentLevels === null) {
  311.             $this->studentLevels null;
  312.             return $this;
  313.         }
  314.         sort($studentLevels);
  315.         $this->studentLevels json_encode($studentLevels,
  316.             JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES);
  317.         return $this;
  318.     }
  319.     public function getFromStudentLevel(): ?int
  320.     {
  321.         $levels $this->getStudentLevels(true);
  322.         if ($levels) {
  323.             return min($levels);
  324.         }
  325.         return null;
  326.     }
  327.     public function getToStudentLevel(): ?int
  328.     {
  329.         $levels $this->getStudentLevels(true);
  330.         if ($levels) {
  331.             return max($levels);
  332.         }
  333.         return null;
  334.     }
  335.     public function getStudentStatuses(): ?array
  336.     {
  337.         return $this->studentStatuses json_decode($this->studentStatusestrue) : null;
  338.     }
  339.     public function setStudentStatuses(?array $studentStatuses): self
  340.     {
  341.         if ($studentStatuses === null) {
  342.             $this->studentStatuses null;
  343.             return $this;
  344.         }
  345.         sort($studentStatuses);
  346.         $this->studentStatuses json_encode($studentStatuses,
  347.             JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES);
  348.         return $this;
  349.     }
  350.     public function getCandidatesStatuses(): ?array
  351.     {
  352.         return $this->candidatesStatuses json_decode($this->candidatesStatusestrue) : null;
  353.     }
  354.     public function setCandidatesStatuses(?array $candidatesStatuses): self
  355.     {
  356.         if ($candidatesStatuses === null) {
  357.             $this->candidatesStatuses null;
  358.             return $this;
  359.         }
  360.         sort($candidatesStatuses);
  361.         $this->candidatesStatuses json_encode($candidatesStatuses,
  362.             JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES);
  363.         return $this;
  364.     }
  365.     /**
  366.      * @return Collection<int, self>
  367.      */
  368.     public function getDenyCalendarEventsParticipations(): Collection
  369.     {
  370.         return $this->denyCalendarEventsParticipations;
  371.     }
  372.     public function addDenyCalendarEventsParticipation(self $denyCalendarEventsParticipation): self
  373.     {
  374.         if (!$this->denyCalendarEventsParticipations->contains($denyCalendarEventsParticipation)) {
  375.             $this->denyCalendarEventsParticipations[] = $denyCalendarEventsParticipation;
  376.         }
  377.         return $this;
  378.     }
  379.     public function removeDenyCalendarEventsParticipation(self $denyCalendarEventsParticipation): self
  380.     {
  381.         $this->denyCalendarEventsParticipations->removeElement($denyCalendarEventsParticipation);
  382.         return $this;
  383.     }
  384.     public function getDenyCalendarEventsParticipationsText(): string
  385.     {
  386.         $texts = [];
  387.         foreach ($this->getDenyCalendarEventsParticipations() as $event) {
  388.             $texts[] = mb_lcfirst($event->getNameText(), 'UTF-8');
  389.         }
  390.         return implode(", "$texts);
  391.     }
  392. //    /**
  393. //     * @return Collection<int, CalendarEvent>
  394. //     */
  395. //    public function getCalendarEvents(): Collection
  396. //    {
  397. //        return $this->calendarEvents;
  398. //    }
  399. //
  400. //    public function addCalendarEvent(CalendarEvent $calendarEvent): self
  401. //    {
  402. //        if (!$this->calendarEvents->contains($calendarEvent)) {
  403. //            $this->calendarEvents[] = $calendarEvent;
  404. //        }
  405. //
  406. //        return $this;
  407. //    }
  408. //
  409. //    public function removeCalendarEvent(CalendarEvent $calendarEvent): self
  410. //    {
  411. //        $this->calendarEvents->removeElement($calendarEvent);
  412. //
  413. //        return $this;
  414. //    }
  415.     public function isPayedOnly(): ?bool
  416.     {
  417.         return $this->payedOnly;
  418.     }
  419.     public function setPayedOnly(?bool $payedOnly): self
  420.     {
  421.         $this->payedOnly $payedOnly;
  422.         return $this;
  423.     }
  424.     public function isForParticipantsUpdateType(): bool
  425.     {
  426.         return Type::isForParticipantsUpdateType($this->getType());
  427.     }
  428.     public function isTypeDeclinedCandidates(): bool
  429.     {
  430.         return $this->getType() === Type::TYPE_DECLINED_CANDIDATES;
  431.     }
  432.     public function isTypeApprovedCandidates(): bool
  433.     {
  434.         return $this->getType() === Type::TYPE_APPROVED_CANDIDATES;
  435.     }
  436.     public function isTypeRegisteredCandidates(): bool
  437.     {
  438.         return $this->getType() === Type::TYPE_REGISTERED_CANDIDATES;
  439.     }
  440.     public function isTypeCalendarEventTelegramChannelLink(): bool
  441.     {
  442.         return $this->getType() === Type::TYPE_CALENDAR_EVENT_TELEGRAM_CHANNEL_LINK;
  443.     }
  444.     public function isTypeZoom(): bool
  445.     {
  446.         return $this->getType() === Type::TYPE_ZOOM;
  447.     }
  448.     public function isTypeInformational(): bool
  449.     {
  450.         return $this->getType() === Type::TYPE_INFORMATIONAL;
  451.     }
  452. }