<?php
namespace App\Entity\SatisfactionSurveys;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use App\Annotation\SatisfactionSurveyAware;
use App\Annotation\SiteAware;
use App\Entity\AbstractBase;
use App\Entity\Interfaces\SiteInterface;
use App\Entity\Traits\ActiveTrait;
use App\Entity\Traits\NameTrait;
use App\Entity\Traits\SiteTrait;
use App\Enum\SiteEnum;
use App\Repository\SatisfactionSurveys\SatisfactionSurveyRepository;
use DateTimeImmutable;
use DateTimeInterface;
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\Serializer\Annotation\SerializedName;
/**
* @ApiResource(
* shortName="satisfaction-survey",
* normalizationContext={"groups"={"api:read", "satisfaction_survey:read"}, "swagger_definition_name"="Read"},
* denormalizationContext={"groups"={"satisfaction_survey:write"}, "swagger_definition_name"="Write"},
* collectionOperations={"get"},
* itemOperations={"get", "put"}
* )
* @ApiFilter(SearchFilter::class, properties={"name": "exact"})
* @ORM\Table(name="vulco_satisfaction_survey", indexes={@ORM\Index(name="satisfaction_survey_site_idx", columns={"site"})})
* @ORM\Entity(repositoryClass=SatisfactionSurveyRepository::class)
* @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
* @SatisfactionSurveyAware(satisfactionSurveyFieldName="id")
* @SiteAware(siteFieldName="site")
*/
class SatisfactionSurvey extends AbstractBase implements SiteInterface
{
use ActiveTrait;
use NameTrait;
use SiteTrait;
public const DEFAULT_ES_NAME = '0000-ES-default-satisfaction-survey-name';
public const DEFAULT_PT_NAME = '0000-PT-default-satisfaction-survey-name';
/**
* @ApiProperty(identifier=false)
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"api:read", "api:write"})
*/
protected int $id;
/**
* @ORM\OneToMany(targetEntity="SatisfactionSurveyQuestion", mappedBy="survey", cascade={"persist", "remove"})
*/
private ?Collection $questions;
/**
* @ORM\Column(type="date", nullable=true)
*/
private ?DateTimeInterface $begin;
/**
* @ApiProperty(identifier=true)
* @ORM\Column(type="string", length=255, nullable=false, unique=true)
* @Groups({"satisfaction_survey:read"})
*/
private string $name = '';
/**
* @ORM\Column(type="boolean", nullable=false, options={"default": 1})
* @Groups({"satisfaction_survey:read"})
*/
private bool $hasCustomerCommentsEnabled = true;
/**
* @ORM\Column(type="boolean", nullable=false, options={"default": 1})
* @Groups({"satisfaction_survey:read"})
*/
private bool $hasHowDidYouFindUsQuestionEnabled = true;
/**
* @ORM\Column(type="text", length=10000, nullable=false)
* @Groups({"satisfaction_survey:read"})
*/
private string $customerCommentsTextHelper = '';
/**
* @ORM\Column(type="boolean", nullable=false, options={"default": 1})
*/
private bool $active = true;
public function __construct()
{
$this->questions = new ArrayCollection();
$this->begin = new DateTimeImmutable();
}
public static function getNameByLocale(string $locale): string
{
$result = self::DEFAULT_ES_NAME;
if (strtolower($locale) === strtolower(SiteEnum::SITE_STR_PT)) {
$result = self::DEFAULT_PT_NAME;
}
return $result;
}
public function getQuestions(): ?Collection
{
return $this->questions;
}
/**
* @Groups({"satisfaction_survey:read"})
* @SerializedName("questions")
*/
public function getActiveQuestionsSortedByPosition(): array
{
$result = [];
if (count($this->getQuestions()) > 0) {
/** @var SatisfactionSurveyQuestion $question */
foreach ($this->getQuestions() as $question) {
if ($question->isActive()) {
$result[] = $question;
}
}
usort($result, static fn (SatisfactionSurveyQuestion $a, SatisfactionSurveyQuestion $b) => strcmp($a->getPosition(), $b->getPosition()));
}
return $result;
}
public function setQuestions(?Collection $questions): self
{
$this->questions = $questions;
return $this;
}
public function addQuestion(SatisfactionSurveyQuestion $question): self
{
if (!$this->questions->contains($question)) {
$question->setSurvey($this);
$this->questions->add($question);
}
return $this;
}
public function removeQuestion(SatisfactionSurveyQuestion $question): self
{
if ($this->questions->contains($question)) {
$this->questions->removeElement($question);
}
return $this;
}
public function getBegin(): ?DateTimeInterface
{
return $this->begin;
}
public function setBegin(?DateTimeInterface $begin): self
{
$this->begin = $begin;
return $this;
}
public function isHasCustomerCommentsEnabled(): bool
{
return $this->hasCustomerCommentsEnabled;
}
public function getHasCustomerCommentsEnabled(): bool
{
return $this->isHasCustomerCommentsEnabled();
}
public function setHasCustomerCommentsEnabled(bool $hasCustomerCommentsEnabled): self
{
$this->hasCustomerCommentsEnabled = $hasCustomerCommentsEnabled;
return $this;
}
public function isHasHowDidYouFindUsQuestionEnabled(): bool
{
return $this->hasHowDidYouFindUsQuestionEnabled;
}
public function getHasHowDidYouFindUsQuestionEnabled(): bool
{
return $this->isHasHowDidYouFindUsQuestionEnabled();
}
public function setHasHowDidYouFindUsQuestionEnabled(bool $hasHowDidYouFindUsQuestionEnabled): self
{
$this->hasHowDidYouFindUsQuestionEnabled = $hasHowDidYouFindUsQuestionEnabled;
return $this;
}
public function getCustomerCommentsTextHelper(): string
{
return $this->customerCommentsTextHelper;
}
public function setCustomerCommentsTextHelper(string $customerCommentsTextHelper): self
{
$this->customerCommentsTextHelper = $customerCommentsTextHelper;
return $this;
}
}