<?php
namespace App\Containers\CompanySection\EmployeeContainer\Entities;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiProperty;
use App\Ship\Entities\UserEntity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\DBAL\Types\Types;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Serializer\Annotation\MaxDepth;
use App\Ship\Utils\FormattingUtil;
use App\Containers\CompanySection\EmployeeContainer\Data\Repositories\EmployeeRepository;
use App\Containers\CompanySection\StructureContainer\Entities\StructureEntity;
use App\Containers\CompanySection\IndexContainer\Entities\TranslationEntity;
use App\Ship\ApiPlatform\Filter\SimpleSearchFilter;
use App\Ship\ApiPlatform\Filter\FilterLogic;
use App\Containers\CompanySection\EmployeeContainer\UI\ApiPlatform\Controllers\PostSetAvatarByBinaryController;
use App\Ship\Entities\MediaEntity;
/**
* Сотрудники
*/
#[ApiResource(
routePrefix: '/company',
normalizationContext: ['groups' => 'read', 'enable_max_depth' => true],
operations: [
new Get(
uriTemplate: '/employees/{id}', requirements: ['id' => '\d+'],
security: "is_granted('ROLE_ADMIN') or is_granted('ROLE_READONLY')"
),
new Patch(
uriTemplate: '/employees/{id}', requirements: ['id' => '\d+'],
security: "is_granted('ROLE_ADMIN')"
),
new Post(
uriTemplate: '/employees',
security: "is_granted('ROLE_ADMIN')"
),
new Post(
uriTemplate: '/employee/set_avatar_by_binary',
controller: PostSetAvatarByBinaryController::class,
deserialize: false,
validationContext: ['groups' => ['read', 'media_object_create']],
openapiContext: [
'requestBody' => [
'content' => [
'multipart/form-data' => [
'schema' => [
'type' => 'object',
'properties' => [
'file' => [
'type' => 'string',
'format' => 'binary'
]
],
'required' => [
'file'
]
]
]
]
]
],
security: "is_granted('ROLE_ADMIN')"
),
new Delete(
uriTemplate: '/employees/{id}', requirements: ['id' => '\d+'],
security: "is_granted('ROLE_ADMIN')"
),
new GetCollection(
uriTemplate: '/employees',
security: "is_granted('ROLE_ADMIN') or is_granted('ROLE_READONLY')"
),
],
shortName: 'Company_Employees'
)]
#[ORM\Entity(repositoryClass: EmployeeRepository::class)]
#[ORM\Table(name: '`company_employee`')]
#[ApiFilter(SearchFilter::class, properties: [
'id' => 'exact',
'name' => 'partial',
'lastName' => 'partial',
'patronymic' => 'partial',
'structure' => 'exact',
'workPhone' => 'partial',
'internalPhone' => 'partial'
])]
#[ApiFilter(filterClass: SimpleSearchFilter::class, properties: [
'name',
'lastName',
'patronymic',
'email',
'workPhone',
'internalPhone'
])]
#[ApiFilter(FilterLogic::class)]
#[Gedmo\TranslationEntity(class: TranslationEntity::class)]
class EmployeeEntity extends UserEntity implements PasswordAuthenticatedUserInterface
{
// Пароль
#[ORM\Column(type: Types::STRING, length: 255, nullable: false)]
private string $password;
// Активность
#[ORM\Column(type: Types::BOOLEAN)]
#[Groups(['read'])]
private bool $isActive = false;
// Имя
#[ORM\Column(type: Types::STRING, length: 150)]
#[Gedmo\Translatable]
#[Groups(['read'])]
private string $name;
// Фамилия
#[ORM\Column(type: Types::STRING, length: 150)]
#[Gedmo\Translatable]
#[Groups(['read'])]
private string $lastName;
// Отчество
#[ORM\Column(type: Types::STRING, length: 150, nullable: true)]
#[Gedmo\Translatable]
#[Groups(['read'])]
private ?string $patronymic = null;
// Фото
#[ORM\ManyToOne(targetEntity: MediaEntity::class)]
#[ORM\JoinColumn(name: 'photo_id', referencedColumnName: 'id', nullable: true)]
private ?MediaEntity $photo = null;
#[ApiProperty(types: ['https://schema.org/contentUrl'])]
#[Groups(['read', 'media_object:read'])]
public ?string $photoUrl = null;
// Рабочий телефон
#[ORM\Column(type: Types::STRING, length: 150, nullable: false, unique: true)]
#[Groups(['read'])]
private string $workPhone;
// Внутренний номер телефона
#[ORM\Column(type: Types::STRING, length: 50, nullable: true, unique: true)]
#[Groups(['read'])]
private ?string $internalPhone = null;
// Должность
#[ORM\Column(type: Types::STRING, length: 150, nullable: true)]
#[Gedmo\Translatable]
#[Groups(['read'])]
private ?string $position = null;
// Структура
#[ORM\ManyToOne(targetEntity: StructureEntity::class)]
#[ORM\JoinColumn(name: 'structure_id', referencedColumnName: 'id', nullable: true)]
#[Groups(['read'])]
#[MaxDepth(1)]
private ?StructureEntity $structure = null;
#[ORM\Column(type: Types::JSON, length: 150, nullable: true)]
#[Groups(['read'])]
#[ApiProperty(
security: "is_granted('ROLE_ADMIN')"
)]
private $accessProperties = [];
/**
* Used locale to override Translation listener`s locale
* this is not a mapped field of entity metadata, just a simple property
*/
#[Gedmo\Locale]
private $locale;
public function getPassword(): string
{
return (string) $this->password;
}
public function setPassword(string $password): void
{
$this->password = $password;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getLastName()
{
return $this->lastName;
}
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
public function getPatronymic()
{
return $this->patronymic;
}
public function setPatronymic($patronymic)
{
$this->patronymic = $patronymic;
return $this;
}
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return \array_unique($roles);
}
public function getPhoto()
{
return $this->photo;
}
public function setPhoto($photo)
{
$this->photo = $photo;
return $this;
}
public function getWorkPhone()
{
return $this->workPhone;
}
public function setWorkPhone($value)
{
$this->workPhone = (!empty($value))
? FormattingUtil::phoneNumber($value)
: $value;
return $this;
}
public function getInternalPhone()
{
return $this->internalPhone;
}
public function setInternalPhone($value)
{
$this->internalPhone = (!empty($value))
? FormattingUtil::phoneNumber($value)
: $value;
return $this;
}
public function getPosition()
{
return $this->position;
}
public function setPosition($position)
{
$this->position = $position;
return $this;
}
public function getIsActive()
{
return $this->isActive;
}
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
public function getStructure()
{
return $this->structure;
}
public function setStructure($structure)
{
$this->structure = $structure;
return $this;
}
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
/**
* Get the value of accessProperties
*/
public function getAccessProperties()
{
return $this->accessProperties;
}
/**
* Set the value of accessProperties
*
* @return self
*/
public function setAccessProperties($accessProperties)
{
$this->accessProperties = $accessProperties;
return $this;
}
}