src/Entity/CalendarEvent.php line 13

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\ORM\Mapping as ORM;
  7. /**
  8.  * @ORM\Entity(repositoryClass=CalendarEventRepository::class)
  9.  */
  10. class CalendarEvent
  11. {
  12.     /**
  13.      * @ORM\Id
  14.      * @ORM\GeneratedValue
  15.      * @ORM\Column(type="integer")
  16.      */
  17.     private $id;
  18.     /**
  19.      * @ORM\Column(type="string", length=255)
  20.      */
  21.     private $name;
  22.     /**
  23.      * @ORM\Column(type="date")
  24.      */
  25.     private $startDate;
  26.     /**
  27.      * @ORM\Column(type="date")
  28.      */
  29.     private $endDate;
  30.     /**
  31.      * @ORM\Column(type="integer", nullable=true)
  32.      */
  33.     private $price;
  34.     /**
  35.      * @ORM\Column(type="smallint", nullable=true)
  36.      */
  37.     private $fromLevel;
  38.     /**
  39.      * @ORM\Column(type="smallint", nullable=true)
  40.      */
  41.     private $earnLevel;
  42.     /**
  43.      * @ORM\Column(type="time")
  44.      */
  45.     private $startTime;
  46.     /**
  47.      * @ORM\Column(type="time")
  48.      */
  49.     private $endTime;
  50.     /**
  51.      * @ORM\Column(type="enum", options={"values"="calendar_event_enum"})
  52.      */
  53.     private $type Type::TYPE_COURSE;
  54.     /**
  55.      * @ORM\Column(type="boolean", nullable=true)
  56.      */
  57.     private $isCandidateReviewRequired;
  58.     /**
  59.     * @ORM\ManyToOne(targetEntity=PaymentRequisites::class)
  60.     * @ORM\JoinColumn(nullable=true)
  61.      */
  62.     private $paymentRequisites;
  63.     /**
  64.      * @ORM\Column(type="string", length=255, nullable=true)
  65.      */
  66.     private $telegramChannelUrl;
  67.     /**
  68.      * @ORM\Column(type="string", length=100, nullable=true)
  69.      */
  70.     private $telegramChannelId;
  71.     public function __construct()
  72.     {
  73.         $this->startDate = new \DateTime();
  74.         $this->endDate = new \DateTime();
  75.         $this->startTime = new \DateTime('10:00:00');
  76.         $this->endTime = new \DateTime('13:00:00');
  77.     }
  78.     public function getShortInfo(): string
  79.     {
  80.         return $this->getName() . " " $this->getStartDate()->format('d.m.Y') .
  81.             ($this->getFromLevel() ? " (с " $this->getFromLevel() . " ур.)" " (все ур.)");
  82.     }
  83.     public function getText(array $options null)
  84.     {
  85.         $options array_merge([
  86.             "quotes" => false,
  87.         ], $options ?? []);
  88.         $result = ($options['quotes'] ? $this->getNameText() : $this->getName());
  89.         return $result;
  90.     }
  91.     public function getNameText(): string
  92.     {
  93.         return $this->getTypeText() . " «" $this->getName() . "»";
  94.     }
  95.     public function getOnTypeText(): string
  96.     {
  97.         $typeText null;
  98.         switch ($this->getType()) {
  99.             case Type::TYPE_COURSE:
  100.                 $typeText "курс";
  101.                 break;
  102.             default:
  103.                 $typeText $this->getTypeText();
  104.                 break;
  105.         }
  106.         return $typeText;
  107.     }
  108.     public function getOfTypeText(): string
  109.     {
  110.         $typeText null;
  111.         switch ($this->getType()) {
  112.             case Type::TYPE_COURSE:
  113.                 $typeText "курса";
  114.                 break;
  115.             default:
  116.                 $typeText $this->getTypeText();
  117.                 break;
  118.         }
  119.         return $typeText;
  120.     }
  121.     public function getOnNameText(): string
  122.     {
  123.         $typeText $this->getOnTypeText();
  124.         return $typeText " «" $this->getName() . "»";
  125.     }
  126.     public function getOfNameText(): string
  127.     {
  128.         $typeText $this->getOfTypeText();
  129.         return $typeText " «" $this->getName() . "»";
  130.     }
  131.     public function getId(): ?int
  132.     {
  133.         return $this->id;
  134.     }
  135.     public function getName(): ?string
  136.     {
  137.         return $this->name;
  138.     }
  139.     public function setName(string $name): self
  140.     {
  141.         $this->name $name;
  142.         return $this;
  143.     }
  144.     public function getStartDate(): ?\DateTimeInterface
  145.     {
  146.         return $this->startDate;
  147.     }
  148.     public function setStartDate(\DateTimeInterface $startDate): self
  149.     {
  150.         $this->startDate $startDate;
  151.         return $this;
  152.     }
  153.     public function getEndDate(): ?\DateTimeInterface
  154.     {
  155.         return $this->endDate;
  156.     }
  157.     public function setEndDate(\DateTimeInterface $endDate): self
  158.     {
  159.         $this->endDate $endDate;
  160.         return $this;
  161.     }
  162.     public function getPrice(): ?int
  163.     {
  164.         return $this->price;
  165.     }
  166.     public function setPrice(int $price): self
  167.     {
  168.         $this->price $price;
  169.         return $this;
  170.     }
  171.     public function getFromLevel(): ?int
  172.     {
  173.         return $this->fromLevel;
  174.     }
  175.     public function getFromLevelText(): string
  176.     {
  177.         if ($this->getFromLevel() === null) {
  178.             return "";
  179.         }
  180.         if ($this->getFromLevel() === 0) {
  181.             return "все ур.";
  182.         }
  183.         return "с" StringUtils::getEnding($this->getFromLevel(), """о""") . " " . (string)$this->getFromLevel() . " ур.";
  184.     }
  185.     public function setFromLevel(int $fromLevel): self
  186.     {
  187.         $this->fromLevel $fromLevel;
  188.         return $this;
  189.     }
  190.     public function getEarnLevel(): ?int
  191.     {
  192.         return $this->earnLevel;
  193.     }
  194.     public function setEarnLevel(?int $level): self
  195.     {
  196.         $this->earnLevel $level;
  197.         return $this;
  198.     }
  199.     public function getStartTime(): ?\DateTimeInterface
  200.     {
  201.         return $this->startTime;
  202.     }
  203.     public function setStartTime(\DateTimeInterface $startTime): self
  204.     {
  205.         $this->startTime $startTime;
  206.         return $this;
  207.     }
  208.     public function getEndTime(): ?\DateTimeInterface
  209.     {
  210.         return $this->endTime;
  211.     }
  212.     public function setEndTime(\DateTimeInterface $endTime): self
  213.     {
  214.         $this->endTime $endTime;
  215.         return $this;
  216.     }
  217.     public function getType()
  218.     {
  219.         return $this->type;
  220.     }
  221.     public function getTypeText(): string
  222.     {
  223.         return Type::getText($this->getType());
  224.     }
  225.     public function setType($type): self
  226.     {
  227.         $this->type $type;
  228.         return $this;
  229.     }
  230.     public function getTopic(): string
  231.     {
  232.         if ($this->type === Type::TYPE_COURSE) {
  233.             return "Курс «" $this->getName() . "»";
  234.         } else {
  235.             return $this->getName();
  236.         }
  237.     }
  238.     public function isIsCandidateReviewRequired(): ?bool
  239.     {
  240.         return $this->isCandidateReviewRequired;
  241.     }
  242.     public function setIsCandidateReviewRequired(?bool $isCandidateReviewRequired): self
  243.     {
  244.         $this->isCandidateReviewRequired $isCandidateReviewRequired;
  245.         return $this;
  246.     }
  247.     public function getPaymentRequisites(): ?PaymentRequisites
  248.     {
  249.         return $this->paymentRequisites;
  250.     }
  251.     public function setPaymentRequisites(?PaymentRequisites $paymentRequisites): self
  252.     {
  253.         $this->paymentRequisites $paymentRequisites;
  254.         return $this;
  255.     }
  256.     public function getNameWithCode(): string
  257.     {
  258.         return "Код " $this->getPrice() . " – " $this->getShortInfo();
  259.     }
  260.     public function isFinished(): bool
  261.     {
  262.         if (!$this->getEndDate()) {
  263.             return true;
  264.         }
  265.         $now = new \DateTime();
  266.         return $this->getEndDate() < $now;
  267.     }
  268.     public function isStarted(int $addDaysToStartDate null): bool
  269.     {
  270.         if (!$this->getStartDate() || $this->isFinished()) {
  271.             return false;
  272.         }
  273.         $now = (new \DateTime())->setTime(000);
  274.         $startDate = (clone $this->getStartDate())->setTime(000);
  275.         if ($addDaysToStartDate) {
  276.             $startDate $startDate->modify("+$addDaysToStartDate days");
  277.         }
  278.         return $startDate <= $now;
  279.     }
  280.     public function isRegistrationOpen(): bool
  281.     {
  282.         return !$this->isStarted(-2);
  283.     }
  284.     public function getLastRegistrationDate(): ?\DateTime
  285.     {
  286.         if (!$this->getStartDate()) {
  287.             return null;
  288.         }
  289.         $lastRegistrationDate = (clone $this->getStartDate())->modify("-2 days");
  290.         $weekendDays 0;
  291.         if (in_array((int)$lastRegistrationDate->format('N'), [67])) {
  292.             $weekendDays = (int)$lastRegistrationDate->format('N') - 5;
  293.         }
  294.         if ($weekendDays 0) {
  295.             $lastRegistrationDate $lastRegistrationDate->modify("-$weekendDays days");
  296.         }
  297.         return $lastRegistrationDate;
  298.     }
  299.     public function getHoursLeftBeforeStart(): int
  300.     {
  301.         return (int)ceil((($this->getStartDate()->getTimestamp() - (new \DateTime())->getTimestamp()) / 3600));
  302.     }
  303.     public function getTelegramChannelUrl(): ?string
  304.     {
  305.         return $this->telegramChannelUrl;
  306.     }
  307.     public function setTelegramChannelUrl(?string $telegramChannelUrl): self
  308.     {
  309.         $this->telegramChannelUrl $telegramChannelUrl;
  310.         return $this;
  311.     }
  312.     public function getTelegramChannelId(): ?string
  313.     {
  314.         return $this->telegramChannelId;
  315.     }
  316.     public function setTelegramChannelId(?string $telegramChannelId): self
  317.     {
  318.         $this->telegramChannelId $telegramChannelId;
  319.         return $this;
  320.     }
  321. }