src/Entity/CalendarEvent.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Enum\CalendarEvent\Status;
  4. use App\Enum\CalendarEvent\Type;
  5. use App\Enum\Student\VirtualStatus;
  6. use App\Library\Utils\StringUtils;
  7. use App\Repository\CalendarEventRepository;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. /**
  12.  * @ORM\Entity(repositoryClass=CalendarEventRepository::class)
  13.  */
  14. class CalendarEvent
  15. {
  16.     /**
  17.      * @ORM\Id
  18.      * @ORM\GeneratedValue
  19.      * @ORM\Column(type="integer")
  20.      */
  21.     private $id;
  22.     /**
  23.      * @ORM\Column(type="string", length=255)
  24.      */
  25.     private $name;
  26.     /**
  27.      * @ORM\Column(type="date")
  28.      */
  29.     private $startDate;
  30.     /**
  31.      * @ORM\Column(type="date")
  32.      */
  33.     private $endDate;
  34.     /**
  35.      * @ORM\Column(type="integer", nullable=true)
  36.      */
  37.     private $price;
  38.     /**
  39.      * @ORM\Column(type="smallint", nullable=true)
  40.      */
  41.     private $fromLevel;
  42.     /**
  43.      * @ORM\Column(type="smallint", nullable=true)
  44.      */
  45.     private $earnLevel;
  46.     /**
  47.      * @ORM\Column(type="time")
  48.      */
  49.     private $startTime;
  50.     /**
  51.      * @ORM\Column(type="time")
  52.      */
  53.     private $endTime;
  54.     /**
  55.      * @ORM\Column(type="enum", options={"values"="calendar_event_enum"})
  56.      */
  57.     private $type Type::TYPE_COURSE;
  58.     /**
  59.      * @ORM\Column(type="boolean", nullable=true)
  60.      */
  61.     private $isCandidateReviewRequired true;
  62.     /**
  63.     * @ORM\ManyToOne(targetEntity=PaymentRequisites::class)
  64.     * @ORM\JoinColumn(nullable=true)
  65.      */
  66.     private $paymentRequisites;
  67.     /**
  68.      * @ORM\Column(type="string", length=255, nullable=true)
  69.      */
  70.     private $telegramChannelUrl;
  71.     /**
  72.      * @ORM\Column(type="string", length=100, nullable=true)
  73.      */
  74.     private $telegramChannelId;
  75.     /**
  76.      * @ORM\ManyToMany(targetEntity=CalendarEvent::class)
  77.      */
  78.     private $requireCalendarEventsParticipations;
  79.     /**
  80.      * @ORM\Column(type="boolean", nullable=true)
  81.      */
  82.     private $eventDatesApproximate;
  83.     /**
  84.      * @ORM\Column(type="enum", options={"values"="calendar_event_enum", "default"="active"}))
  85.      */
  86.     private $status Status::STATUS_ACTIVE;
  87.     /**
  88.      * @ORM\ManyToMany(targetEntity=CalendarEvent::class)
  89.      * @ORM\JoinTable(name="calendar_event_deny_participations",
  90.      *       joinColumns={@ORM\JoinColumn(name="calendar_event_id", referencedColumnName="id")},
  91.      *       inverseJoinColumns={@ORM\JoinColumn(name="deny_calendar_event_id", referencedColumnName="id")}
  92.      *  )
  93.      */
  94.     private $denyCalendarEventsParticipations;
  95.     /**
  96.      * @ORM\Column(type="smallint", nullable=true)
  97.      */
  98.     private $toLevel;
  99.     /**
  100.      * @ORM\Column(type="enum", nullable=true)
  101.      */
  102.     private $requireStudentStatus;
  103.     /**
  104.      * @ORM\ManyToOne(targetEntity=CalendarEvent::class, inversedBy="connectedCalendarEvents")
  105.      * @ORM\JoinColumn(nullable=true)
  106.      */
  107.     private $connectingCalendarEvent;
  108.     /**
  109.      * @ORM\OneToMany(targetEntity=CalendarEvent::class, mappedBy="connectingCalendarEvent")
  110.      */
  111.     private $connectedCalendarEvents;
  112.     /**
  113.      * @ORM\ManyToMany(targetEntity=TelegramCampaignMessage::class)
  114.      */
  115.     private $approvedCandidatesCampaignMessages;
  116.     public function __construct()
  117.     {
  118.         $this->startDate = new \DateTime();
  119.         $this->endDate = new \DateTime();
  120.         $this->startTime = new \DateTime('10:00:00');
  121.         $this->endTime = new \DateTime('13:00:00');
  122.         $this->requireCalendarEventsParticipations = new ArrayCollection();
  123.         $this->approvedCandidatesCampaignMessages = new ArrayCollection();
  124.         $this->denyCalendarEventsParticipations = new ArrayCollection();
  125.         $this->connectedCalendarEvents = new ArrayCollection();
  126.     }
  127.     public function getShortInfo(): string
  128.     {
  129.         return $this->getName() . " " . ($this->getStartDate() ? $this->getStartDate()->format('d.m.Y') : "") .
  130.             ($this->getFromLevel() ? " (с " $this->getFromLevel() . " ур.)" " (все ур.)");
  131.     }
  132.     public function getText(array $options null)
  133.     {
  134.         $options array_merge([
  135.             "quotes" => false,
  136.         ], $options ?? []);
  137.         $result = ($options['quotes'] ? $this->getNameText() : $this->getName());
  138.         return $result;
  139.     }
  140.     public function getNameText(): string
  141.     {
  142.         return $this->getTypeText() . " «" $this->getName() . "»";
  143.     }
  144.     public function getOnTypeText(): string
  145.     {
  146.         $typeText null;
  147.         switch ($this->getType()) {
  148.             case Type::TYPE_COURSE:
  149.                 $typeText "курс";
  150.                 break;
  151.             case Type::TYPE_OPEN_MEETING:
  152.                 $typeText "открытую встречу";
  153.                 break;
  154.             case Type::TYPE_MEETING:
  155.                 $typeText "конференцию";
  156.                 break;
  157.             default:
  158.                 $typeText $this->getTypeText();
  159.                 break;
  160.         }
  161.         return $typeText;
  162.     }
  163.     public function getOfTypeText(): string
  164.     {
  165.         $typeText null;
  166.         switch ($this->getType()) {
  167.             case Type::TYPE_COURSE:
  168.                 $typeText "курса";
  169.                 break;
  170.                 case Type::TYPE_OPEN_MEETING:
  171.                 $typeText "открытой встречи";
  172.                 break;
  173.             case Type::TYPE_MEETING:
  174.                 $typeText "конференции";
  175.                 break;
  176.             default:
  177.                 $typeText $this->getTypeText();
  178.                 break;
  179.         }
  180.         return $typeText;
  181.     }
  182.     public function getOnNameText(): string
  183.     {
  184.         $typeText $this->getOnTypeText();
  185.         return $typeText " «" $this->getName() . "»";
  186.     }
  187.     public function getOfNameText(): string
  188.     {
  189.         $typeText $this->getOfTypeText();
  190.         return $typeText " «" $this->getName() . "»";
  191.     }
  192.     public function getId(): ?int
  193.     {
  194.         return $this->id;
  195.     }
  196.     public function getName(): ?string
  197.     {
  198.         return $this->name;
  199.     }
  200.     public function setName(string $name): self
  201.     {
  202.         $this->name $name;
  203.         return $this;
  204.     }
  205.     public function getStartDate(): ?\DateTimeInterface
  206.     {
  207.         return $this->startDate;
  208.     }
  209.     public function setStartDate(\DateTimeInterface $startDate): self
  210.     {
  211.         $this->startDate $startDate;
  212.         return $this;
  213.     }
  214.     public function getEndDate(): ?\DateTimeInterface
  215.     {
  216.         return $this->endDate;
  217.     }
  218.     public function setEndDate(\DateTimeInterface $endDate): self
  219.     {
  220.         $this->endDate $endDate;
  221.         return $this;
  222.     }
  223.     private function _getPrice($isFirstCall false): ?int
  224.     {
  225.         if ($isFirstCall) {
  226.             if (!$this->_getPrice() && $this->getConnectedCalendarEvents()->count()) {
  227.                 $isAllPricesEqual true;
  228.                 $price null;
  229.                 foreach ($this->getConnectedCalendarEvents() as $connectedEvent) {
  230.                     if ($connectedEvent->_getPrice()) {
  231.                         if ($price === null) {
  232.                             $price $connectedEvent->_getPrice();
  233.                         } elseif ($price != $connectedEvent->_getPrice()) {
  234.                             $isAllPricesEqual false;
  235.                             break;
  236.                         }
  237.                     }
  238.                 }
  239.                 if ($isAllPricesEqual) {
  240.                     return $price;
  241.                 }
  242.             }
  243.         }
  244.         return $this->price;
  245.     }
  246.     public function getPrice(): ?int
  247.     {
  248.       return $this->_getPrice(true);
  249.     }
  250.     public function setPrice(?int $price): self
  251.     {
  252.         $this->price $price;
  253.         return $this;
  254.     }
  255.     public function getFromLevel(): ?int
  256.     {
  257.         return $this->fromLevel;
  258.     }
  259.     public function getFromLevelEmoji(): string
  260.     {
  261.         $level $this->getFromLevel();
  262.         if ($level === null || $level === 0) {
  263.             return "🔢";
  264.         }
  265.         $emojiNumbers = [
  266.             '0' => '0️⃣',
  267.             '1' => '1️⃣',
  268.             '2' => '2️⃣',
  269.             '3' => '3️⃣',
  270.             '4' => '4️⃣',
  271.             '5' => '5️⃣',
  272.             '6' => '6️⃣',
  273.             '7' => '7️⃣',
  274.             '8' => '8️⃣',
  275.             '9' => '9️⃣',
  276.         ];
  277.         $levelStr = (string)$level;
  278.         return $emojiNumbers[$levelStr[0]] . ($levelStr[1] ?? '');
  279.     }
  280.     public function getFromLevelText(): string
  281.     {
  282.         if ($this->getFromLevel() === null) {
  283.             return "";
  284.         }
  285.         if ($this->getFromLevel() === 0) {
  286.             return "все ур.";
  287.         }
  288.         return "с" StringUtils::getEnding($this->getFromLevel(),
  289.                 """о""",
  290.                 "")
  291.             . " " . (string)$this->getFromLevel() . " ур.";
  292.     }
  293.     public function setFromLevel(?int $fromLevel): self
  294.     {
  295.         $this->fromLevel $fromLevel;
  296.         return $this;
  297.     }
  298.     public function getEarnLevel(): ?int
  299.     {
  300.         return $this->earnLevel;
  301.     }
  302.     public function setEarnLevel(?int $level): self
  303.     {
  304.         $this->earnLevel $level;
  305.         return $this;
  306.     }
  307.     public function getStartTime(): ?\DateTimeInterface
  308.     {
  309.         return $this->startTime;
  310.     }
  311.     public function setStartTime(\DateTimeInterface $startTime): self
  312.     {
  313.         $this->startTime $startTime;
  314.         return $this;
  315.     }
  316.     public function getEndTime(): ?\DateTimeInterface
  317.     {
  318.         return $this->endTime;
  319.     }
  320.     public function setEndTime(\DateTimeInterface $endTime): self
  321.     {
  322.         $this->endTime $endTime;
  323.         return $this;
  324.     }
  325.     public function getType()
  326.     {
  327.         return $this->type;
  328.     }
  329.     public function getTypeText(): string
  330.     {
  331.         return Type::getText($this->getType());
  332.     }
  333.     public function setType($type): self
  334.     {
  335.         $this->type $type;
  336.         return $this;
  337.     }
  338.     public function getTopic(): string
  339.     {
  340.         if ($this->type === Type::TYPE_COURSE) {
  341.             return "Курс «" $this->getName() . "»";
  342.         } elseif ($this->type === Type::TYPE_MEETING) {
  343.             return "Конференция «" $this->getName() . "»";
  344.         } elseif ($this->type === Type::TYPE_OPEN_MEETING) {
  345.             return "Открытая встреча «" $this->getName() . "»";
  346.         } else {
  347.             return $this->getTypeText() . " «" $this->getName() . "»";
  348.         }
  349.     }
  350.     public function isIsCandidateReviewRequired(): ?bool
  351.     {
  352.         return $this->isCandidateReviewRequired;
  353.     }
  354.     public function setIsCandidateReviewRequired(?bool $isCandidateReviewRequired): self
  355.     {
  356.         $this->isCandidateReviewRequired $isCandidateReviewRequired;
  357.         return $this;
  358.     }
  359.     public function getPaymentRequisites(): ?PaymentRequisites
  360.     {
  361.         return $this->paymentRequisites;
  362.     }
  363.     public function setPaymentRequisites(?PaymentRequisites $paymentRequisites): self
  364.     {
  365.         $this->paymentRequisites $paymentRequisites;
  366.         return $this;
  367.     }
  368.     public function getNameWithCode(): string
  369.     {
  370.         return "Код " $this->getPrice() . " – " $this->getShortInfo();
  371.     }
  372.     public function isFinished(): bool
  373.     {
  374.         if (!$this->getEndDate()) {
  375.             return true;
  376.         }
  377.         $now = new \DateTime();
  378.         return $this->getEndDate() < $now;
  379.     }
  380.     public function isStarted(int $addDaysToStartDate null): bool
  381.     {
  382.         if (!$this->getStartDate() || $this->isFinished()) {
  383.             return false;
  384.         }
  385.         $now = (new \DateTime())->setTime(000);
  386.         $startDate = (clone $this->getStartDate())->setTime(000);
  387.         if ($addDaysToStartDate) {
  388.             $startDate $startDate->modify("+$addDaysToStartDate days");
  389.         }
  390.         return $startDate <= $now;
  391.     }
  392.     public function isRegistrationOpen(): bool
  393.     {
  394.         return !$this->isStarted(-2);
  395.     }
  396.     public function getLastRegistrationDate(): ?\DateTime
  397.     {
  398.         if (!$this->getStartDate()) {
  399.             return null;
  400.         }
  401.         $lastRegistrationDate = (clone $this->getStartDate())->modify("-3 days");
  402.         $weekendDays 0;
  403.         if (in_array((int)$lastRegistrationDate->format('N'), [67])) {
  404.             $weekendDays = (int)$lastRegistrationDate->format('N') - 5;
  405.         }
  406.         if ($weekendDays 0) {
  407.             $lastRegistrationDate $lastRegistrationDate->modify("-$weekendDays days");
  408.         }
  409.         return $lastRegistrationDate;
  410.     }
  411.     public function getHoursLeftBeforeStart(): int
  412.     {
  413.         return (int)ceil((($this->getStartDate()->getTimestamp() - (new \DateTime())->getTimestamp()) / 3600));
  414.     }
  415.     public function getTelegramChannelUrl(): ?string
  416.     {
  417.         return $this->telegramChannelUrl;
  418.     }
  419.     public function setTelegramChannelUrl(?string $telegramChannelUrl): self
  420.     {
  421.         $this->telegramChannelUrl $telegramChannelUrl;
  422.         return $this;
  423.     }
  424.     public function getTelegramChannelId(): ?string
  425.     {
  426.         return $this->telegramChannelId;
  427.     }
  428.     public function setTelegramChannelId(?string $telegramChannelId): self
  429.     {
  430.         $this->telegramChannelId $telegramChannelId;
  431.         return $this;
  432.     }
  433.     /**
  434.      * @return Collection<int, self>
  435.      */
  436.     public function getRequireCalendarEventsParticipations(): Collection
  437.     {
  438.         return $this->requireCalendarEventsParticipations;
  439.     }
  440.     public function getRequireCalendarEventsParticipationsText(): string
  441.     {
  442.         $texts = [];
  443.         foreach ($this->getRequireCalendarEventsParticipations() as $event) {
  444.             $texts[] = mb_lcfirst($event->getNameText(), 'UTF-8');
  445.         }
  446.         return implode(", "$texts);
  447.     }
  448.     public function getDenyCalendarEventsParticipationsText(): string
  449.     {
  450.         $texts = [];
  451.         foreach ($this->getDenyCalendarEventsParticipations() as $event) {
  452.             $texts[] = mb_lcfirst($event->getNameText(), 'UTF-8');
  453.         }
  454.         return implode(", "$texts);
  455.     }
  456.     public function addRequireCalendarEventsParticipation(self $requireCalendarEventsParticipation): self
  457.     {
  458.         if (!$this->requireCalendarEventsParticipations->contains($requireCalendarEventsParticipation)) {
  459.             $this->requireCalendarEventsParticipations[] = $requireCalendarEventsParticipation;
  460.         }
  461.         return $this;
  462.     }
  463.     public function removeRequireCalendarEventsParticipation(self $requireCalendarEventsParticipation): self
  464.     {
  465.         $this->requireCalendarEventsParticipations->removeElement($requireCalendarEventsParticipation);
  466.         return $this;
  467.     }
  468.     public function isStudentLevelCorrespond(?Person $person): bool
  469.     {
  470.         if (!$person) {
  471.             return false;
  472.         }
  473.         $eventLevel $this->getFromLevel();
  474.         if ($eventLevel === null) {
  475.             return true;
  476.         }
  477.         if (!$person->getStudent() || $person->getStudent()->getLevel() === null) {
  478.             return false;
  479.         }
  480.         return $person->getStudent()->getLevel() >= $eventLevel;
  481.     }
  482.     public function isEventDatesApproximate(): ?bool
  483.     {
  484.         return $this->eventDatesApproximate;
  485.     }
  486.     public function setEventDatesApproximate(?bool $eventDatesApproximate): self
  487.     {
  488.         $this->eventDatesApproximate $eventDatesApproximate;
  489.         return $this;
  490.     }
  491. //    /**
  492. //     * @return Collection<int, TelegramCampaignMessage>
  493. //     */
  494. //    public function getApprovedCandidatesCampaignMessages(): Collection
  495. //    {
  496. //        return $this->approvedCandidatesCampaignMessages;
  497. //    }
  498. //
  499. //    public function addApprovedCandidatesCampaignMessage(TelegramCampaignMessage $approvedCandidatesCampaignMessage): self
  500. //    {
  501. //        if (!$this->approvedCandidatesCampaignMessages->contains($approvedCandidatesCampaignMessage)) {
  502. //            $this->approvedCandidatesCampaignMessages[] = $approvedCandidatesCampaignMessage;
  503. //        }
  504. //
  505. //        return $this;
  506. //    }
  507. //
  508. //    public function removeApprovedCandidatesCampaignMessage(TelegramCampaignMessage $approvedCandidatesCampaignMessage): self
  509. //    {
  510. //        $this->approvedCandidatesCampaignMessages->removeElement($approvedCandidatesCampaignMessage);
  511. //
  512. //        return $this;
  513. //    }
  514.     public function getStatus()
  515.     {
  516.         return $this->status;
  517.     }
  518.     public function setStatus($status): self
  519.     {
  520.         $this->status $status;
  521.         return $this;
  522.     }
  523.     public function getStatusText(): ?string
  524.     {
  525.         return Status::getText($this->getStatus());
  526.     }
  527.     public function getStatusCssClass($prefix null): ?string
  528.     {
  529.         return Status::getCssClass($this->getStatus(), $prefix);
  530.     }
  531.     /**
  532.      * @return Collection<int, self>
  533.      */
  534.     public function getDenyCalendarEventsParticipations(): Collection
  535.     {
  536.         return $this->denyCalendarEventsParticipations;
  537.     }
  538.     public function addDenyCalendarEventsParticipation(self $denyCalendarEventsParticipation): self
  539.     {
  540.         if (!$this->denyCalendarEventsParticipations->contains($denyCalendarEventsParticipation)) {
  541.             $this->denyCalendarEventsParticipations[] = $denyCalendarEventsParticipation;
  542.         }
  543.         return $this;
  544.     }
  545.     public function removeDenyCalendarEventsParticipation(self $denyCalendarEventsParticipation): self
  546.     {
  547.         $this->denyCalendarEventsParticipations->removeElement($denyCalendarEventsParticipation);
  548.         return $this;
  549.     }
  550.     public function getToLevel(): ?int
  551.     {
  552.         return $this->toLevel;
  553.     }
  554.     public function setToLevel(?int $toLevel): self
  555.     {
  556.         $this->toLevel $toLevel;
  557.         return $this;
  558.     }
  559.     public function getRequireStudentStatus()
  560.     {
  561.         return $this->requireStudentStatus;
  562.     }
  563.     public function getRequireStudentStatusText(): string
  564.     {
  565.         return VirtualStatus::getText($this->getRequireStudentStatus());
  566.     }
  567.     public function setRequireStudentStatus($requireStudentStatus): self
  568.     {
  569.         $this->requireStudentStatus $requireStudentStatus;
  570.         return $this;
  571.     }
  572.     public function getConnectingCalendarEvent(): ?self
  573.     {
  574.         return $this->connectingCalendarEvent;
  575.     }
  576.     public function setConnectingCalendarEvent(?self $connectingCalendarEvent): self
  577.     {
  578.         $this->connectingCalendarEvent $connectingCalendarEvent;
  579.         return $this;
  580.     }
  581.     /**
  582.      * @return Collection<int, self>
  583.      */
  584.     public function getConnectedCalendarEvents(): Collection
  585.     {
  586.         return $this->connectedCalendarEvents;
  587.     }
  588.     public function addConnectedCalendarEvent(self $connectedCalendarEvent): self
  589.     {
  590.         if (!$this->connectedCalendarEvents->contains($connectedCalendarEvent)) {
  591.             $this->connectedCalendarEvents[] = $connectedCalendarEvent;
  592.             $connectedCalendarEvent->setConnectingCalendarEvent($this);
  593.         }
  594.         return $this;
  595.     }
  596.     public function removeConnectedCalendarEvent(self $connectedCalendarEvent): self
  597.     {
  598.         if ($this->connectedCalendarEvents->removeElement($connectedCalendarEvent)) {
  599.             // set the owning side to null (unless already changed)
  600.             if ($connectedCalendarEvent->getConnectingCalendarEvent() === $this) {
  601.                 $connectedCalendarEvent->setConnectingCalendarEvent(null);
  602.             }
  603.         }
  604.         return $this;
  605.     }
  606. }