<?php
namespace App\Entity;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use App\Controller\Action\SectionRankedController;
use App\Repository\EvaluationRepository;
use App\State\SectionDeleteProcessor;
use App\State\SectionRankedProcessor;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use App\Controller\Action\SectionWithEvaluationStatAction;
use Symfony\Component\Serializer\Annotation\Groups;
use Doctrine\Common\Collections\Criteria;
#[ORM\Table(name: 'section')]
#[ORM\Index(name: 'document_id', columns: ['document_id'])]
#[ORM\Entity]
#[ApiResource(
order: ['ranked' => 'ASC'],
normalizationContext: ['groups' => ['section.get']],
operations: [
new GetCollection(),
new GetCollection(
name: '_api_/sections/with_stat',
uriTemplate: '/sections/with_stat',
controller: SectionWithEvaluationStatAction::class,
write: false,
read: false
),
new Post(
security: 'user.isMasterAdmin()',
securityMessage: 'You do not have permission to create a section.',
),
new Post(
name: '_api_/sections/reorder',
uriTemplate: '/sections/reorder',
processor: SectionRankedProcessor::class
),
new Get(),
new Put(
name: '_api_/sections/reorder/{id}',
uriTemplate: '/sections/reorder/{id}',
processor: SectionRankedProcessor::class,
security: 'user.isMasterAdmin()',
securityMessage: 'You do not have permission to reorder this section.',
),
new Delete(
processor: SectionDeleteProcessor::class,
security: 'user.isMasterAdmin()',
securityMessage: 'You do not have permission to delete this section.',
)
])
]
#[ApiFilter(
SearchFilter::class, properties: [
'title' => 'partial',
'document' => 'exact',
]
)]
class Section
{
#[ORM\Column(name: 'id', type: 'integer', nullable: false)]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
#[Groups(['section.get', 'plan.get'])]
private int $id;
#[ORM\Column(name: 'old_id', type: 'string', length: 255, nullable: true)]
private ?string $oldId = null;
#[ORM\Column(name: 'title', type: 'string', length: 16777215, nullable: true)]
#[Groups(['section.get', 'plan.get'])]
private string $title;
#[ORM\Column(name: 'name', type: 'string', length: 16777215, nullable: true)]
#[Groups('section.get')]
private string $name;
#[ORM\Column(name: 'ranked', type: 'integer')]
#[Groups('section.get')]
private int $ranked = 1;
#[ORM\JoinColumn(name: 'document_id', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: 'Document', inversedBy: 'sections')]
#[Groups(['plan.get'])]
private Document $document;
#[ORM\OneToMany(mappedBy: 'section', targetEntity: Evaluation::class)]
private Collection $evaluations;
#[ORM\OneToMany(mappedBy: 'section', targetEntity: Compliance::class)]
private Collection $compliances;
#[ORM\Column(name: 'last_update_compliance', type: 'datetime', nullable: true)]
#[Groups('section.get')]
private ?\DateTimeInterface $lastUpdateCompliance = null;
public function __construct()
{
$this->evaluations = new ArrayCollection();
$this->compliances = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
public function getOldId(): ?string
{
return $this->oldId;
}
public function setOldId(?string $oldId): self
{
$this->oldId = $oldId;
return $this;
}
public function getTitle(): string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getRanked(): int
{
return $this->ranked;
}
public function setRanked(int $ranked): self
{
$this->ranked = $ranked;
return $this;
}
public function getDocument(): Document
{
return $this->document;
}
public function setDocument(Document $document): self
{
$this->document = $document;
return $this;
}
/**
* @return Collection<int, Evaluation>
*/
public function getEvaluations(): Collection
{
return $this->evaluations;
}
/**
* @return Collection<int, Evaluation>
*/
public function getEvaluationsBySiteId(Site $site): Collection
{
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->eq('site', $site));
return $this->evaluations->matching($criteria);
}
public function addEvaluation(Evaluation $evaluation): self
{
if (!$this->evaluations->contains($evaluation)) {
$this->evaluations->add($evaluation);
$evaluation->setSection($this);
}
return $this;
}
public function removeEvaluation(Evaluation $evaluation): self
{
if ($this->evaluations->removeElement($evaluation)) {
// set the owning side to null (unless already changed)
if ($evaluation->getSection() === $this) {
$evaluation->setSection(null);
}
}
return $this;
}
/**
* @return Collection
*/
public function getCompliances(): Collection
{
return $this->compliances;
}
#[Groups(['section.get', 'document.get'])]
public function getCompliancesCount()
{
return $this->compliances->count();
}
public function getLastUpdateCompliance(): ?\DateTimeInterface
{
return $this->lastUpdateCompliance;
}
public function setLastUpdateCompliance(?\DateTimeInterface $lastUpdateCompliance): self
{
$this->lastUpdateCompliance = $lastUpdateCompliance;
return $this;
}
}