src/Entity/Promotions/LocalPromotion.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Promotions;
  3. use App\Annotation\SiteAware;
  4. use App\Entity\AbstractBase;
  5. use App\Entity\Garages\Garage;
  6. use App\Entity\Interfaces\SiteInterface;
  7. use App\Entity\Traits\GarageTrait;
  8. use App\Entity\Traits\NameTrait;
  9. use App\Entity\Traits\PublishedTrait;
  10. use App\Entity\Traits\SiteTrait;
  11. use App\Enum\LocalPromotionTypeEnum;
  12. use App\Enum\SiteEnum;
  13. use App\Repository\Promotions\LocalPromotionRepository;
  14. use DateTime;
  15. use Doctrine\ORM\Mapping as ORM;
  16. use Gedmo\Mapping\Annotation as Gedmo;
  17. use Symfony\Component\Validator\Constraints\Callback;
  18. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  19. use Symfony\Component\Validator\Mapping\ClassMetadata;
  20. /**
  21.  * @ORM\Table(name="vulco_local_promotion_v2", indexes={@ORM\Index(name="local_promotion_v2_site_idx", columns={"site"})})
  22.  *
  23.  * @ORM\Entity(repositoryClass=LocalPromotionRepository::class)
  24.  *
  25.  * @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
  26.  *
  27.  * @SiteAware(siteFieldName="site")
  28.  */
  29. class LocalPromotion extends AbstractBase implements SiteInterface
  30. {
  31.     use GarageTrait;
  32.     use NameTrait;
  33.     use SiteTrait;
  34.     use PublishedTrait;
  35.     /**
  36.      * @ORM\Column(type="string", length=255, nullable=false)
  37.      */
  38.     private string $name;
  39.     /**
  40.      * @ORM\ManyToOne(targetEntity="App\Entity\Garages\Garage", inversedBy="localPromotions")
  41.      *
  42.      * @ORM\JoinColumn(name="garage_id", referencedColumnName="id")
  43.      */
  44.     private Garage $garage;
  45.     /**
  46.      * @ORM\Column(type="integer", nullable=false, options={"default": 0})
  47.      */
  48.     private int $type 0;
  49.     /**
  50.      * @ORM\Column(type="datetime")
  51.      */
  52.     private \DateTimeInterface $begin;
  53.     /**
  54.      * @ORM\Column(type="datetime")
  55.      */
  56.     private \DateTimeInterface $end;
  57.     /**
  58.      * @ORM\Column(type="boolean", nullable=false, options={"default": 0})
  59.      */
  60.     private bool $featured false;
  61.     /**
  62.      * @ORM\Column(type="boolean", nullable=false, options={"default": 0})
  63.      */
  64.     private bool $approved false;
  65.     /**
  66.      * @ORM\Column(type="boolean", nullable=false, options={"default": 0})
  67.      */
  68.     private bool $approvalPending false;
  69.     /**
  70.      * @ORM\Column(type="string", length=255, nullable=true)
  71.      */
  72.     private ?string $url null;
  73.     /**
  74.      * @ORM\Column(type="string", length=255, nullable=true)
  75.      */
  76.     private ?string $text1 null;
  77.     /**
  78.      * @ORM\Column(type="string", length=255, nullable=true)
  79.      */
  80.     private ?string $text2 null;
  81.     /**
  82.      * @ORM\Column(type="string", length=255, nullable=true)
  83.      */
  84.     private ?string $text3 null;
  85.     /**
  86.      * @ORM\Column(type="string", length=255, nullable=true)
  87.      */
  88.     private ?string $text4 null;
  89.     /**
  90.      * @ORM\Column(type="string", length=255, nullable=true)
  91.      */
  92.     private ?string $text5 null;
  93.     /**
  94.      * @ORM\Column(type="boolean", nullable=false, options={"default": 1})
  95.      */
  96.     private bool $showConditions true;
  97.     public function __construct()
  98.     {
  99.     }
  100.     public static function loadValidatorMetadata(ClassMetadata $metadata): void
  101.     {
  102.         $metadata->addConstraint(new Callback('validate'));
  103.     }
  104.     public function validate(ExecutionContextInterface $context): void
  105.     {
  106.         if ($this->getBegin()->getTimestamp() > $this->getEnd()->getTimestamp()) {
  107.             $context->buildViolation(SiteEnum::SITE_STR_ES === $this->getSite() ? 'La fecha inicial tiene que ser anterior a la fecha final.' 'A data inicial deve ser antes da data final.')
  108.                 ->atPath('begin')
  109.                 ->addViolation()
  110.             ;
  111.         }
  112.     }
  113.     public function getType(): int
  114.     {
  115.         return $this->type;
  116.     }
  117.     public function getTypeAsTranlationKeyString(): string
  118.     {
  119.         return LocalPromotionTypeEnum::getTranslatedEnumArray()[$this->getType()];
  120.     }
  121.     public function setType(int $type): self
  122.     {
  123.         $this->type $type;
  124.         return $this;
  125.     }
  126.     public function getBegin(): \DateTimeInterface
  127.     {
  128.         return $this->begin;
  129.     }
  130.     public function setBegin(\DateTimeInterface $begin): self
  131.     {
  132.         $this->begin $begin;
  133.         return $this;
  134.     }
  135.     public function getEnd(): \DateTimeInterface
  136.     {
  137.         return $this->end;
  138.     }
  139.     public function setEnd(\DateTimeInterface $end): self
  140.     {
  141.         $this->end $end;
  142.         return $this;
  143.     }
  144.     public function isFeatured(): bool
  145.     {
  146.         return $this->featured;
  147.     }
  148.     public function setFeatured(bool $featured): self
  149.     {
  150.         $this->featured $featured;
  151.         return $this;
  152.     }
  153.     public function isApproved(): bool
  154.     {
  155.         return $this->approved;
  156.     }
  157.     public function setApproved(bool $approved): self
  158.     {
  159.         $this->approved $approved;
  160.         return $this;
  161.     }
  162.     public function isApprovalPending(): bool
  163.     {
  164.         return $this->approvalPending;
  165.     }
  166.     public function setApprovalPending(bool $approvalPending): self
  167.     {
  168.         $this->approvalPending $approvalPending;
  169.         return $this;
  170.     }
  171.     public function getUrl(): ?string
  172.     {
  173.         return $this->url;
  174.     }
  175.     public function setUrl(?string $url): self
  176.     {
  177.         $this->url $url;
  178.         return $this;
  179.     }
  180.     public function getText1(): ?string
  181.     {
  182.         return $this->text1;
  183.     }
  184.     public function setText1(?string $text1): self
  185.     {
  186.         $this->text1 $text1;
  187.         return $this;
  188.     }
  189.     public function getText2(): ?string
  190.     {
  191.         return $this->text2;
  192.     }
  193.     public function setText2(?string $text2): self
  194.     {
  195.         $this->text2 $text2;
  196.         return $this;
  197.     }
  198.     public function getText3(): ?string
  199.     {
  200.         return $this->text3;
  201.     }
  202.     public function setText3(?string $text3): self
  203.     {
  204.         $this->text3 $text3;
  205.         return $this;
  206.     }
  207.     public function getText4(): ?string
  208.     {
  209.         return $this->text4;
  210.     }
  211.     public function setText4(?string $text4): self
  212.     {
  213.         $this->text4 $text4;
  214.         return $this;
  215.     }
  216.     public function getText5(): ?string
  217.     {
  218.         return $this->text5;
  219.     }
  220.     public function setText5(?string $text5): self
  221.     {
  222.         $this->text5 $text5;
  223.         return $this;
  224.     }
  225.     public function isShowConditions(): bool
  226.     {
  227.         return $this->showConditions;
  228.     }
  229.     public function setShowConditions(bool $showConditions): self
  230.     {
  231.         $this->showConditions $showConditions;
  232.         return $this;
  233.     }
  234.     public function isActive(): bool
  235.     {
  236.         $now = new \DateTime();
  237.         $nowStr $now->format('d-m-Y');
  238.         if ($this->getBegin()) {
  239.             $beginStr $this->getBegin()->format('d-m-Y');
  240.         } else {
  241.             return false;
  242.         }
  243.         if ($this->getEnd()) {
  244.             $endStr $this->getEnd()->format('d-m-Y');
  245.         } else {
  246.             return false;
  247.         }
  248.         return strtotime($beginStr) <= strtotime($nowStr) && strtotime($nowStr) <= strtotime($endStr) && $this->isApproved() && $this->isPublished();
  249.     }
  250.     public function hasToBeValidated(): bool
  251.     {
  252.         return !$this->isApproved() && !$this->isApprovalPending();
  253.     }
  254. }