<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiFilter;
use App\Entity\Traits\TimestampableEntity;
use Doctrine\Common\Collections\Collection;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\ElLessionTopicRepository;
use App\Entity\Interfaces\OrderMappedInterface;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Interfaces\ElCopyRestrictInterface;
use App\Entity\Interfaces\ContainerMappedInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use App\Entity\Interfaces\OperationLogMappedInterface;
use Symfony\Component\Validator\Constraints as Assert;
use App\Entity\Interfaces\ElCourseIndexUpdateInterface;
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"={"ElLessionTopic:Read"}, "skip_null_values"=false},
* denormalizationContext={"groups"={"ElLessionTopic:Write"}},
* collectionOperations={
* "get",
* "get_for_copy"={
* "path"="/el_lession_topics/get-for-copy",
* "method"="GET",
* "normalization_context"={"groups"={"ElLessionTopic:Select"}, "skip_null_values"=false},
* "pagination_enabled"=false
* },
* "post"={
* "security"="is_granted('ROLE_OPERATOR') or is_granted('ROLE_INSTRUCTOR')"
* }
* },
* itemOperations={
* "get"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"},
* "get_next"={
* "path"="/el_lession_topics/{id}/get-next",
* "method"="GET",
* "normalization_context"={"groups"={"ElLessionTopic:Read"}, "skip_null_values"=false},
* "security"="is_granted('IS_AUTHENTICATED_FULLY')"
* },
* "get_previous"={
* "path"="/el_lession_topics/{id}/get-previous",
* "method"="GET",
* "normalization_context"={"groups"={"ElLessionTopic:Read"}, "skip_null_values"=false},
* "security"="is_granted('IS_AUTHENTICATED_FULLY')"
* },
* "put"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"},
* "patch"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"},
* "patch_change_ord"={
* "path"="/el_lession_topics/{id}/change-ord",
* "method"="PATCH",
* "denormalization_context"={"groups"={"ElLessionTopic:ChangeOrd", "ElLessionTopic:Move"}},
* "security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"
* },
* "delete"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"}
* }
* )
* @ApiFilter(SearchFilter::class, properties={"id": "exact", "container.id": "exact", "elLession.id": "exact", "translations.name": "partial"})
* @ApiFilter(OrderFilter::class, properties={"ord": "ASC", "translations.name", "id"})
* @ORM\Entity(repositoryClass=ElLessionTopicRepository::class)
*/
class ElLessionTopic extends AbstractTranslatable implements ContainerMappedInterface, OrderMappedInterface, ElCourseIndexUpdateInterface, OperationLogMappedInterface, ElCopyRestrictInterface
{
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
/**
* @ORM\OneToMany(targetEntity="ElLessionTopicTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
*
* @Groups({"ElLessionTopic:Write", "ElLessionTopicTranslationGroup"})
* @Assert\Valid()
*/
protected $translations;
/**
* @Groups({"ElLessionTopic:Read", "ElLessionTopic:Select", "ElLession:Read", "ElLession:Detail", "ElExam:Read", "ElTask:Read"})
*/
private $name;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"ElLessionTopic:Read", "ElLessionTopic:Select", "ElLession:Read", "ElLession:Detail", "ElExam:Read", "ElTask:Read"})
*/
private $id;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups({"ElLessionTopic:Read", "ElLessionTopic:Write", "ElLession:Read", "ElLession:Detail", "ElTask:Read"})
*/
private $ord;
/**
* @ORM\ManyToOne(targetEntity=Container::class)
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*
* @Groups({"ElLessionTopic:Write"})
*/
private $container;
/**
* @ORM\ManyToOne(targetEntity=ElLession::class, inversedBy="elLessionTopics")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*
* @Groups({"ElLessionTopic:Read", "ElLessionTopic:Write", "ElLessionTopic:Move"})
*/
private $elLession;
/**
* @ORM\OneToMany(targetEntity=ElTask::class, mappedBy="elLessionTopic", orphanRemoval=true)
* @Groups({"ElLession:Detail"})
*/
private $elTasks;
/**
* @ORM\ManyToOne(targetEntity=ElCourse::class, inversedBy="elLessionTopics")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*
* @Groups({"ElLessionTopic:Read", "ElLessionTopic:Write"})
*/
public $elCourse;
/**
* @ORM\OneToMany(targetEntity=ElExam::class, mappedBy="elLesstionTopic")
* @Groups({"ElLessionTopic:Read", "ElLession:Detail"})
*/
private $elExams;
/**
* @Groups({"ElLessionTopic:ChangeOrd"})
*/
private int $newOrd;
private $ordChangeDirection;
public function __construct()
{
parent::__construct();
$this->elTasks = new ArrayCollection();
$this->elExams = new ArrayCollection();
}
protected function createTranslation(): TranslationInterface
{
return new ElLessionTopicTranslation();
}
public function getName(): ?string
{
return $this->getTranslation()->getName();
}
public function setName(string $name): self
{
$this->getTranslation()->setName($name);
return $this;
}
public function getDescription(): ?string
{
return $this->getTranslation()->getDescription();
}
public function setDescription(string $description): self
{
$this->getTranslation()->setDescription($description);
return $this;
}
public function getId(): ?int
{
return $this->id;
}
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(),
'elLession' => $this->getElLession()
];
}
public function getOrdChangeDirection()
{
return $this->ordChangeDirection;
}
public function getContainer(): ?Container
{
return $this->container;
}
public function setContainer(?Container $container): self
{
$this->container = $container;
return $this;
}
public function getElLession(): ?ElLession
{
return $this->elLession;
}
public function setElLession(?ElLession $elLession): self
{
$this->elLession = $elLession;
return $this;
}
/**
* @return Collection|ElTask[]
*/
public function getElTasks(): Collection
{
return $this->elTasks;
}
public function addElTask(ElTask $elTask): self
{
if (!$this->elTasks->contains($elTask)) {
$this->elTasks[] = $elTask;
$elTask->setElLessionTopic($this);
}
return $this;
}
public function removeElTask(ElTask $elTask): self
{
if ($this->elTasks->removeElement($elTask)) {
// set the owning side to null (unless already changed)
if ($elTask->getElLessionTopic() === $this) {
$elTask->setElLessionTopic(null);
}
}
return $this;
}
public function getElCourse(): ?ElCourse
{
return $this->elCourse;
}
public function setElCourse(?ElCourse $elCourse): self
{
$this->elCourse = $elCourse;
return $this;
}
/**
* @return Collection|ElExam[]
*/
public function getElExams(): Collection
{
return $this->elExams;
}
public function addElExam(ElExam $elExam): self
{
if (!$this->elExams->contains($elExam)) {
$this->elExams[] = $elExam;
$elExam->setElLesstionTopic($this);
}
return $this;
}
public function removeElExam(ElExam $elExam): self
{
if ($this->elExams->removeElement($elExam)) {
// set the owning side to null (unless already changed)
if ($elExam->getElLesstionTopic() === $this) {
$elExam->setElLesstionTopic(null);
}
}
return $this;
}
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 getMetadataTranslation()
{
$translations = [];
foreach ($this->getTranslations() as $translation) {
$translations[] = [
$translation->getLocale() => [
'name' => $translation->getName()
]
];
}
return $translations;
}
public function getParentCourse()
{
return $this->elCourse;
}
public function getForIndex()
{
$elTasks = [];
foreach ($this->getElTasks() as $task) {
$elTasks[] = $task->getForIndex();
}
$elExams = [];
foreach ($this->getElExams() as $exam) {
$elExams[] = $exam->getForIndex();
}
return [
'id' => $this->getId(),
'ord' => $this->getOrd(),
'elLession' => $this->getElLession()->getId(),
'elCourse' => $this->getElCourse()->getId(),
'translations' => $this->getMetadataTranslation(),
'elTasks' => $elTasks,
'elExams' => $elExams
];
}
}