src/Entity/Person.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Enum\Student\Status;
  4. use App\Repository\PersonRepository;
  5. use App\Service\Person\PersonService;
  6. use App\Service\Transaction\TransactionService;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Routing\RouterInterface;
  9. /**
  10.  * @ORM\Entity(repositoryClass=PersonRepository::class)
  11.  */
  12. class Person
  13. {
  14.     const PERSON_FIELD_NAME 'person_name';
  15.     /**
  16.      * @ORM\Id
  17.      * @ORM\GeneratedValue
  18.      * @ORM\Column(type="integer")
  19.      */
  20.     private $id;
  21.     /**
  22.      * @ORM\Column(type="string", length=255)
  23.      */
  24.     private $firstName;
  25.     /**
  26.      * @ORM\Column(type="string", length=255)
  27.      */
  28.     private $lastName;
  29.     /**
  30.      * @ORM\OneToOne(targetEntity=Student::class, inversedBy="person")
  31.      */
  32.     private $student;
  33.     /**
  34.      * @ORM\ManyToOne(targetEntity=City::class)
  35.      */
  36.     private $city;
  37.     /**
  38.      * @ORM\Column(type="string", length=255, nullable=true)
  39.      */
  40.     private $phone;
  41.     /**
  42.      * @ORM\Column(type="string", length=255, nullable=true)
  43.      */
  44.     private $email;
  45.     /**
  46.      * @ORM\OneToOne(targetEntity=Image::class, cascade={"persist", "remove"})
  47.      */
  48.     private $image;
  49.     /**
  50.      * @ORM\Column(type="text", nullable=true)
  51.      */
  52.     private $comment;
  53.     /**
  54.      * @ORM\Column(type="string", length=255, nullable=true)
  55.      */
  56.     private $lastName2;
  57.     /**
  58.      * @ORM\Column(type="string", length=255, nullable=true, unique=true)
  59.      */
  60.     private $telegramUserId;
  61.     /**
  62.      * @ORM\Column(type="boolean", nullable=true, options={"default"=true})
  63.      */
  64.     private $canSendTelegramCampaigns true;
  65.     /**
  66.      * @ORM\ManyToOne(targetEntity=Person::class)
  67.      */
  68.     private $messageReceiver;
  69.     /**
  70.      * @ORM\Column(type="boolean", nullable=true)
  71.      */
  72.     private $isStudent;
  73.     /**
  74.      * @ORM\Column(type="boolean", nullable=true)
  75.      */
  76.     private $isSubscriber;
  77.     /**
  78.      * @var array|null
  79.      */
  80.     private $balance null;
  81.     /**
  82.      * @var array|null
  83.      */
  84.     private $balanceNoInvoices null;
  85.     /**
  86.      * @ORM\Column(type="string", length=255, nullable=true)
  87.      */
  88.     private $surname;
  89.     /**
  90.      * @ORM\Column(type="datetime")
  91.      */
  92.     private $createdAt;
  93.     /**
  94.      * @var TransactionService
  95.      */
  96.     private $transactionService;
  97.     /**
  98.      * @ORM\Column(type="text", nullable=true)
  99.      */
  100.     private $emails;
  101.     /**
  102.      * @ORM\ManyToOne(targetEntity=Job::class)
  103.      */
  104.     private $job;
  105.     /**
  106.      * @ORM\Column(type="date", nullable=true)
  107.      */
  108.     private $birthday;
  109.     /**
  110.      * @ORM\Column(type="enum", options={"values"="person_enum", "default"="active"}))
  111.      */
  112.     private $status \App\Enum\Person\Status::STATUS_ACTIVE;
  113.     /**
  114.      * @ORM\Column(type="boolean", nullable=true)
  115.      */
  116.     private $isUnsubscribed;
  117.     /**
  118.      * @ORM\Column(type="datetime", nullable=true)
  119.      */
  120.     private $tgChannelAddErrorDate;
  121.     /**
  122.      * @ORM\Column(type="text", nullable=true)
  123.      */
  124.     private $tgChannelAddResponse;
  125.     private ?RouterInterface $router null;
  126.     public function __construct()
  127.     {
  128.         $this->createdAt = new \DateTime();
  129.     }
  130.     public function __toString()
  131.     {
  132.         return PersonService::getFirstLastNameWithOld($this) . " (" .
  133.             $this->getStudentLevelText() . ", " .
  134.             ($this->getCity() && trim($this->getCity()->getName()) ? $this->getCity()->getName() : "город не указан")
  135.             . ")";
  136.     }
  137.     public function getId(): ?int
  138.     {
  139.         return $this->id;
  140.     }
  141.     public function getFirstName(): ?string
  142.     {
  143.         return $this->firstName;
  144.     }
  145.     public function setFirstName(string $firstName): self
  146.     {
  147.         $this->firstName $firstName;
  148.         return $this;
  149.     }
  150.     public function getLastName(): ?string
  151.     {
  152.         return $this->lastName;
  153.     }
  154.     public function setLastName(string $lastName): self
  155.     {
  156.         $this->lastName $lastName;
  157.         return $this;
  158.     }
  159.     public function getStudent(): ?Student
  160.     {
  161.         return $this->student;
  162.     }
  163.     public function setStudent(?Student $student): self
  164.     {
  165.         $this->student $student;
  166.         return $this;
  167.     }
  168.     public function getName(array $options null): string
  169.     {
  170.         $options array_merge(
  171.             [
  172.                 "lastNameFirst" => false
  173.             ], ($options ?? [])
  174.         );
  175.         $name = ($options["lastNameFirst"] ? $this->getLastFirstName() : $this->firstName ' ' $this->lastName );
  176.         return $name;
  177.     }
  178.     public function isSameName($name): bool
  179.     {
  180.         $nameParts explode(' '$name);
  181.         if (count($nameParts) === 2) {
  182.             $firstName $nameParts[0];
  183.             $lastName $nameParts[1];
  184.             $firstName mb_strtolower($firstName'UTF-8');
  185.             $lastName mb_strtolower($lastName'UTF-8');
  186.             $currentFirstName mb_strtolower($this->firstName'UTF-8');
  187.             $currentLastName mb_strtolower($this->lastName'UTF-8');
  188.             return ($firstName == $currentFirstName && $lastName == $currentLastName)
  189.                 || ($firstName == $currentLastName && $lastName == $currentFirstName);
  190.         } else {
  191.             return false;
  192.         }
  193.     }
  194.     public function getLastFirstName(): string
  195.     {
  196.         return $this->lastName ' ' $this->firstName;
  197.     }
  198.     public function getLastName2FirstName(): string
  199.     {
  200.         return $this->lastName2 ' ' $this->firstName;
  201.     }
  202.     public function getNameAndCityText(): string
  203.     {
  204.         return $this->getName() . " (" .
  205.             ($this->getCity() && trim($this->getCity()->getName()) ? $this->getCity()->getName() : "город не указан")
  206.             . ")";
  207.     }
  208.     public function getCityText(): string
  209.     {
  210.         return $this->getCity() ? $this->getCity()->getName() : "город не указан";
  211.     }
  212.     public function getCity(): ?City
  213.     {
  214.         return $this->city;
  215.     }
  216.     public function setCity(?City $city): self
  217.     {
  218.         $this->city $city;
  219.         return $this;
  220.     }
  221.     public function getPhone(): ?string
  222.     {
  223.         return $this->phone;
  224.     }
  225.     public function setPhone(?string $phone): self
  226.     {
  227.         $this->phone $phone;
  228.         return $this;
  229.     }
  230.     public function getEmail($skipErrors true): ?string
  231.     {
  232.         $email $this->email ?? "";
  233.         if (!$skipErrors && preg_match('/\s|[,]/'$email)) {
  234.             throw new \Exception("E-mail contains restricted characters: " $email);
  235.         }
  236.         return $this->email;
  237.     }
  238.     public function setEmail(?string $email): self
  239.     {
  240.         $this->email $email;
  241.         return $this;
  242.     }
  243.     public function getImage(): ?Image
  244.     {
  245.         return $this->image;
  246.     }
  247.     public function setImage(?Image $image): self
  248.     {
  249.         $this->image $image;
  250.         return $this;
  251.     }
  252.     public function getComment(): ?string
  253.     {
  254.         return $this->comment;
  255.     }
  256.     public function setComment(?string $comment): self
  257.     {
  258.         $this->comment $comment;
  259.         return $this;
  260.     }
  261.     public function addComment(string $textbool $checkContains true,
  262.                                bool $ucfirst falsebool $addDot false): void
  263.     {
  264.         if ($ucfirst) {
  265.             $text mb_ucfirst($text'UTF-8');
  266.         }
  267.         if ($addDot && mb_substr($text, -1null'UTF-8') !== '.') {
  268.             $text .= '.';
  269.         }
  270.         $newComment trim($this->comment ?? "");
  271.         if (!$checkContains || mb_strpos($newComment $textnull'UTF-8') === false) {
  272.             $newComment $newComment
  273.                 . ($addDot && $newComment && mb_substr($newComment, -1null'UTF-8') !== '.' '.' '')
  274.                 . ($newComment ?  "\n" "")
  275.                 . $text;
  276.             $this->setComment($newComment);
  277.         }
  278.     }
  279.     public function getLastName2(): ?string
  280.     {
  281.         return $this->lastName2;
  282.     }
  283.     public function setLastName2(?string $lastName2): self
  284.     {
  285.         $this->lastName2 $lastName2;
  286.         return $this;
  287.     }
  288.     public function getTelegramUserId(): ?string
  289.     {
  290.         return $this->telegramUserId;
  291.     }
  292.     public function setTelegramUserId(?string $telegramUserId): self
  293.     {
  294.         $this->telegramUserId $telegramUserId;
  295.         return $this;
  296.     }
  297.     public function isCanSendTelegramCampaigns(): ?bool
  298.     {
  299.         if ($this->isUnsubscribed) {
  300.             return false;
  301.         }
  302.         return $this->canSendTelegramCampaigns;
  303.     }
  304.     public function setCanSendTelegramCampaigns(?bool $canSendTelegramCampaigns): self
  305.     {
  306.         $this->canSendTelegramCampaigns $canSendTelegramCampaigns;
  307.         return $this;
  308.     }
  309.     public function getMessageReceiver(): ?self
  310.     {
  311.         return $this->messageReceiver;
  312.     }
  313.     public function setMessageReceiver(?self $messageReceiver): self
  314.     {
  315.         $this->messageReceiver $messageReceiver;
  316.         return $this;
  317.     }
  318.     public function isStudent(): ?bool
  319.     {
  320.         return Status::isStudentStatus($this->getStudentStatus());
  321.     }
  322.     public function setIsStudent(?bool $isStudent): self
  323.     {
  324.         $this->isStudent $isStudent;
  325.         return $this;
  326.     }
  327.     public function isSubscriber(): ?bool
  328.     {
  329.         return Status::isSubscriberStatus($this->getStudentStatus());
  330.     }
  331.     public function isActiveSubscriberStatus(): bool
  332.     {
  333.         return Status::isActiveSubscriberStatus($this->getStudentStatus());
  334.     }
  335.     public function setIsSubscriber(?bool $isSubscriber): self
  336.     {
  337.         $this->isSubscriber $isSubscriber;
  338.         return $this;
  339.     }
  340. //    public function getVirtualStudentStatus(): ?string
  341. //    {
  342. //        if ($this->getStatus() == \App\Enum\Person\Status::STATUS_DELETED) {
  343. //            return Status::STATUS_DELETED;
  344. //        }
  345. //
  346. //        if ($this->isStudent()) {
  347. //            return $this->getStudent()->getStatus();
  348. //        } elseif ($this->isSubscriber()) {
  349. //            return VirtualStatus::VIRTUAL_STATUS_SUBSCRIBER;
  350. //        }
  351. //        return null;
  352. //    }
  353.     public function getStudentStatus(): ?string
  354.     {
  355.         return $this->getStudent() ? $this->getStudent()->getStatus() : null;
  356.     }
  357.     public function getStudentStatusText(): ?string
  358.     {
  359.         return $this->getStudent() ? $this->getStudent()->getStatusText() : null;
  360.     }
  361.     public function getStudentStatusCssClass($prefix null): ?string
  362.     {
  363.         return $this->getStudent() ? $this->getStudent()->getStatusCssClass($prefix) : null;
  364.     }
  365.     public function getSurname(): ?string
  366.     {
  367.         return $this->surname;
  368.     }
  369.     public function setSurname(?string $surname): self
  370.     {
  371.         $this->surname $surname;
  372.         return $this;
  373.     }
  374.     public function getFullNameWithSurname(): string
  375.     {
  376.         return $this->getName() . ($this->surname " " $this->surname "");
  377.     }
  378.     public function getFullNameWithSurnameAndCityText(): string
  379.     {
  380.         return $this->getFullNameWithSurname() . " (" .
  381.             ($this->getCity() && trim($this->getCity()->getName()) ? $this->getCity()->getName() : "город не указан")
  382.             . ")";
  383.     }
  384.     public function getCreatedAt(): ?\DateTimeInterface
  385.     {
  386.         return $this->createdAt;
  387.     }
  388.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  389.     {
  390.         $this->createdAt $createdAt;
  391.         return $this;
  392.     }
  393.     public function isCalendarEventStatusAllowed(CalendarEvent $calendarEvent): bool
  394.     {
  395.         if (!$calendarEvent->getRequireStudentStatus()) {
  396.             return true;
  397.         }
  398.         return $this->getStudentStatus() == $calendarEvent->getRequireStudentStatus();
  399.     }
  400.     public function getBalance($forceUpdate false): array
  401.     {
  402.         if ($this->balance === null || $forceUpdate) {
  403.             $this->setBalances();
  404.         }
  405.         return $this->balance;
  406.     }
  407.     public function getBalanceNoInvoices($forceUpdate false): array
  408.     {
  409.         if ($this->balanceNoInvoices === null || $forceUpdate) {
  410.             $this->setBalances();
  411.         }
  412.         return $this->balanceNoInvoices;
  413.     }
  414.     private function setBalances()
  415.     {
  416.         $balanceResult $this->transactionService->getPersonBalance($this);
  417.         $this->balance $balanceResult['balance'];
  418.         $this->balanceNoInvoices $balanceResult['balanceNoInvoices'];
  419.     }
  420.     /**
  421.      * @param FinanceCategory|int|null $financeCategory
  422.      * @param $forceUpdate
  423.      * @return int
  424.      */
  425.     public function getBalanceByFinanceCategory($financeCategory,
  426.                                                 $forceUpdate false): int
  427.     {
  428.         if (!$financeCategory) {
  429.             return 0;
  430.         }
  431.         $balance $this->getBalance($forceUpdate);
  432.         if (!is_numeric($financeCategory)) {
  433.             $financeCategory $financeCategory->getId();
  434.         }
  435.         return $balance[$financeCategory] ?? 0;
  436.     }
  437.     public function setTransactionService(TransactionService $transactionService): void
  438.     {
  439.         $this->transactionService $transactionService;
  440.     }
  441.     public function getEmails(): ?string
  442.     {
  443.         return $this->emails;
  444.     }
  445.     public function setEmails(?string $emails): self
  446.     {
  447.         $this->emails $emails;
  448.         return $this;
  449.     }
  450.     public function getJob(): ?Job
  451.     {
  452.         return $this->job;
  453.     }
  454.     public function setJob(?Job $job): self
  455.     {
  456.         $this->job $job;
  457.         return $this;
  458.     }
  459.     public function getBirthday(): ?\DateTimeInterface
  460.     {
  461.         return $this->birthday;
  462.     }
  463.     public function setBirthday(?\DateTimeInterface $birthday): self
  464.     {
  465.         $this->birthday $birthday;
  466.         return $this;
  467.     }
  468.     public function getStatus()
  469.     {
  470.         return $this->status;
  471.     }
  472.     public function setStatus($status): self
  473.     {
  474.         $this->status $status;
  475.         return $this;
  476.     }
  477.     public function isIsUnsubscribed(): ?bool
  478.     {
  479.         return $this->isUnsubscribed;
  480.     }
  481.     public function setIsUnsubscribed(?bool $isUnsubscribed): self
  482.     {
  483.         if (!$isUnsubscribed) {
  484.             $this->setCanSendTelegramCampaigns(true);
  485.         }
  486.         $this->isUnsubscribed $isUnsubscribed;
  487.         return $this;
  488.     }
  489.     public function getTgChannelAddErrorDate(): ?\DateTimeInterface
  490.     {
  491.         return $this->tgChannelAddErrorDate;
  492.     }
  493.     public function setTgChannelAddErrorDate(?\DateTimeInterface $tgChannelAddErrorDate): self
  494.     {
  495.         $this->tgChannelAddErrorDate $tgChannelAddErrorDate;
  496.         return $this;
  497.     }
  498.     public function getTgChannelAddResponse(): ?string
  499.     {
  500.         return $this->tgChannelAddResponse;
  501.     }
  502.     public function setTgChannelAddResponse(?string $tgChannelAddResponse): self
  503.     {
  504.         $this->tgChannelAddResponse $tgChannelAddResponse;
  505.         return $this;
  506.     }
  507.     public function getStudentLevelText(): string
  508.     {
  509.         if (!$this->getStudent()) {
  510.             return "нет уровня";
  511.         }
  512.         return $this->getStudent()->getLevelText();
  513.     }
  514.     public function archiveTelegramUserId()
  515.     {
  516.         $tgUserId $this->getTelegramUserId();
  517.         $shouldArchive = !str_starts_with($tgUserId"archived:");
  518.         if ($shouldArchive) {
  519.             $this->setTelegramUserId("archived:" $tgUserId
  520.                 "_" date("Y-m-d_H-i-s")
  521.             );
  522.         }
  523.     }
  524.     public function getLearnerUrl(): string
  525.     {
  526.         return getenv("SERVER_URL")
  527.             . $this->router->generate("moderator_learner_edit",
  528.             ["id" => $this->getId()]);
  529.     }
  530.     public function getLearnerAnchorHtml(): string
  531.     {
  532.         $url $this->getLearnerUrl();
  533.         $html "<a href='" $url "'>" $this "</a>";
  534.         return $html;
  535.     }
  536.     public function setRouter(RouterInterface $router)
  537.     {
  538.         $this->router $router;
  539.     }
  540. }