<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Controller\SurveyCompanyController;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\SurveyCompanyRepository")
* @ORM\HasLifecycleCallbacks()
*/
#[ApiResource(
attributes: ['security' => "is_granted('ROLE_USER')"],
normalizationContext: ['groups' => ['surveyComapany:item', 'surveyComapany:list']],
denormalizationContext: ['groups' => ['surveyComapany:write']],
collectionOperations: [
'get' => ['normalization_context' => ['groups' => ['surveyComapany:list']]],
'post' => [
'security' => "is_granted('ROLE_ADMIN')",
],
],
itemOperations: [
'get' => ['normalization_context' => ['groups' => ['surveyComapany:item']]],
'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
'question_reset' => [
'method' => 'GET',
'path' => '/survey_companies/{id}/reset',
'controller' => SurveyCompanyController::class,
'normalization_context' => ['groups' => ['surveyComapany:item']],
],
],
order: ['createdAt' => 'DESC']
)]
#[ApiFilter(SearchFilter::class, properties: ['id', 'title' => 'partial', 'company' => 'exact', 'deploymentType' => 'exact'])]
#[ApiFilter(PropertyFilter::class)]
#[ApiFilter(OrderFilter::class, properties: ['id', 'title', 'company.name', 'deploymentType.name', 'startDate', 'endDate', 'createdAt', 'isActive'], arguments: ['orderParameterName' => 'order'])]
class SurveyCompany
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"surveyComapany:item","surveyComapany:list"})
*/
public int $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="surveyCompanies")
* @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
* @Groups({"surveyComapany:item","surveyComapany:list", "surveyComapany:write"})
*/
public $company;
/**
* @ORM\Column(type="string", length=128)
* @Assert\Length(max="128")
* @Groups({"surveyComapany:item","surveyComapany:list" ,"surveyComapany:write"})
* @Assert\NotNull()
*/
public $title;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\DeploymentType", inversedBy="surveyCompanies")
* @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
* @Groups({"surveyComapany:item","surveyComapany:list", "surveyComapany:write"})
*/
public $deploymentType;
/**
* @var DateTime
*
* @ORM\Column(type="datetime", nullable=true)
* @Groups({"surveyComapany:item", "surveyComapany:write"})
*/
private $startDate;
/**
* @var DateTime
*
* @ORM\Column(type="datetime", nullable=true)
* @Groups({"surveyComapany:item","surveyComapany:list", "surveyComapany:write"})
*/
private $endDate;
/**
* @ORM\Column(type="boolean")
* @Assert\Type(type="boolean")
* @Groups({"surveyComapany:item","surveyComapany:list", "surveyComapany:write"})
*/
private $isActive;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups({"surveyComapany:item", "surveyComapany:write"})
*/
private $sortOrder;
/**
* @var ArrayCollection|PinpointCampaign[]
*
* @ORM\OneToMany(targetEntity="App\Entity\PinpointCampaign", mappedBy="surveyCompany", cascade={"persist"})
* @ORM\JoinColumn(name="survey_form_question_id", referencedColumnName="id")
*/
public $pinpointCampaigns;
/**
* @var ArrayCollection|SurveyForm[]
*
* @ORM\ManyToMany(targetEntity="App\Entity\SurveyForm", mappedBy="surveyCompany", cascade={"persist","remove"}, fetch="EXTRA_LAZY")
* @ORM\JoinTable(name="survey_form_survey_company",
* joinColumns={@ORM\JoinColumn(name="survey_company_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="survey_form_id", referencedColumnName="id")}
* )
* @ORM\OrderBy({"id" = "ASC"})
* @Groups({"surveyComapany:item", "surveyComapany:write"})
*/
public $surveyForms;
/** @var ArrayCollection|SurveyFormSurveyCompany[]
* @ORM\OneToMany(targetEntity=SurveyFormSurveyCompany::class, mappedBy="surveyCompany", orphanRemoval=true)
*/
public $forms;
/**
* @var ArrayCollection|SurveyFormStepSurveyFormQuestion[]
*
* @ORM\OneToMany(targetEntity=SurveyFormStepSurveyFormQuestion::class, mappedBy="surveyCompany", orphanRemoval=true)
*/
public $surveyQuestionForms;
/**
* @var ArrayCollection|SurveyCompanySurveyFormQuestion[]
*
* @ORM\OneToMany(targetEntity=SurveyCompanySurveyFormQuestion::class, mappedBy="surveyCompany", orphanRemoval=true)
* @ORM\OrderBy({"level"="ASC","rank" = "ASC"})
* @Groups({"surveyComapany:item", "surveyComapany:write"})
*/
private $surveyCompanyQuestions;
/**
* @var ArrayCollection|SurveyCompanyToken[]
* @ORM\OneToMany(targetEntity=SurveyCompanyToken::class, mappedBy="surveyCompany", orphanRemoval=true,cascade={"persist","remove"}, fetch="EXTRA_LAZY")
* @Groups({"surveyComapany:item", "surveyComapany:write"})
*/
private $surveyCompanyTokens;
public function __construct()
{
$this->isActive = false;
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
$this->surveyForms = new ArrayCollection();
$this->pinpointCampaigns = new ArrayCollection();
$this->surveyQuestionForms = new ArrayCollection();
$this->surveyCompanyQuestions = new ArrayCollection();
$this->forms = new ArrayCollection();
$this->surveyCompanyTokens = new ArrayCollection();
}
/**
* @return mixed
*/
public function __toString()
{
return $this->title ?? '';
}
public function getId(): ?int
{
return $this->id;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getStartDate(): ?DateTime
{
return $this->startDate;
}
public function setStartDate(?DateTime $startDate): self
{
$this->startDate = $startDate;
return $this;
}
public function getEndDate(): ?DateTime
{
return $this->endDate;
}
public function setEndDate(?DateTime $endDate): self
{
$this->endDate = $endDate;
return $this;
}
public function getIsActive(): bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getSortOrder(): ?int
{
return $this->sortOrder;
}
public function setSortOrder(?int $sortOrder): self
{
$this->sortOrder = $sortOrder;
return $this;
}
public function getDeploymentType(): ?DeploymentType
{
return $this->deploymentType;
}
public function setDeploymentType(?DeploymentType $deploymentType): self
{
$this->deploymentType = $deploymentType;
return $this;
}
/**
* @return Collection|PinpointCampaign[]
*/
public function getPinpointCampaigns(): Collection
{
return $this->pinpointCampaigns;
}
public function addPinpointCampaign(PinpointCampaign $pinpointCampaign): self
{
if (!$this->pinpointCampaigns->contains($pinpointCampaign)) {
$this->pinpointCampaigns[] = $pinpointCampaign;
$pinpointCampaign->setSurveyCompany($this);
}
return $this;
}
public function removePinpointCampaign(PinpointCampaign $pinpointCampaign): self
{
if ($this->pinpointCampaigns->contains($pinpointCampaign)) {
$this->pinpointCampaigns->removeElement($pinpointCampaign);
// set the owning side to null (unless already changed)
if ($pinpointCampaign->getSurveyCompany() === $this) {
$pinpointCampaign->setSurveyCompany(null);
}
}
return $this;
}
/**
* @return Collection|SurveyForm[]
*/
public function getSurveyForms(): Collection
{
return $this->surveyForms;
}
public function addSurveyForm(SurveyForm $surveyForm): self
{
if (!$this->surveyForms->contains($surveyForm)) {
$this->surveyForms[] = $surveyForm;
$surveyForm->addSurveyCompany($this);
}
return $this;
}
public function removeSurveyForm(SurveyForm $surveyForm): self
{
if ($this->surveyForms->contains($surveyForm)) {
$this->surveyForms->removeElement($surveyForm);
$surveyForm->removeSurveyCompany($this);
}
return $this;
}
/**
* @return Collection|SurveyFormSurveyCompany[]
*/
public function getForms(): Collection
{
return $this->forms;
}
public function addForm(SurveyFormSurveyCompany $form): self
{
if (!$this->forms->contains($form)) {
$this->forms[] = $form;
$form->setSurveyCompany($this);
}
return $this;
}
public function removeForm(SurveyFormSurveyCompany $form): self
{
if ($this->forms->removeElement($form)) {
// set the owning side to null (unless already changed)
if ($form->getSurveyCompany() === $this) {
$form->setSurveyCompany(null);
}
}
return $this;
}
/**
* @return Collection|SurveyCompanySurveyFormQuestion[]
*/
public function getSurveyCompanyQuestions(): Collection
{
$criteria = Criteria::create()->orderBy(['parentSurveyFormQuestion' => Criteria::ASC, 'rank' => Criteria::ASC]);
$this->surveyCompanyQuestions->matching($criteria);
$questions = new ArrayCollection();
$i = 1;
foreach ($this->surveyCompanyQuestions as $question) {
if (0 == $question->getLevel() && null == $question->getParentSurveyFormQuestion()) {
if (0 == $question->getRank()) {
$question->setRank(($i - 1) * 3);
}
$questions[] = $question;
$i1 = 0;
foreach ($this->surveyCompanyQuestions as $question1st) {
if (1 == $question1st->getLevel() && $question1st->getParentSurveyFormQuestion() && $question1st->getParentSurveyFormQuestion()->getId() == $question->getSurveyFormQuestion()->getId()) {
if (0 == $question1st->getRank()) {
$question1st->setRank($i1 * 3);
}
$questions[] = $question1st;
$i2 = 0;
foreach ($this->surveyCompanyQuestions as $question2nd) {
if (2 == $question2nd->getLevel() && $question2nd->getParentSurveyFormQuestion() && $question2nd->getParentSurveyFormQuestion()->getId() == $question1st->getSurveyFormQuestion()->getId()) {
if (0 == $question2nd->getRank()) {
$question2nd->setRank($i2 * 3);
}
$questions[] = $question2nd;
$i3 = 0;
foreach ($this->surveyCompanyQuestions as $question3rd) {
if (3 == $question3rd->getLevel() && $question3rd->getParentSurveyFormQuestion() && $question3rd->getParentSurveyFormQuestion()->getId() == $question2nd->getSurveyFormQuestion()->getId()) {
if (0 == $question3rd->getRank()) {
$question3rd->setRank($i3 * 3);
}
$questions[] = $question3rd;
$i4 = 0;
foreach ($this->surveyCompanyQuestions as $question4rth) {
if (4 == $question4rth->getLevel() && $question4rth->getParentSurveyFormQuestion() && $question4rth->getParentSurveyFormQuestion()->getId() == $question3rd->getSurveyFormQuestion()->getId()) {
if (0 == $question4rth->getRank()) {
$question4rth->setRank($i4 * 3);
}
$questions[] = $question4rth;
++$i4;
}
}
++$i3;
}
}
++$i2;
}
}
++$i1;
}
}
++$i;
}
}
return $questions;
}
public function addSurveyCompanyQuestions(SurveyFormQuestion $surveyFormQuestion): self
{
if (!$this->surveyCompanyQuestions->contains($surveyFormQuestion)) {
$this->surveyCompanyQuestions[] = $surveyFormQuestion;
$surveyFormQuestion->setSurveyFormQuestion($this);
}
return $this;
}
public function removeSurveyCompanyQuestions(SurveyFormQuestion $surveyFormQuestion): self
{
if ($this->surveyCompanyQuestions->removeElement($surveyFormQuestion)) {
// set the owning side to null (unless already changed)
if ($surveyFormQuestion->getSurveyFormQuestion() === $this) {
$surveyFormQuestion->setSurveyFormQuestion(null);
}
}
return $this;
}
/**
* @return Collection|SurveyCompanySurveyFormQuestion[]
*/
public function getSurveyQuestionForms(): Collection
{
return $this->surveyQuestionForms;
}
public function addSurveyQuestionForm(SurveyCompanySurveyFormQuestion $form): self
{
if (!$this->surveyQuestionForms->contains($form)) {
$this->surveyQuestionForms[] = $form;
$form->setSurveyCompany($this);
}
return $this;
}
public function removeSurveyQuestionForm(SurveyCompanySurveyFormQuestion $form): self
{
if ($this->surveyQuestionForms->removeElement($form)) {
// set the owning side to null (unless already changed)
if ($form->getSurveyCompany() === $this) {
$form->setSurveyCompany(null);
}
}
return $this;
}
/**
* @return Collection<int, SurveyCompanyToken>
*/
public function getSurveyCompanyTokens(): Collection
{
return $this->surveyCompanyTokens;
}
public function addSurveyCompanyToken(SurveyCompanyToken $surveyCompanyToken): self
{
if (!$this->surveyCompanyTokens->contains($surveyCompanyToken)) {
$this->surveyCompanyTokens[] = $surveyCompanyToken;
$surveyCompanyToken->setSurveyCompany($this);
}
return $this;
}
public function removeSurveyCompanyToken(SurveyCompanyToken $surveyCompanyToken): self
{
if ($this->surveyCompanyTokens->removeElement($surveyCompanyToken)) {
// set the owning side to null (unless already changed)
if ($surveyCompanyToken->getSurveyCompany() === $this) {
$surveyCompanyToken->setSurveyCompany(null);
}
}
return $this;
}
/**
* @Groups({"surveyComapany:item","surveyComapany:list"})
* Returns createdAt.
*/
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
/**
* @Groups({"surveyComapany:item","surveyComapany:list"})
* Returns bool.
*/
public function getIsExpired(): ?bool
{
$now = new DateTime('now');
$end = $this->endDate;
return ($end < $now ) ? true : false;
}
/**
* @Groups({"surveyComapany:item","surveyComapany:list"})
* Returns int.
*/
public function getTotalQuestion(): ?int
{
return $this->surveyCompanyQuestions->count();
}
/**
* @Groups({"surveyComapany:item","surveyComapany:list"})
* Returns int.
*/
public function getTotalToken(): ?int
{
return $this->surveyCompanyTokens->count();
}
/**
* @Groups({"surveyComapany:item","surveyComapany:list"})
* Returns int.
*/
public function getTotalForms(): ?int
{
return $this->surveyForms->count();
}
}