src/Entity/Candidate.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Enum\Candidate\Status;
  4. use App\Repository\CandidateRepository;
  5. use Doctrine\ORM\Mapping as ORM;
  6. /**
  7.  * @ORM\Entity(repositoryClass=CandidateRepository::class)
  8.  */
  9. class Candidate
  10. {
  11.     const CANDIDATE_FIELD_STATUS 'candidate_status';
  12.     const CANDIDATE_FIELD_PERSON 'candidate_person';
  13.     const CANDIDATE_FIELD_AUTHOR 'candidate_author';
  14.     const CANDIDATE_FIELD_CALENDAR_EVENT 'candidate_calendar_event';
  15.     const CANDIDATE_FIELD_CREATED_AT 'candidate_created_at';
  16.     const CANDIDATE_FIELD_APPROVED_AT 'candidate_approved_at';
  17.     /**
  18.      * @ORM\Id
  19.      * @ORM\GeneratedValue
  20.      * @ORM\Column(type="integer")
  21.      */
  22.     private $id;
  23.     /**
  24.      * @ORM\Column(type="enum", options={"values": "candidate_enum"})
  25.      */
  26.     private $status Status::STATUS_DRAFT;
  27.     /**
  28.      * @ORM\ManyToOne(targetEntity=User::class)
  29.      * @ORM\JoinColumn(nullable=false)
  30.      */
  31.     private $author;
  32.     /**
  33.      * @ORM\ManyToOne(targetEntity=Person::class)
  34.      * @ORM\JoinColumn(nullable=true)
  35.      */
  36.     private $person;
  37.     /**
  38.      * @ORM\ManyToOne(targetEntity=CalendarEvent::class)
  39.      */
  40.     private $calendarEvent;
  41.     /**
  42.      * @ORM\Column(type="datetime")
  43.      */
  44.     private $createdAt;
  45.     /**
  46.      * @ORM\Column(type="text", nullable=true)
  47.      */
  48.     private $viewerComment;
  49.     /**
  50.      * @ORM\Column(type="datetime", nullable=true)
  51.      */
  52.     private $approvedAt;
  53.     /**
  54.      * @ORM\ManyToOne(targetEntity=Image::class, cascade={"persist", "remove"})
  55.      */
  56.     private $image;
  57.     /**
  58.      * @ORM\Column(type="text", nullable=true)
  59.      */
  60.     private $comment;
  61.     /**
  62.      * @ORM\Column(type="boolean", nullable=true)
  63.      */
  64.     private $paymentDeferment;
  65.     /**
  66.      * @ORM\ManyToOne(targetEntity=User::class)
  67.      */
  68.     private $selectedAuthor;
  69.     /**
  70.      * @ORM\Column(type="text", nullable=true)
  71.      */
  72.     private $recommendations;
  73.     /**
  74.      * @ORM\ManyToOne(targetEntity=User::class)
  75.      */
  76.     private $curator;
  77.     /**
  78.      * @ORM\Column(type="datetime")
  79.      */
  80.     private $updatedAt;
  81.     /**
  82.      * @ORM\Column(type="string", length=255, nullable=true)
  83.      */
  84.     private $reviewApproveComment;
  85.     /**
  86.      * @ORM\Column(type="boolean", nullable=true)
  87.      */
  88.     private $accessByReceipt;
  89.     public function __construct()
  90.     {
  91.         $this->createdAt = new \DateTime();
  92.         $this->updatedAt = new \DateTime();
  93.     }
  94.     public function getId(): ?int
  95.     {
  96.         return $this->id;
  97.     }
  98.     public function getStatus()
  99.     {
  100.         return $this->status;
  101.     }
  102.     public function isStatusApproved(): bool
  103.     {
  104.         return $this->status === Status::STATUS_APPROVED;
  105.     }
  106.     public function getCalendarEventListedVirtualStatus(bool $hasPayments): string
  107.     {
  108.         if ($this->isStatusApproved()) {
  109.             if ($hasPayments) {
  110.                 return "approved";
  111.             } else {
  112.                 return "unpaid";
  113.             }
  114.         } else if ($this->isStatusLeaved()) {
  115.             return "leaved";
  116.         } else {
  117.             return "not_active";
  118.         }
  119.     }
  120.     public function getCalendarEventListedVirtualStatusText(bool $hasPayments): string
  121.     {
  122.         if ($this->isStatusApproved()) {
  123.             if ($hasPayments) {
  124.                 return "Зачислен";
  125.             } else if ($this->accessByReceipt) {
  126.                 return "Зачислен по квитанции";
  127.             } else {
  128.                 return "Не оплачен";
  129.             }
  130.         } else if ($this->isStatusLeaved()) {
  131.             return "Выбыл с мероприятия";
  132.         } else if ($this->isStatusRefused()) {
  133.             return "Не пошел";
  134.         } else {
  135.             return "Не активен";
  136.         }
  137.     }
  138.     public function getCalendarEventListedVirtualStatusCssClass(bool $hasPayments): string
  139.     {
  140.         if ($this->isStatusApproved()) {
  141.             if ($hasPayments || $this->accessByReceipt) {
  142.                 return "success";
  143.             } else {
  144.                 return "light";
  145.             }
  146.         } else if ($this->isStatusLeaved()) {
  147.             return "warning";
  148.         } else {
  149.             return "danger";
  150.         }
  151.     }
  152.     public function isStatusDeclined(): bool
  153.     {
  154.         return $this->status === Status::STATUS_DECLINED;
  155.     }
  156.     public function isStatusRefused(): bool
  157.     {
  158.         return $this->status === Status::STATUS_REFUSED;
  159.     }
  160.     public function setStatus($status): self
  161.     {
  162.         if ($this->status != Status::STATUS_APPROVED &&
  163.             $this->status != Status::STATUS_REFUSED &&
  164.             $status == Status::STATUS_REFUSED) {
  165.             throw new \InvalidArgumentException("Нельзя поменять статус кандидата на Не пошел, "
  166.             "текущий статус должен быть Принят, т.к. под статусом Не пошел подразумевается, что кандидат прошел отбор.");
  167.         }
  168.         $this->status $status;
  169.         if ($status == Status::STATUS_APPROVED && !$this->getApprovedAt()) {
  170.             $this->setApprovedAt(new \DateTime());
  171.         }
  172.         return $this;
  173.     }
  174.     public function getAuthor(): ?User
  175.     {
  176.         // if ($this->getSelectedAuthor()) {
  177.         //     return $this->getSelectedAuthor();
  178.         // }
  179.         return $this->author;
  180.     }
  181.     public function setAuthor(?User $author): self
  182.     {
  183.         $this->author $author;
  184.         return $this;
  185.     }
  186.     public function getPerson(): ?Person
  187.     {
  188.         return $this->person;
  189.     }
  190.     public function setPerson(?Person $person): self
  191.     {
  192.         $this->person $person;
  193.         return $this;
  194.     }
  195.     public function getCalendarEvent(): ?CalendarEvent
  196.     {
  197.         return $this->calendarEvent;
  198.     }
  199.     public function setCalendarEvent(?CalendarEvent $calendarEvent): self
  200.     {
  201.         $this->calendarEvent $calendarEvent;
  202.         return $this;
  203.     }
  204.     public function getCreatedAt(): ?\DateTimeInterface
  205.     {
  206.         return $this->createdAt;
  207.     }
  208.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  209.     {
  210.         $this->createdAt $createdAt;
  211.         return $this;
  212.     }
  213.     public function getViewerComment(): ?string
  214.     {
  215.         return $this->viewerComment;
  216.     }
  217.     public function setViewerComment(?string $viewerComment): self
  218.     {
  219.         $this->viewerComment $viewerComment;
  220.         return $this;
  221.     }
  222.     public function getAllComments(): string
  223.     {
  224.         $comments "";
  225.         if ($this->getComment()) {
  226.             $comments $this->getComment();
  227.             $comments str_ends_with($comments'.') ? $comments : ($comments '.');
  228.         }
  229.         if ($this->getReviewApproveComment()) {
  230.             if ($comments) {
  231.                 $comments .= "\n";
  232.             }
  233.             $reviewApproveComment $this->getReviewApproveComment();
  234.             $reviewApproveComment str_ends_with($reviewApproveComment'.')
  235.                 ? $reviewApproveComment : ($reviewApproveComment '.');
  236.             $comments .= $reviewApproveComment;
  237.         }
  238.         return $comments;
  239.     }
  240.     public function getApprovedAt(): ?\DateTimeInterface
  241.     {
  242.         return $this->approvedAt;
  243.     }
  244.     public function setApprovedAt(?\DateTimeInterface $approvedAt): self
  245.     {
  246.         $this->approvedAt $approvedAt;
  247.         return $this;
  248.     }
  249.     public function getImage(): ?Image
  250.     {
  251.         return $this->image;
  252.     }
  253.     public function setImage(?Image $image): self
  254.     {
  255.         $this->image $image;
  256.         return $this;
  257.     }
  258.     public function getComment(): ?string
  259.     {
  260.         return $this->comment;
  261.     }
  262.     public function setComment(?string $comment): self
  263.     {
  264.         $this->comment $comment;
  265.         return $this;
  266.     }
  267.     public function addComment(string $textbool $checkContains true,
  268.                                bool $ucfirst falsebool $addDot false): void
  269.     {
  270.         if ($ucfirst) {
  271.             $text mb_ucfirst($text'UTF-8');
  272.         }
  273.         if ($addDot && mb_substr($text, -1null'UTF-8') !== '.') {
  274.             $text .= '.';
  275.         }
  276.         $newComment trim($this->comment ?? "");
  277.         if (!$checkContains || mb_strpos($newComment $textnull'UTF-8') === false) {
  278.             $newComment $newComment
  279.                 . ($addDot && $newComment && mb_substr($newComment, -1null'UTF-8') !== '.' '.' '')
  280.                 . ($newComment ?  "\n" "")
  281.                 . $text;
  282.             $this->setComment($newComment);
  283.         }
  284.     }
  285.     public function getStatusText(): string
  286.     {
  287.         return Status::getText($this->status);
  288.     }
  289.     public function getVirtualStatusText(): string
  290.     {
  291.         if ($this->isVirtualStatusNotRequired()) {
  292.             return Status::getText(Status::STATUS_NOT_REQUIRED);
  293.         }
  294.         return Status::getText($this->status);
  295.     }
  296.     public function getCssClass(): string
  297.     {
  298.         return Status::getCssClass($this->status);
  299.     }
  300.     public function isStatusLeaved(): bool
  301.     {
  302.         return $this->status === Status::STATUS_LEAVED;
  303.     }
  304.     public function isVirtualStatusNotRequired(): bool
  305.     {
  306.         return Status::isVirtualStatusNotRequired(
  307.             $this->status,
  308.             $this->getCalendarEvent(),
  309.             $this->getPerson()
  310.         );
  311.     }
  312.     public function getVirtualCssClass(): string
  313.     {
  314.         if ($this->isVirtualStatusNotRequired()) {
  315.             return Status::getCssClass(Status::STATUS_NOT_REQUIRED);
  316.         }
  317.         return Status::getCssClass($this->status);
  318.     }
  319.     public function isPaymentDeferment(): ?bool
  320.     {
  321.         return $this->paymentDeferment;
  322.     }
  323.     public function setPaymentDeferment(?bool $paymentDeferment): self
  324.     {
  325.         $this->paymentDeferment $paymentDeferment;
  326.         return $this;
  327.     }
  328.     public function getSelectedAuthor(): ?User
  329.     {
  330.         return $this->selectedAuthor;
  331.     }
  332.     public function setSelectedAuthor(?User $selectedAuthor): self
  333.     {
  334.         $this->selectedAuthor $selectedAuthor;
  335.         return $this;
  336.     }
  337.     public function getRecommendations(): ?string
  338.     {
  339.         return $this->recommendations;
  340.     }
  341.     public function setRecommendations(?string $recommendations): self
  342.     {
  343.         $this->recommendations $recommendations;
  344.         return $this;
  345.     }
  346.     public function getCurator(): ?User
  347.     {
  348.         return $this->curator;
  349.     }
  350.     public function setCurator(?User $curator): self
  351.     {
  352.         $this->curator $curator;
  353.         return $this;
  354.     }
  355.     public function getUpdatedAt(): ?\DateTimeInterface
  356.     {
  357.         if (!$this->updatedAt) {
  358.             return $this->getCreatedAt();
  359.         }
  360.         return $this->updatedAt;
  361.     }
  362.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  363.     {
  364.         $this->updatedAt $updatedAt;
  365.         return $this;
  366.     }
  367.     public function isTimeToSendReviewResultMessage(): array
  368.     {
  369.         $result = [
  370.             "result" => false,
  371.             'waitMinutes' => null,
  372.         ];
  373.         $minutesLeftSinceUpdate = (int)ceil((time() - $this->getUpdatedAt()->getTimestamp()) / 60);
  374.         if ($this->getCalendarEvent()->getHoursLeftBeforeStart() >= &&
  375.             $minutesLeftSinceUpdate 15
  376.         ) {
  377.             $result['result'] = false;
  378.             $result['waitMinutes'] = 15 $minutesLeftSinceUpdate;
  379.         } else {
  380.             $result['result'] = true;
  381.         }
  382.         return $result;
  383.     }
  384.     public function getReviewApproveComment(): ?string
  385.     {
  386.         return $this->reviewApproveComment;
  387.     }
  388.     public function setReviewApproveComment(?string $reviewApproveComment): self
  389.     {
  390.         $reviewApproveComment trim($reviewApproveComment);
  391.         if (!$reviewApproveComment) {
  392.             return $this;
  393.         }
  394.         $this->reviewApproveComment $reviewApproveComment;
  395.         return $this;
  396.     }
  397.     public function isAccessByReceipt(): ?bool
  398.     {
  399.         return $this->accessByReceipt;
  400.     }
  401.     public function setAccessByReceipt(?bool $accessByReceipt): self
  402.     {
  403.         $this->accessByReceipt $accessByReceipt;
  404.         return $this;
  405.     }
  406. }