<?php
namespace App\Entity;
use App\Annotation\EsUploadable;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiFilter;
use App\Entity\Traits\TimestampableEntity;
use App\Repository\ElTaskChapterRepository;
use Doctrine\Common\Collections\Collection;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Interfaces\OrderMappedInterface;
use App\Entity\Interfaces\UploadMappedInterface;
use App\Entity\Interfaces\ContainerMappedInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;
/**
* @ApiResource(
* attributes={"security"="is_granted('ROLE_OPERATOR')", "filters"={"translation.groups"}},
* normalizationContext={"groups"={"ElTaskChapter:Read"}, "skip_null_values"=false},
* denormalizationContext={"groups"={"ElTaskChapter:Write", "ElTaskChapterTranslationsGroup"}},
* collectionOperations={
* "get"={"security"="is_granted('IS_AUTHENTICATED_FULLY')"},
* "post"={
* "security"="is_granted('ROLE_OPERATOR') or is_granted('ROLE_INSTRUCTOR')"
* },
* },
* itemOperations={
* "get"={"security"="is_granted('IS_CO_ANY', object)"},
* "put"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elTask.elCourse)"},
* "patch"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elTask.elCourse)"},
* "patch_change_ord"={
* "path"="/el_task_chapters/{id}/change-ord",
* "method"="PATCH",
* "denormalization_context"={"groups"={"ElTaskChapter:ChangeOrd"}},
* "security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elTask.elCourse)"
* },
* "delete"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elTask.elCourse)"}
* }
* )
* @ApiFilter(SearchFilter::class, properties={"id": "exact", "container.id": "exact", "elTask.id": "exact", "translations.name": "partial"})
* @ApiFilter(OrderFilter::class, properties={"ord": "ASC", "startTime", "translations.name"})
* @ORM\Entity(repositoryClass=ElTaskChapterRepository::class)
*/
class ElTaskChapter extends AbstractTranslatable implements ContainerMappedInterface, UploadMappedInterface, OrderMappedInterface
{
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
/**
* @ORM\OneToMany(targetEntity="ElTaskChapterTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
*
* @Groups({"ElTaskChapter:Write", "ElTaskChapterTranslationGroup"})
* @Assert\Valid()
*/
protected $translations;
/**
* @Groups({"ElTaskChapter:Read", "ElTask:Read"})
*/
private $name;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"ElTaskChapter:Read", "ElTask:Read"})
*/
private $id;
/**
* @ORM\Column(type="integer")
* @Groups({"ElTaskChapter:Read", "ElTaskChapter:Write", "ElTask:Read"})
*/
private $startTime;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"ElTaskChapter:Read", "ElTaskChapter:Write", "ElTask:Read"})
* @EsUploadable()
*/
private $imageName;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups({"ElTaskChapter:Read", "ElTaskChapter:Write", "ElTask:Read"})
*/
private $ord;
/**
* @Groups({"ElTaskChapter:ChangeOrd"})
*/
private int $newOrd;
private $ordChangeDirection;
/**
* @ORM\ManyToOne(targetEntity=Container::class)
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
* @Groups({"ElTaskChapter:Write"})
*/
private $container;
/**
* @ORM\ManyToOne(targetEntity=ElTask::class, inversedBy="elTaskChapters")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
* @Groups({"ElTaskChapter:Write"})
*/
public $elTask;
public function __construct()
{
parent::__construct();
}
protected function createTranslation(): TranslationInterface
{
return new ElTaskChapterTranslation();
}
public function getName(): ?string
{
return $this->getTranslation()->getName();
}
public function setName(string $name): self
{
$this->getTranslation()->setName($name);
return $this;
}
public function getId(): ?int
{
return $this->id;
}
public function getStartTime(): ?int
{
return $this->startTime;
}
public function setStartTime(int $startTime): self
{
$this->startTime = $startTime;
return $this;
}
public function getImageName(): ?string
{
return $this->imageName;
}
public function setImageName(?string $imageName): self
{
$this->imageName = $imageName;
return $this;
}
public function getOrd(): ?int
{
return $this->ord;
}
public function setOrd(?int $ord): self
{
$this->ord = $ord;
return $this;
}
public function getOrdParents(): array
{
return [
'container' => $this->getContainer(),
'elTask' => $this->getElTask()
];
}
public function getOrdChangeDirection()
{
return $this->ordChangeDirection;
}
public function getNewOrd(): int
{
return $this->newOrd ?? 0;
}
public function setNewOrd(int $newOrd): self
{
$oldOrd = $this->ord;
$this->newOrd = $newOrd;
$this->ord = $newOrd;
$this->ordChangeDirection = ($this->newOrd < $oldOrd) ? 'desc' : 'asc';
return $this;
}
public function getContainer(): ?Container
{
return $this->container;
}
public function setContainer(?Container $container): self
{
$this->container = $container;
return $this;
}
public function getElTask(): ?ElTask
{
return $this->elTask;
}
public function setElTask(?ElTask $elTask): self
{
$elTask->setUpdatedAt(new \DateTime());
$this->elTask = $elTask;
return $this;
}
}