<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
use ApiPlatform\Core\Serializer\Filter\GroupFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Repository\ChatRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=ChatRepository::class)
* @ApiResource(
* mercure={
* "private"=true,
* "normalization_context"={
* "groups"={
* "chat:read:id",
* "chat:read:countChatMessages",
* "chat:read:unreadChatMessageCount",
* "chat:read:active",
* "chat:read:read",
* "chat:read:lastChatMessage",
* "chat_message:read:id",
* "chat_message:read:content",
* "chat_message:read:received",
* "chat_message:read:read",
* "chat_message:read:createdAt",
* "chat_message:read:user",
* "user:read:id",
* "user:read:firstName",
* "user:read:lastName",
* "user:read:client",
* "client:read:id",
* "client:read:firstName",
* "client:read:lastName",
* "client:read:avatar",
* "chat_message:read:attachments",
* "media_object:read:contentUrl"
* }
* }
* },
* normalizationContext={
* "groups"={"chat:read"}
* },
* denormalizationContext={
* "groups"={"chat:write"}
* },
* itemOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "patch"={
* "security"="is_granted('ROLE_USER')"
* },
* "delete"={
* "security"="is_granted('ROLE_USER')"
* }
* },
* collectionOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "post"={
* "security"="is_granted('ROLE_USER')"
* },
* "get_by_user_and_recipient"={
* "security"="is_granted('ROLE_USER')",
* "path"="/users/{user}/teams/{team}/recipients/{recipient}/chat",
* "method"="GET",
* "controller"="App\Controller\Api\ChatController::getChatByTeamAndUserAndRecipient"
* }
* }
* )
* @ApiFilter(SearchFilter::class, properties={"team.id", "active", "company.id", "type", "users.id", "countUsers", "countChatMessages"})
* @ApiFilter(ExistsFilter::class, properties={"type", "countUsers", "team", "company", "countChatMessages"})
* @ApiFilter(OrderFilter::class)
* @ApiFilter(GroupFilter::class,
* arguments={
* "parameterName": "groups",
* "overrideDefaultGroups": true
* }
* )
* @ApiFilter(PropertyFilter::class,
* arguments={
* "parameterName"="fields",
* "overrideDefaultProperties"=true
* }
* )
*/
class Chat
{
const TYPE_GLOBAL = "global";
const TYPE_TEAM = "team";
const TYPE_MAIN = "main";
const TYPES = [
self::TYPE_GLOBAL => self::TYPE_GLOBAL,
self::TYPE_TEAM => self::TYPE_TEAM,
];
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @ApiProperty(identifier=true)
* @Groups({"chat:read", "chat:read:id", "team:read", "team_user:read"})
*/
private ?int $id = null;
/**
* @ORM\Column(type="string", length=255, nullable=true, options={"default": "global"})
* @Groups({"chat:read", "chat:read:type", "client:read", "user:read", "company:read", "team_user:read", "team:read"})
* @Assert\NotBlank
*/
private ?string $type = self::TYPE_GLOBAL;
/**
* @ORM\ManyToMany(targetEntity=User::class, mappedBy="chats")
* @Groups({"chat:read", "chat:write", "chat:read:users"})
*/
private $users;
/**
* @ORM\Column(type="integer", nullable=true, options={"default": 0})
* @Groups({"chat:read", "chat:read:countUsers", "team:read"})
*/
private ?int $countUsers = 0;
/**
* @ORM\ManyToOne(targetEntity=Team::class)
* @Groups({"chat:read", "chat:write", "chat:read:team"})
*/
private ?Team $team;
/**
* @ORM\ManyToOne(targetEntity=Company::class)
* @Groups({"chat:read", "chat:write", "chat:read:company"})
*/
private ?Company $company = null;
/**
* @ORM\OneToMany(targetEntity=ChatMessage::class, mappedBy="chat", orphanRemoval=true)
* @Groups({"chat:read", "chat:read:chatMessages"})
*/
private $chatMessages;
/**
* @ORM\Column(type="integer", nullable=true, options={"default": 0})
* @Groups({"chat:read", "chat:read:countChatMessages", "team:read"})
*/
private ?int $countChatMessages = 0;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default": 1})
* @Groups({"chat:read", "chat:write", "chat:read:active", "team:read"})
*/
private $active = true;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Gedmo\Timestampable(on="create")
* @Groups({"chat:read", "chat:read:createdAt"})
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Gedmo\Timestampable(on="update")
* @Groups({"chat:read", "chat:read:updatedAt"})
*/
private $updatedAt;
/**
* @Groups({"chat:read", "chat:read:lastChatMessage", "team:read"})
* @var ?ChatMessage
*/
private $lastChatMessage = null;
/**
* Custom field
* @var ?string
* @Groups({"chat:read", "chat:read:name", "team:read"})
*/
public $name = null;
/**
* Custom field
* @var ?MediaObject
* @Groups({"chat:read", "chat:read:image", "team:read"})
*/
public $image = null;
/**
* Post Custom Field
* @Groups({"chat:read", "chat:read:read"})
*/
private bool $read = false;
/**
* Post Custom Field
* @Groups({"chat:read", "chat:read:unreadChatMessageCount"})
*/
private ?int $unreadChatMessageCount = null;
public function __construct()
{
$this->users = new ArrayCollection();
$this->chatMessages = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(?string $type): self
{
$this->type = $type;
return $this;
}
/**
* @return Collection<int, User>
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->addChat($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->removeElement($user)) {
$user->removeChat($this);
}
return $this;
}
public function getCountUsers(): ?int
{
return $this->countUsers;
}
public function setCountUsers(?int $countUsers): self
{
$this->countUsers = $countUsers;
return $this;
}
public function getTeam(): ?Team
{
return $this->team;
}
public function setTeam(?Team $team): self
{
$this->team = $team;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
/**
* @return Collection|ChatMessage[]
*/
public function getChatMessages(): Collection
{
return $this->chatMessages;
}
public function addChatMessage(ChatMessage $chatMessage): self
{
if ($chatMessage instanceof ChatMessage) {
if (!$this->chatMessages->contains($chatMessage)) {
$this->chatMessages[] = $chatMessage;
$chatMessage->setChat($this);
}
}
return $this;
}
public function removeChatMessage(ChatMessage $chatMessage): self
{
if ($this->chatMessages->removeElement($chatMessage)) {
// set the owning side to null (unless already changed)
if ($chatMessage->getChat() === $this) {
$chatMessage->setChat(null);
}
}
return $this;
}
public function getCountChatMessages(): ?int
{
return $this->countChatMessages;
}
public function setCountChatMessages(?int $countChatMessages): self
{
$this->countChatMessages = $countChatMessages;
return $this;
}
public function getActive(): ?bool
{
return $this->active;
}
public function isActive(): ?bool
{
return $this->active;
}
public function setActive(?bool $active): self
{
$this->active = $active;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getLastChatMessage(): ?ChatMessage
{
if (!$this->chatMessages->isEmpty()) {
$this->lastChatMessage = $this->chatMessages->last();
}
return $this->lastChatMessage;
}
public function setLastChatMessage(?ChatMessage $lastChatMessage): self
{
$this->lastChatMessage = $lastChatMessage;
return $this;
}
public function getRead(): ?bool
{
return $this->read;
}
/**
* @Groups({"chat:read", "chat:read:isRead"})
*/
public function isRead(): ?bool
{
return $this->read;
}
public function setRead(?bool $read): self
{
$this->read = $read;
return $this;
}
public function getUnreadChatMessageCount(): ?int
{
return $this->unreadChatMessageCount;
}
public function setUnreadChatMessageCount(?int $unreadChatMessageCount): self
{
$this->unreadChatMessageCount = $unreadChatMessageCount;
return $this;
}
}