<?php
namespace App\Entity;
use App\Enum\CalendarEvent\Type;
use App\Library\Utils\StringUtils;
use App\Repository\CalendarEventRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=CalendarEventRepository::class)
*/
class CalendarEvent
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="date")
*/
private $startDate;
/**
* @ORM\Column(type="date")
*/
private $endDate;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $price;
/**
* @ORM\Column(type="smallint", nullable=true)
*/
private $fromLevel;
/**
* @ORM\Column(type="smallint", nullable=true)
*/
private $earnLevel;
/**
* @ORM\Column(type="time")
*/
private $startTime;
/**
* @ORM\Column(type="time")
*/
private $endTime;
/**
* @ORM\Column(type="enum", options={"values"="calendar_event_enum"})
*/
private $type = Type::TYPE_COURSE;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isCandidateReviewRequired;
/**
* @ORM\ManyToOne(targetEntity=PaymentRequisites::class)
* @ORM\JoinColumn(nullable=true)
*/
private $paymentRequisites;
public function __construct()
{
$this->startDate = new \DateTime();
$this->endDate = new \DateTime();
$this->startTime = new \DateTime('10:00:00');
$this->endTime = new \DateTime('13:00:00');
}
public function getShortInfo(): string
{
return $this->getName() . " " . $this->getStartDate()->format('d.m.Y') .
($this->getFromLevel() ? " (с " . $this->getFromLevel() . " ур.)" : " (все ур.)");
}
public function getText(array $options = null)
{
$options = array_merge([
"quotes" => false,
], $options ?? []);
$result = ($options['quotes'] ? $this->getNameText() : $this->getName());
return $result;
}
public function getNameText(): string
{
return $this->getTypeText() . " «" . $this->getName() . "»";
}
public function getOnTypeText(): string
{
$typeText = null;
switch ($this->getType()) {
case Type::TYPE_COURSE:
$typeText = "курс";
break;
default:
$typeText = $this->getTypeText();
break;
}
return $typeText;
}
public function getOnNameText(): string
{
$typeText = $this->getOnTypeText();
return $typeText . " «" . $this->getName() . "»";
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getStartDate(): ?\DateTimeInterface
{
return $this->startDate;
}
public function setStartDate(\DateTimeInterface $startDate): self
{
$this->startDate = $startDate;
return $this;
}
public function getEndDate(): ?\DateTimeInterface
{
return $this->endDate;
}
public function setEndDate(\DateTimeInterface $endDate): self
{
$this->endDate = $endDate;
return $this;
}
public function getPrice(): ?int
{
return $this->price;
}
public function setPrice(int $price): self
{
$this->price = $price;
return $this;
}
public function getFromLevel(): ?int
{
return $this->fromLevel;
}
public function getFromLevelText(): string
{
if ($this->getFromLevel() === null) {
return "";
}
if ($this->getFromLevel() === 0) {
return "все ур.";
}
return "с" . StringUtils::getEnding($this->getFromLevel(), "", "о", "") . " " . (string)$this->getFromLevel() . " ур.";
}
public function setFromLevel(int $fromLevel): self
{
$this->fromLevel = $fromLevel;
return $this;
}
public function getEarnLevel(): ?int
{
return $this->earnLevel;
}
public function setEarnLevel(?int $level): self
{
$this->earnLevel = $level;
return $this;
}
public function getStartTime(): ?\DateTimeInterface
{
return $this->startTime;
}
public function setStartTime(\DateTimeInterface $startTime): self
{
$this->startTime = $startTime;
return $this;
}
public function getEndTime(): ?\DateTimeInterface
{
return $this->endTime;
}
public function setEndTime(\DateTimeInterface $endTime): self
{
$this->endTime = $endTime;
return $this;
}
public function getType()
{
return $this->type;
}
public function getTypeText(): string
{
return Type::getText($this->getType());
}
public function setType($type): self
{
$this->type = $type;
return $this;
}
public function getTopic(): string
{
if ($this->type === Type::TYPE_COURSE) {
return "Курс «" . $this->getName() . "»";
} else {
return $this->getName();
}
}
public function isIsCandidateReviewRequired(): ?bool
{
return $this->isCandidateReviewRequired;
}
public function setIsCandidateReviewRequired(?bool $isCandidateReviewRequired): self
{
$this->isCandidateReviewRequired = $isCandidateReviewRequired;
return $this;
}
public function getPaymentRequisites(): ?PaymentRequisites
{
return $this->paymentRequisites;
}
public function setPaymentRequisites(?PaymentRequisites $paymentRequisites): self
{
$this->paymentRequisites = $paymentRequisites;
return $this;
}
public function getNameWithCode(): string
{
return "Код " . $this->getPrice() . " – " . $this->getShortInfo();
}
public function isFinished(): bool
{
if (!$this->getEndDate()) {
return true;
}
$now = new \DateTime();
return $this->getEndDate() < $now;
}
public function isStarted(int $addDays = null): bool
{
if (!$this->getStartDate() || $this->isFinished()) {
return false;
}
$now = (new \DateTime())->setTime(0, 0, 0);
$startDate = (clone $this->getStartDate())->setTime(0, 0, 0);
if ($addDays) {
$startDate = $startDate->modify("+$addDays days");
}
return $startDate <= $now;
}
public function getLastRegistrationDate(): ?\DateTime
{
if (!$this->getStartDate()) {
return null;
}
$lastRegistrationDate = (clone $this->getStartDate())->modify("-2 days");
$weekendDays = 0;
if (in_array((int)$lastRegistrationDate->format('N'), [6, 7])) {
$weekendDays = (int)$lastRegistrationDate->format('N') - 5;
}
if ($weekendDays > 0) {
$lastRegistrationDate = $lastRegistrationDate->modify("-$weekendDays days");
}
return $lastRegistrationDate;
}
public function getHoursLeftBeforeStart(): int
{
return (int)ceil((($this->getStartDate()->getTimestamp() - (new \DateTime())->getTimestamp()) / 3600));
}
}