src/Entity/Group.php line 502

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Doctrine\Common\Collections\Collection;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. use EADPlataforma\Validator\Constraints as EADAssert;
  7. use EADPlataforma\Enum\GroupEnum;
  8. use EADPlataforma\Util\StringUtil;
  9. use \DateTime;
  10. /**
  11.  * Group
  12.  *
  13.  * @ORM\Table(name="`group`")
  14.  * @ORM\Table(name="`group`", indexes={
  15.  *      @ORM\Index(name="fk_group_user_manager_id", columns={"user_manager_id"}),
  16.  *      @ORM\Index(name="fk_group_user_delete_id", columns={"user_delete_id"})
  17.  * })
  18.  *
  19.  * @ORM\Entity(repositoryClass="EADPlataforma\Repository\GroupRepository")
  20.  */
  21. class Group
  22. {
  23.     /**
  24.      * @var int
  25.      *
  26.      * @ORM\Column(name="id", type="integer", nullable=false)
  27.      * @ORM\Id
  28.      * @ORM\GeneratedValue(strategy="IDENTITY")
  29.      */
  30.     private $id;
  31.     /**
  32.      * @Assert\NotBlank(
  33.      *      message = "Deleted not informed"
  34.      * )
  35.      *
  36.      * @Assert\Choice(
  37.      *      choices = { 
  38.      *                      GroupEnum::ITEM_NO_DELETED, 
  39.      *                      GroupEnum::ITEM_ON_TRASH,
  40.      *                      GroupEnum::ITEM_DELETED
  41.      *                },
  42.      *      message = "Delete Option Invalid"
  43.      * )
  44.      *
  45.      * @var int
  46.      *
  47.      * @ORM\Column(name="deleted", type="integer", nullable=false, options={"default"="0"})
  48.      */
  49.     private $deleted GroupEnum::ITEM_NO_DELETED;
  50.     /**
  51.      * @Assert\NotBlank(
  52.      *    message = "Name not informed"
  53.      * )
  54.      * 
  55.      * @Assert\Length(
  56.      *      min = 0,
  57.      *      max = 250
  58.      * )
  59.      *
  60.      * @var string
  61.      *
  62.      * @ORM\Column(name="name", type="string", length=255, nullable=false)
  63.      */
  64.     private $name;
  65.     /**
  66.      * @Assert\NotBlank(
  67.      *    message = "Status not informed"
  68.      * )
  69.      *
  70.      * @Assert\Choice(
  71.      *      choices = { GroupEnum::BLOCK, GroupEnum::UNBLOCK },
  72.      *      message = "Status Invalid"
  73.      * )
  74.      *
  75.      * @var int
  76.      *
  77.      * @ORM\Column(name="status", type="integer", nullable=false, options={"default"="1"})
  78.      */
  79.     private $status GroupEnum::UNBLOCK;
  80.     /**
  81.      * @Assert\NotBlank(
  82.      *    message = "TypeDateAccess not informed"
  83.      * )
  84.      *
  85.      * @Assert\Choice(
  86.      *      choices = { GroupEnum::TYPE_DATE_DEFAULT, GroupEnum::TYPE_DATE_FIXED, GroupEnum::TYPE_DATE_PERIOD },
  87.      *      message = "Invalid date period type"
  88.      * )
  89.      *
  90.      * @var int
  91.      *
  92.      * @ORM\Column(name="type_date_access", type="integer", nullable=false, options={"default"="0"})
  93.      */
  94.     private $typeDateAccess GroupEnum::TYPE_DATE_DEFAULT;
  95.     /**
  96.      * @var DateTime|null
  97.      *
  98.      * @ORM\Column(name="date_access_conclusion", type="datetime", nullable=true)
  99.      */
  100.     private $dateAccessConclusion;
  101.     /**
  102.      * @var int|null
  103.      *
  104.      * @ORM\Column(name="date_access_days", type="integer", nullable=true)
  105.      */
  106.     private $dateAccessDays;
  107.     /**
  108.      * @Assert\NotBlank(
  109.      *    message = "TypeDateSupport not informed"
  110.      * )
  111.      *
  112.      * @Assert\Choice(
  113.      *      choices = { GroupEnum::TYPE_DATE_DEFAULT, GroupEnum::TYPE_DATE_FIXED, GroupEnum::TYPE_DATE_PERIOD },
  114.      *      message = "Invalid date period type"
  115.      * )
  116.      *
  117.      * @var int
  118.      *
  119.      * @ORM\Column(name="type_date_support", type="integer", nullable=false, options={"default"="0"})
  120.      */
  121.     private $typeDateSupport GroupEnum::TYPE_DATE_DEFAULT;
  122.     /**
  123.      * @var DateTime|null
  124.      *
  125.      * @ORM\Column(name="date_support_conclusion", type="datetime", nullable=true)
  126.      */
  127.     private $dateSupportConclusion;
  128.     /**
  129.      * @var int|null
  130.      *
  131.      * @ORM\Column(name="date_support_days", type="integer", nullable=true)
  132.      */
  133.     private $dateSupportDays;
  134.     /**
  135.      * @var \Doctrine\Common\Collections\Collection
  136.      *
  137.      * @ORM\ManyToMany(targetEntity="Course", inversedBy="`group`")
  138.      * @ORM\JoinTable(name="group_x_course",
  139.      *   joinColumns={
  140.      *     @ORM\JoinColumn(name="group_id", referencedColumnName="id")
  141.      *   },
  142.      *   inverseJoinColumns={
  143.      *     @ORM\JoinColumn(name="course_id", referencedColumnName="id")
  144.      *   }
  145.      * )
  146.      */
  147.     private $course;
  148.     /**
  149.      * @var \Doctrine\Common\Collections\Collection
  150.      *
  151.      * @ORM\ManyToMany(targetEntity="User", inversedBy="`group`")
  152.      * @ORM\JoinTable(name="group_x_user",
  153.      *   joinColumns={
  154.      *     @ORM\JoinColumn(name="group_id", referencedColumnName="id")
  155.      *   },
  156.      *   inverseJoinColumns={
  157.      *     @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  158.      *   }
  159.      * )
  160.      */
  161.     private $user;
  162.     /**
  163.      * @Assert\NotBlank(
  164.      *    message = "User not informed"
  165.      * )
  166.      * 
  167.      * @Assert\Valid
  168.      *
  169.      * @var \EADPlataforma\Entity\User
  170.      *
  171.      * @ORM\ManyToOne(targetEntity="User")
  172.      * @ORM\JoinColumns({
  173.      *   @ORM\JoinColumn(name="user_manager_id", referencedColumnName="id", nullable=false)
  174.      * })
  175.      */
  176.     private $userManager;
  177.     /**
  178.      * @Assert\Valid
  179.      *
  180.      * @var \EADPlataforma\Entity\User
  181.      *
  182.      * @ORM\ManyToOne(targetEntity="User")
  183.      * @ORM\JoinColumns({
  184.      *   @ORM\JoinColumn(name="user_delete_id", referencedColumnName="id", nullable=true)
  185.      * })
  186.      */
  187.     private $userDelete;
  188.     /**
  189.      * @Assert\Choice(
  190.      *      choices = { 
  191.      *                      GroupEnum::INDIVIDUAL, 
  192.      *                      GroupEnum::CASCADE
  193.      *                },
  194.      *      message = "Type Delete Invalid"
  195.      * )
  196.      *
  197.      * @var int
  198.      *
  199.      * @ORM\Column(name="type_delete", type="integer", nullable=true)
  200.      */
  201.     private $typeDelete;
  202.     /**
  203.      * @EADAssert\DateTimeEAD(
  204.      *      message = "Date Delete Invalid"
  205.      * )
  206.      *
  207.      * @var \DateTime|null
  208.      *
  209.      * @ORM\Column(name="date_delete", type="datetime", nullable=true)
  210.      */
  211.     private $dateDelete;
  212.     /**
  213.      * Constructor
  214.      */
  215.     public function __construct()
  216.     {
  217.         $this->course = new \Doctrine\Common\Collections\ArrayCollection();
  218.         $this->user = new \Doctrine\Common\Collections\ArrayCollection();
  219.     }
  220.     public function getId(): ?int
  221.     {
  222.         return $this->id;
  223.     }
  224.     public function getName(): ?string
  225.     {
  226.         return StringUtil::encodeStringStatic($this->name);
  227.     }
  228.     public function setName(string $name): self
  229.     {
  230.         $this->name $name;
  231.         return $this;
  232.     }
  233.     public function getStatus(): ?int
  234.     {
  235.         return $this->status;
  236.     }
  237.     public function setStatus(int $status): self
  238.     {
  239.         $this->status $status;
  240.         return $this;
  241.     }
  242.     public function getTypeDateAccess(): ?int
  243.     {
  244.         return $this->typeDateAccess;
  245.     }
  246.     public function setTypeDateAccess(int $typeDateAccess): self
  247.     {
  248.         $this->typeDateAccess $typeDateAccess;
  249.         return $this;
  250.     }
  251.     public function getDateAccessConclusion($dateFormat 'Y-m-d H:i:s')
  252.     {
  253.         if($this->dateAccessConclusion) {
  254.             return $this->dateAccessConclusion->format($dateFormat);
  255.         }
  256.         return $this->dateAccessConclusion;
  257.     }
  258.     
  259.     public function setDateAccessConclusion($dateAccessConclusion null): self
  260.     {
  261.         if(!empty($dateAccessConclusion)){
  262.             $dateAccessConclusion DateTime::createFromFormat('Y-m-d H:i:s'$dateAccessConclusion);
  263.         }
  264.         $this->dateAccessConclusion $dateAccessConclusion;
  265.         return $this;
  266.     }
  267.     public function getDateAccessDays(): ?int
  268.     {
  269.         return $this->dateAccessDays;
  270.     }
  271.     public function setDateAccessDays(?int $dateAccessDays): self
  272.     {
  273.         $this->dateAccessDays $dateAccessDays;
  274.         return $this;
  275.     }
  276.     public function getTypeDateSupport(): ?int
  277.     {
  278.         return $this->typeDateSupport;
  279.     }
  280.     public function setTypeDateSupport(int $typeDateSupport): self
  281.     {
  282.         $this->typeDateSupport $typeDateSupport;
  283.         return $this;
  284.     }
  285.     public function getDateSupportConclusion($dateFormat 'Y-m-d H:i:s')
  286.     {
  287.         if($this->dateSupportConclusion) {
  288.             return $this->dateSupportConclusion->format($dateFormat);
  289.         }
  290.         return $this->dateSupportConclusion;
  291.     }
  292.     public function setDateSupportConclusion($dateSupportConclusion): self
  293.     {
  294.         if(!empty($dateSupportConclusion)){
  295.             $dateSupportConclusion DateTime::createFromFormat('Y-m-d H:i:s'$dateSupportConclusion);
  296.         }
  297.         $this->dateSupportConclusion $dateSupportConclusion;
  298.         return $this;
  299.     }
  300.     public function getDateSupportDays(): ?int
  301.     {
  302.         return $this->dateSupportDays;
  303.     }
  304.     public function setDateSupportDays(?int $dateSupportDays): self
  305.     {
  306.         $this->dateSupportDays $dateSupportDays;
  307.         return $this;
  308.     }
  309.     public function getUserManager(): ?User
  310.     {
  311.         return $this->userManager;
  312.     }
  313.     public function setUserManager(?User $userManager): self
  314.     {
  315.         $this->userManager $userManager;
  316.         return $this;
  317.     }
  318.     public function getUserDelete(): ?User
  319.     {
  320.         return $this->userDelete;
  321.     }
  322.     public function setUserDelete(?User $userDelete): self
  323.     {
  324.         $this->userDelete $userDelete;
  325.         return $this;
  326.     }
  327.     public function getDateDelete($dateFormat 'Y-m-d H:i:s')
  328.     {
  329.         if($this->dateDelete){
  330.             return $this->dateDelete->format($dateFormat);
  331.         }
  332.         return $this->dateDelete;
  333.     }
  334.     public function setDateDelete($dateDelete): self
  335.     {
  336.         if(!empty($dateDelete)){
  337.             $dateDelete DateTime::createFromFormat('Y-m-d H:i:s'$dateDelete);
  338.         }
  339.         
  340.         $this->dateDelete $dateDelete;
  341.         return $this;
  342.     }
  343.     /**
  344.      * @return Collection|Course[]
  345.      */
  346.     public function getCourse(): Collection
  347.     {
  348.         return $this->course;
  349.     }
  350.     public function addCourse(Course $course): self
  351.     {
  352.         if (!$this->course->contains($course)) {
  353.             $this->course[] = $course;
  354.         }
  355.         return $this;
  356.     }
  357.     public function removeCourse(Course $course): self
  358.     {
  359.         if ($this->course->contains($course)) {
  360.             $this->course->removeElement($course);
  361.         }
  362.         return $this;
  363.     }
  364.     /**
  365.      * @return Collection|User[]
  366.      */
  367.     public function getUser(): Collection
  368.     {
  369.         return $this->user;
  370.     }
  371.     public function addUser(\EADPlataforma\Entity\User $user): self
  372.     {
  373.         if (!$this->user->contains($user)) {
  374.             $this->user[] = $user;
  375.         }
  376.         return $this;
  377.     }
  378.     public function removeUser(\EADPlataforma\Entity\User $user): self
  379.     {
  380.         if ($this->user->contains($user)) {
  381.             $this->user->removeElement($user);
  382.         }
  383.         return $this;
  384.     }
  385.     public function removeAllUser(): self
  386.     {
  387.         $this->user = new \Doctrine\Common\Collections\ArrayCollection();
  388.         return $this;
  389.     }
  390.     public function individual(): self
  391.     {
  392.         $this->typeDelete GroupEnum::INDIVIDUAL;
  393.         return $this;
  394.     }
  395.     public function cascade(): self
  396.     {
  397.         $this->typeDelete GroupEnum::CASCADE;
  398.         return $this;
  399.     }
  400.     public function isOnTrash(): bool
  401.     {
  402.         return ($this->deleted == GroupEnum::ITEM_ON_TRASH);
  403.     }
  404.     public function isDeleted(): bool
  405.     {
  406.         return ($this->deleted == GroupEnum::ITEM_DELETED);
  407.     }
  408.     public function restore(): self
  409.     {
  410.         $this->deleted GroupEnum::ITEM_NO_DELETED;
  411.         return $this;
  412.     }
  413.     public function trash(): self
  414.     {
  415.         $this->deleted GroupEnum::ITEM_ON_TRASH;
  416.         return $this;
  417.     }
  418.     public function delete(): self
  419.     {
  420.         $this->deleted GroupEnum::ITEM_DELETED;
  421.         return $this;
  422.     }
  423.     public function toReturn(){
  424.         $arrCourse = [];
  425.         foreach ($this->course as $key => $course) {
  426.             $arrCourse[] = (object)[
  427.                 "id" => $course->getId(),
  428.                 "title" => $course->getTitle(),
  429.             ];
  430.         }
  431.         $arrUser = [];
  432.         foreach ($this->user as $key => $user) {
  433.             $arrUser[] = (object)[
  434.                 "id" => $user->getId(),
  435.                 "title" => $user->getName(),
  436.             ];
  437.         }
  438.         $data = [
  439.             "id" => $this->id,
  440.             "deleted" => $this->deleted,
  441.             "name" => $this->getName(),
  442.             "status" => $this->status,
  443.             "user" => $arrUser,
  444.             "course" => $arrCourse,
  445.             "userManager" => ( $this->userManager $this->userManager->getId() : null ),
  446.             "userDelete" => ( $this->userDelete $this->userDelete->getId() : null ),
  447.             "typeDelete" => $this->typeDelete,
  448.             "dateDelete" => $this->getDateDelete(),
  449.             "typeDateAccess" => $this->getTypeDateAccess(),
  450.             "dateAccessConclusion" => $this->getDateAccessConclusion(),
  451.             "dateAccessDays" => $this->getDateAccessDays(),
  452.             "typeDateSupport" => $this->getTypeDateSupport(),
  453.             "dateSupportConclusion" => $this->getDateSupportConclusion(),
  454.             "dateSupportDays" => $this->getDateSupportDays()
  455.         ];
  456.         return $data;
  457.     }
  458.     public function toReturnApi(){
  459.         $arrCourse = [];
  460.         foreach ($this->course as $key => $course) {
  461.             $arrCourse[] = (object)[
  462.                 "id" => $course->getId(),
  463.                 "titulo" => $course->getTitle(),
  464.             ];
  465.         }
  466.         $arrUser = [];
  467.         foreach ($this->user as $key => $user) {
  468.             $arrUser[] = (object)[
  469.                 "id" => $user->getId(),
  470.                 "nome" => $user->getName(),
  471.             ];
  472.         }
  473.         $data = [
  474.             "id" => $this->id,
  475.             "grupo_nome" => $this->getName(),
  476.             "status" => $this->status,
  477.             "aluno" => $arrUser,
  478.             "curso" => $arrCourse,
  479.             "userManager" => ( $this->userManager $this->userManager->getId() : null )
  480.         ];
  481.         return $data;
  482.     }
  483. }