<?php
namespace App\Entity;
use App\Entity\Contracts\FormInterface;
use App\Entity\Traits\FormTrait;
use App\Entity\Traits\ActivableTrait;
use App\Entity\Traits\EntityIdTrait;
use App\Entity\Traits\TimestampableTrait;
use App\Repository\FormRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Validator\Constraints as Assert;
use App\Validator as CustomAssert;
/**
* @ORM\Entity(repositoryClass=FormRepository::class)
* @ORM\HasLifecycleCallbacks()
* @CustomAssert\FormClass()
*/
class Form implements FormInterface
{
use EntityIdTrait;
use ActivableTrait;
use TimestampableTrait;
use FormTrait;
// Types
public const TYPE_CREATE = 'create';
public const TYPE_FULL_CREATE = 'full_create';
public const TYPE_EDIT = 'edit';
public const TYPE_DELETE = 'delete';
public const TYPES = [
self::TYPE_CREATE => 'Creación',
self::TYPE_FULL_CREATE => 'Creación con precarga de un registro',
self::TYPE_EDIT => 'Edición',
self::TYPE_DELETE => 'Borrado'
];
// Deactivable scales
public const NOT_ALLOW_SCALES = FormActionsSet::RECURRING_DATE_SCALES;
/**
* @var string
* @ORM\Column(type="string", length=255)
*/
private $type;
/**
* @ORM\ManyToOne(targetEntity=Campaign::class, inversedBy="forms")
* @ORM\JoinColumn(nullable=false)
* @Assert\NotBlank
*/
private $campaign;
/**
* @ORM\Column(type="text")
*/
private $confirmationMessage;
/**
* @var int
* @ORM\Column(type="integer", nullable=true)
*/
private $notAllowStep;
/**
* @var string
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $notAllowScale;
/**
* @var int
* @ORM\Column(type="integer", nullable=true)
*/
private $notAllowDuringStep;
/**
* @var string
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $notAllowDuringScale;
/**
* @var bool
* @ORM\Column(type="boolean")
*/
private $oneUpdateByPeriod = false;
/**
* @ORM\OneToMany(targetEntity=FormActionsSet::class, mappedBy="form", orphanRemoval=true, cascade={"remove"})
*/
private $formActionsSets;
/**
* @var MasterForm
* @ORM\ManyToOne(targetEntity=MasterForm::class, inversedBy="forms")
* @ORM\JoinColumn(nullable=true)
*/
private $masterForm;
/**
* @var FormFieldAdding[]
* @ORM\OneToMany(targetEntity=FormFieldAdding::class, mappedBy="form", cascade={"persist", "remove"})
* @ORM\JoinColumn()
*/
private $formFieldAddings;
public function __construct()
{
$this->uuid = Uuid::v6();
$this->formActionsSets = new ArrayCollection();
$this->formFieldAddings = new ArrayCollection();
}
/**
* @return string|null
*/
public function getDefinitionFile(): ?string
{
if ($this->masterForm) {
return $this->getMasterForm()->getDefinitionFile();
}
return $this->definitionFile;
}
/**
* @param string|null $definitionFile
* @return $this
*/
public function setDefinitionFile(?string $definitionFile): self
{
$this->definitionFile = $definitionFile;
return $this;
}
/**
* @return string|null
*/
public function getType(): ?string
{
return $this->type;
}
/**
* @param string $type
* @return $this
*/
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
/**
* @return Campaign|null
*/
public function getCampaign(): ?Campaign
{
return $this->campaign;
}
/**
* @param Campaign|null $campaign
* @return $this
*/
public function setCampaign(?Campaign $campaign): self
{
$this->campaign = $campaign;
return $this;
}
/**
* @return string|null
*/
public function getConfirmationMessage(): ?string
{
return $this->confirmationMessage;
}
/**
* @param string $confirmationMessage
* @return $this
*/
public function setConfirmationMessage(string $confirmationMessage): self
{
$this->confirmationMessage = $confirmationMessage;
return $this;
}
/**
* @return int|null
*/
public function getNotAllowStep(): ?int
{
return $this->notAllowStep;
}
/**
* @param int|null $notAllowStep
* @return $this
*/
public function setNotAllowStep(?int $notAllowStep): self
{
$this->notAllowStep = $notAllowStep;
return $this;
}
/**
* @return string|null
*/
public function getNotAllowScale(): ?string
{
return $this->notAllowScale;
}
/**
* @param string|null $notAllowScale
* @return $this
*/
public function setNotAllowScale(?string $notAllowScale): self
{
$this->notAllowScale = $notAllowScale;
return $this;
}
/**
* @return int|null
*/
public function getNotAllowDuringStep(): ?int
{
return $this->notAllowDuringStep;
}
/**
* @param int|null $notAllowDuringStep
* @return $this
*/
public function setNotAllowDuringStep(?int $notAllowDuringStep): self
{
$this->notAllowDuringStep = $notAllowDuringStep;
return $this;
}
/**
* @return string|null
*/
public function getNotAllowDuringScale(): ?string
{
return $this->notAllowDuringScale;
}
/**
* @param string|null $notAllowDuringScale
* @return $this
*/
public function setNotAllowDuringScale(?string $notAllowDuringScale): self
{
$this->notAllowDuringScale = $notAllowDuringScale;
return $this;
}
/**
* @param bool $oneUpdateByPeriod
* @return $this
*/
public function setOneUpdateByPeriod(bool $oneUpdateByPeriod = false): self
{
$this->oneUpdateByPeriod = $oneUpdateByPeriod;
return $this;
}
/**
* @return bool|null
*/
public function isOneUpdateByPeriod(): ?bool
{
return $this->oneUpdateByPeriod;
}
/**
* @param string|null $type
* @return Collection<int, FormActionsSet>
*/
public function getFormActionsSets(string $type = null): Collection
{
if (!$type) {
return $this->formActionsSets;
}
return $this->formActionsSets->filter(function (FormActionsSet $formActionsSet) use ($type) {
return $formActionsSet->getEvent() == $type;
});
}
/**
* @param FormActionsSet $formActionsSet
* @return $this
*/
public function addFormActionsSet(FormActionsSet $formActionsSet): self
{
if (!$this->formActionsSets->contains($formActionsSet)) {
$this->formActionsSets[] = $formActionsSet;
$formActionsSet->setForm($this);
}
return $this;
}
/**
* @param FormActionsSet $formActionsSet
* @return $this
*/
public function removeFormActionsSet(FormActionsSet $formActionsSet): self
{
if ($this->formActionsSets->removeElement($formActionsSet)) {
// set the owning side to null (unless already changed)
if ($formActionsSet->getForm() === $this) {
$formActionsSet->setForm(null);
}
}
return $this;
}
/**
* @return MasterForm|null
*/
public function getMasterForm(): ?MasterForm
{
return $this->masterForm;
}
/**
* @param MasterForm|null $masterForm
* @return $this
*/
public function setMasterForm(?MasterForm $masterForm): self
{
$this->masterForm = $masterForm;
return $this;
}
/**
* @return Collection<int, FormFieldAdding>
*/
public function getFormFieldAddings(): ?Collection
{
return $this->formFieldAddings;
}
/**
* @param FormFieldAdding $formFieldAdding
* @return $this
*/
public function addFormFieldAdding(FormFieldAdding $formFieldAdding): self
{
if (!$this->formActionsSets->contains($formFieldAdding)) {
$this->formFieldAddings[] = $formFieldAdding;
$formFieldAdding->setForm($this);
}
return $this;
}
/**
* @param FormFieldAdding $formFieldAdding
* @return $this
*/
public function removeFormFieldAdding(FormFieldAdding $formFieldAdding): self
{
if ($this->formFieldAddings->removeElement($formFieldAdding)) {
// set the owning side to null (unless already changed)
if ($formFieldAdding->getForm() === $this) {
$formFieldAdding->setForm(null);
}
}
return $this;
}
/**
* @param FormFieldAdding[] $formFieldAddings
* @return $this
*/
public function setFormFieldAddings(array $formFieldAddings): self
{
$this->formFieldAddings = $formFieldAddings;
return $this;
}
/**
* @return bool
*/
public function isTemporarilyAllow(): bool
{
return $this->notAllowStep && $this->notAllowScale;
}
/**
* @return string
*/
public function __toString()
{
return $this->getCampaign()->getCompany() . ' - ' . $this->getCampaign() . ' - ' . '[#' . $this->getId() . '] ' . $this->getName();
}
}