<?php
namespace App\Entity;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use App\BaseContext;
use App\Repository\ServiceRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Id;
use Ramsey\Uuid\Doctrine\UuidGenerator;
use Ramsey\Uuid\UuidInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: ServiceRepository::class)]
#[ORM\Table]
#[UniqueEntity(fields: ['url'], message: 'Url musi być unikalny')]
#[ApiResource(
operations: [
new Get(normalizationContext: self::CONTEXT_ITEM_READ, name: 'getServiceItem'),
new GetCollection(name: 'getServiceCollection'),
],
normalizationContext: self::CONTEXT_READ,
denormalizationContext: self::CONTEXT_WRITE
)]
#[ApiFilter(filterClass: SearchFilter::class, properties: [
'name' => 'ipartial',
'url' => 'ipartial',
])]
#[ApiFilter(filterClass: BooleanFilter::class, properties: ['isCaseRelated'])]
class Service
{
public const GROUP_ALL = 'service:all';
public const GROUP_READ = 'service:read';
public const GROUP_WRITE = 'service:write';
public const GROUP_ITEM_READ = 'service:item_read';
public const DEFAULT_FOOTER_CONTENT = '<p><small> Szczegółowe informacje na temat realizacji procesu uzyskać można pod nr telefonu <a href="tel:74 66 55 121">74 66 55 121</a></small></p>';
public const CONTEXT_READ = [
'groups' => [BaseContext::GROUP_ALL, BaseContext::GROUP_READ, self::GROUP_ALL, self::GROUP_READ],
'swagger_definition_name' => self::class . 'Read',
'openapi_definition_name' => self::class . 'Read',
];
public const CONTEXT_WRITE = [
'groups' => [BaseContext::GROUP_ALL, BaseContext::GROUP_WRITE, self::GROUP_ALL, self::GROUP_WRITE],
'swagger_definition_name' => self::class . 'Write',
'openapi_definition_name' => self::class . 'Write',
];
public const CONTEXT_ITEM_READ = [
'groups' => [BaseContext::GROUP_ALL, BaseContext::GROUP_READ, self::GROUP_ALL, self::GROUP_READ, self::GROUP_ITEM_READ],
'swagger_definition_name' => self::class . 'ItemRead',
'openapi_definition_name' => self::class . 'ItemRead',
];
#[Id]
#[Column(type: 'uuid', nullable: false)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
#[Groups([self::GROUP_READ, BaseContext::GROUP_READ])]
protected UuidInterface $id;
#[Groups([self::GROUP_READ, BaseContext::GROUP_READ])]
#[ORM\Column(type: 'string', length: 4000)]
public ?string $name = null;
#[Groups([self::GROUP_READ, BaseContext::GROUP_READ])]
#[ORM\Column(type: 'string', length: 255)]
public ?string $icon = null;
#[Groups([self::GROUP_READ, BaseContext::GROUP_READ])]
#[ORM\Column(type: 'string', length: 4000, unique: true)]
public ?string $url = null;
#[Groups([self::GROUP_READ, BaseContext::GROUP_READ])]
#[ORM\Column(type: 'string', length: 255, nullable: true)]
public ?string $code = null;
#[ORM\Column(type: 'integer')]
public ?int $position = null;
#[Groups([self::GROUP_READ, BaseContext::GROUP_READ])]
#[ORM\Column(type: 'text',nullable: true)]
public ?string $description = null;
#[Groups([self::GROUP_READ, self::GROUP_ITEM_READ, BaseContext::GROUP_READ])]
#[ORM\Column(type: 'text', nullable: true)]
public ?string $content = null;
#[Groups([self::GROUP_READ, BaseContext::GROUP_READ])]
#[ORM\Column(type: 'boolean')]
public bool $isActive = true;
#[Groups([self::GROUP_READ, BaseContext::GROUP_READ])]
#[ORM\Column(type: 'boolean')]
public bool $isCaseRelated = false;
#[ORM\ManyToOne]
public ?DocumentTemplate $documentTemplate = null;
#[Groups([self::GROUP_READ, Application::GROUP_READ])]
#[ORM\OneToMany(mappedBy: 'service', targetEntity: ServiceAttachmentPosition::class, orphanRemoval: true,cascade: ['persist', 'remove'])]
private Collection $serviceAttachmentPositions;
#[Groups([self::GROUP_READ, self::GROUP_ITEM_READ, BaseContext::GROUP_READ])]
#[ORM\Column(type: 'text', nullable: true)]
public ?string $gdpr = null;
#[Groups([self::GROUP_READ, BaseContext::GROUP_READ])]
#[ORM\Column(type: 'text', nullable: true)]
public ?string $editorFooterContent = null;
public function __construct()
{
$this->serviceAttachmentPositions = new ArrayCollection();
$this->editorFooterContent = self::DEFAULT_FOOTER_CONTENT;
}
public function getId(): UuidInterface
{
return $this->id;
}
#[Groups([self::GROUP_READ])]
public function getUuid(): UuidInterface
{
return $this->id;
}
/**
* @return Collection<int, ServiceAttachmentPosition>
*/
public function getServiceAttachmentPositions(): Collection
{
return $this->serviceAttachmentPositions;
}
public function addServiceAttachmentPosition(ServiceAttachmentPosition $serviceAttachmentPosition): self
{
if (!$this->serviceAttachmentPositions->contains($serviceAttachmentPosition)) {
$this->serviceAttachmentPositions->add($serviceAttachmentPosition);
$serviceAttachmentPosition->setService($this);
}
return $this;
}
public function removeServiceAttachmentPosition(ServiceAttachmentPosition $serviceAttachmentPosition): self
{
if ($this->serviceAttachmentPositions->removeElement($serviceAttachmentPosition)) {
// set the owning side to null (unless already changed)
if ($serviceAttachmentPosition->getService() === $this) {
$serviceAttachmentPosition->setService(null);
}
}
return $this;
}
}