src/Entity/ElLessionTopic.php line 70

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use ApiPlatform\Core\Annotation\ApiFilter;
  5. use App\Entity\Traits\TimestampableEntity;
  6. use Doctrine\Common\Collections\Collection;
  7. use ApiPlatform\Core\Annotation\ApiResource;
  8. use App\Repository\ElLessionTopicRepository;
  9. use App\Entity\Interfaces\OrderMappedInterface;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use App\Entity\Interfaces\ElCopyRestrictInterface;
  12. use App\Entity\Interfaces\ContainerMappedInterface;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. use App\Entity\Interfaces\OperationLogMappedInterface;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. use App\Entity\Interfaces\ElCourseIndexUpdateInterface;
  17. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  18. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  19. use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
  20. use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;
  21. /**
  22.  * @ApiResource(
  23.  *      attributes={"security"="is_granted('ROLE_OPERATOR')", "filters"={"translation.groups"}},
  24.  *      normalizationContext={"groups"={"ElLessionTopic:Read"}, "skip_null_values"=false},
  25.  *      denormalizationContext={"groups"={"ElLessionTopic:Write"}},
  26.  *      collectionOperations={
  27.  *          "get",
  28.  *          "get_for_copy"={
  29.  *              "path"="/el_lession_topics/get-for-copy",
  30.  *              "method"="GET",
  31.  *              "normalization_context"={"groups"={"ElLessionTopic:Select"}, "skip_null_values"=false},
  32.  *              "pagination_enabled"=false
  33.  *          },
  34.  *          "post"={
  35.  *              "security"="is_granted('ROLE_OPERATOR') or is_granted('ROLE_INSTRUCTOR')"
  36.  *          }
  37.  *      },
  38.  *      itemOperations={
  39.  *          "get"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"},
  40.  *          "get_next"={
  41.  *              "path"="/el_lession_topics/{id}/get-next",
  42.  *              "method"="GET",
  43.  *              "normalization_context"={"groups"={"ElLessionTopic:Read"}, "skip_null_values"=false},
  44.  *              "security"="is_granted('IS_AUTHENTICATED_FULLY')"
  45.  *          },
  46.  *          "get_previous"={
  47.  *              "path"="/el_lession_topics/{id}/get-previous",
  48.  *              "method"="GET",
  49.  *              "normalization_context"={"groups"={"ElLessionTopic:Read"}, "skip_null_values"=false},
  50.  *              "security"="is_granted('IS_AUTHENTICATED_FULLY')"
  51.  *          },
  52.  *          "put"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"},
  53.  *          "patch"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"},
  54.  *          "patch_change_ord"={
  55.  *              "path"="/el_lession_topics/{id}/change-ord",
  56.  *              "method"="PATCH",
  57.  *              "denormalization_context"={"groups"={"ElLessionTopic:ChangeOrd", "ElLessionTopic:Move"}},
  58.  *              "security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"
  59.  *          },
  60.  *          "delete"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"}
  61.  *     }
  62.  * )
  63.  * @ApiFilter(SearchFilter::class, properties={"id": "exact", "container.id": "exact", "elLession.id": "exact", "translations.name": "partial"})
  64.  * @ApiFilter(OrderFilter::class, properties={"ord": "ASC", "translations.name", "id"})
  65.  * @ORM\Entity(repositoryClass=ElLessionTopicRepository::class)
  66.  */
  67. class ElLessionTopic extends AbstractTranslatable implements ContainerMappedInterfaceOrderMappedInterfaceElCourseIndexUpdateInterfaceOperationLogMappedInterfaceElCopyRestrictInterface
  68. {
  69.     /**
  70.      * Hook timestampable behavior
  71.      * updates createdAt, updatedAt fields
  72.      */
  73.     use TimestampableEntity;
  74.     /**
  75.      * @ORM\OneToMany(targetEntity="ElLessionTopicTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
  76.      *
  77.      * @Groups({"ElLessionTopic:Write", "ElLessionTopicTranslationGroup"})
  78.      * @Assert\Valid()
  79.      */
  80.     protected $translations;
  81.     /**
  82.      * @Groups({"ElLessionTopic:Read", "ElLessionTopic:Select", "ElLession:Read", "ElLession:Detail", "ElExam:Read", "ElTask:Read"})
  83.      */
  84.     private $name;
  85.     /**
  86.      * @ORM\Id
  87.      * @ORM\GeneratedValue
  88.      * @ORM\Column(type="integer")
  89.      * @Groups({"ElLessionTopic:Read", "ElLessionTopic:Select", "ElLession:Read", "ElLession:Detail", "ElExam:Read", "ElTask:Read"})
  90.      */
  91.     private $id;
  92.     /**
  93.      * @ORM\Column(type="integer", nullable=true)
  94.      * @Groups({"ElLessionTopic:Read", "ElLessionTopic:Write", "ElLession:Read", "ElLession:Detail", "ElTask:Read"})
  95.      */
  96.     private $ord;
  97.     /**
  98.      * @ORM\ManyToOne(targetEntity=Container::class)
  99.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  100.      *
  101.      * @Groups({"ElLessionTopic:Write"})
  102.      */
  103.     private $container;
  104.     /**
  105.      * @ORM\ManyToOne(targetEntity=ElLession::class, inversedBy="elLessionTopics")
  106.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  107.      *
  108.      * @Groups({"ElLessionTopic:Read", "ElLessionTopic:Write", "ElLessionTopic:Move"})
  109.      */
  110.     private $elLession;
  111.     /**
  112.      * @ORM\OneToMany(targetEntity=ElTask::class, mappedBy="elLessionTopic", orphanRemoval=true)
  113.      * @Groups({"ElLession:Detail"})
  114.      */
  115.     private $elTasks;
  116.     /**
  117.      * @ORM\ManyToOne(targetEntity=ElCourse::class, inversedBy="elLessionTopics")
  118.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  119.      *
  120.      * @Groups({"ElLessionTopic:Read", "ElLessionTopic:Write"})
  121.      */
  122.     public $elCourse;
  123.     /**
  124.      * @ORM\OneToMany(targetEntity=ElExam::class, mappedBy="elLesstionTopic")
  125.      * @Groups({"ElLessionTopic:Read", "ElLession:Detail"})
  126.      */
  127.     private $elExams;
  128.     /**
  129.      * @Groups({"ElLessionTopic:ChangeOrd"})
  130.      */
  131.     private int $newOrd;
  132.     private $ordChangeDirection;
  133.     public function __construct()
  134.     {
  135.         parent::__construct();
  136.         $this->elTasks = new ArrayCollection();
  137.         $this->elExams = new ArrayCollection();
  138.     }
  139.     protected function createTranslation(): TranslationInterface
  140.     {
  141.         return new ElLessionTopicTranslation();
  142.     }
  143.     public function getName(): ?string
  144.     {
  145.         return $this->getTranslation()->getName();
  146.     }
  147.     public function setName(string $name): self
  148.     {
  149.         $this->getTranslation()->setName($name);
  150.         return $this;
  151.     }
  152.     public function getDescription(): ?string
  153.     {
  154.         return $this->getTranslation()->getDescription();
  155.     }
  156.     public function setDescription(string $description): self
  157.     {
  158.         $this->getTranslation()->setDescription($description);
  159.         return $this;
  160.     }
  161.     public function getId(): ?int
  162.     {
  163.         return $this->id;
  164.     }
  165.     public function getOrd(): ?int
  166.     {
  167.         return $this->ord;
  168.     }
  169.     public function setOrd(?int $ord): self
  170.     {
  171.         $this->ord $ord;
  172.         return $this;
  173.     }
  174.     public function getOrdParents(): array
  175.     {
  176.         return [
  177.             'container' => $this->getContainer(),
  178.             'elLession' => $this->getElLession()
  179.         ];
  180.     }
  181.     public function getOrdChangeDirection()
  182.     {
  183.         return $this->ordChangeDirection;
  184.     }
  185.     public function getContainer(): ?Container
  186.     {
  187.         return $this->container;
  188.     }
  189.     public function setContainer(?Container $container): self
  190.     {
  191.         $this->container $container;
  192.         return $this;
  193.     }
  194.     public function getElLession(): ?ElLession
  195.     {
  196.         return $this->elLession;
  197.     }
  198.     public function setElLession(?ElLession $elLession): self
  199.     {
  200.         $this->elLession $elLession;
  201.         return $this;
  202.     }
  203.     /**
  204.      * @return Collection|ElTask[]
  205.      */
  206.     public function getElTasks(): Collection
  207.     {
  208.         return $this->elTasks;
  209.     }
  210.     public function addElTask(ElTask $elTask): self
  211.     {
  212.         if (!$this->elTasks->contains($elTask)) {
  213.             $this->elTasks[] = $elTask;
  214.             $elTask->setElLessionTopic($this);
  215.         }
  216.         return $this;
  217.     }
  218.     public function removeElTask(ElTask $elTask): self
  219.     {
  220.         if ($this->elTasks->removeElement($elTask)) {
  221.             // set the owning side to null (unless already changed)
  222.             if ($elTask->getElLessionTopic() === $this) {
  223.                 $elTask->setElLessionTopic(null);
  224.             }
  225.         }
  226.         return $this;
  227.     }
  228.     public function getElCourse(): ?ElCourse
  229.     {
  230.         return $this->elCourse;
  231.     }
  232.     public function setElCourse(?ElCourse $elCourse): self
  233.     {
  234.         $this->elCourse $elCourse;
  235.         return $this;
  236.     }
  237.     /**
  238.      * @return Collection|ElExam[]
  239.      */
  240.     public function getElExams(): Collection
  241.     {
  242.         return $this->elExams;
  243.     }
  244.     public function addElExam(ElExam $elExam): self
  245.     {
  246.         if (!$this->elExams->contains($elExam)) {
  247.             $this->elExams[] = $elExam;
  248.             $elExam->setElLesstionTopic($this);
  249.         }
  250.         return $this;
  251.     }
  252.     public function removeElExam(ElExam $elExam): self
  253.     {
  254.         if ($this->elExams->removeElement($elExam)) {
  255.             // set the owning side to null (unless already changed)
  256.             if ($elExam->getElLesstionTopic() === $this) {
  257.                 $elExam->setElLesstionTopic(null);
  258.             }
  259.         }
  260.         return $this;
  261.     }
  262.     public function getNewOrd(): int
  263.     {
  264.         return $this->newOrd ?? 0;
  265.     }
  266.     public function setNewOrd(int $newOrd): self
  267.     {
  268.         $oldOrd $this->ord;
  269.         $this->newOrd $newOrd;
  270.         $this->ord $newOrd;
  271.         $this->ordChangeDirection = ($this->newOrd $oldOrd) ? 'desc' 'asc';
  272.         return $this;
  273.     }
  274.     public function getMetadataTranslation()
  275.     {
  276.         $translations = [];
  277.         foreach ($this->getTranslations() as $translation) {
  278.             $translations[] = [
  279.                 $translation->getLocale() => [
  280.                     'name' => $translation->getName()
  281.                 ]
  282.             ];
  283.         }
  284.         return $translations;
  285.     }
  286.     public function getParentCourse()
  287.     {
  288.         return $this->elCourse;
  289.     }
  290.     public function getForIndex()
  291.     {
  292.         $elTasks = [];
  293.         foreach ($this->getElTasks() as $task) {
  294.             $elTasks[] = $task->getForIndex();
  295.         }
  296.         $elExams = [];
  297.         foreach ($this->getElExams() as $exam) {
  298.             $elExams[] = $exam->getForIndex();
  299.         }
  300.         return [
  301.             'id' => $this->getId(),
  302.             'ord' => $this->getOrd(),
  303.             'elLession' => $this->getElLession()->getId(),
  304.             'elCourse' => $this->getElCourse()->getId(),
  305.             'translations' => $this->getMetadataTranslation(),
  306.             'elTasks' => $elTasks,
  307.             'elExams' => $elExams
  308.         ];
  309.     }
  310. }