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.     public function __construct()
  64.     {
  65.         $this->startDate = new \DateTime();
  66.         $this->endDate = new \DateTime();
  67.         $this->startTime = new \DateTime('10:00:00');
  68.         $this->endTime = new \DateTime('13:00:00');
  69.     }
  70.     public function getShortInfo(): string
  71.     {
  72.         return $this->getName() . " " $this->getStartDate()->format('d.m.Y') .
  73.             ($this->getFromLevel() ? " (с " $this->getFromLevel() . " ур.)" " (все ур.)");
  74.     }
  75.     public function getText(array $options null)
  76.     {
  77.         $options array_merge([
  78.             "quotes" => false,
  79.         ], $options ?? []);
  80.         $result = ($options['quotes'] ? $this->getNameText() : $this->getName());
  81.         return $result;
  82.     }
  83.     public function getNameText(): string
  84.     {
  85.         return $this->getTypeText() . " «" $this->getName() . "»";
  86.     }
  87.     public function getOnTypeText(): string
  88.     {
  89.         $typeText null;
  90.         switch ($this->getType()) {
  91.             case Type::TYPE_COURSE:
  92.                 $typeText "курс";
  93.                 break;
  94.             default:
  95.                 $typeText $this->getTypeText();
  96.                 break;
  97.         }
  98.         return $typeText;
  99.     }
  100.     public function getOnNameText(): string
  101.     {
  102.         $typeText $this->getOnTypeText();
  103.         return $typeText " «" $this->getName() . "»";
  104.     }
  105.     public function getId(): ?int
  106.     {
  107.         return $this->id;
  108.     }
  109.     public function getName(): ?string
  110.     {
  111.         return $this->name;
  112.     }
  113.     public function setName(string $name): self
  114.     {
  115.         $this->name $name;
  116.         return $this;
  117.     }
  118.     public function getStartDate(): ?\DateTimeInterface
  119.     {
  120.         return $this->startDate;
  121.     }
  122.     public function setStartDate(\DateTimeInterface $startDate): self
  123.     {
  124.         $this->startDate $startDate;
  125.         return $this;
  126.     }
  127.     public function getEndDate(): ?\DateTimeInterface
  128.     {
  129.         return $this->endDate;
  130.     }
  131.     public function setEndDate(\DateTimeInterface $endDate): self
  132.     {
  133.         $this->endDate $endDate;
  134.         return $this;
  135.     }
  136.     public function getPrice(): ?int
  137.     {
  138.         return $this->price;
  139.     }
  140.     public function setPrice(int $price): self
  141.     {
  142.         $this->price $price;
  143.         return $this;
  144.     }
  145.     public function getFromLevel(): ?int
  146.     {
  147.         return $this->fromLevel;
  148.     }
  149.     public function getFromLevelText(): string
  150.     {
  151.         if ($this->getFromLevel() === null) {
  152.             return "";
  153.         }
  154.         if ($this->getFromLevel() === 0) {
  155.             return "все ур.";
  156.         }
  157.         return "с" StringUtils::getEnding($this->getFromLevel(), """о""") . " " . (string)$this->getFromLevel() . " ур.";
  158.     }
  159.     public function setFromLevel(int $fromLevel): self
  160.     {
  161.         $this->fromLevel $fromLevel;
  162.         return $this;
  163.     }
  164.     public function getEarnLevel(): ?int
  165.     {
  166.         return $this->earnLevel;
  167.     }
  168.     public function setEarnLevel(?int $level): self
  169.     {
  170.         $this->earnLevel $level;
  171.         return $this;
  172.     }
  173.     public function getStartTime(): ?\DateTimeInterface
  174.     {
  175.         return $this->startTime;
  176.     }
  177.     public function setStartTime(\DateTimeInterface $startTime): self
  178.     {
  179.         $this->startTime $startTime;
  180.         return $this;
  181.     }
  182.     public function getEndTime(): ?\DateTimeInterface
  183.     {
  184.         return $this->endTime;
  185.     }
  186.     public function setEndTime(\DateTimeInterface $endTime): self
  187.     {
  188.         $this->endTime $endTime;
  189.         return $this;
  190.     }
  191.     public function getType()
  192.     {
  193.         return $this->type;
  194.     }
  195.     public function getTypeText(): string
  196.     {
  197.         return Type::getText($this->getType());
  198.     }
  199.     public function setType($type): self
  200.     {
  201.         $this->type $type;
  202.         return $this;
  203.     }
  204.     public function getTopic(): string
  205.     {
  206.         if ($this->type === Type::TYPE_COURSE) {
  207.             return "Курс «" $this->getName() . "»";
  208.         } else {
  209.             return $this->getName();
  210.         }
  211.     }
  212.     public function isIsCandidateReviewRequired(): ?bool
  213.     {
  214.         return $this->isCandidateReviewRequired;
  215.     }
  216.     public function setIsCandidateReviewRequired(?bool $isCandidateReviewRequired): self
  217.     {
  218.         $this->isCandidateReviewRequired $isCandidateReviewRequired;
  219.         return $this;
  220.     }
  221.     public function getPaymentRequisites(): ?PaymentRequisites
  222.     {
  223.         return $this->paymentRequisites;
  224.     }
  225.     public function setPaymentRequisites(?PaymentRequisites $paymentRequisites): self
  226.     {
  227.         $this->paymentRequisites $paymentRequisites;
  228.         return $this;
  229.     }
  230.     public function getNameWithCode(): string
  231.     {
  232.         return "Код " $this->getPrice() . " – " $this->getShortInfo();
  233.     }
  234.     public function isFinished(): bool
  235.     {
  236.         if (!$this->getEndDate()) {
  237.             return true;
  238.         }
  239.         $now = new \DateTime();
  240.         return $this->getEndDate() < $now;
  241.     }
  242.     public function isStarted(int $addDays null): bool
  243.     {
  244.         if (!$this->getStartDate() || $this->isFinished()) {
  245.             return false;
  246.         }
  247.         $now = (new \DateTime())->setTime(000);
  248.         $startDate = (clone $this->getStartDate())->setTime(000);
  249.         if ($addDays) {
  250.             $startDate $startDate->modify("+$addDays days");
  251.         }
  252.         return $startDate <= $now;
  253.     }
  254.     public function getLastRegistrationDate(): ?\DateTime
  255.     {
  256.         if (!$this->getStartDate()) {
  257.             return null;
  258.         }
  259.         $lastRegistrationDate = (clone $this->getStartDate())->modify("-2 days");
  260.         $weekendDays 0;
  261.         if (in_array((int)$lastRegistrationDate->format('N'), [67])) {
  262.             $weekendDays = (int)$lastRegistrationDate->format('N') - 5;
  263.         }
  264.         if ($weekendDays 0) {
  265.             $lastRegistrationDate $lastRegistrationDate->modify("-$weekendDays days");
  266.         }
  267.         return $lastRegistrationDate;
  268.     }
  269.     public function getHoursLeftBeforeStart(): int
  270.     {
  271.         return (int)ceil((($this->getStartDate()->getTimestamp() - (new \DateTime())->getTimestamp()) / 3600));
  272.     }
  273. }