src/Entity/SurveyCompany.php line 49

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\OrderFilter;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  7. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  8. use App\Controller\SurveyCompanyController;
  9. use DateTime;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\Common\Collections\Criteria;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Gedmo\Timestampable\Traits\TimestampableEntity;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. /**
  18.  * @ORM\Entity(repositoryClass="App\Repository\SurveyCompanyRepository")
  19.  * @ORM\HasLifecycleCallbacks()
  20.  */
  21. #[ApiResource(
  22.     attributes: ['security' => "is_granted('ROLE_USER')"],
  23.     normalizationContext: ['groups' => ['surveyComapany:item''surveyComapany:list']],
  24.     denormalizationContext: ['groups' => ['surveyComapany:write']],
  25.     collectionOperations: [
  26.         'get' => ['normalization_context' => ['groups' => ['surveyComapany:list']]],
  27.         'post' => [
  28.             'security' => "is_granted('ROLE_ADMIN')",
  29.         ],
  30.     ],
  31.     itemOperations: [
  32.         'get' => ['normalization_context' => ['groups' => ['surveyComapany:item']]],
  33.         'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
  34.         'question_reset' => [
  35.             'method' => 'GET',
  36.             'path' => '/survey_companies/{id}/reset',
  37.             'controller' => SurveyCompanyController::class,
  38.             'normalization_context' => ['groups' => ['surveyComapany:item']],
  39.         ],
  40.     ],
  41.     order: ['createdAt' => 'DESC']
  42. )]
  43. #[ApiFilter(SearchFilter::class, properties: ['id''title' => 'partial''company' => 'exact''deploymentType' => 'exact'])]
  44. #[ApiFilter(PropertyFilter::class)]
  45. #[ApiFilter(OrderFilter::class, properties: ['id''title''company.name''deploymentType.name''startDate''endDate''createdAt''isActive'], arguments: ['orderParameterName' => 'order'])]
  46. class SurveyCompany
  47. {
  48.     use TimestampableEntity;
  49.     /**
  50.      * @ORM\Id()
  51.      * @ORM\GeneratedValue()
  52.      * @ORM\Column(type="integer")
  53.      * @Groups({"surveyComapany:item","surveyComapany:list"})
  54.      */
  55.     public int $id;
  56.     /**
  57.      * @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="surveyCompanies")
  58.      * @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
  59.      * @Groups({"surveyComapany:item","surveyComapany:list", "surveyComapany:write"})
  60.      */
  61.     public $company;
  62.     /**
  63.      * @ORM\Column(type="string", length=128)
  64.      * @Assert\Length(max="128")
  65.      * @Groups({"surveyComapany:item","surveyComapany:list" ,"surveyComapany:write"})
  66.      * @Assert\NotNull()
  67.      */
  68.     public $title;
  69.     /**
  70.      * @ORM\ManyToOne(targetEntity="App\Entity\DeploymentType", inversedBy="surveyCompanies")
  71.      * @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
  72.      * @Groups({"surveyComapany:item","surveyComapany:list", "surveyComapany:write"})
  73.      */
  74.     public $deploymentType;
  75.     /**
  76.      * @var DateTime
  77.      *
  78.      * @ORM\Column(type="datetime", nullable=true)
  79.      * @Groups({"surveyComapany:item", "surveyComapany:write"})
  80.      */
  81.     private $startDate;
  82.     /**
  83.      * @var DateTime
  84.      *
  85.      * @ORM\Column(type="datetime", nullable=true)
  86.      * @Groups({"surveyComapany:item","surveyComapany:list", "surveyComapany:write"})
  87.      */
  88.     private $endDate;
  89.     /**
  90.      * @ORM\Column(type="boolean")
  91.      * @Assert\Type(type="boolean")
  92.      * @Groups({"surveyComapany:item","surveyComapany:list", "surveyComapany:write"})
  93.      */
  94.     private $isActive;
  95.     /**
  96.      * @ORM\Column(type="integer", nullable=true)
  97.      * @Groups({"surveyComapany:item", "surveyComapany:write"})
  98.      */
  99.     private $sortOrder;
  100.     /**
  101.      * @var ArrayCollection|PinpointCampaign[]
  102.      *
  103.      * @ORM\OneToMany(targetEntity="App\Entity\PinpointCampaign", mappedBy="surveyCompany", cascade={"persist"})
  104.      * @ORM\JoinColumn(name="survey_form_question_id", referencedColumnName="id")
  105.      */
  106.     public $pinpointCampaigns;
  107.     /**
  108.      * @var ArrayCollection|SurveyForm[]
  109.      *
  110.      * @ORM\ManyToMany(targetEntity="App\Entity\SurveyForm", mappedBy="surveyCompany", cascade={"persist","remove"}, fetch="EXTRA_LAZY")
  111.      * @ORM\JoinTable(name="survey_form_survey_company",
  112.      *   joinColumns={@ORM\JoinColumn(name="survey_company_id", referencedColumnName="id")},
  113.      *   inverseJoinColumns={@ORM\JoinColumn(name="survey_form_id", referencedColumnName="id")}
  114.      * )
  115.      * @ORM\OrderBy({"id" = "ASC"})
  116.      * @Groups({"surveyComapany:item", "surveyComapany:write"})
  117.      */
  118.     public $surveyForms;
  119.     /** @var ArrayCollection|SurveyFormSurveyCompany[]
  120.      * @ORM\OneToMany(targetEntity=SurveyFormSurveyCompany::class, mappedBy="surveyCompany", orphanRemoval=true)
  121.      */
  122.     public $forms;
  123.     /**
  124.      * @var ArrayCollection|SurveyFormStepSurveyFormQuestion[]
  125.      *
  126.      * @ORM\OneToMany(targetEntity=SurveyFormStepSurveyFormQuestion::class, mappedBy="surveyCompany", orphanRemoval=true)
  127.      */
  128.     public $surveyQuestionForms;
  129.     /**
  130.      * @var ArrayCollection|SurveyCompanySurveyFormQuestion[]
  131.      *
  132.      * @ORM\OneToMany(targetEntity=SurveyCompanySurveyFormQuestion::class, mappedBy="surveyCompany", orphanRemoval=true)
  133.      * @ORM\OrderBy({"level"="ASC","rank" = "ASC"})
  134.      * @Groups({"surveyComapany:item", "surveyComapany:write"})
  135.      */
  136.     private $surveyCompanyQuestions;
  137.     /**
  138.      * @var ArrayCollection|SurveyCompanyToken[]
  139.      * @ORM\OneToMany(targetEntity=SurveyCompanyToken::class, mappedBy="surveyCompany", orphanRemoval=true,cascade={"persist","remove"}, fetch="EXTRA_LAZY")
  140.      * @Groups({"surveyComapany:item", "surveyComapany:write"})
  141.      */
  142.     private $surveyCompanyTokens;
  143.     public function __construct()
  144.     {
  145.         $this->isActive false;
  146.         $this->createdAt = new \DateTime();
  147.         $this->updatedAt = new \DateTime();
  148.         $this->surveyForms = new ArrayCollection();
  149.         $this->pinpointCampaigns = new ArrayCollection();
  150.         $this->surveyQuestionForms = new ArrayCollection();
  151.         $this->surveyCompanyQuestions = new ArrayCollection();
  152.         $this->forms = new ArrayCollection();
  153.         $this->surveyCompanyTokens = new ArrayCollection();
  154.     }
  155.     /**
  156.      * @return mixed
  157.      */
  158.     public function __toString()
  159.     {
  160.         return $this->title ?? '';
  161.     }
  162.     public function getId(): ?int
  163.     {
  164.         return $this->id;
  165.     }
  166.     public function getCompany(): ?Company
  167.     {
  168.         return $this->company;
  169.     }
  170.     public function setCompany(?Company $company): self
  171.     {
  172.         $this->company $company;
  173.         return $this;
  174.     }
  175.     public function getTitle(): ?string
  176.     {
  177.         return $this->title;
  178.     }
  179.     public function setTitle(string $title): self
  180.     {
  181.         $this->title $title;
  182.         return $this;
  183.     }
  184.     public function getStartDate(): ?DateTime
  185.     {
  186.         return $this->startDate;
  187.     }
  188.     public function setStartDate(?DateTime $startDate): self
  189.     {
  190.         $this->startDate $startDate;
  191.         return $this;
  192.     }
  193.     public function getEndDate(): ?DateTime
  194.     {
  195.         return $this->endDate;
  196.     }
  197.     public function setEndDate(?DateTime $endDate): self
  198.     {
  199.         $this->endDate $endDate;
  200.         return $this;
  201.     }
  202.     public function getIsActive(): bool
  203.     {
  204.         return $this->isActive;
  205.     }
  206.     public function setIsActive(bool $isActive): self
  207.     {
  208.         $this->isActive $isActive;
  209.         return $this;
  210.     }
  211.     public function getSortOrder(): ?int
  212.     {
  213.         return $this->sortOrder;
  214.     }
  215.     public function setSortOrder(?int $sortOrder): self
  216.     {
  217.         $this->sortOrder $sortOrder;
  218.         return $this;
  219.     }
  220.     public function getDeploymentType(): ?DeploymentType
  221.     {
  222.         return $this->deploymentType;
  223.     }
  224.     public function setDeploymentType(?DeploymentType $deploymentType): self
  225.     {
  226.         $this->deploymentType $deploymentType;
  227.         return $this;
  228.     }
  229.     /**
  230.      * @return Collection|PinpointCampaign[]
  231.      */
  232.     public function getPinpointCampaigns(): Collection
  233.     {
  234.         return $this->pinpointCampaigns;
  235.     }
  236.     public function addPinpointCampaign(PinpointCampaign $pinpointCampaign): self
  237.     {
  238.         if (!$this->pinpointCampaigns->contains($pinpointCampaign)) {
  239.             $this->pinpointCampaigns[] = $pinpointCampaign;
  240.             $pinpointCampaign->setSurveyCompany($this);
  241.         }
  242.         return $this;
  243.     }
  244.     public function removePinpointCampaign(PinpointCampaign $pinpointCampaign): self
  245.     {
  246.         if ($this->pinpointCampaigns->contains($pinpointCampaign)) {
  247.             $this->pinpointCampaigns->removeElement($pinpointCampaign);
  248.             // set the owning side to null (unless already changed)
  249.             if ($pinpointCampaign->getSurveyCompany() === $this) {
  250.                 $pinpointCampaign->setSurveyCompany(null);
  251.             }
  252.         }
  253.         return $this;
  254.     }
  255.     /**
  256.      * @return Collection|SurveyForm[]
  257.      */
  258.     public function getSurveyForms(): Collection
  259.     {
  260.         return $this->surveyForms;
  261.     }
  262.     public function addSurveyForm(SurveyForm $surveyForm): self
  263.     {
  264.         if (!$this->surveyForms->contains($surveyForm)) {
  265.             $this->surveyForms[] = $surveyForm;
  266.             $surveyForm->addSurveyCompany($this);
  267.         }
  268.         return $this;
  269.     }
  270.     public function removeSurveyForm(SurveyForm $surveyForm): self
  271.     {
  272.         if ($this->surveyForms->contains($surveyForm)) {
  273.             $this->surveyForms->removeElement($surveyForm);
  274.             $surveyForm->removeSurveyCompany($this);
  275.         }
  276.         return $this;
  277.     }
  278.     /**
  279.      * @return Collection|SurveyFormSurveyCompany[]
  280.      */
  281.     public function getForms(): Collection
  282.     {
  283.         return $this->forms;
  284.     }
  285.     public function addForm(SurveyFormSurveyCompany $form): self
  286.     {
  287.         if (!$this->forms->contains($form)) {
  288.             $this->forms[] = $form;
  289.             $form->setSurveyCompany($this);
  290.         }
  291.         return $this;
  292.     }
  293.     public function removeForm(SurveyFormSurveyCompany $form): self
  294.     {
  295.         if ($this->forms->removeElement($form)) {
  296.             // set the owning side to null (unless already changed)
  297.             if ($form->getSurveyCompany() === $this) {
  298.                 $form->setSurveyCompany(null);
  299.             }
  300.         }
  301.         return $this;
  302.     }
  303.     /**
  304.      * @return Collection|SurveyCompanySurveyFormQuestion[]
  305.      */
  306.     public function getSurveyCompanyQuestions(): Collection
  307.     {
  308.         $criteria Criteria::create()->orderBy(['parentSurveyFormQuestion' => Criteria::ASC'rank' => Criteria::ASC]);
  309.         $this->surveyCompanyQuestions->matching($criteria);
  310.         $questions = new ArrayCollection();
  311.         $i 1;
  312.         foreach ($this->surveyCompanyQuestions as $question) {
  313.             if (== $question->getLevel() && null == $question->getParentSurveyFormQuestion()) {
  314.                 if (== $question->getRank()) {
  315.                     $question->setRank(($i 1) * 3);
  316.                 }
  317.                 $questions[] = $question;
  318.                 $i1 0;
  319.                 foreach ($this->surveyCompanyQuestions as $question1st) {
  320.                     if (== $question1st->getLevel() && $question1st->getParentSurveyFormQuestion() && $question1st->getParentSurveyFormQuestion()->getId() == $question->getSurveyFormQuestion()->getId()) {
  321.                         if (== $question1st->getRank()) {
  322.                             $question1st->setRank($i1 3);
  323.                         }
  324.                         $questions[] = $question1st;
  325.                         $i2 0;
  326.                         foreach ($this->surveyCompanyQuestions as $question2nd) {
  327.                             if (== $question2nd->getLevel() && $question2nd->getParentSurveyFormQuestion() && $question2nd->getParentSurveyFormQuestion()->getId() == $question1st->getSurveyFormQuestion()->getId()) {
  328.                                 if (== $question2nd->getRank()) {
  329.                                     $question2nd->setRank($i2 3);
  330.                                 }
  331.                                 $questions[] = $question2nd;
  332.                                 $i3 0;
  333.                                 foreach ($this->surveyCompanyQuestions as $question3rd) {
  334.                                     if (== $question3rd->getLevel() && $question3rd->getParentSurveyFormQuestion() && $question3rd->getParentSurveyFormQuestion()->getId() == $question2nd->getSurveyFormQuestion()->getId()) {
  335.                                         if (== $question3rd->getRank()) {
  336.                                             $question3rd->setRank($i3 3);
  337.                                         }
  338.                                         $questions[] = $question3rd;
  339.                                         $i4 0;
  340.                                         foreach ($this->surveyCompanyQuestions as $question4rth) {
  341.                                             if (== $question4rth->getLevel() && $question4rth->getParentSurveyFormQuestion() && $question4rth->getParentSurveyFormQuestion()->getId() == $question3rd->getSurveyFormQuestion()->getId()) {
  342.                                                 if (== $question4rth->getRank()) {
  343.                                                     $question4rth->setRank($i4 3);
  344.                                                 }
  345.                                                 $questions[] = $question4rth;
  346.                                                 ++$i4;
  347.                                             }
  348.                                         }
  349.                                         ++$i3;
  350.                                     }
  351.                                 }
  352.                                 ++$i2;
  353.                             }
  354.                         }
  355.                         ++$i1;
  356.                     }
  357.                 }
  358.                 ++$i;
  359.             }
  360.         }
  361.         return $questions;
  362.     }
  363.     public function addSurveyCompanyQuestions(SurveyFormQuestion $surveyFormQuestion): self
  364.     {
  365.         if (!$this->surveyCompanyQuestions->contains($surveyFormQuestion)) {
  366.             $this->surveyCompanyQuestions[] = $surveyFormQuestion;
  367.             $surveyFormQuestion->setSurveyFormQuestion($this);
  368.         }
  369.         return $this;
  370.     }
  371.     public function removeSurveyCompanyQuestions(SurveyFormQuestion $surveyFormQuestion): self
  372.     {
  373.         if ($this->surveyCompanyQuestions->removeElement($surveyFormQuestion)) {
  374.             // set the owning side to null (unless already changed)
  375.             if ($surveyFormQuestion->getSurveyFormQuestion() === $this) {
  376.                 $surveyFormQuestion->setSurveyFormQuestion(null);
  377.             }
  378.         }
  379.         return $this;
  380.     }
  381.     /**
  382.      * @return Collection|SurveyCompanySurveyFormQuestion[]
  383.      */
  384.     public function getSurveyQuestionForms(): Collection
  385.     {
  386.         return $this->surveyQuestionForms;
  387.     }
  388.     public function addSurveyQuestionForm(SurveyCompanySurveyFormQuestion $form): self
  389.     {
  390.         if (!$this->surveyQuestionForms->contains($form)) {
  391.             $this->surveyQuestionForms[] = $form;
  392.             $form->setSurveyCompany($this);
  393.         }
  394.         return $this;
  395.     }
  396.     public function removeSurveyQuestionForm(SurveyCompanySurveyFormQuestion $form): self
  397.     {
  398.         if ($this->surveyQuestionForms->removeElement($form)) {
  399.             // set the owning side to null (unless already changed)
  400.             if ($form->getSurveyCompany() === $this) {
  401.                 $form->setSurveyCompany(null);
  402.             }
  403.         }
  404.         return $this;
  405.     }
  406.     /**
  407.      * @return Collection<int, SurveyCompanyToken>
  408.      */
  409.     public function getSurveyCompanyTokens(): Collection
  410.     {
  411.         return $this->surveyCompanyTokens;
  412.     }
  413.     public function addSurveyCompanyToken(SurveyCompanyToken $surveyCompanyToken): self
  414.     {
  415.         if (!$this->surveyCompanyTokens->contains($surveyCompanyToken)) {
  416.             $this->surveyCompanyTokens[] = $surveyCompanyToken;
  417.             $surveyCompanyToken->setSurveyCompany($this);
  418.         }
  419.         return $this;
  420.     }
  421.     public function removeSurveyCompanyToken(SurveyCompanyToken $surveyCompanyToken): self
  422.     {
  423.         if ($this->surveyCompanyTokens->removeElement($surveyCompanyToken)) {
  424.             // set the owning side to null (unless already changed)
  425.             if ($surveyCompanyToken->getSurveyCompany() === $this) {
  426.                 $surveyCompanyToken->setSurveyCompany(null);
  427.             }
  428.         }
  429.         return $this;
  430.     }
  431.     /**
  432.      * @Groups({"surveyComapany:item","surveyComapany:list"})
  433.      * Returns createdAt.
  434.      */
  435.     public function getCreatedAt(): ?\DateTimeInterface
  436.     {
  437.         return $this->createdAt;
  438.     }
  439.     /**
  440.      * @Groups({"surveyComapany:item","surveyComapany:list"})
  441.      * Returns bool.
  442.      */
  443.     public function getIsExpired(): ?bool
  444.     {
  445.         $now  = new DateTime('now');
  446.         $end $this->endDate;
  447.         return ($end $now ) ? true false;
  448.     }
  449.     /**
  450.      * @Groups({"surveyComapany:item","surveyComapany:list"})
  451.      * Returns int.
  452.      */
  453.     public function getTotalQuestion(): ?int
  454.     {
  455.         return $this->surveyCompanyQuestions->count();
  456.     }
  457.     /**
  458.      * @Groups({"surveyComapany:item","surveyComapany:list"})
  459.      * Returns int.
  460.      */
  461.     public function getTotalToken(): ?int
  462.     {
  463.         return $this->surveyCompanyTokens->count();
  464.     }
  465.     /**
  466.      * @Groups({"surveyComapany:item","surveyComapany:list"})
  467.      * Returns int.
  468.      */
  469.     public function getTotalForms(): ?int
  470.     {
  471.         return $this->surveyForms->count();
  472.     }
  473. }