src/Entity/CalendarEvent.php line 16

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\Library\Utils\StringUtils;
  6. use App\Repository\CalendarEventRepository;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. /**
  11.  * @ORM\Entity(repositoryClass=CalendarEventRepository::class)
  12.  */
  13. class CalendarEvent
  14. {
  15.     /**
  16.      * @ORM\Id
  17.      * @ORM\GeneratedValue
  18.      * @ORM\Column(type="integer")
  19.      */
  20.     private $id;
  21.     /**
  22.      * @ORM\Column(type="string", length=255)
  23.      */
  24.     private $name;
  25.     /**
  26.      * @ORM\Column(type="date")
  27.      */
  28.     private $startDate;
  29.     /**
  30.      * @ORM\Column(type="date")
  31.      */
  32.     private $endDate;
  33.     /**
  34.      * @ORM\Column(type="integer", nullable=true)
  35.      */
  36.     private $price;
  37.     /**
  38.      * @ORM\Column(type="smallint", nullable=true)
  39.      */
  40.     private $fromLevel;
  41.     /**
  42.      * @ORM\Column(type="smallint", nullable=true)
  43.      */
  44.     private $earnLevel;
  45.     /**
  46.      * @ORM\Column(type="time")
  47.      */
  48.     private $startTime;
  49.     /**
  50.      * @ORM\Column(type="time")
  51.      */
  52.     private $endTime;
  53.     /**
  54.      * @ORM\Column(type="enum", options={"values"="calendar_event_enum"})
  55.      */
  56.     private $type Type::TYPE_COURSE;
  57.     /**
  58.      * @ORM\Column(type="boolean", nullable=true)
  59.      */
  60.     private $isCandidateReviewRequired;
  61.     /**
  62.     * @ORM\ManyToOne(targetEntity=PaymentRequisites::class)
  63.     * @ORM\JoinColumn(nullable=true)
  64.      */
  65.     private $paymentRequisites;
  66.     /**
  67.      * @ORM\Column(type="string", length=255, nullable=true)
  68.      */
  69.     private $telegramChannelUrl;
  70.     /**
  71.      * @ORM\Column(type="string", length=100, nullable=true)
  72.      */
  73.     private $telegramChannelId;
  74.     /**
  75.      * @ORM\ManyToMany(targetEntity=CalendarEvent::class)
  76.      */
  77.     private $requireCalendarEventsParticipations;
  78.     /**
  79.      * @ORM\Column(type="boolean", nullable=true)
  80.      */
  81.     private $eventDatesApproximate;
  82.     /**
  83.      * @ORM\Column(type="enum", options={"values"="calendar_event_enum", "default"="active"}))
  84.      */
  85.     private $status Status::STATUS_ACTIVE;
  86. //    /**
  87. //     * @ORM\ManyToMany(targetEntity=TelegramCampaignMessage::class)
  88. //     */
  89. //    private $approvedCandidatesCampaignMessages;
  90.     public function __construct()
  91.     {
  92.         $this->startDate = new \DateTime();
  93.         $this->endDate = new \DateTime();
  94.         $this->startTime = new \DateTime('10:00:00');
  95.         $this->endTime = new \DateTime('13:00:00');
  96.         $this->requireCalendarEventsParticipations = new ArrayCollection();
  97.         $this->approvedCandidatesCampaignMessages = new ArrayCollection();
  98.     }
  99.     public function getShortInfo(): string
  100.     {
  101.         return $this->getName() . " " . ($this->getStartDate() ? $this->getStartDate()->format('d.m.Y') : "") .
  102.             ($this->getFromLevel() ? " (с " $this->getFromLevel() . " ур.)" " (все ур.)");
  103.     }
  104.     public function getText(array $options null)
  105.     {
  106.         $options array_merge([
  107.             "quotes" => false,
  108.         ], $options ?? []);
  109.         $result = ($options['quotes'] ? $this->getNameText() : $this->getName());
  110.         return $result;
  111.     }
  112.     public function getNameText(): string
  113.     {
  114.         return $this->getTypeText() . " «" $this->getName() . "»";
  115.     }
  116.     public function getOnTypeText(): string
  117.     {
  118.         $typeText null;
  119.         switch ($this->getType()) {
  120.             case Type::TYPE_COURSE:
  121.                 $typeText "курс";
  122.                 break;
  123.             default:
  124.                 $typeText $this->getTypeText();
  125.                 break;
  126.         }
  127.         return $typeText;
  128.     }
  129.     public function getOfTypeText(): string
  130.     {
  131.         $typeText null;
  132.         switch ($this->getType()) {
  133.             case Type::TYPE_COURSE:
  134.                 $typeText "курса";
  135.                 break;
  136.             default:
  137.                 $typeText $this->getTypeText();
  138.                 break;
  139.         }
  140.         return $typeText;
  141.     }
  142.     public function getOnNameText(): string
  143.     {
  144.         $typeText $this->getOnTypeText();
  145.         return $typeText " «" $this->getName() . "»";
  146.     }
  147.     public function getOfNameText(): string
  148.     {
  149.         $typeText $this->getOfTypeText();
  150.         return $typeText " «" $this->getName() . "»";
  151.     }
  152.     public function getId(): ?int
  153.     {
  154.         return $this->id;
  155.     }
  156.     public function getName(): ?string
  157.     {
  158.         return $this->name;
  159.     }
  160.     public function setName(string $name): self
  161.     {
  162.         $this->name $name;
  163.         return $this;
  164.     }
  165.     public function getStartDate(): ?\DateTimeInterface
  166.     {
  167.         return $this->startDate;
  168.     }
  169.     public function setStartDate(\DateTimeInterface $startDate): self
  170.     {
  171.         $this->startDate $startDate;
  172.         return $this;
  173.     }
  174.     public function getEndDate(): ?\DateTimeInterface
  175.     {
  176.         return $this->endDate;
  177.     }
  178.     public function setEndDate(\DateTimeInterface $endDate): self
  179.     {
  180.         $this->endDate $endDate;
  181.         return $this;
  182.     }
  183.     public function getPrice(): ?int
  184.     {
  185.         return $this->price;
  186.     }
  187.     public function setPrice(?int $price): self
  188.     {
  189.         $this->price $price;
  190.         return $this;
  191.     }
  192.     public function getFromLevel(): ?int
  193.     {
  194.         return $this->fromLevel;
  195.     }
  196.     public function getFromLevelEmoji(): string
  197.     {
  198.         $level $this->getFromLevel();
  199.         if ($level === null || $level === 0) {
  200.             return "🔢";
  201.         }
  202.         $emojiNumbers = [
  203.             '0' => '0️⃣',
  204.             '1' => '1️⃣',
  205.             '2' => '2️⃣',
  206.             '3' => '3️⃣',
  207.             '4' => '4️⃣',
  208.             '5' => '5️⃣',
  209.             '6' => '6️⃣',
  210.             '7' => '7️⃣',
  211.             '8' => '8️⃣',
  212.             '9' => '9️⃣',
  213.         ];
  214.         $levelStr = (string)$level;
  215.         return $emojiNumbers[$levelStr[0]] . ($levelStr[1] ?? '');
  216.     }
  217.     public function getFromLevelText(): string
  218.     {
  219.         if ($this->getFromLevel() === null) {
  220.             return "";
  221.         }
  222.         if ($this->getFromLevel() === 0) {
  223.             return "все ур.";
  224.         }
  225.         return "с" StringUtils::getEnding($this->getFromLevel(),
  226.                 """о""",
  227.                 "")
  228.             . " " . (string)$this->getFromLevel() . " ур.";
  229.     }
  230.     public function setFromLevel(?int $fromLevel): self
  231.     {
  232.         $this->fromLevel $fromLevel;
  233.         return $this;
  234.     }
  235.     public function getEarnLevel(): ?int
  236.     {
  237.         return $this->earnLevel;
  238.     }
  239.     public function setEarnLevel(?int $level): self
  240.     {
  241.         $this->earnLevel $level;
  242.         return $this;
  243.     }
  244.     public function getStartTime(): ?\DateTimeInterface
  245.     {
  246.         return $this->startTime;
  247.     }
  248.     public function setStartTime(\DateTimeInterface $startTime): self
  249.     {
  250.         $this->startTime $startTime;
  251.         return $this;
  252.     }
  253.     public function getEndTime(): ?\DateTimeInterface
  254.     {
  255.         return $this->endTime;
  256.     }
  257.     public function setEndTime(\DateTimeInterface $endTime): self
  258.     {
  259.         $this->endTime $endTime;
  260.         return $this;
  261.     }
  262.     public function getType()
  263.     {
  264.         return $this->type;
  265.     }
  266.     public function getTypeText(): string
  267.     {
  268.         return Type::getText($this->getType());
  269.     }
  270.     public function setType($type): self
  271.     {
  272.         $this->type $type;
  273.         return $this;
  274.     }
  275.     public function getTopic(): string
  276.     {
  277.         if ($this->type === Type::TYPE_COURSE) {
  278.             return "Курс «" $this->getName() . "»";
  279.         } elseif ($this->type === Type::TYPE_MEETING) {
  280.             return "Конференция «" $this->getName() . "»";
  281.         } elseif ($this->type === Type::TYPE_OPEN_MEETING) {
  282.             return "Открытая встреча «" $this->getName() . "»";
  283.         } else {
  284.             return $this->getName();
  285.         }
  286.     }
  287.     public function isIsCandidateReviewRequired(): ?bool
  288.     {
  289.         return $this->isCandidateReviewRequired;
  290.     }
  291.     public function setIsCandidateReviewRequired(?bool $isCandidateReviewRequired): self
  292.     {
  293.         $this->isCandidateReviewRequired $isCandidateReviewRequired;
  294.         return $this;
  295.     }
  296.     public function getPaymentRequisites(): ?PaymentRequisites
  297.     {
  298.         return $this->paymentRequisites;
  299.     }
  300.     public function setPaymentRequisites(?PaymentRequisites $paymentRequisites): self
  301.     {
  302.         $this->paymentRequisites $paymentRequisites;
  303.         return $this;
  304.     }
  305.     public function getNameWithCode(): string
  306.     {
  307.         return "Код " $this->getPrice() . " – " $this->getShortInfo();
  308.     }
  309.     public function isFinished(): bool
  310.     {
  311.         if (!$this->getEndDate()) {
  312.             return true;
  313.         }
  314.         $now = new \DateTime();
  315.         return $this->getEndDate() < $now;
  316.     }
  317.     public function isStarted(int $addDaysToStartDate null): bool
  318.     {
  319.         if (!$this->getStartDate() || $this->isFinished()) {
  320.             return false;
  321.         }
  322.         $now = (new \DateTime())->setTime(000);
  323.         $startDate = (clone $this->getStartDate())->setTime(000);
  324.         if ($addDaysToStartDate) {
  325.             $startDate $startDate->modify("+$addDaysToStartDate days");
  326.         }
  327.         return $startDate <= $now;
  328.     }
  329.     public function isRegistrationOpen(): bool
  330.     {
  331.         return !$this->isStarted(-2);
  332.     }
  333.     public function getLastRegistrationDate(): ?\DateTime
  334.     {
  335.         if (!$this->getStartDate()) {
  336.             return null;
  337.         }
  338.         $lastRegistrationDate = (clone $this->getStartDate())->modify("-3 days");
  339.         $weekendDays 0;
  340.         if (in_array((int)$lastRegistrationDate->format('N'), [67])) {
  341.             $weekendDays = (int)$lastRegistrationDate->format('N') - 5;
  342.         }
  343.         if ($weekendDays 0) {
  344.             $lastRegistrationDate $lastRegistrationDate->modify("-$weekendDays days");
  345.         }
  346.         return $lastRegistrationDate;
  347.     }
  348.     public function getHoursLeftBeforeStart(): int
  349.     {
  350.         return (int)ceil((($this->getStartDate()->getTimestamp() - (new \DateTime())->getTimestamp()) / 3600));
  351.     }
  352.     public function getTelegramChannelUrl(): ?string
  353.     {
  354.         return $this->telegramChannelUrl;
  355.     }
  356.     public function setTelegramChannelUrl(?string $telegramChannelUrl): self
  357.     {
  358.         $this->telegramChannelUrl $telegramChannelUrl;
  359.         return $this;
  360.     }
  361.     public function getTelegramChannelId(): ?string
  362.     {
  363.         return $this->telegramChannelId;
  364.     }
  365.     public function setTelegramChannelId(?string $telegramChannelId): self
  366.     {
  367.         $this->telegramChannelId $telegramChannelId;
  368.         return $this;
  369.     }
  370.     /**
  371.      * @return Collection<int, self>
  372.      */
  373.     public function getRequireCalendarEventsParticipations(): Collection
  374.     {
  375.         return $this->requireCalendarEventsParticipations;
  376.     }
  377.     public function getRequireCalendarEventsParticipationsText(): string
  378.     {
  379.         $texts = [];
  380.         foreach ($this->getRequireCalendarEventsParticipations() as $event) {
  381.             $texts[] = mb_lcfirst($event->getNameText(), 'UTF-8');
  382.         }
  383.         return implode(", "$texts);
  384.     }
  385.     public function addRequireCalendarEventsParticipation(self $requireCalendarEventsParticipation): self
  386.     {
  387.         if (!$this->requireCalendarEventsParticipations->contains($requireCalendarEventsParticipation)) {
  388.             $this->requireCalendarEventsParticipations[] = $requireCalendarEventsParticipation;
  389.         }
  390.         return $this;
  391.     }
  392.     public function removeRequireCalendarEventsParticipation(self $requireCalendarEventsParticipation): self
  393.     {
  394.         $this->requireCalendarEventsParticipations->removeElement($requireCalendarEventsParticipation);
  395.         return $this;
  396.     }
  397.     public function isStudentLevelCorrespond(?Person $person): bool
  398.     {
  399.         if (!$person) {
  400.             return false;
  401.         }
  402.         $eventLevel $this->getFromLevel();
  403.         if ($eventLevel === null) {
  404.             return true;
  405.         }
  406.         if (!$person->getStudent() || $person->getStudent()->getLevel() === null) {
  407.             return false;
  408.         }
  409.         return $person->getStudent()->getLevel() >= $eventLevel;
  410.     }
  411.     public function isEventDatesApproximate(): ?bool
  412.     {
  413.         return $this->eventDatesApproximate;
  414.     }
  415.     public function setEventDatesApproximate(?bool $eventDatesApproximate): self
  416.     {
  417.         $this->eventDatesApproximate $eventDatesApproximate;
  418.         return $this;
  419.     }
  420. //    /**
  421. //     * @return Collection<int, TelegramCampaignMessage>
  422. //     */
  423. //    public function getApprovedCandidatesCampaignMessages(): Collection
  424. //    {
  425. //        return $this->approvedCandidatesCampaignMessages;
  426. //    }
  427. //
  428. //    public function addApprovedCandidatesCampaignMessage(TelegramCampaignMessage $approvedCandidatesCampaignMessage): self
  429. //    {
  430. //        if (!$this->approvedCandidatesCampaignMessages->contains($approvedCandidatesCampaignMessage)) {
  431. //            $this->approvedCandidatesCampaignMessages[] = $approvedCandidatesCampaignMessage;
  432. //        }
  433. //
  434. //        return $this;
  435. //    }
  436. //
  437. //    public function removeApprovedCandidatesCampaignMessage(TelegramCampaignMessage $approvedCandidatesCampaignMessage): self
  438. //    {
  439. //        $this->approvedCandidatesCampaignMessages->removeElement($approvedCandidatesCampaignMessage);
  440. //
  441. //        return $this;
  442. //    }
  443. public function getStatus()
  444. {
  445.     return $this->status;
  446. }
  447. public function setStatus($status): self
  448. {
  449.     $this->status $status;
  450.     return $this;
  451. }
  452. public function getStatusText(): ?string
  453. {
  454.     return Status::getText($this->getStatus());
  455. }
  456. public function getStatusCssClass($prefix null): ?string
  457. {
  458.     return Status::getCssClass($this->getStatus(), $prefix);
  459. }
  460. }