src/Entity/ElTaskChapter.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Annotation\EsUploadable;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use ApiPlatform\Core\Annotation\ApiFilter;
  6. use App\Entity\Traits\TimestampableEntity;
  7. use App\Repository\ElTaskChapterRepository;
  8. use Doctrine\Common\Collections\Collection;
  9. use ApiPlatform\Core\Annotation\ApiResource;
  10. use App\Entity\Interfaces\OrderMappedInterface;
  11. use App\Entity\Interfaces\UploadMappedInterface;
  12. use App\Entity\Interfaces\ContainerMappedInterface;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  16. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  17. use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
  18. use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;
  19. /**
  20.  * @ApiResource(
  21.  *      attributes={"security"="is_granted('ROLE_OPERATOR')", "filters"={"translation.groups"}},
  22.  *      normalizationContext={"groups"={"ElTaskChapter:Read"}, "skip_null_values"=false},
  23.  *      denormalizationContext={"groups"={"ElTaskChapter:Write", "ElTaskChapterTranslationsGroup"}},
  24.  *      collectionOperations={
  25.  *          "get"={"security"="is_granted('IS_AUTHENTICATED_FULLY')"},
  26.  *          "post"={
  27.  *              "security"="is_granted('ROLE_OPERATOR') or is_granted('ROLE_INSTRUCTOR')"
  28.  *          },
  29.  *      },
  30.  *      itemOperations={
  31.  *          "get"={"security"="is_granted('IS_CO_ANY', object)"},
  32.  *          "put"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elTask.elCourse)"},
  33.  *          "patch"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elTask.elCourse)"},
  34.  *          "patch_change_ord"={
  35.  *              "path"="/el_task_chapters/{id}/change-ord",
  36.  *              "method"="PATCH",
  37.  *              "denormalization_context"={"groups"={"ElTaskChapter:ChangeOrd"}},
  38.  *              "security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elTask.elCourse)"
  39.  *          },
  40.  *          "delete"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elTask.elCourse)"}
  41.  *     }
  42.  * )
  43.  * @ApiFilter(SearchFilter::class, properties={"id": "exact", "container.id": "exact", "elTask.id": "exact", "translations.name": "partial"})
  44.  * @ApiFilter(OrderFilter::class, properties={"ord": "ASC", "startTime", "translations.name"})
  45.  * @ORM\Entity(repositoryClass=ElTaskChapterRepository::class)
  46.  */
  47. class ElTaskChapter extends AbstractTranslatable implements ContainerMappedInterfaceUploadMappedInterfaceOrderMappedInterface
  48. {
  49.     /**
  50.      * Hook timestampable behavior
  51.      * updates createdAt, updatedAt fields
  52.      */
  53.     use TimestampableEntity;
  54.     /**
  55.      * @ORM\OneToMany(targetEntity="ElTaskChapterTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
  56.      *
  57.      * @Groups({"ElTaskChapter:Write", "ElTaskChapterTranslationGroup"})
  58.      * @Assert\Valid()
  59.      */
  60.     protected $translations;
  61.     /**
  62.      * @Groups({"ElTaskChapter:Read", "ElTask:Read"})
  63.      */
  64.     private $name;
  65.     /**
  66.      * @ORM\Id
  67.      * @ORM\GeneratedValue
  68.      * @ORM\Column(type="integer")
  69.      * @Groups({"ElTaskChapter:Read", "ElTask:Read"})
  70.      */
  71.     private $id;
  72.     /**
  73.      * @ORM\Column(type="integer")
  74.      * @Groups({"ElTaskChapter:Read", "ElTaskChapter:Write", "ElTask:Read"})
  75.      */
  76.     private $startTime;
  77.     /**
  78.      * @ORM\Column(type="string", length=255, nullable=true)
  79.      * @Groups({"ElTaskChapter:Read", "ElTaskChapter:Write", "ElTask:Read"})
  80.      * @EsUploadable()
  81.      */
  82.     private $imageName;
  83.     /**
  84.      * @ORM\Column(type="integer", nullable=true)
  85.      * @Groups({"ElTaskChapter:Read", "ElTaskChapter:Write", "ElTask:Read"})
  86.      */
  87.     private $ord;
  88.     /**
  89.      * @Groups({"ElTaskChapter:ChangeOrd"})
  90.      */
  91.     private int $newOrd;
  92.     private $ordChangeDirection;
  93.     /**
  94.      * @ORM\ManyToOne(targetEntity=Container::class)
  95.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  96.      * @Groups({"ElTaskChapter:Write"})
  97.      */
  98.     private $container;
  99.     /**
  100.      * @ORM\ManyToOne(targetEntity=ElTask::class, inversedBy="elTaskChapters")
  101.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  102.      * @Groups({"ElTaskChapter:Write"})
  103.      */
  104.     public $elTask;
  105.     public function __construct()
  106.     {
  107.         parent::__construct();
  108.     }
  109.     protected function createTranslation(): TranslationInterface
  110.     {
  111.         return new ElTaskChapterTranslation();
  112.     }
  113.     public function getName(): ?string
  114.     {
  115.         return $this->getTranslation()->getName();
  116.     }
  117.     public function setName(string $name): self
  118.     {
  119.         $this->getTranslation()->setName($name);
  120.         return $this;
  121.     }
  122.     public function getId(): ?int
  123.     {
  124.         return $this->id;
  125.     }
  126.     public function getStartTime(): ?int
  127.     {
  128.         return $this->startTime;
  129.     }
  130.     public function setStartTime(int $startTime): self
  131.     {
  132.         $this->startTime $startTime;
  133.         return $this;
  134.     }
  135.     public function getImageName(): ?string
  136.     {
  137.         return $this->imageName;
  138.     }
  139.     public function setImageName(?string $imageName): self
  140.     {
  141.         $this->imageName $imageName;
  142.         return $this;
  143.     }
  144.     public function getOrd(): ?int
  145.     {
  146.         return $this->ord;
  147.     }
  148.     public function setOrd(?int $ord): self
  149.     {
  150.         $this->ord $ord;
  151.         return $this;
  152.     }
  153.     public function getOrdParents(): array
  154.     {
  155.         return [
  156.             'container' => $this->getContainer(),
  157.             'elTask' => $this->getElTask()
  158.         ];
  159.     }
  160.     public function getOrdChangeDirection()
  161.     {
  162.         return $this->ordChangeDirection;
  163.     }
  164.     public function getNewOrd(): int
  165.     {
  166.         return $this->newOrd ?? 0;
  167.     }
  168.     public function setNewOrd(int $newOrd): self
  169.     {
  170.         $oldOrd $this->ord;
  171.         $this->newOrd $newOrd;
  172.         $this->ord $newOrd;
  173.         $this->ordChangeDirection = ($this->newOrd $oldOrd) ? 'desc' 'asc';
  174.         return $this;
  175.     }
  176.     public function getContainer(): ?Container
  177.     {
  178.         return $this->container;
  179.     }
  180.     public function setContainer(?Container $container): self
  181.     {
  182.         $this->container $container;
  183.         return $this;
  184.     }
  185.     public function getElTask(): ?ElTask
  186.     {
  187.         return $this->elTask;
  188.     }
  189.     public function setElTask(?ElTask $elTask): self
  190.     {
  191.         $elTask->setUpdatedAt(new \DateTime());
  192.         $this->elTask $elTask;
  193.         return $this;
  194.     }
  195. }