<?php
namespace App\Entity\Extranet;
use App\Annotation\SiteAware;
use App\Entity\AbstractBase;
use App\Entity\Interfaces\ImageInterface;
use App\Entity\Interfaces\SiteInterface;
use App\Entity\Traits\HasImageTrait;
use App\Entity\Traits\SiteTrait;
use App\Entity\User;
use App\Entity\UserGroup;
use App\Enum\SiteEnum;
use App\Repository\Extranet\AlertRepository;
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\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Table(name="vulco_alert", indexes={@ORM\Index(name="alert_address_site_idx", columns={"site"})})
*
* @ORM\Entity(repositoryClass=AlertRepository::class)
*
* @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
*
* @SiteAware(siteFieldName="site")
*
* @Vich\Uploadable
*/
class Alert extends AbstractBase implements SiteInterface, ImageInterface
{
use SiteTrait;
use HasImageTrait;
/**
* @ORM\Column(type="integer")
*/
private int $type;
/**
* @ORM\Column(type="string")
*/
private string $title;
/**
* @ORM\Column(type="text", length=10000, nullable=true, options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"})
*/
private ?string $text = null;
/**
* @ORM\Column(type="text", length=10000, nullable=true)
*/
private ?string $checkboxLabel = null;
/**
* @ORM\Column(type="datetime")
*/
private \DateTimeInterface $begin;
/**
* @ORM\Column(type="datetime")
*/
private \DateTimeInterface $end;
/**
* @Vich\UploadableField(mapping="alert_image", fileNameProperty="imageName")
*/
private ?File $image = null;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\UserGroup", inversedBy="alerts")
*
* @ORM\JoinTable(name="vulco_alerts_user_groups",
* joinColumns={@ORM\JoinColumn(name="alert_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="user_group_id", referencedColumnName="id")}
* )
*/
public ?Collection $userGroups;
public function __construct()
{
$this->userGroups = new ArrayCollection();
}
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addConstraint(new Callback('validate'));
}
public function validate(ExecutionContextInterface $context): void
{
if ($this->getBegin()->getTimestamp() > $this->getEnd()->getTimestamp()) {
$context->buildViolation(SiteEnum::SITE_STR_ES === $this->getSite() ? 'ERROR! La fecha inicial tiene que ser anterior a la fecha final.' : 'ERRO! A data inicial deve ser antes da data final.')
->atPath('begin')
->addViolation()
;
}
}
public function getType(): int
{
return $this->type;
}
public function setType(int $type): self
{
$this->type = $type;
return $this;
}
public function getTitle(): string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getText(): ?string
{
return $this->text;
}
public function setText(?string $text): self
{
$this->text = $text;
return $this;
}
public function getCheckboxLabel(): ?string
{
return $this->checkboxLabel;
}
public function setCheckboxLabel(?string $checkboxLabel): self
{
$this->checkboxLabel = $checkboxLabel;
return $this;
}
public function getBegin(): \DateTimeInterface
{
return $this->begin;
}
public function setBegin(\DateTimeInterface $begin): self
{
$this->begin = $begin;
return $this;
}
public function getEnd(): \DateTimeInterface
{
return $this->end;
}
public function setEnd(\DateTimeInterface $end): self
{
$this->end = $end;
return $this;
}
public function getUserGroups(): ?Collection
{
return $this->userGroups;
}
public function addUserGroup(UserGroup $userGroup): self
{
if (!$this->userGroups->contains($userGroup)) {
$this->userGroups->add($userGroup);
$userGroup->addAlert($this);
}
return $this;
}
public function removeUserGroup(UserGroup $userGroup): self
{
if ($this->userGroups->contains($userGroup)) {
$this->userGroups->removeElement($userGroup);
}
return $this;
}
public function isExcludedFor(User $user): bool
{
$result = false;
if (null !== $this->userGroups) {
/** @var UserGroup $userGroup */
foreach ($this->userGroups as $userGroup) {
if ((null !== $user->getUserGroup()) && $userGroup->getId() === $user->getUserGroup()->getId()) {
$result = true;
break;
}
}
}
return $result;
}
}