<?php
namespace App\Entity;
use App\Enum\Student\Status;
use App\Enum\Student\VirtualStatus;
use App\Library\Utils\Other\Other;
use App\Repository\PersonRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=PersonRepository::class)
*/
class Person
{
const PERSON_FIELD_NAME = 'person_name';
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255)
*/
private $lastName;
/**
* @ORM\OneToOne(targetEntity=Student::class, inversedBy="person", cascade={"persist", "remove"})
*/
private $student;
/**
* @ORM\ManyToOne(targetEntity=City::class)
*/
private $city;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $phone;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $email;
/**
* @ORM\OneToOne(targetEntity=Image::class, cascade={"persist", "remove"})
*/
private $image;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $comment;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $lastName2;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $telegramUserId;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default"=true})
*/
private $canSendTelegramCampaigns = true;
/**
* @ORM\ManyToOne(targetEntity=Person::class)
*/
private $messageReceiver;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isStudent;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isSubscriber;
private $virtualStudentStatus = VirtualStatus::VIRTUAL_STATUS_SUBSCRIBER;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $surname;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
public function __construct()
{
$this->createdAt = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getStudent(): ?Student
{
return $this->student;
}
public function setStudent(?Student $student): self
{
$this->student = $student;
return $this;
}
public function getName(array $options = null): string
{
$options = array_merge(
[
"lastNameFirst" => false
], ($options ?? [])
);
$name = ($options["lastNameFirst"] ? $this->getLastFirstName() : $this->firstName . ' ' . $this->lastName );
return $name;
}
public function isSameName($name): bool
{
$nameParts = explode(' ', $name);
if (count($nameParts) === 2) {
$firstName = $nameParts[0];
$lastName = $nameParts[1];
$firstName = mb_strtolower($firstName, 'UTF-8');
$lastName = mb_strtolower($lastName, 'UTF-8');
$currentFirstName = mb_strtolower($this->firstName, 'UTF-8');
$currentLastName = mb_strtolower($this->lastName, 'UTF-8');
return ($firstName == $currentFirstName && $lastName == $currentLastName)
|| ($firstName == $currentLastName && $lastName == $currentFirstName);
} else {
return false;
}
}
public function getLastFirstName(): string
{
return $this->lastName . ' ' . $this->firstName;
}
public function getLastName2FirstName(): string
{
return $this->lastName2 . ' ' . $this->firstName;
}
public function getNameAndCityText(): string
{
return $this->getName() . " (" .
($this->getCity() && trim($this->getCity()->getName()) ? $this->getCity()->getName() : "город не указан")
. ")";
}
public function getCityText(): string
{
return $this->getCity() ? $this->getCity()->getName() : "город не указан";
}
public function getCity(): ?City
{
return $this->city;
}
public function setCity(?City $city): self
{
$this->city = $city;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
public function getImage(): ?Image
{
return $this->image;
}
public function setImage(?Image $image): self
{
$this->image = $image;
return $this;
}
public function getComment(): ?string
{
return $this->comment;
}
public function setComment(?string $comment): self
{
$this->comment = $comment;
return $this;
}
public function addComment(string $text, bool $checkContains = true,
bool $ucfirst = false, bool $addDot = false): void
{
if ($ucfirst) {
$text = mb_ucfirst($text, 'UTF-8');
}
if ($addDot && mb_substr($text, -1, null, 'UTF-8') !== '.') {
$text .= '.';
}
$newComment = trim($this->comment ?? "");
if (!$checkContains || mb_strpos($newComment , $text, null, 'UTF-8') === false) {
$newComment = $newComment
. ($addDot && $newComment && mb_substr($newComment, -1, null, 'UTF-8') !== '.' ? '.' : '')
. ($newComment ? "\n" : "")
. $text;
$this->setComment($newComment);
}
}
public function getLastName2(): ?string
{
return $this->lastName2;
}
public function setLastName2(?string $lastName2): self
{
$this->lastName2 = $lastName2;
return $this;
}
public function getTelegramUserId(): ?string
{
return $this->telegramUserId;
}
public function setTelegramUserId(?string $telegramUserId): self
{
$this->telegramUserId = $telegramUserId;
return $this;
}
public function isCanSendTelegramCampaigns(): ?bool
{
return $this->canSendTelegramCampaigns;
}
public function setCanSendTelegramCampaigns(?bool $canSendTelegramCampaigns): self
{
$this->canSendTelegramCampaigns = $canSendTelegramCampaigns;
return $this;
}
public function getMessageReceiver(): ?self
{
return $this->messageReceiver;
}
public function setMessageReceiver(?self $messageReceiver): self
{
$this->messageReceiver = $messageReceiver;
return $this;
}
public function isIsStudent(): ?bool
{
return $this->isStudent;
}
public function setIsStudent(?bool $isStudent): self
{
$this->isStudent = $isStudent;
return $this;
}
public function isIsSubscriber(): ?bool
{
return $this->isSubscriber;
}
public function setIsSubscriber(?bool $isSubscriber): self
{
$this->isSubscriber = $isSubscriber;
return $this;
}
public function getVirtualStudentStatus(): ?string
{
if ($this->isIsStudent()) {
return $this->getStudent()->getStatus();
} elseif ($this->isIsSubscriber()) {
return VirtualStatus::VIRTUAL_STATUS_SUBSCRIBER;
}
return null;
}
public function setVirtualStudentStatus(?string $virtualStatus): void
{
if ($virtualStatus === VirtualStatus::VIRTUAL_STATUS_SUBSCRIBER) {
$this->setIsSubscriber(true);
$this->setIsStudent(false);
} elseif (Status::isCorrect($virtualStatus)) {
$this->setIsStudent(true);
$this->setIsSubscriber(false);
$this->getStudent()->setStatus($virtualStatus);
} else {
throw new \Exception("Unknown: " . $virtualStatus);
}
}
public function getVirtualStudentStatusText(): ?string
{
if (Status::isCorrect($this->getVirtualStudentStatus())) {
return $this->getStudent()->getStatusText();
}
return VirtualStatus::getText($this->getVirtualStudentStatus());
}
public function getVirtualStudentStatusCssClass($prefix = null): ?string
{
if (Status::isCorrect($this->getVirtualStudentStatus())) {
return $this->getStudent()->getStatusCssClass($prefix);
}
return VirtualStatus::getCssClass($this->getVirtualStudentStatus(), $prefix);
}
public function getSurname(): ?string
{
return $this->surname;
}
public function setSurname(?string $surname): self
{
$this->surname = $surname;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function isCalendarEventStatusAllowed(CalendarEvent $calendarEvent): bool
{
return $this->getVirtualStudentStatus() == $calendarEvent->getRequireStudentStatus();
}
}