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.     public function __construct()
  71.     {
  72.         $this->participants = new ArrayCollection();
  73.         $this->messages = new ArrayCollection();
  74.         $this->denyCalendarEventsParticipations = new ArrayCollection();
  75.     }
  76.     public function getId(): ?int
  77.     {
  78.         return $this->id;
  79.     }
  80.     public function getType()
  81.     {
  82.         return $this->type;
  83.     }
  84.     public function setType($type): self
  85.     {
  86.         $this->type $type;
  87.         return $this;
  88.     }
  89.     public function getTypeText(): string
  90.     {
  91.         if ($this->type) {
  92.             return Type::getText($this->type);
  93.         }
  94.         return 'Информационная рассылка';
  95.     }
  96.     public function getCalendarEvent(): ?CalendarEvent
  97.     {
  98.         return $this->calendarEvent;
  99.     }
  100.     public function setCalendarEvent(?CalendarEvent $calendarEvent): self
  101.     {
  102.         $this->calendarEvent $calendarEvent;
  103.         return $this;
  104.     }
  105.     public function getTitle(): ?string
  106.     {
  107.         return $this->title;
  108.     }
  109.     public function setTitle(?string $title): self
  110.     {
  111.         $this->title $title;
  112.         return $this;
  113.     }
  114.     /**
  115.      * @return Collection<int, TelegramCampaignParticipant>
  116.      */
  117.     public function getParticipants(): Collection
  118.     {
  119.         return $this->participants;
  120.     }
  121.     public function addParticipant(TelegramCampaignParticipant $participant): self
  122.     {
  123.         if (!$this->participants->contains($participant)) {
  124.             $this->participants[] = $participant;
  125.             $participant->setTelegramCampaign($this);
  126.         }
  127.         return $this;
  128.     }
  129.     public function removeParticipant(TelegramCampaignParticipant $participant): self
  130.     {
  131.         if ($this->participants->removeElement($participant)) {
  132.             // set the owning side to null (unless already changed)
  133.             if ($participant->getTelegramCampaign() === $this) {
  134.                 $participant->setTelegramCampaign(null);
  135.             }
  136.         }
  137.         return $this;
  138.     }
  139.     public function hasParticipantCandidate(Candidate $candidate): bool
  140.     {
  141.         foreach ($this->getParticipants() as $participant) {
  142.             if ($participant->getCandidate()->getId() === $candidate->getId()) {
  143.                 return true;
  144.             }
  145.         }
  146.         return false;
  147.     }
  148.     public function hasParticipantPerson(Person $person): bool
  149.     {
  150.         foreach ($this->getParticipants() as $participant) {
  151.             if (($participant->getCandidate() !== null && $participant->getCandidate()->getPerson()->getId() == $person->getId())
  152.                 || $participant->getPerson()->getId() == $person->getId()) {
  153.                 return true;
  154.             }
  155.         }
  156.         return false;
  157.     }
  158.     public function getStatus()
  159.     {
  160.         return $this->status;
  161.     }
  162.     public function setStatus($status): self
  163.     {
  164.         $this->status $status;
  165.         return $this;
  166.     }
  167.     public function isActive(): bool
  168.     {
  169.         return $this->getStatus() === Status::STATUS_ACTIVE;
  170.     }
  171.     public function isPaused(): bool
  172.     {
  173.         return $this->getStatus() === Status::STATUS_PAUSE;
  174.     }
  175.     public function isDeleted(): bool
  176.     {
  177.         return $this->getStatus() === Status::STATUS_DELETED;
  178.     }
  179.     /**
  180.      * @return Collection<int, TelegramCampaignMessage>
  181.      */
  182.     public function getMessages(): Collection
  183.     {
  184.         return $this->messages;
  185.     }
  186.     public function addMessage(TelegramCampaignMessage $message): self
  187.     {
  188.         if (!$this->messages->contains($message)) {
  189.             $this->messages[] = $message;
  190.             $message->setTelegramCampaign($this);
  191.         }
  192.         return $this;
  193.     }
  194.     public function removeMessage(TelegramCampaignMessage $message): self
  195.     {
  196.         if ($this->messages->removeElement($message)) {
  197.             // set the owning side to null (unless already changed)
  198.             if ($message->getTelegramCampaign() === $this) {
  199.                 $message->setTelegramCampaign(null);
  200.             }
  201.         }
  202.         return $this;
  203.     }
  204.     public function hasMessageWithPaymentRequisites(): bool
  205.     {
  206.         /**@var TelegramCampaignMessage $message */
  207.         foreach ($this->messages as $message) {
  208.             if ($message->isSendPaymentRequisites()) {
  209.                 return true;
  210.             }
  211.         }
  212.         return false;
  213.     }
  214.     public function hasMessageWithCalendarEventPriceInsert(): bool
  215.     {
  216.         /**@var TelegramCampaignMessage $message */
  217.         foreach ($this->messages as $message) {
  218.             if (str_contains($message->getText(), "[" Insert::INSERT_CALENDAR_EVENT_PRICE "]")) {
  219.                 return true;
  220.             }
  221.         }
  222.         return false;
  223.     }
  224.     public function isIsAutoCampaignMessages(): ?bool
  225.     {
  226.         return $this->isAutoCampaignMessages;
  227.     }
  228.     public function setIsAutoCampaignMessages(?bool $isAutoCampaignMessages): self
  229.     {
  230.         $this->isAutoCampaignMessages $isAutoCampaignMessages;
  231.         return $this;
  232.     }
  233.     public function hasPaymentRequisiteMessages(): bool
  234.     {
  235.         /**@var TelegramCampaignMessage $message */
  236.         foreach ($this->messages as $message) {
  237.             if ($message->isSendPaymentRequisites()) {
  238.                 return true;
  239.             }
  240.         }
  241.         return false;
  242.     }
  243.     public function getStartDate(): ?\DateTimeInterface
  244.     {
  245.         return $this->startDate;
  246.     }
  247.     public function setStartDate(?\DateTimeInterface $startDate): self
  248.     {
  249.         $this->startDate $startDate;
  250.         return $this;
  251.     }
  252.     public function getStudentLevels($asInt false): ?array
  253.     {
  254.         $result $this->studentLevels json_decode($this->studentLevelstrue) : null;
  255.         if ($asInt && $result) {
  256.             return array_map(function ($item) {
  257.                 return (int)$item;
  258.             }, $result);
  259.         }
  260.         return $result;
  261.     }
  262.     public function setStudentLevels(?array $studentLevels): self
  263.     {
  264.         if ($studentLevels === null) {
  265.             $this->studentLevels null;
  266.             return $this;
  267.         }
  268.         sort($studentLevels);
  269.         $this->studentLevels json_encode($studentLevels,
  270.             JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES);
  271.         return $this;
  272.     }
  273.     public function getFromStudentLevel(): ?int
  274.     {
  275.         $levels $this->getStudentLevels(true);
  276.         if ($levels) {
  277.             return min($levels);
  278.         }
  279.         return null;
  280.     }
  281.     public function getToStudentLevel(): ?int
  282.     {
  283.         $levels $this->getStudentLevels(true);
  284.         if ($levels) {
  285.             return max($levels);
  286.         }
  287.         return null;
  288.     }
  289.     public function getStudentStatuses(): ?array
  290.     {
  291.         return $this->studentStatuses json_decode($this->studentStatusestrue) : null;
  292.     }
  293.     public function setStudentStatuses(?array $studentStatuses): self
  294.     {
  295.         if ($studentStatuses === null) {
  296.             $this->studentStatuses null;
  297.             return $this;
  298.         }
  299.         sort($studentStatuses);
  300.         $this->studentStatuses json_encode($studentStatuses,
  301.             JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES);
  302.         return $this;
  303.     }
  304.     /**
  305.      * @return Collection<int, self>
  306.      */
  307.     public function getDenyCalendarEventsParticipations(): Collection
  308.     {
  309.         return $this->denyCalendarEventsParticipations;
  310.     }
  311.     public function addDenyCalendarEventsParticipation(self $denyCalendarEventsParticipation): self
  312.     {
  313.         if (!$this->denyCalendarEventsParticipations->contains($denyCalendarEventsParticipation)) {
  314.             $this->denyCalendarEventsParticipations[] = $denyCalendarEventsParticipation;
  315.         }
  316.         return $this;
  317.     }
  318.     public function removeDenyCalendarEventsParticipation(self $denyCalendarEventsParticipation): self
  319.     {
  320.         $this->denyCalendarEventsParticipations->removeElement($denyCalendarEventsParticipation);
  321.         return $this;
  322.     }
  323.     public function getDenyCalendarEventsParticipationsText(): string
  324.     {
  325.         $texts = [];
  326.         foreach ($this->getDenyCalendarEventsParticipations() as $event) {
  327.             $texts[] = mb_lcfirst($event->getNameText(), 'UTF-8');
  328.         }
  329.         return implode(", "$texts);
  330.     }
  331. }