src/Entity/Extranet/Alert.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Extranet;
  3. use App\Annotation\SiteAware;
  4. use App\Entity\AbstractBase;
  5. use App\Entity\Interfaces\ImageInterface;
  6. use App\Entity\Interfaces\SiteInterface;
  7. use App\Entity\Traits\HasImageTrait;
  8. use App\Entity\Traits\SiteTrait;
  9. use App\Entity\User;
  10. use App\Entity\UserGroup;
  11. use App\Enum\SiteEnum;
  12. use App\Repository\Extranet\AlertRepository;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Doctrine\Common\Collections\Collection;
  15. use Doctrine\ORM\Mapping as ORM;
  16. use Gedmo\Mapping\Annotation as Gedmo;
  17. use Symfony\Component\HttpFoundation\File\File;
  18. use Symfony\Component\Validator\Constraints\Callback;
  19. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  20. use Symfony\Component\Validator\Mapping\ClassMetadata;
  21. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  22. /**
  23.  * @ORM\Table(name="vulco_alert", indexes={@ORM\Index(name="alert_address_site_idx", columns={"site"})})
  24.  *
  25.  * @ORM\Entity(repositoryClass=AlertRepository::class)
  26.  *
  27.  * @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
  28.  *
  29.  * @SiteAware(siteFieldName="site")
  30.  *
  31.  * @Vich\Uploadable
  32.  */
  33. class Alert extends AbstractBase implements SiteInterfaceImageInterface
  34. {
  35.     use SiteTrait;
  36.     use HasImageTrait;
  37.     /**
  38.      * @ORM\Column(type="integer")
  39.      */
  40.     private int $type;
  41.     /**
  42.      * @ORM\Column(type="string")
  43.      */
  44.     private string $title;
  45.     /**
  46.      * @ORM\Column(type="text", length=10000, nullable=true, options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"})
  47.      */
  48.     private ?string $text null;
  49.     /**
  50.      * @ORM\Column(type="text", length=10000, nullable=true)
  51.      */
  52.     private ?string $checkboxLabel null;
  53.     /**
  54.      * @ORM\Column(type="datetime")
  55.      */
  56.     private \DateTimeInterface $begin;
  57.     /**
  58.      * @ORM\Column(type="datetime")
  59.      */
  60.     private \DateTimeInterface $end;
  61.     /**
  62.      * @Vich\UploadableField(mapping="alert_image", fileNameProperty="imageName")
  63.      */
  64.     private ?File $image null;
  65.     /**
  66.      * @ORM\ManyToMany(targetEntity="App\Entity\UserGroup", inversedBy="alerts")
  67.      *
  68.      * @ORM\JoinTable(name="vulco_alerts_user_groups",
  69.      *     joinColumns={@ORM\JoinColumn(name="alert_id", referencedColumnName="id")},
  70.      *     inverseJoinColumns={@ORM\JoinColumn(name="user_group_id", referencedColumnName="id")}
  71.      * )
  72.      */
  73.     public ?Collection $userGroups;
  74.     public function __construct()
  75.     {
  76.         $this->userGroups = new ArrayCollection();
  77.     }
  78.     public static function loadValidatorMetadata(ClassMetadata $metadata): void
  79.     {
  80.         $metadata->addConstraint(new Callback('validate'));
  81.     }
  82.     public function validate(ExecutionContextInterface $context): void
  83.     {
  84.         if ($this->getBegin()->getTimestamp() > $this->getEnd()->getTimestamp()) {
  85.             $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.')
  86.                 ->atPath('begin')
  87.                 ->addViolation()
  88.             ;
  89.         }
  90.     }
  91.     public function getType(): int
  92.     {
  93.         return $this->type;
  94.     }
  95.     public function setType(int $type): self
  96.     {
  97.         $this->type $type;
  98.         return $this;
  99.     }
  100.     public function getTitle(): string
  101.     {
  102.         return $this->title;
  103.     }
  104.     public function setTitle(string $title): self
  105.     {
  106.         $this->title $title;
  107.         return $this;
  108.     }
  109.     public function getText(): ?string
  110.     {
  111.         return $this->text;
  112.     }
  113.     public function setText(?string $text): self
  114.     {
  115.         $this->text $text;
  116.         return $this;
  117.     }
  118.     public function getCheckboxLabel(): ?string
  119.     {
  120.         return $this->checkboxLabel;
  121.     }
  122.     public function setCheckboxLabel(?string $checkboxLabel): self
  123.     {
  124.         $this->checkboxLabel $checkboxLabel;
  125.         return $this;
  126.     }
  127.     public function getBegin(): \DateTimeInterface
  128.     {
  129.         return $this->begin;
  130.     }
  131.     public function setBegin(\DateTimeInterface $begin): self
  132.     {
  133.         $this->begin $begin;
  134.         return $this;
  135.     }
  136.     public function getEnd(): \DateTimeInterface
  137.     {
  138.         return $this->end;
  139.     }
  140.     public function setEnd(\DateTimeInterface $end): self
  141.     {
  142.         $this->end $end;
  143.         return $this;
  144.     }
  145.     public function getUserGroups(): ?Collection
  146.     {
  147.         return $this->userGroups;
  148.     }
  149.     public function addUserGroup(UserGroup $userGroup): self
  150.     {
  151.         if (!$this->userGroups->contains($userGroup)) {
  152.             $this->userGroups->add($userGroup);
  153.             $userGroup->addAlert($this);
  154.         }
  155.         return $this;
  156.     }
  157.     public function removeUserGroup(UserGroup $userGroup): self
  158.     {
  159.         if ($this->userGroups->contains($userGroup)) {
  160.             $this->userGroups->removeElement($userGroup);
  161.         }
  162.         return $this;
  163.     }
  164.     public function isExcludedFor(User $user): bool
  165.     {
  166.         $result false;
  167.         if (null !== $this->userGroups) {
  168.             /** @var UserGroup $userGroup */
  169.             foreach ($this->userGroups as $userGroup) {
  170.                 if ((null !== $user->getUserGroup()) && $userGroup->getId() === $user->getUserGroup()->getId()) {
  171.                     $result true;
  172.                     break;
  173.                 }
  174.             }
  175.         }
  176.         return $result;
  177.     }
  178. }