<?php
namespace EADPlataforma\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;
use EADPlataforma\Validator\Constraints as EADAssert;
use EADPlataforma\Enum\GroupEnum;
use EADPlataforma\Util\StringUtil;
use \DateTime;
/**
* Group
*
* @ORM\Table(name="`group`")
* @ORM\Table(name="`group`", indexes={
* @ORM\Index(name="fk_group_user_manager_id", columns={"user_manager_id"}),
* @ORM\Index(name="fk_group_user_delete_id", columns={"user_delete_id"})
* })
*
* @ORM\Entity(repositoryClass="EADPlataforma\Repository\GroupRepository")
*/
class Group
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @Assert\NotBlank(
* message = "Deleted not informed"
* )
*
* @Assert\Choice(
* choices = {
* GroupEnum::ITEM_NO_DELETED,
* GroupEnum::ITEM_ON_TRASH,
* GroupEnum::ITEM_DELETED
* },
* message = "Delete Option Invalid"
* )
*
* @var int
*
* @ORM\Column(name="deleted", type="integer", nullable=false, options={"default"="0"})
*/
private $deleted = GroupEnum::ITEM_NO_DELETED;
/**
* @Assert\NotBlank(
* message = "Name not informed"
* )
*
* @Assert\Length(
* min = 0,
* max = 250
* )
*
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* @Assert\NotBlank(
* message = "Status not informed"
* )
*
* @Assert\Choice(
* choices = { GroupEnum::BLOCK, GroupEnum::UNBLOCK },
* message = "Status Invalid"
* )
*
* @var int
*
* @ORM\Column(name="status", type="integer", nullable=false, options={"default"="1"})
*/
private $status = GroupEnum::UNBLOCK;
/**
* @Assert\NotBlank(
* message = "TypeDateAccess not informed"
* )
*
* @Assert\Choice(
* choices = { GroupEnum::TYPE_DATE_DEFAULT, GroupEnum::TYPE_DATE_FIXED, GroupEnum::TYPE_DATE_PERIOD },
* message = "Invalid date period type"
* )
*
* @var int
*
* @ORM\Column(name="type_date_access", type="integer", nullable=false, options={"default"="0"})
*/
private $typeDateAccess = GroupEnum::TYPE_DATE_DEFAULT;
/**
* @var DateTime|null
*
* @ORM\Column(name="date_access_conclusion", type="datetime", nullable=true)
*/
private $dateAccessConclusion;
/**
* @var int|null
*
* @ORM\Column(name="date_access_days", type="integer", nullable=true)
*/
private $dateAccessDays;
/**
* @Assert\NotBlank(
* message = "TypeDateSupport not informed"
* )
*
* @Assert\Choice(
* choices = { GroupEnum::TYPE_DATE_DEFAULT, GroupEnum::TYPE_DATE_FIXED, GroupEnum::TYPE_DATE_PERIOD },
* message = "Invalid date period type"
* )
*
* @var int
*
* @ORM\Column(name="type_date_support", type="integer", nullable=false, options={"default"="0"})
*/
private $typeDateSupport = GroupEnum::TYPE_DATE_DEFAULT;
/**
* @var DateTime|null
*
* @ORM\Column(name="date_support_conclusion", type="datetime", nullable=true)
*/
private $dateSupportConclusion;
/**
* @var int|null
*
* @ORM\Column(name="date_support_days", type="integer", nullable=true)
*/
private $dateSupportDays;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Course", inversedBy="`group`")
* @ORM\JoinTable(name="group_x_course",
* joinColumns={
* @ORM\JoinColumn(name="group_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="course_id", referencedColumnName="id")
* }
* )
*/
private $course;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="User", inversedBy="`group`")
* @ORM\JoinTable(name="group_x_user",
* joinColumns={
* @ORM\JoinColumn(name="group_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* }
* )
*/
private $user;
/**
* @Assert\NotBlank(
* message = "User not informed"
* )
*
* @Assert\Valid
*
* @var \EADPlataforma\Entity\User
*
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user_manager_id", referencedColumnName="id", nullable=false)
* })
*/
private $userManager;
/**
* @Assert\Valid
*
* @var \EADPlataforma\Entity\User
*
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user_delete_id", referencedColumnName="id", nullable=true)
* })
*/
private $userDelete;
/**
* @Assert\Choice(
* choices = {
* GroupEnum::INDIVIDUAL,
* GroupEnum::CASCADE
* },
* message = "Type Delete Invalid"
* )
*
* @var int
*
* @ORM\Column(name="type_delete", type="integer", nullable=true)
*/
private $typeDelete;
/**
* @EADAssert\DateTimeEAD(
* message = "Date Delete Invalid"
* )
*
* @var \DateTime|null
*
* @ORM\Column(name="date_delete", type="datetime", nullable=true)
*/
private $dateDelete;
/**
* Constructor
*/
public function __construct()
{
$this->course = new \Doctrine\Common\Collections\ArrayCollection();
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return StringUtil::encodeStringStatic($this->name);
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getStatus(): ?int
{
return $this->status;
}
public function setStatus(int $status): self
{
$this->status = $status;
return $this;
}
public function getTypeDateAccess(): ?int
{
return $this->typeDateAccess;
}
public function setTypeDateAccess(int $typeDateAccess): self
{
$this->typeDateAccess = $typeDateAccess;
return $this;
}
public function getDateAccessConclusion($dateFormat = 'Y-m-d H:i:s')
{
if($this->dateAccessConclusion) {
return $this->dateAccessConclusion->format($dateFormat);
}
return $this->dateAccessConclusion;
}
public function setDateAccessConclusion($dateAccessConclusion = null): self
{
if(!empty($dateAccessConclusion)){
$dateAccessConclusion = DateTime::createFromFormat('Y-m-d H:i:s', $dateAccessConclusion);
}
$this->dateAccessConclusion = $dateAccessConclusion;
return $this;
}
public function getDateAccessDays(): ?int
{
return $this->dateAccessDays;
}
public function setDateAccessDays(?int $dateAccessDays): self
{
$this->dateAccessDays = $dateAccessDays;
return $this;
}
public function getTypeDateSupport(): ?int
{
return $this->typeDateSupport;
}
public function setTypeDateSupport(int $typeDateSupport): self
{
$this->typeDateSupport = $typeDateSupport;
return $this;
}
public function getDateSupportConclusion($dateFormat = 'Y-m-d H:i:s')
{
if($this->dateSupportConclusion) {
return $this->dateSupportConclusion->format($dateFormat);
}
return $this->dateSupportConclusion;
}
public function setDateSupportConclusion($dateSupportConclusion): self
{
if(!empty($dateSupportConclusion)){
$dateSupportConclusion = DateTime::createFromFormat('Y-m-d H:i:s', $dateSupportConclusion);
}
$this->dateSupportConclusion = $dateSupportConclusion;
return $this;
}
public function getDateSupportDays(): ?int
{
return $this->dateSupportDays;
}
public function setDateSupportDays(?int $dateSupportDays): self
{
$this->dateSupportDays = $dateSupportDays;
return $this;
}
public function getUserManager(): ?User
{
return $this->userManager;
}
public function setUserManager(?User $userManager): self
{
$this->userManager = $userManager;
return $this;
}
public function getUserDelete(): ?User
{
return $this->userDelete;
}
public function setUserDelete(?User $userDelete): self
{
$this->userDelete = $userDelete;
return $this;
}
public function getDateDelete($dateFormat = 'Y-m-d H:i:s')
{
if($this->dateDelete){
return $this->dateDelete->format($dateFormat);
}
return $this->dateDelete;
}
public function setDateDelete($dateDelete): self
{
if(!empty($dateDelete)){
$dateDelete = DateTime::createFromFormat('Y-m-d H:i:s', $dateDelete);
}
$this->dateDelete = $dateDelete;
return $this;
}
/**
* @return Collection|Course[]
*/
public function getCourse(): Collection
{
return $this->course;
}
public function addCourse(Course $course): self
{
if (!$this->course->contains($course)) {
$this->course[] = $course;
}
return $this;
}
public function removeCourse(Course $course): self
{
if ($this->course->contains($course)) {
$this->course->removeElement($course);
}
return $this;
}
/**
* @return Collection|User[]
*/
public function getUser(): Collection
{
return $this->user;
}
public function addUser(\EADPlataforma\Entity\User $user): self
{
if (!$this->user->contains($user)) {
$this->user[] = $user;
}
return $this;
}
public function removeUser(\EADPlataforma\Entity\User $user): self
{
if ($this->user->contains($user)) {
$this->user->removeElement($user);
}
return $this;
}
public function removeAllUser(): self
{
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
return $this;
}
public function individual(): self
{
$this->typeDelete = GroupEnum::INDIVIDUAL;
return $this;
}
public function cascade(): self
{
$this->typeDelete = GroupEnum::CASCADE;
return $this;
}
public function isOnTrash(): bool
{
return ($this->deleted == GroupEnum::ITEM_ON_TRASH);
}
public function isDeleted(): bool
{
return ($this->deleted == GroupEnum::ITEM_DELETED);
}
public function restore(): self
{
$this->deleted = GroupEnum::ITEM_NO_DELETED;
return $this;
}
public function trash(): self
{
$this->deleted = GroupEnum::ITEM_ON_TRASH;
return $this;
}
public function delete(): self
{
$this->deleted = GroupEnum::ITEM_DELETED;
return $this;
}
public function toReturn(){
$arrCourse = [];
foreach ($this->course as $key => $course) {
$arrCourse[] = (object)[
"id" => $course->getId(),
"title" => $course->getTitle(),
];
}
$arrUser = [];
foreach ($this->user as $key => $user) {
$arrUser[] = (object)[
"id" => $user->getId(),
"title" => $user->getName(),
];
}
$data = [
"id" => $this->id,
"deleted" => $this->deleted,
"name" => $this->getName(),
"status" => $this->status,
"user" => $arrUser,
"course" => $arrCourse,
"userManager" => ( $this->userManager ? $this->userManager->getId() : null ),
"userDelete" => ( $this->userDelete ? $this->userDelete->getId() : null ),
"typeDelete" => $this->typeDelete,
"dateDelete" => $this->getDateDelete(),
"typeDateAccess" => $this->getTypeDateAccess(),
"dateAccessConclusion" => $this->getDateAccessConclusion(),
"dateAccessDays" => $this->getDateAccessDays(),
"typeDateSupport" => $this->getTypeDateSupport(),
"dateSupportConclusion" => $this->getDateSupportConclusion(),
"dateSupportDays" => $this->getDateSupportDays()
];
return $data;
}
public function toReturnApi(){
$arrCourse = [];
foreach ($this->course as $key => $course) {
$arrCourse[] = (object)[
"id" => $course->getId(),
"titulo" => $course->getTitle(),
];
}
$arrUser = [];
foreach ($this->user as $key => $user) {
$arrUser[] = (object)[
"id" => $user->getId(),
"nome" => $user->getName(),
];
}
$data = [
"id" => $this->id,
"grupo_nome" => $this->getName(),
"status" => $this->status,
"aluno" => $arrUser,
"curso" => $arrCourse,
"userManager" => ( $this->userManager ? $this->userManager->getId() : null )
];
return $data;
}
}