<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\GroupFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Repository\TvTagRepository;
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=TvTagRepository::class)
* @ORM\HasLifecycleCallbacks
* @ApiResource(
* normalizationContext={"groups"={"tv_tag:read"}},
* denormalizationContext={"groups"={"tv_tag:write"}},
* collectionOperations={
* "get"={"security"="is_granted('ROLE_ADMIN')"},
* "post"={"security"="is_granted('ROLE_ADMIN')"},
* "get_vitality_balance_tv_tag_by_client"={
* "method"="GET",
* "controller"="App\Controller\Api\VitalityBalanceController::getDailyVitalityTvTagByClient",
* "path"="/clients/{id}/vitality-balance/daily-tag",
* "security"="is_granted('ROLE_CLIENT')",
* "openapi_context"={
* "summary"="Récupère le tvTag du jour d'un Client (pour le bilan de vitalité)"
* }
* },
* "get_tv_tags_by_survey"={
* "description"="Get tv_tags by survey.",
* "path"="/surveys/{id}/tv_tags",
* "method"="GET",
* "controller"="App\Controller\Api\TvTagController::getTvTagsBySurvey",
* "security"="is_granted('ROLE_USER')"
* }
* },
* itemOperations={
* "get"={"security"="is_granted('ROLE_USER')"},
* "delete"={"security"="is_granted('ROLE_ADMIN')"},
* "patch"={"security"="is_granted('ROLE_ADMIN')"},
* "patch_with_motivations_phrases"={
* "method"="PATCH",
* "security"="is_granted('ROLE_ADMIN')",
* "path"="/tv_tags/{id}/update-with-motivations-phrases",
* "controller"="App\Controller\Api\TvTagController::patchWithMotivationsPhrases",
* "input_formats"={"json"={"application/merge-patch+json"}}
* }
* }
* )
* @ApiFilter(SearchFilter::class, properties={"name": "partial", "active": "exact", "type": "exact", "questions.survey"})
* @ApiFilter(GroupFilter::class, arguments={
* "parameterName": "groups",
* "overrideDefaultGroups": true
* })
* @ApiFilter(PropertyFilter::class,
* arguments={
* "parameterName"="fields",
* "overrideDefaultProperties"=true
* }
* )
*/
class TvTag
{
const HOMEPAGE_SURVEY_TYPE = "homepage_survey";
const GLOBAL_TYPE = "global";
const TYPES = [
self::HOMEPAGE_SURVEY_TYPE,
self::GLOBAL_TYPE
];
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"tv_tag:read", "tv_tag:read:id", "question:read", "survey:read", "program:read", "channel:read", "survey_config_list", "survey_vitality_front",
* "tv_tag_list", "recommendation_survey_edit", "recommendation_survey_tag_edit"
* })
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"tv_tag:read", "tv_tag:read:name", "tv_tag:write", "video:read", "survey:read", "question:read", "program:read", "channel:read", "video_form_complete", "program_edit",
* "survey_config_list", "survey_vitality_front", "tv_tag_list", "recommendation_survey_edit", "recommendation_survey_tag_edit"
* })
* @Assert\NotNull(message="tvTag.name.blank")
* @Assert\Length(max="255", min="3", minMessage="tvTag.name.min", maxMessage="tvTag.name.max")
*/
private $name;
/**
* @ORM\Column(type="integer")
* @Groups({"tv_tag:read", "tv_tag:read:value", "tv_tag:write", "recommendation_survey_tag_edit"})
* @Assert\GreaterThan(0, message="tvTag.value.min")
*/
private $value;
/**
* @ORM\Column(type="datetime")
* @Groups({"tv_tag:read", "tv_tag:read:updatedAt"})
* @Gedmo\Timestampable(on="update")
*/
private $updatedAt;
/**
* @ORM\Column(type="datetime")
* @Groups({"tv_tag:read", "tv_tag:read:createdAt"})
* @Gedmo\Timestampable(on="create")
*/
private $createdAt;
/**
* @ORM\Column(type="boolean", options={"default": 1})
* @Groups({"tv_tag:read", "tv_tag:read:active", "tv_tag:write", "survey:read", "program:read", "recommendation_survey_tag_edit"})
*/
private $active = true;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Choice(choices=self::TYPES)
* @Groups({"tv_tag:read", "tv_tag:read:type", "tv_tag:write", "survey:read", "program:read", "recommendation_survey_tag_edit"})
*/
private $type;
/**
* @ORM\OneToMany(targetEntity=Question::class, mappedBy="tvTag")
* @Groups({"tv_tag:read", "tv_tag:read:questions"})
*/
private $questions;
/**
* @ORM\OneToMany(targetEntity=UserResponse::class, mappedBy="tvTag")
* @Groups({"tv_tag:read", "tv_tag:read:userResponses"})
*/
private $userResponses;
/**
* @var ?string
* @Groups({"tv_tag:read", "tv_tag:read:motivationPhrase1"})
*/
private $motivationPhrase1;
/**
* @var ?string
* @Groups({"tv_tag:read", "tv_tag:read:motivationPhrase2"})
*/
private $motivationPhrase2;
/**
* @var ?string
* @Groups({"tv_tag:read", "tv_tag:read:motivationPhrase3"})
*/
private $motivationPhrase3;
/**
* @var ?string
* @Groups({"tv_tag:read", "tv_tag:read:motivationPhrase4"})
*/
private $motivationPhrase4;
public function __construct()
{
$this->value = 1;
$this->createdAt = new \DateTime("now");
$this->updatedAt = new \DateTime("now");
$this->active = true;
$this->type = self::GLOBAL_TYPE;
$this->questions = new ArrayCollection();
$this->userResponses = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function setId(?int $id): self
{
$this->id = $id;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getValue(): ?int
{
return $this->value;
}
public function setValue(int $value): self
{
$this->value = $value;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
/**
* The tvTag is it's name preceded by an hashtag
* @return string
*/
public function __toString(): string
{
return "#{$this->name}";
}
public function getActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(?string $type): self
{
$this->type = $type;
return $this;
}
/**
* @return Collection|Question[]
*/
public function getQuestions(): Collection
{
return $this->questions;
}
public function addQuestion(Question $question): self
{
if (!$this->questions->contains($question)) {
$this->questions[] = $question;
$question->setTvTag($this);
}
return $this;
}
public function removeQuestion(Question $question): self
{
if ($this->questions->removeElement($question)) {
// set the owning side to null (unless already changed)
if ($question->getTvTag() === $this) {
$question->setTvTag(null);
}
}
return $this;
}
/**
* @return Collection|UserResponse[]
*/
public function getUserResponses(): Collection
{
return $this->userResponses;
}
public function addUserResponse(UserResponse $userResponse): self
{
if (!$this->userResponses->contains($userResponse)) {
$this->userResponses[] = $userResponse;
$userResponse->setTvTag($this);
}
return $this;
}
public function removeUserResponse(UserResponse $userResponse): self
{
if ($this->userResponses->removeElement($userResponse)) {
// set the owning side to null (unless already changed)
if ($userResponse->getTvTag() === $this) {
$userResponse->setTvTag(null);
}
}
return $this;
}
/**
* @Groups({"recommendation_survey_tag_edit"})
*/
public function getMotivationPhrase1(): ?string
{
$this->motivationPhrase1 = null;
if(!empty($this->questions->toArray())) {
foreach ($this->questions->toArray() as $question) {
if($question instanceof Question && $question->getSurvey() instanceof Survey && $question->getSurvey()->getType() === Survey::TYPE_HOMEPAGE){
if(!empty($question->getSurvey()->getConfigs()->toArray())) {
foreach ($question->getSurvey()->getConfigs()->toArray() as $config) {
if($config instanceof SurveyConfig && $config->getTvTag() instanceof TvTag && $config->getTvTag()->getId() === $this->id && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES
&& $config->getSubType() === SurveyConfig::INTERVALLE_1){
$this->motivationPhrase1 = $config->getValue();
break;
}
}
}
}
}
}
return $this->motivationPhrase1;
}
/**
* @Groups({"recommendation_survey_tag_edit"})
*/
public function getMotivationPhrase2(): ?string
{
$this->motivationPhrase2 = null;
if(!empty($this->questions->toArray())) {
foreach ($this->questions->toArray() as $question) {
if($question instanceof Question && $question->getSurvey() instanceof Survey && $question->getSurvey()->getType() === Survey::TYPE_HOMEPAGE){
if(!empty($question->getSurvey()->getConfigs()->toArray())) {
foreach ($question->getSurvey()->getConfigs()->toArray() as $config) {
if($config instanceof SurveyConfig && $config->getTvTag() instanceof TvTag && $config->getTvTag()->getId() === $this->id && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES
&& $config->getSubType() === SurveyConfig::INTERVALLE_2){
$this->motivationPhrase2 = $config->getValue();
break;
}
}
}
}
}
}
return $this->motivationPhrase2;
}
/**
* @Groups({"recommendation_survey_tag_edit"})
*/
public function getMotivationPhrase3(): ?string
{
$this->motivationPhrase3 = null;
if(!empty($this->questions->toArray())) {
foreach ($this->questions->toArray() as $question) {
if($question instanceof Question && $question->getSurvey() instanceof Survey && $question->getSurvey()->getType() === Survey::TYPE_HOMEPAGE){
if(!empty($question->getSurvey()->getConfigs()->toArray())) {
foreach ($question->getSurvey()->getConfigs()->toArray() as $config) {
if($config instanceof SurveyConfig && $config->getTvTag() instanceof TvTag && $config->getTvTag()->getId() === $this->id && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES
&& $config->getSubType() === SurveyConfig::INTERVALLE_3){
$this->motivationPhrase3 = $config->getValue();
break;
}
}
}
}
}
}
return $this->motivationPhrase3;
}
/**
* @Groups({"recommendation_survey_tag_edit"})
*/
public function getMotivationPhrase4(): ?string
{
$this->motivationPhrase4 = null;
if(!empty($this->questions->toArray())) {
foreach ($this->questions->toArray() as $question) {
if($question instanceof Question && $question->getSurvey() instanceof Survey && $question->getSurvey()->getType() === Survey::TYPE_HOMEPAGE){
if(!empty($question->getSurvey()->getConfigs()->toArray())) {
foreach ($question->getSurvey()->getConfigs()->toArray() as $config) {
if($config instanceof SurveyConfig && $config->getTvTag() instanceof TvTag && $config->getTvTag()->getId() === $this->id && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES
&& $config->getSubType() === SurveyConfig::INTERVALLE_4){
$this->motivationPhrase4 = $config->getValue();
break;
}
}
}
}
}
}
return $this->motivationPhrase4;
}
}