src/Entity/Landing.php line 84

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM,
  4.     Doctrine\DBAL\Types\Types,
  5.     Doctrine\Common\Collections\Collection,
  6.     Doctrine\Common\Collections\ArrayCollection;
  7. use Symfony\Component\Serializer\Annotation\Groups,
  8.     Symfony\Component\Validator\Constraints as Assert;
  9. use ApiPlatform\Metadata\ApiResource,
  10.     ApiPlatform\Metadata\ApiProperty,
  11.     ApiPlatform\Metadata\Get,
  12.     ApiPlatform\Metadata\GetCollection,
  13.     ApiPlatform\Metadata\Post,
  14.     ApiPlatform\Metadata\Put,
  15.     ApiPlatform\Metadata\Delete,
  16.     ApiPlatform\Metadata\ApiFilter,
  17.     ApiPlatform\Doctrine\Orm\Filter\OrderFilter,
  18.     ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
  19. use App\Entity\Trait\IdTrait,
  20.     App\Entity\Trait\UuidTrait,
  21.     App\Entity\Trait\OrdStatTrait,
  22.     App\Entity\Trait\TimestampableTrait,
  23.     App\Entity\Trait\TranslatableTrait,
  24.     App\Entity\Trait\ArchivedTrait,
  25.     App\Entity\Trait\Languages\LandingLanguagesTrait,
  26.     App\Entity\Interface\TranslatableInterface,
  27.     App\Entity\Interface\OrdStatableInterface,
  28.     App\Entity\Interface\LanguageableInterface,
  29.     App\Entity\Interface\CloneableInterface,
  30.     App\Entity\Interface\ArchivableInterface,
  31.     App\Repository\LandingRepository,
  32.     App\Util\ClassUtils,
  33.     App\Lib\Actions,
  34.     App\DTO\CloneDTO,
  35.     App\StateProcessor\CloneProcessor,
  36.     App\Security\Voter\CloneVoter,
  37.     App\Security\Voter\ArchivableVoter,
  38.     App\Enum\Language;
  39. #[ApiResource(
  40.     description'Landings',
  41.     normalizationContext: ['groups' => [
  42.         'read',
  43.         'read:' self::class,
  44.         'read:' self::class . 'Translation',
  45.         'read:' AbstractThumb::class
  46.     ]],
  47.     denormalizationContext: ['groups' => ['write']],
  48.     security'is_granted("' Landing::class . '")',
  49.     order: ['ord' => 'desc'],
  50.     operations: [
  51.         new GetCollection(),
  52.         new Post(
  53.             uriTemplate'/landings/clone',
  54.             inputCloneDTO::class,
  55.             processorCloneProcessor::class,
  56.             security'is_granted("' Actions::CLONE .'")',
  57.             securityMessageCloneVoter::MESSAGE
  58.         ),
  59.         new Post(denormalizationContext: ['groups' => ['write',  'post']]),
  60.         new Get(),
  61.         new Put(),
  62.         new Delete(
  63.             securityPostDenormalize'is_granted("' Actions::DELETE '", object)',
  64.             securityPostDenormalizeMessageArchivableVoter::MESSAGE
  65.         ),
  66.     ],
  67.     extraProperties: ['standard_put' => false],
  68. )]
  69. #[ApiFilter(BooleanFilter::class, properties: ['isArchived''stat'])]
  70. #[ApiFilter(OrderFilter::class, properties: [
  71.     'workingTitle',
  72.     'nativeTitle',
  73.     'nativeLanguage',
  74.     'updatedAt',
  75.     'stat',
  76.     'ord'
  77. ])]
  78. #[ORM\Entity(repositoryClassLandingRepository::class)]
  79. class Landing implements
  80.     TranslatableInterface,
  81.     OrdStatableInterface,
  82.     LanguageableInterface,
  83.     ArchivableInterface,
  84.     CloneableInterface
  85. {
  86.     use IdTrait,
  87.         UuidTrait,
  88.         OrdStatTrait,
  89.         TimestampableTrait,
  90.         TranslatableTrait,
  91.         LandingLanguagesTrait,
  92.         ArchivedTrait;
  93.     #[ApiProperty(description'Working title')]
  94.     #[Groups(['read''write'])]
  95.     #[Assert\NotBlank]
  96.     #[ORM\Column(typeTypes::STRINGlength511)]
  97.     protected string $workingTitle;
  98.     #[ApiProperty(description'Subdomain')]
  99.     #[Groups(['read''write'])]
  100.     #[Assert\NotNull]
  101.     #[Assert\Regex(
  102.         pattern'/^[a-zA-Z0-9](?:[a-zA-Z0-9-_]{0,61}[a-zA-Z0-9])?$/',
  103.         message'Invalid subdomain. Only english letters, numbers and middle or lower dash are allowed.'
  104.     )]
  105.     #[ORM\Column(typeTypes::STRINGlength32uniquetrue)]
  106.     private string $subdomain;
  107.     #[ApiProperty(description'Per page')]
  108.     #[Groups(['read:' self::class, 'write'])]
  109.     #[Assert\Range(min1)]
  110.     #[ORM\Column(typeTypes::INTEGERoptions: ['default' => 32])]
  111.     private int $perPage 32;
  112.     #[ApiProperty(description'Journals')]
  113.     #[ORM\OneToMany(targetEntityLandingJournal::class, mappedBy'parent'cascade: ['persist''remove'])]
  114.     #[ORM\OrderBy(['ord' => 'desc'])]
  115.     private Collection $journals;
  116.     #[ApiProperty(description'Slides')]
  117.     #[ORM\OneToMany(targetEntityLandingSlide::class, mappedBy'parent'cascade: ['persist''remove'])]
  118.     #[ORM\OrderBy(['ord' => 'desc'])]
  119.     private Collection $slides;
  120.     #[ApiProperty(description'Pages')]
  121.     #[ORM\OneToMany(targetEntityLandingPage::class, mappedBy'parent'cascade: ['persist''remove'])]
  122.     #[ORM\OrderBy(['ord' => 'desc'])]
  123.     private Collection $pages;
  124.     #[ApiProperty(description'Menu items')]
  125.     #[ORM\OneToMany(targetEntityLandingMenuItem::class, mappedBy'parent'cascade: ['persist''remove'])]
  126.     #[ORM\OrderBy(['ord' => 'desc'])]
  127.     private Collection $menuItems;
  128.     #[ApiProperty(description'Menu items')]
  129.     #[ORM\OneToMany(targetEntityLandingFooterItem::class, mappedBy'parent'cascade: ['persist''remove'])]
  130.     #[ORM\OrderBy(['ord' => 'desc'])]
  131.     private Collection $footerItems;
  132.     public function __construct(?array $languages)
  133.     {
  134.         $this->setUuid();
  135.         $this->translations = new ArrayCollection();
  136.         $this->journals = new ArrayCollection();
  137.         $this->slides = new ArrayCollection();
  138.         $this->pages = new ArrayCollection();
  139.         $this->menuItems = new ArrayCollection();
  140.         $this->footerItems = new ArrayCollection();
  141.         $this->setLanguages($languages ?? Language::DEFAULT_LANDING);
  142.         new LandingPage(
  143.             parent$this,
  144.             workingTitle'Strona główna',
  145.             idName'home',
  146.             ord1,
  147.             stattrue
  148.         );
  149.         $this->createdAt = new \DateTimeImmutable();
  150.         $this->updatedAt = new \DateTimeImmutable();
  151.     }
  152.     public function getWorkingTitle(): string
  153.     {
  154.         return $this->workingTitle;
  155.     }
  156.     public function setWorkingTitle(string $workingTitle): self
  157.     {
  158.         $this->workingTitle $workingTitle;
  159.         return $this;
  160.     }
  161.     public function getSubdomain(): string
  162.     {
  163.         return $this->subdomain;
  164.     }
  165.     public function setSubdomain(string $subdomain): self
  166.     {
  167.         $this->subdomain $subdomain;
  168.         return $this;
  169.     }
  170.     public function getPerPage(): int
  171.     {
  172.         return $this->perPage;
  173.     }
  174.     public function setPerPage(int $perPage): self
  175.     {
  176.         $this->perPage $perPage;
  177.         return $this;
  178.     }
  179.     /**
  180.      * @return Collection|LandingJournal[]
  181.      */
  182.     public function getJournals(): Collection
  183.     {
  184.         return $this->journals;
  185.     }
  186.     public function addJournal(LandingJournal $journal): self
  187.     {
  188.         if ($this->journals->contains($journal)) {
  189.             return $this;
  190.         }
  191.         $this->journals[] = $journal;
  192.         return $this;
  193.     }
  194.     public function removeJournal(LandingJournal $journal): self
  195.     {
  196.         $this->journals->removeElement($journal);
  197.         return $this;
  198.     }
  199.     public function resetJournals(): self
  200.     {
  201.         $this->journals = new ArrayCollection();
  202.         return $this;
  203.     }
  204.     #[Groups(['read:' self::class])]
  205.     public function getJournalsCount(): int
  206.     {
  207.         return $this->journals->count();
  208.     }
  209.     /**
  210.      * @return Collection|LandingSlide[]
  211.      */
  212.     public function getSlides(): Collection
  213.     {
  214.         return $this->slides;
  215.     }
  216.     public function addSlide(LandingSlide $slide): self
  217.     {
  218.         if ($this->slides->contains($slide)) {
  219.             return $this;
  220.         }
  221.         $this->slides[] = $slide;
  222.         return $this;
  223.     }
  224.     public function removeSlide(LandingSlide $slide): self
  225.     {
  226.         $this->slides->removeElement($slide);
  227.         return $this;
  228.     }
  229.     public function resetSlides(): self
  230.     {
  231.         $this->slides = new ArrayCollection();
  232.         return $this;
  233.     }
  234.     /**
  235.      * @return Collection|LandingPage[]
  236.      */
  237.     public function getPages(): Collection
  238.     {
  239.         return $this->pages;
  240.     }
  241.     public function addPage(LandingPage $page): self
  242.     {
  243.         if ($this->pages->contains($page)) {
  244.             return $this;
  245.         }
  246.         $this->pages[] = $page;
  247.         return $this;
  248.     }
  249.     public function removePage(LandingPage $page): self
  250.     {
  251.         $this->pages->removeElement($page);
  252.         return $this;
  253.     }
  254.     public function resetPages(): self
  255.     {
  256.         $this->pages = new ArrayCollection();
  257.         return $this;
  258.     }
  259.     /**
  260.      * @return Collection|LandingMenuItem[]
  261.      */
  262.     public function getMenuItems(): Collection
  263.     {
  264.         return $this->menuItems;
  265.     }
  266.     public function addMenuItem(LandingMenuItem $menuItem): self
  267.     {
  268.         if ($this->menuItems->contains($menuItem)) {
  269.             return $this;
  270.         }
  271.         $this->menuItems[] = $menuItem;
  272.         return $this;
  273.     }
  274.     public function removeMenuItem(LandingMenuItem $menuItem): self
  275.     {
  276.         $this->menuItems->removeElement($menuItem);
  277.         return $this;
  278.     }
  279.     public function resetMenuItems(): self
  280.     {
  281.         $this->menuItems = new ArrayCollection();
  282.         return $this;
  283.     }
  284.     /**
  285.      * @return Collection|LandingFooterItem[]
  286.      */
  287.     public function getFooterItems(): Collection
  288.     {
  289.         return $this->footerItems;
  290.     }
  291.     public function addFooterItem(LandingFooterItem $footerItem): self
  292.     {
  293.         if ($this->footerItems->contains($footerItem)) {
  294.             return $this;
  295.         }
  296.         $this->footerItems[] = $footerItem;
  297.         return $this;
  298.     }
  299.     public function removeFooterItem(LandingFooterItem $footerItem): self
  300.     {
  301.         $this->footerItems->removeElement($footerItem);
  302.         return $this;
  303.     }
  304.     public function resetFooterItems(): self
  305.     {
  306.         $this->footerItems = new ArrayCollection();
  307.         return $this;
  308.     }
  309.     public function clone(): self
  310.     {
  311.         $clone = clone $this;
  312.         $clone->id null;
  313.         $clone->setUuid();
  314.         $clone->ord 0;
  315.         $clone->stat false;
  316.         $nativeLanguage $clone->getNativeLanguage();
  317.         $nativeTranslation $clone->getTranslation($nativeLanguage);
  318.         $nativeTranslation->setTitle($nativeTranslation->getTitle() . ' [KOPIA]');
  319.         ClassUtils::cloneCollection($this$clone'translations');
  320.         $clone
  321.             ->resetJournals()
  322.             ->resetSlides()
  323.             ->resetPages()
  324.             ->resetMenuItems()
  325.             ->resetFooterItems();
  326.         $clone->createdAt = new \DateTimeImmutable();
  327.         $clone->updatedAt = new \DateTimeImmutable();
  328.         return $clone;
  329.     }
  330. }