src/Entity/CalendarEvent.php line 15

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