src/Entity/TvTag.php line 69

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  6. use ApiPlatform\Core\Serializer\Filter\GroupFilter;
  7. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  8. use App\Repository\TvTagRepository;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Gedmo\Mapping\Annotation as Gedmo;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. /**
  16.  * @ORM\Entity(repositoryClass=TvTagRepository::class)
  17.  * @ORM\HasLifecycleCallbacks
  18.  * @ApiResource(
  19.  *      normalizationContext={"groups"={"tv_tag:read"}},
  20.  *      denormalizationContext={"groups"={"tv_tag:write"}},
  21.  *      collectionOperations={
  22.  *          "get"={"security"="is_granted('ROLE_ADMIN')"}, 
  23.  *          "post"={"security"="is_granted('ROLE_ADMIN')"},
  24.  *          "get_vitality_balance_tv_tag_by_client"={
  25.  *              "method"="GET",
  26.  *              "controller"="App\Controller\Api\VitalityBalanceController::getDailyVitalityTvTagByClient",
  27.  *              "path"="/clients/{id}/vitality-balance/daily-tag",
  28.  *              "security"="is_granted('ROLE_CLIENT')",
  29.  *              "openapi_context"={
  30.  *                  "summary"="Récupère le tvTag du jour d'un Client (pour le bilan de vitalité)"
  31.  *              }
  32.  *           },
  33.  *          "get_tv_tags_by_survey"={
  34.  *             "description"="Get tv_tags by survey.",
  35.  *             "path"="/surveys/{id}/tv_tags",
  36.  *             "method"="GET",
  37.  *             "controller"="App\Controller\Api\TvTagController::getTvTagsBySurvey",
  38.  *             "security"="is_granted('ROLE_USER')"
  39.  *          }
  40.  *      },
  41.  *      itemOperations={
  42.  *          "get"={"security"="is_granted('ROLE_USER')"}, 
  43.  *          "delete"={"security"="is_granted('ROLE_ADMIN')"}, 
  44.  *          "patch"={"security"="is_granted('ROLE_ADMIN')"},
  45.  *          "patch_with_motivations_phrases"={
  46.  *                  "method"="PATCH",
  47.  *                  "security"="is_granted('ROLE_ADMIN')",
  48.  *                  "path"="/tv_tags/{id}/update-with-motivations-phrases",
  49.  *                  "controller"="App\Controller\Api\TvTagController::patchWithMotivationsPhrases",
  50.  *                  "input_formats"={"json"={"application/merge-patch+json"}}
  51.  *          }
  52.  *      }
  53.  * )
  54.  * @ApiFilter(SearchFilter::class, properties={"name": "partial",  "active": "exact", "type": "exact", "questions.survey"})
  55.  * @ApiFilter(GroupFilter::class, arguments={
  56.  *      "parameterName": "groups", 
  57.  *      "overrideDefaultGroups": true
  58.  * })
  59.  * @ApiFilter(PropertyFilter::class, 
  60.  *      arguments={
  61.  *          "parameterName"="fields", 
  62.  *          "overrideDefaultProperties"=true
  63.  *     }
  64.  * )
  65.  */
  66. class TvTag
  67. {
  68.     const HOMEPAGE_SURVEY_TYPE "homepage_survey";
  69.     const GLOBAL_TYPE "global";
  70.     const TYPES = [
  71.         self::HOMEPAGE_SURVEY_TYPE,
  72.         self::GLOBAL_TYPE
  73.     ];
  74.     /**
  75.      * @ORM\Id
  76.      * @ORM\GeneratedValue
  77.      * @ORM\Column(type="integer")
  78.      * @Groups({"tv_tag:read", "tv_tag:read:id", "question:read", "survey:read", "program:read", "channel:read", "survey_config_list", "survey_vitality_front", 
  79.      *      "tv_tag_list", "recommendation_survey_edit", "recommendation_survey_tag_edit"
  80.      * })
  81.      */
  82.     private $id;
  83.     /**
  84.      * @ORM\Column(type="string", length=255)
  85.      * @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", 
  86.      *      "survey_config_list", "survey_vitality_front", "tv_tag_list", "recommendation_survey_edit", "recommendation_survey_tag_edit"
  87.      * })
  88.      * @Assert\NotNull(message="tvTag.name.blank")
  89.      * @Assert\Length(max="255", min="3", minMessage="tvTag.name.min", maxMessage="tvTag.name.max")
  90.      */
  91.     private $name;
  92.     /**
  93.      * @ORM\Column(type="integer")
  94.      * @Groups({"tv_tag:read", "tv_tag:read:value", "tv_tag:write", "recommendation_survey_tag_edit"})
  95.      * @Assert\GreaterThan(0, message="tvTag.value.min")
  96.      */
  97.     private $value;
  98.     /**
  99.      * @ORM\Column(type="datetime")
  100.      * @Groups({"tv_tag:read", "tv_tag:read:updatedAt"})
  101.      * @Gedmo\Timestampable(on="update")
  102.      */
  103.     private $updatedAt;
  104.     /**
  105.      * @ORM\Column(type="datetime")
  106.      * @Groups({"tv_tag:read", "tv_tag:read:createdAt"})
  107.      * @Gedmo\Timestampable(on="create")
  108.      */
  109.     private $createdAt;
  110.     /**
  111.      * @ORM\Column(type="boolean", options={"default": 1})
  112.      * @Groups({"tv_tag:read", "tv_tag:read:active", "tv_tag:write", "survey:read", "program:read", "recommendation_survey_tag_edit"})
  113.      */
  114.     private $active true;
  115.     /**
  116.      * @ORM\Column(type="string", length=255, nullable=true)
  117.      * @Assert\Choice(choices=self::TYPES)
  118.      * @Groups({"tv_tag:read", "tv_tag:read:type", "tv_tag:write", "survey:read", "program:read", "recommendation_survey_tag_edit"})
  119.      */
  120.     private $type;
  121.     /**
  122.      * @ORM\OneToMany(targetEntity=Question::class, mappedBy="tvTag")
  123.      * @Groups({"tv_tag:read", "tv_tag:read:questions"})
  124.      */
  125.     private $questions;
  126.     /**
  127.      * @ORM\OneToMany(targetEntity=UserResponse::class, mappedBy="tvTag")
  128.      * @Groups({"tv_tag:read", "tv_tag:read:userResponses"})
  129.      */
  130.     private $userResponses;
  131.     /**
  132.      * @var ?string
  133.      * @Groups({"tv_tag:read", "tv_tag:read:motivationPhrase1"})
  134.      */
  135.     private $motivationPhrase1;
  136.     /**
  137.      * @var ?string
  138.      * @Groups({"tv_tag:read", "tv_tag:read:motivationPhrase2"})
  139.      */
  140.     private $motivationPhrase2;
  141.     
  142.     /**
  143.      * @var ?string
  144.      * @Groups({"tv_tag:read", "tv_tag:read:motivationPhrase3"})
  145.      */
  146.     private $motivationPhrase3;
  147.     /**
  148.      * @var ?string
  149.      * @Groups({"tv_tag:read", "tv_tag:read:motivationPhrase4"})
  150.      */
  151.     private $motivationPhrase4;
  152.     public function __construct()
  153.     {
  154.         $this->value 1;
  155.         $this->createdAt = new \DateTime("now");
  156.         $this->updatedAt = new \DateTime("now");
  157.         $this->active true;
  158.         $this->type self::GLOBAL_TYPE;
  159.         $this->questions = new ArrayCollection();
  160.         $this->userResponses = new ArrayCollection();
  161.     }
  162.     public function getId(): ?int
  163.     {
  164.         return $this->id;
  165.     }
  166.     public function setId(?int $id): self
  167.     {
  168.         $this->id $id;
  169.         return $this;
  170.     }
  171.     public function getName(): ?string
  172.     {
  173.         return $this->name;
  174.     }
  175.     public function setName(string $name): self
  176.     {
  177.         $this->name $name;
  178.         return $this;
  179.     }
  180.     public function getValue(): ?int
  181.     {
  182.         return $this->value;
  183.     }
  184.     public function setValue(int $value): self
  185.     {
  186.         $this->value $value;
  187.         return $this;
  188.     }
  189.     public function getUpdatedAt(): ?\DateTimeInterface
  190.     {
  191.         return $this->updatedAt;
  192.     }
  193.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  194.     {
  195.         $this->updatedAt $updatedAt;
  196.         return $this;
  197.     }
  198.     public function getCreatedAt(): ?\DateTimeInterface
  199.     {
  200.         return $this->createdAt;
  201.     }
  202.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  203.     {
  204.         $this->createdAt $createdAt;
  205.         return $this;
  206.     }
  207.     /**
  208.      * The tvTag is it's name preceded by an hashtag
  209.      * @return string
  210.      */
  211.     public function __toString(): string
  212.     {
  213.         return "#{$this->name}";
  214.     }
  215.     public function getActive(): ?bool
  216.     {
  217.         return $this->active;
  218.     }
  219.     public function setActive(bool $active): self
  220.     {
  221.         $this->active $active;
  222.         return $this;
  223.     }
  224.     public function getType(): ?string
  225.     {
  226.         return $this->type;
  227.     }
  228.     public function setType(?string $type): self
  229.     {
  230.         $this->type $type;
  231.         return $this;
  232.     }
  233.     /**
  234.      * @return Collection|Question[]
  235.      */
  236.     public function getQuestions(): Collection
  237.     {
  238.         return $this->questions;
  239.     }
  240.     public function addQuestion(Question $question): self
  241.     {
  242.         if (!$this->questions->contains($question)) {
  243.             $this->questions[] = $question;
  244.             $question->setTvTag($this);
  245.         }
  246.         return $this;
  247.     }
  248.     public function removeQuestion(Question $question): self
  249.     {
  250.         if ($this->questions->removeElement($question)) {
  251.             // set the owning side to null (unless already changed)
  252.             if ($question->getTvTag() === $this) {
  253.                 $question->setTvTag(null);
  254.             }
  255.         }
  256.         return $this;
  257.     }
  258.     /**
  259.      * @return Collection|UserResponse[]
  260.      */
  261.     public function getUserResponses(): Collection
  262.     {
  263.         return $this->userResponses;
  264.     }
  265.     public function addUserResponse(UserResponse $userResponse): self
  266.     {
  267.         if (!$this->userResponses->contains($userResponse)) {
  268.             $this->userResponses[] = $userResponse;
  269.             $userResponse->setTvTag($this);
  270.         }
  271.         return $this;
  272.     }
  273.     public function removeUserResponse(UserResponse $userResponse): self
  274.     {
  275.         if ($this->userResponses->removeElement($userResponse)) {
  276.             // set the owning side to null (unless already changed)
  277.             if ($userResponse->getTvTag() === $this) {
  278.                 $userResponse->setTvTag(null);
  279.             }
  280.         }
  281.         return $this;
  282.     }
  283.     /**
  284.      * @Groups({"recommendation_survey_tag_edit"})
  285.      */
  286.     public function getMotivationPhrase1(): ?string
  287.     {
  288.         $this->motivationPhrase1 null;
  289.         if(!empty($this->questions->toArray())) {
  290.             foreach ($this->questions->toArray() as $question) {
  291.                 if($question instanceof Question && $question->getSurvey() instanceof Survey && $question->getSurvey()->getType() === Survey::TYPE_HOMEPAGE){
  292.                     if(!empty($question->getSurvey()->getConfigs()->toArray())) {
  293.                         foreach ($question->getSurvey()->getConfigs()->toArray() as $config) {
  294.                             if($config instanceof SurveyConfig && $config->getTvTag() instanceof TvTag && $config->getTvTag()->getId() === $this->id && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES 
  295.                                 && $config->getSubType() === SurveyConfig::INTERVALLE_1){
  296.                                 $this->motivationPhrase1 $config->getValue(); 
  297.                                 break;
  298.                             }
  299.                         }
  300.                     }
  301.                 }
  302.             }
  303.         }
  304.         return $this->motivationPhrase1;
  305.     }
  306.     /**
  307.      * @Groups({"recommendation_survey_tag_edit"})
  308.      */
  309.     public function getMotivationPhrase2(): ?string
  310.     {
  311.         $this->motivationPhrase2 null;
  312.         if(!empty($this->questions->toArray())) {
  313.             foreach ($this->questions->toArray() as $question) {
  314.                 if($question instanceof Question && $question->getSurvey() instanceof Survey && $question->getSurvey()->getType() === Survey::TYPE_HOMEPAGE){
  315.                     if(!empty($question->getSurvey()->getConfigs()->toArray())) {
  316.                         foreach ($question->getSurvey()->getConfigs()->toArray() as $config) {
  317.                             if($config instanceof SurveyConfig && $config->getTvTag() instanceof TvTag && $config->getTvTag()->getId() === $this->id && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES 
  318.                                 && $config->getSubType() === SurveyConfig::INTERVALLE_2){
  319.                                 $this->motivationPhrase2 $config->getValue(); 
  320.                                 break;
  321.                             }
  322.                         }
  323.                     }
  324.                 }
  325.             }
  326.         }
  327.         return $this->motivationPhrase2;
  328.     }
  329.     /**
  330.      * @Groups({"recommendation_survey_tag_edit"})
  331.      */
  332.     public function getMotivationPhrase3(): ?string
  333.     {
  334.         $this->motivationPhrase3 null;
  335.         if(!empty($this->questions->toArray())) {
  336.             foreach ($this->questions->toArray() as $question) {
  337.                 if($question instanceof Question && $question->getSurvey() instanceof Survey && $question->getSurvey()->getType() === Survey::TYPE_HOMEPAGE){
  338.                     if(!empty($question->getSurvey()->getConfigs()->toArray())) {
  339.                         foreach ($question->getSurvey()->getConfigs()->toArray() as $config) {
  340.                             if($config instanceof SurveyConfig && $config->getTvTag() instanceof TvTag && $config->getTvTag()->getId() === $this->id && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES 
  341.                                 && $config->getSubType() === SurveyConfig::INTERVALLE_3){
  342.                                 $this->motivationPhrase3 $config->getValue(); 
  343.                                 break;
  344.                             }
  345.                         }
  346.                     }
  347.                 }
  348.             }
  349.         }
  350.         return $this->motivationPhrase3;
  351.     }
  352.     /**
  353.      * @Groups({"recommendation_survey_tag_edit"})
  354.      */
  355.     public function getMotivationPhrase4(): ?string
  356.     {
  357.         $this->motivationPhrase4 null;
  358.         if(!empty($this->questions->toArray())) {
  359.             foreach ($this->questions->toArray() as $question) {
  360.                 if($question instanceof Question && $question->getSurvey() instanceof Survey && $question->getSurvey()->getType() === Survey::TYPE_HOMEPAGE){
  361.                     if(!empty($question->getSurvey()->getConfigs()->toArray())) {
  362.                         foreach ($question->getSurvey()->getConfigs()->toArray() as $config) {
  363.                             if($config instanceof SurveyConfig && $config->getTvTag() instanceof TvTag && $config->getTvTag()->getId() === $this->id && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES 
  364.                                 && $config->getSubType() === SurveyConfig::INTERVALLE_4){
  365.                                 $this->motivationPhrase4 $config->getValue(); 
  366.                                 break;
  367.                             }
  368.                         }
  369.                     }
  370.                 }
  371.             }
  372.         }
  373.         return $this->motivationPhrase4;
  374.     }
  375. }