<?php
namespace App\Entity;
use App\Annotation\EsUploadable;
use Doctrine\ORM\Mapping as ORM;
use App\Annotation\EsElasticIndex;
use App\Repository\NewsfeedRepository;
use ApiPlatform\Core\Annotation\ApiFilter;
use App\Entity\Traits\TimestampableEntity;
use Doctrine\Common\Collections\Collection;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Interfaces\OwnerMappedInterface;
use App\Entity\Interfaces\UploadMappedInterface;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Interfaces\ElasticMappedInterface;
use App\Entity\Interfaces\ContainerMappedInterface;
use App\Entity\Interfaces\TotalLikeMappedInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\SerializedName;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
/**
* @ApiResource(
* attributes={"security"="is_granted('IS_AUTHENTICATED_FULLY')"},
* normalizationContext={"groups"={"Newsfeed:Read", "TZ:Read", "Container:Storage"}, "skip_null_values"=false},
* denormalizationContext={"groups"={"Newsfeed:Write"}},
* collectionOperations={
* "get"={
* "method"="GET",
* "normalization_context"={"groups"={"Newsfeed:Read", "TZ:Read", "Container:Storage"}, "skip_null_values"=false},
* },
* "get_user_feed"={
* "path"="/newsfeeds/get-user-feed",
* "method"="GET",
* "normalization_context"={"groups"={"Newsfeed:Read", "TZ:Read", "Container:Storage"}, "skip_null_values"=false},
* "security"="is_granted('IS_AUTHENTICATED_FULLY')"
* },
* "get_user_saved"={
* "path"="/newsfeeds/get-user-saved",
* "method"="GET",
* "normalization_context"={"groups"={"Newsfeed:Read", "TZ:Read", "Container:Storage"}, "skip_null_values"=false},
* "security"="is_granted('IS_AUTHENTICATED_FULLY')"
* },
* "post"={"security"="is_granted('ROLE_USER')"}
* },
* itemOperations={
* "get"={
* "normalization_context"={"groups"={"Newsfeed:Read", "TZ:Read", "Container:Storage"}, "skip_null_values"=false},
* "security"="is_granted('IS_CO_USR_SPE', object) || is_granted('IS_CO_OPR', object)"
* },
* "get_dicom"={
* "path"="/newsfeeds/{id}/get-dicom",
* "method"="GET",
* "normalization_context"={"groups"={"Newsfeed:Read", "Newsfeed:Read-Dicom", "TZ:Read", "Container:Storage"}, "skip_null_values"=false},
* "security"="is_granted('IS_CO_USR_SPE', object) || is_granted('IS_CO_OPR', object)"
* },
* "put"={"security"="is_granted('IS_CO_USR_SPE', object) || is_granted('IS_CO_OPR', object)"},
* "patch"={"security"="is_granted('IS_CO_USR_SPE', object) || is_granted('IS_CO_OPR', object)"},
* "patch_like_toggle"={
* "route_name"="api_newsfeeds_like_toggle_collection",
* "method"="PATCH",
* "security"="is_granted('ROLE_USER', object)",
* "denormalization_context"={"groups"={"Newsfeed:Like-Toggle"}}
* },
* "delete"={"security"="is_granted('IS_CO_USR_SPE', object) || is_granted('IS_CO_OPR', object)"}
* }
* )
* @ApiFilter(SearchFilter::class, properties={"postText": "partial", "user.id": "exact", "container.id": "exact", "postType": "exact", "typeId": "exact", "newsfeedCategory.id": "exact", "newsfeedTags.id": "exact", "userGroups.id": "exact", "company.id": "exact"})
* @ApiFilter(BooleanFilter::class, properties={"isPublished"})
* @ApiFilter(OrderFilter::class, properties={"id": "DESC", "updatedAt", "createdAt", "commentedAt", "totalLikes"})
* @ORM\Entity(repositoryClass=NewsfeedRepository::class)
* @EsElasticIndex("Newsfeed:EL")
*/
class Newsfeed implements ContainerMappedInterface, OwnerMappedInterface, UploadMappedInterface, TotalLikeMappedInterface, ElasticMappedInterface
{
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"Newsfeed:EL", "Newsfeed:Read"})
*/
private $id;
/**
* @ORM\Column(type="text", nullable=true)
* @Assert\Regex(
* pattern="/(\b)(on\S+)(\s*)=|javascript|(<\s*)(\/*)script/mi",
* match=false,
* message="validation.newsfeed:message.invalidContent"
* )
* @Groups({"Newsfeed:EL", "Newsfeed:Read", "Newsfeed:Write"})
*/
private $postText;
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
* @Groups({"Newsfeed:EL", "Newsfeed:Read"})
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity=Container::class)
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
* @Groups({"Newsfeed:Read", "Newsfeed:Write"})
*/
private $container;
/**
* @ORM\Column(type="boolean")
* @Groups({"Newsfeed:EL", "Newsfeed:Read", "Newsfeed:Write"})
*/
private $isPublished;
/**
* @ORM\OneToMany(targetEntity=NewsfeedComment::class, mappedBy="newsfeed", orphanRemoval=true)
*/
private $newsfeedComments;
/**
* @ORM\Column(type="array", nullable=true)
* @Groups({"Newsfeed:EL", "Newsfeed:Read", "Newsfeed:Write"})
* @EsUploadable()
*/
protected $mediaFileNames = [];
/**
* @ORM\OneToMany(targetEntity=CommonComment::class, mappedBy="newsfeed")
*/
private $commonComments;
/**
* @ORM\Column(type="string", length=32)
* @Groups({"Newsfeed:EL", "Newsfeed:Read", "Newsfeed:Write"})
* @ApiProperty(
* attributes={
* "openapi_context"={
* "type"="string",
* "enum"={self::POSTTYPE_TEXT, self::POSTTYPE_COMPANY, self::POSTTYPE_SESSION, self::POSTTYPE_ELTASK, self::POSTTYPE_ELEXAM}
* }
* }
* )
*
* @Assert\NotBlank(message="validation.newsfeed:postType.notBlank")
* @Assert\Choice({self::POSTTYPE_TEXT, self::POSTTYPE_COMPANY, self::POSTTYPE_SESSION, self::POSTTYPE_ELTASK, self::POSTTYPE_ELEXAM})
*/
private $postType = self::POSTTYPE_TEXT;
public const POSTTYPE_TEXT = "TEXT";
public const POSTTYPE_COMPANY = "COMPANY";
public const POSTTYPE_SESSION = CommonComment::SOURCE_SESSION;
public const POSTTYPE_ELTASK = CommonComment::SOURCE_ELTASK;
public const POSTTYPE_ELEXAM = CommonComment::SOURCE_ELEXAM;
public const POSTTYPE_DOCFILEBLOCK = CommonComment::SOURCE_DOCFILEBLOCK;
public const POSTTYPE_VIDEOGALLERY = CommonComment::SOURCE_VIDEOGALLERY;
public const POSTTYPE_INFOPAGE = CommonComment::SOURCE_INFOPAGE;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups({"Newsfeed:Read", "Newsfeed:Write"})
*/
private $typeId;
/**
* @ORM\Column(type="json", nullable=true)
* @Groups({"Newsfeed:Read"})
*/
private $initialComments = [];
/**
* @ORM\Column(type="json", nullable=true)
* @Groups({"Newsfeed:Read", "Newsfeed:Write"})
*/
private $postMetadata = [];
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups({"Newsfeed:EL", "Newsfeed:Read"})
*/
private $totalLikes;
/**
* @ORM\Column(type="array", nullable=true)
* @Groups({"Newsfeed:EL", "Newsfeed:Read", "Newsfeed:Write"})
*/
private $assignUsers = [];
/**
* @ORM\Column(type="string", length=32, nullable=true)
* @Groups({"Newsfeed:EL", "Newsfeed:Read", "Newsfeed:Write"})
*/
private $mediaFileType;
public const MEDIAFILETYPE_IMAGE = "IMAGE";
public const MEDIAFILETYPE_VIDEO = "VIDEO";
public const MEDIAFILETYPE_DOCUMENT = "DOCUMENT";
public const MEDIAFILETYPE_DICOM = "DICOM";
public const MEDIAFILETYPE_VOTE = "VOTE";
/**
* @ORM\ManyToOne(targetEntity=CommonCategory::class)
* @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
* @Groups({"Newsfeed:Read", "Newsfeed:Write"})
*/
private $newsfeedCategory;
/**
* @ORM\ManyToMany(targetEntity=NewsfeedTag::class, mappedBy="newsfeeds", cascade={"persist"})
* @Groups({"Newsfeed:Read", "Newsfeed:Write"})
*/
private $newsfeedTags;
/**
* @ORM\ManyToMany(targetEntity=UserGroup::class, inversedBy="newsfeeds")
* @Groups({"Newsfeed:Read", "Newsfeed:Write"})
*/
private $userGroups;
/**
* @ORM\Column(type="string", length=32, nullable=true)
* @Groups({"Newsfeed:EL", "Newsfeed:Read", "Newsfeed:Write"})
* @ApiProperty(
* attributes={
* "openapi_context"={
* "type"="string",
* "enum"={self::POSTSUBTYPE_TEXT, self::POSTSUBTYPE_CONFERENCE, self::POSTSUBTYPE_SESSION, self::POSTSUBTYPE_ELCOURSE, self::POSTSUBTYPE_DOCUMENT, self::POSTSUBTYPE_VIDEOGALLERY}
* }
* }
* )
*
* @Assert\Choice({self::POSTSUBTYPE_TEXT, self::POSTSUBTYPE_CONFERENCE, self::POSTSUBTYPE_SESSION, self::POSTSUBTYPE_ELCOURSE, self::POSTSUBTYPE_DOCUMENT, self::POSTSUBTYPE_VIDEOGALLERY})
*/
private $postSubType = self::POSTSUBTYPE_TEXT;
public const POSTSUBTYPE_TEXT = "TEXT";
public const POSTSUBTYPE_CONFERENCE = "CONFERENCE";
public const POSTSUBTYPE_SESSION = "SESSION";
public const POSTSUBTYPE_ELCOURSE = "COURSE";
public const POSTSUBTYPE_DOCUMENT = "DOCUMENT";
public const POSTSUBTYPE_VIDEOGALLERY = "VIDEOGALLERY";
public const POSTSUBTYPE_INFOPAGE = "INFOPAGE";
/**
* @ORM\Column(type="json", nullable=true)
* @Groups({"Newsfeed:Read-Dicom", "Newsfeed:Write"})
*/
private $dicomFileInfo = [];
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"Newsfeed:EL", "Newsfeed:Read", "Newsfeed:Write"})
*/
private $title;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Groups({"Newsfeed:EL", "Newsfeed:Read"})
*/
private $voteExpiry;
/**
* @ORM\Column(type="json", nullable=true)
* @Groups({"Newsfeed:EL", "Newsfeed:Read"})
*/
private $voteResult = [];
/**
* @ORM\OneToMany(targetEntity=NewsfeedVoteAnswer::class, mappedBy="newsfeed")
*/
private $newsfeedVoteAnswers;
/**
* @ORM\OneToMany(targetEntity=NewsfeedSaved::class, mappedBy="newsfeed")
*/
private $newsfeedSaved;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $commentedAt;
/**
* @ORM\ManyToOne(targetEntity=Company::class, inversedBy="newsfeeds")
* @Groups({"Newsfeed:Read", "Newsfeed:Write"})
*/
private $company;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Groups({"Newsfeed:Read"})
*/
private $isContainerFeedSent;
public function __construct()
{
$this->newsfeedComments = new ArrayCollection();
$this->commonComments = new ArrayCollection();
$this->newsfeedTags = new ArrayCollection();
$this->userGroups = new ArrayCollection();
$this->newsfeedVoteAnswers = new ArrayCollection();
$this->newsfeedSaved = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getPostText(): ?string
{
return $this->postText;
}
public function setPostText(string $postText): self
{
$this->postText = $postText;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getContainer(): ?Container
{
return $this->container;
}
public function setContainer(?Container $container): self
{
$this->container = $container;
return $this;
}
public function getIsPublished(): ?bool
{
return $this->isPublished;
}
public function setIsPublished(bool $isPublished): self
{
$this->isPublished = $isPublished;
return $this;
}
/**
* @return Collection|NewsfeedComment[]
*/
public function getNewsfeedComments()
{
return $this->newsfeedComments;
}
public function addNewsfeedComment(NewsfeedComment $newsfeedComment): self
{
if (!$this->newsfeedComments->contains($newsfeedComment)) {
$this->newsfeedComments[] = $newsfeedComment;
$newsfeedComment->setNewsfeed($this);
}
return $this;
}
public function removeNewsfeedComment(NewsfeedComment $newsfeedComment): self
{
if ($this->newsfeedComments->removeElement($newsfeedComment)) {
// set the owning side to null (unless already changed)
if ($newsfeedComment->getNewsfeed() === $this) {
$newsfeedComment->setNewsfeed(null);
}
}
return $this;
}
public function getMediaFileNames(): ?array
{
return $this->mediaFileNames;
}
public function setMediaFileNames(?array $mediaFileNames): self
{
$this->mediaFileNames = $mediaFileNames;
return $this;
}
/**
* @return Collection|CommonComment[]
*/
public function getCommonComments(): Collection
{
return $this->commonComments;
}
public function addCommonComment(CommonComment $commonComment): self
{
if (!$this->commonComments->contains($commonComment)) {
$this->commonComments[] = $commonComment;
$commonComment->setNewsfeed($this);
}
return $this;
}
public function removeCommonComment(CommonComment $commonComment): self
{
if ($this->commonComments->removeElement($commonComment)) {
// set the owning side to null (unless already changed)
if ($commonComment->getNewsfeed() === $this) {
$commonComment->setNewsfeed(null);
}
}
return $this;
}
public function getPostType(): ?string
{
return $this->postType;
}
public function setPostType(string $postType): self
{
$this->postType = $postType;
return $this;
}
public function getTypeId(): ?int
{
return $this->typeId;
}
public function setTypeId(?int $typeId): self
{
$this->typeId = $typeId;
return $this;
}
public function getInitialComments(): ?array
{
return $this->initialComments;
}
public function setInitialComments(?array $initialComments): self
{
$this->initialComments = $initialComments;
return $this;
}
public function getPostMetadata(): ?array
{
return $this->postMetadata;
}
public function setPostMetadata(?array $postMetadata): self
{
$this->postMetadata = $postMetadata;
return $this;
}
public function getIsPostEnable()
{
return true;
}
public function getTotalLikes(): ?int
{
return $this->totalLikes;
}
public function setTotalLikes(?int $totalLikes)
{
$this->totalLikes = $totalLikes;
return $this;
}
public function getAssignUsers(): ?array
{
return $this->assignUsers;
}
public function setAssignUsers(?array $assignUsers): self
{
$this->assignUsers = $assignUsers;
return $this;
}
public function getMediaFileType(): ?string
{
return $this->mediaFileType;
}
public function setMediaFileType(?string $mediaFileType): self
{
$this->mediaFileType = $mediaFileType;
return $this;
}
public function getNewsfeedCategory(): ?CommonCategory
{
return $this->newsfeedCategory;
}
public function setNewsfeedCategory(?CommonCategory $newsfeedCategory): self
{
$this->newsfeedCategory = $newsfeedCategory;
return $this;
}
/**
* @return Collection<int, NewsfeedTag>
*/
public function getNewsfeedTags(): Collection
{
return $this->newsfeedTags;
}
/**
* @Groups({"Newsfeed:EL"})
* @SerializedName("tags")
*/
public function getTagsArray()
{
$tags = [];
foreach ($this->newsfeedTags as $tag) {
$tags[] = $tag->getName();
}
return $tags;
}
public function addNewsfeedTag(NewsfeedTag $newsfeedTag): self
{
if (!$this->newsfeedTags->contains($newsfeedTag)) {
$this->newsfeedTags[] = $newsfeedTag;
$newsfeedTag->addNewsfeed($this);
}
return $this;
}
public function removeNewsfeedTag(NewsfeedTag $newsfeedTag): self
{
if ($this->newsfeedTags->removeElement($newsfeedTag)) {
$newsfeedTag->removeNewsfeed($this);
}
return $this;
}
/**
* @return Collection<int, UserGroup>
*/
public function getUserGroups(): Collection
{
return $this->userGroups;
}
/**
* @Groups({"Newsfeed:EL"})
* @SerializedName("groups")
*/
public function getUserGroupsArray()
{
$groups = [];
foreach ($this->userGroups as $group) {
if (!$group->getIsGenerated()) {
$groups[] = $group->getId();
}
}
return $groups;
}
public function addUserGroup(UserGroup $userGroup): self
{
if (!$this->userGroups->contains($userGroup)) {
$this->userGroups[] = $userGroup;
}
return $this;
}
public function removeUserGroup(UserGroup $userGroup): self
{
$this->userGroups->removeElement($userGroup);
return $this;
}
public function setUserGroups($userGroups): self
{
$this->userGroups = $userGroups;
return $this;
}
public function getPostSubType(): ?string
{
return $this->postSubType;
}
public function setPostSubType(?string $postSubType): self
{
$this->postSubType = $postSubType;
return $this;
}
public function getPostSubTypeEntity(?string $postSubType = null)
{
switch($this->postSubType ?? $postSubType) {
case self::POSTSUBTYPE_CONFERENCE:
return Conference::class;
case self::POSTSUBTYPE_SESSION:
return Session::class;
case self::POSTSUBTYPE_ELCOURSE:
return ElCourse::class;
case self::POSTSUBTYPE_DOCUMENT:
return DocFile::class;
case self::POSTSUBTYPE_VIDEOGALLERY:
return VideoGallery::class;
case self::POSTSUBTYPE_INFOPAGE:
return InfoPage::class;
}
return false;
}
public function getDicomFileInfo(): ?array
{
return $this->dicomFileInfo;
}
public function setDicomFileInfo(?array $dicomFileInfo): self
{
$this->dicomFileInfo = $dicomFileInfo;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getVoteExpiry(): ?\DateTimeInterface
{
return $this->voteExpiry;
}
public function setVoteExpiry(?\DateTimeInterface $voteExpiry): self
{
$this->voteExpiry = $voteExpiry;
return $this;
}
public function getVoteResult(): ?array
{
return $this->voteResult;
}
public function setVoteResult(?array $voteResult): self
{
$this->voteResult = $voteResult;
return $this;
}
/**
* @return Collection<int, NewsfeedVoteAnswer>
*/
public function getNewsfeedVoteAnswers(): Collection
{
return $this->newsfeedVoteAnswers;
}
public function addNewsfeedVoteAnswer(NewsfeedVoteAnswer $newsfeedVoteAnswer): self
{
if (!$this->newsfeedVoteAnswers->contains($newsfeedVoteAnswer)) {
$this->newsfeedVoteAnswers[] = $newsfeedVoteAnswer;
$newsfeedVoteAnswer->setNewsfeed($this);
}
return $this;
}
public function removeNewsfeedVoteAnswer(NewsfeedVoteAnswer $newsfeedVoteAnswer): self
{
if ($this->newsfeedVoteAnswers->removeElement($newsfeedVoteAnswer)) {
// set the owning side to null (unless already changed)
if ($newsfeedVoteAnswer->getNewsfeed() === $this) {
$newsfeedVoteAnswer->setNewsfeed(null);
}
}
return $this;
}
/**
* @return Collection<int, NewsfeedSaved>
*/
public function getNewsfeedSaved(): Collection
{
return $this->newsfeedSaved;
}
public function addNewsfeedSaved(NewsfeedSaved $newsfeedSaved): self
{
if (!$this->newsfeedSaved->contains($newsfeedSaved)) {
$this->newsfeedSaved[] = $newsfeedSaved;
$newsfeedSaved->setNewsfeed($this);
}
return $this;
}
public function removeNewsfeedSaved(NewsfeedSaved $newsfeedSaved): self
{
if ($this->newsfeedSaved->removeElement($newsfeedSaved)) {
// set the owning side to null (unless already changed)
if ($newsfeedSaved->getNewsfeed() === $this) {
$newsfeedSaved->setNewsfeed(null);
}
}
return $this;
}
public function getCommentedAt(): ?\DateTimeImmutable
{
return $this->commentedAt;
}
public function setCommentedAt(?\DateTimeImmutable $commentedAt): self
{
$this->commentedAt = $commentedAt;
return $this;
}
/**
* @Groups({"Newsfeed:EL"})
*/
public function getCntId()
{
return $this->container->getId();
}
/**
* @Groups({"Newsfeed:EL"})
*/
public function getCategory()
{
return $this->getNewsfeedCategory();
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getIsContainerFeedSent(): ?bool
{
return $this->isContainerFeedSent;
}
public function setIsContainerFeedSent(?bool $isContainerFeedSent): self
{
$this->isContainerFeedSent = $isContainerFeedSent;
return $this;
}
}