<?php
namespace App\Entity;
use App\Controller\Action\CloneSiteActionController;
use App\Controller\Action\DeleteSitesController;
use App\Controller\Action\DocumentSiteDeleteActionController;
use App\Controller\Action\GetFilsOfParentEntityAction;
use App\Controller\Action\GetSiteAction;
use App\Controller\Action\ListProcessingTasksController;
use App\Controller\Action\ReportMonthlyEvolutionController;
use App\Controller\Action\SiteAffectationController;
use App\Controller\Action\StatConformiteSiteController;
use App\Controller\Action\StatECRAction;
use App\Controller\Action\StatGlobalConformityRatpController;
use App\Services\SiteService;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Metadata\ApiResource;
use Doctrine\ORM\Mapping\PrePersist;
use Symfony\Component\Serializer\Annotation\Groups;
use App\Traits\AutoTimestampTrait;
use App\ApiPlatform\CustomerAwareInterface;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use App\Controller\Action\SiteWithEvaluationStatAction;
#[ORM\Table(name: 'site')]
#[ORM\Index(name: 'parent_id', columns: ['parent_id'])]
#[ORM\Index(name: 'customer_id', columns: ['customer_id'])]
#[ORM\Entity]
#[ApiResource(
operations: [
new GetCollection(
order: ['code' => 'ASC'],
paginationEnabled: false
),
new GetCollection(
name: '_api_/sites/with_stat',
uriTemplate: '/sites/with_stat',
controller: SiteWithEvaluationStatAction::class,
write: false,
read: false
),
new GetCollection(
name: '_api_/site_affectation',
uriTemplate: '/site_affectation',
controller: SiteAffectationController::class,
write: false,
read: false
),
new GetCollection(
name: '_api_/report_monthly_evolution',
uriTemplate: '/report_monthly_evolution',
controller: ReportMonthlyEvolutionController::class,
write: false,
read: false
),
new GetCollection(
name: '_api_/stat_conformite_site',
uriTemplate: '/stat_conformite_site',
controller: StatConformiteSiteController::class,
write: false,
read: false
),
new Post(
uriTemplate: '/stat_global_conformity_ratp',
controller: StatGlobalConformityRatpController::class,
read: false,
write: false,
name: '_api_/stat_global_conformity_ratp'
),
new Post(
name: '_api_/list_processing_tasks_site',
uriTemplate: '/list_processing_tasks_site',
controller: ListProcessingTasksController::class,
write: false,
read: false
),
new GetCollection(
name: '_api_/list_site_of_parent',
uriTemplate: '/list_site_of_parent',
controller: GetFilsOfParentEntityAction::class,
write: false,
read: false
),
new Post(
name: '_api_/sites/clone',
uriTemplate: '/sites/clone',
controller: CloneSiteActionController::class,
write: false,
read: false
),
new Post(
security: 'user.isMasterAdmin()',
),
new Get(),
new Get(
name: '_api_/site/{id}',
uriTemplate: '/site/{id}',
controller: GetSiteAction::class,
write: false,
read: false
),
new Post(
name: '_api_/stat_ECR_100',
uriTemplate: '/stat_ECR_100',
controller: StatECRAction::class,
write: false,
read: false
),
new Post(
name: '_api_/delete_site',
uriTemplate: '/delete_site',
controller: DeleteSitesController::class,
write: false,
read: false
),
new Put(
security: 'user.isMasterAdmin()',
),
new Delete(
// Duplicate de l'operation delete , il y a deja une POST pour supprimer un site
security: 'user.isMasterAdmin()',
)
],
normalizationContext: ['groups' => ['site.get', 'document.get', 'user.get', 'customer.get', 'discussion.get']],
denormalizationContext: ['groups' => ['site.write']]
)]
#[ORM\HasLifecycleCallbacks]
#[ApiFilter(
SearchFilter::class,
properties: [
'id' => 'exact',
'type' => 'exact',
'customer' => 'exact',
'userSites.user' => 'exact',
'affectations.document' => 'exact'
]
)]
class Site implements CustomerAwareInterface
{
use AutoTimestampTrait;
const SITE_TYPE = 'site';
const ENTITY_TYPE = 'entity';
#[ORM\Column(name: 'id', type: 'integer', nullable: false)]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
#[Groups(['site.get', 'document.get', 'user.get', 'customer.get', 'plan.get'])]
private int $id;
#[ORM\Column(name: 'old_id', type: 'string', length: 255, nullable: true)]
private ?string $oldId = null;
#[ORM\Column(name: 'code', type: 'string', length: 255, nullable: true)]
#[Groups(['site.get', 'site.write', 'customer.get', 'user.get', 'plan.get'])]
private ?string $code = null;
#[ORM\Column(name: 'name', type: 'string', length: 255, nullable: true)]
#[Groups(['site.get', 'site.write', 'document.get', 'user.get', 'customer.get', 'plan.get'])]
private ?string $name = null;
#[ORM\Column(name: 'kind', type: 'string', length: 255, nullable: true)]
#[Groups(['site.get', 'site.write'])]
private ?string $kind = null;
#[ORM\Column(name: 'address', type: 'string', length: 255, nullable: true)]
#[Groups(['site.get', 'site.write'])]
private ?string $address = null;
#[ORM\Column(name: 'additional_address', type: 'string', length: 255, nullable: true)]
#[Groups(['site.get', 'site.write'])]
private ?string $additionalAddress = null;
#[ORM\Column(name: 'zipcode', type: 'string', length: 255, nullable: true)]
#[Groups(['site.get', 'site.write', 'document.get'])]
private ?string $zipcode = null;
#[ORM\Column(name: 'phone', type: 'string', length: 255, nullable: true)]
#[Groups(['site.get', 'site.write', 'document.get'])]
private ?string $phone = null;
#[ORM\Column(name: 'naf', type: 'string', length: 255, nullable: true)]
#[Groups(['site.get', 'site.write'])]
private ?string $naf = null;
#[ORM\Column(name: 'adr', type: 'string', length: 255, nullable: true)]
#[Groups(['site.get', 'site.write'])]
private ?string $adr = null;
#[ORM\Column(name: 'fax', type: 'string', length: 255, nullable: true)]
#[Groups(['site.get', 'site.write'])]
private ?string $fax = null;
#[ORM\Column(name: 'groupe', type: 'string', length: 255, nullable: true)]
#[Groups(['site.get', 'site.write'])]
private ?string $groupe = null;
#[ORM\Column(name: 'coordinates_lambert', type: 'string', length: 255, nullable: true)]
#[Groups(['site.get', 'site.write'])]
private ?string $coordinatesLambert = null;
#[ORM\Column(name: 'is_active', type: 'boolean', nullable: true)]
#[Groups(['site.get', 'site.write'])]
private ?bool $isActive = null;
#[ORM\Column(name: 'type', type: 'string', length: 255)]
#[Groups(['site.get', 'site.write', 'document.get', 'customer.get'])]
private ?string $type = self::ENTITY_TYPE;
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'sites')]
#[Groups(['site.write'])]
private ?self $parent = null;
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)]
#[Groups(['site.get','customer.get', 'site.write'])]
private Collection $sites;
#[ORM\OneToMany(mappedBy: 'site', targetEntity: Affectation::class, cascade: ['persist'], orphanRemoval: true)]
#[Groups(['site.write'])]
private Collection $affectations;
#[ORM\OneToMany(mappedBy: 'site', targetEntity: UserSite::class)]
#[Groups(['site.write', 'plan.get'])]
private Collection $userSites;
#[ORM\Column(length: 255, nullable: true)]
#[Groups(['site.get', 'site.write'])]
private ?string $city = null;
#[ORM\Column(name: 'reference', type: 'string', length: 255, nullable: true)]
#[Groups(['site.get', 'site.write'])]
private ?string $reference = null;
#[ORM\ManyToMany(targetEntity: Country::class)]
#[Groups(['site.write', 'site.get'])]
private Collection $documentCountries;
#[ORM\ManyToMany(targetEntity: Category::class)]
#[Groups(['site.write', 'site.get'])]
private Collection $documentThemes;
#[ORM\ManyToOne]
private ?Country $country = null;
#[ORM\OneToMany(mappedBy: 'site', targetEntity: Evaluation::class)]
private Collection $evaluations;
#[ORM\ManyToOne(cascade: ['persist'])]
#[ORM\JoinColumn(name: 'customer_id', referencedColumnName: 'id')]
#[Groups(['site.get','site.write'])]
private ?Customer $customer = null;
#[ORM\Column(name: 'init_site', type: 'boolean', nullable: true, options: ['default' => false])]
#[Groups(['site.get','site.write'])]
private ?bool $initStatus = false;
#[ORM\OneToMany(mappedBy: 'site', targetEntity: SiteQuestions::class)]
private Collection $siteQuestions;
#[ORM\Column(nullable: true)]
#[Groups(['site.get','site.write', 'discussion.get'])]
private ?array $path = [];
#[ORM\Column(nullable: true)]
#[Groups(['site.get', 'site.write'])]
private ?int $year = null;
public function __construct()
{
$this->sites = new ArrayCollection();
$this->affectations = new ArrayCollection();
$this->users = new ArrayCollection();
$this->userSites = new ArrayCollection();
$this->countries = new ArrayCollection();
$this->categories = new ArrayCollection();
$this->documentCountries = new ArrayCollection();
$this->documentThemes = new ArrayCollection();
$this->evaluations = new ArrayCollection();
$this->initStatus = true;
$this->siteQuestions = new ArrayCollection();
}
public function getId(): ?int
{
return $this->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 getCode(): ?string
{
return $this->code;
}
public function setCode(?string $code): self
{
$this->code = $code;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getKind(): ?string
{
return $this->kind;
}
public function setKind(?string $kind): self
{
$this->kind = $kind;
return $this;
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(?string $address): self
{
$this->address = $address;
return $this;
}
public function getAdditionalAddress(): ?string
{
return $this->additionalAddress;
}
public function setAdditionalAddress(?string $additionalAddress): self
{
$this->additionalAddress = $additionalAddress;
return $this;
}
public function getZipcode(): ?string
{
return $this->zipcode;
}
public function setZipcode(?string $zipcode): self
{
$this->zipcode = $zipcode;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getNaf(): ?string
{
return $this->naf;
}
public function setNaf(?string $naf): self
{
$this->naf = $naf;
return $this;
}
public function getAdr(): ?string
{
return $this->adr;
}
public function setAdr(?string $adr): self
{
$this->adr = $adr;
return $this;
}
public function getFax(): ?string
{
return $this->fax;
}
public function setFax(?string $fax): self
{
$this->fax = $fax;
return $this;
}
public function getGroupe(): ?string
{
return $this->groupe;
}
public function setGroupe(?string $groupe): self
{
$this->groupe = $groupe;
return $this;
}
public function getCoordinatesLambert(): ?string
{
return $this->coordinatesLambert;
}
public function setCoordinatesLambert(?string $coordinatesLambert): self
{
$this->coordinatesLambert = $coordinatesLambert;
return $this;
}
public function isIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(?bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(?string $type): self
{
$this->type = $type;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection<int, self>
*/
public function getSites(): Collection
{
return $this->sites;
}
public function setSites($sites): self
{
$this->sites = $sites;
return $this;
}
public function addSite(self $site): self
{
if (!$this->sites->contains($site)) {
$this->sites->add($site);
$site->setParent($this);
}
return $this;
}
public function removeSite(self $site): self
{
if ($this->sites->removeElement($site)) {
// set the owning side to null (unless already changed)
if ($site->getParent() === $this) {
$site->setParent(null);
}
}
return $this;
}
/**
* @return Collection<int, Affectation>
*/
public function getAffectations(): Collection
{
return $this->affectations;
}
public function addAffectation(Affectation $affectation): self
{
if (!$this->affectations->contains($affectation)) {
$this->affectations->add($affectation);
$affectation->setSite($this);
}
return $this;
}
public function removeAffectation(Affectation $affectation): self
{
if ($this->affectations->removeElement($affectation)) {
// set the owning side to null (unless already changed)
if ($affectation->getSite() === $this) {
$affectation->setSite(null);
}
}
return $this;
}
/**
* @return Collection<int, UserSite>
*/
public function getUserSites(): Collection
{
return $this->userSites;
}
public function addUserSite(UserSite $userSite): self
{
if (!$this->userSites->contains($userSite)) {
$this->userSites->add($userSite);
$userSite->setSite($this);
}
return $this;
}
public function removeUserSite(UserSite $userSite): self
{
if ($this->userSites->removeElement($userSite)) {
if ($userSite->getSite() === $this) {
$userSite->setSite(null);
}
}
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(?string $city): self
{
$this->city = $city;
return $this;
}
/**
* @return Collection<int, Country>
*/
public function getDocumentCountries(): Collection
{
return $this->documentCountries;
}
public function addDocumentCountry(Country $documentCountry): self
{
if (!$this->documentCountries->contains($documentCountry)) {
$this->documentCountries->add($documentCountry);
}
return $this;
}
public function removeDocumentCountry(Country $documentCountry): self
{
$this->documentCountries->removeElement($documentCountry);
return $this;
}
/**
* @return Collection<int, Category>
*/
public function getDocumentThemes(): Collection
{
return $this->documentThemes;
}
public function addDocumentTheme(Category $documentTheme): self
{
if (!$this->documentThemes->contains($documentTheme)) {
$this->documentThemes->add($documentTheme);
}
return $this;
}
public function removeDocumentTheme(Category $documentTheme): self
{
$this->documentThemes->removeElement($documentTheme);
return $this;
}
public function getCountry(): ?Country
{
return $this->country;
}
public function setCountry(?Country $country): self
{
$this->country = $country;
return $this;
}
public function getEvaluations(): Collection
{
return $this->evaluations;
}
public function addEvaluation(Evaluation $evaluation): self
{
if (!$this->evaluations->contains($evaluation)) {
$this->evaluations->add($evaluation);
$evaluation->setSite($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->getDocument() === $this) {
$evaluation->setSite(null);
}
}
return $this;
}
public function isSite()
{
return self::SITE_TYPE === $this->type;
}
public function isEntity()
{
return self::ENTITY_TYPE === $this->type;
}
public static function getCustomerColumnName(): string
{
return 'customer';
}
public function getCustomer(): ?Customer
{
return $this->customer;
}
public function setCustomer(?Customer $customer): self
{
$this->customer = $customer;
return $this;
}
public function isInitStatus(): ?bool
{
return $this->initStatus;
}
public function setInitStatus(bool $initStatus): self
{
$this->initStatus = $initStatus;
return $this;
}
/**
* @return Collection<int, SiteQuestions>
*/
public function getSiteQuestions(): Collection
{
return $this->siteQuestions;
}
public function addSiteQuestion(SiteQuestions $siteQuestion): self
{
if (!$this->siteQuestions->contains($siteQuestion)) {
$this->siteQuestions->add($siteQuestion);
$siteQuestion->setSite($this);
}
return $this;
}
public function removeSiteQuestion(SiteQuestions $siteQuestion): self
{
if ($this->siteQuestions->removeElement($siteQuestion)) {
// set the owning side to null (unless already changed)
if ($siteQuestion->getSite() === $this) {
$siteQuestion->setSite(null);
}
}
return $this;
}
public function getPath(): ?array
{
return $this->path;
}
public function setPath(?array $path): self
{
$this->path = $path;
return $this;
}
#[PrePersist]
public function setPathNotNull(): self
{
if (empty($this->path))
return SiteService::generateFullPath($this);
else return $this;
}
public function getYear(): ?int
{
return $this->year;
}
public function setYear(?int $year): self
{
$this->year = $year;
return $this;
}
}