<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\NavRepository;
use ApiPlatform\Core\Annotation\ApiFilter;
use App\Entity\Traits\TimestampableEntity;
use Doctrine\Common\Collections\Collection;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Interfaces\OrderMappedInterface;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Interfaces\ContainerMappedInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;
/**
* @ApiResource(
* attributes={"filters"={"translation.groups"}},
* normalizationContext={"groups"={"Nav:Read"}, "skip_null_values"=false},
* denormalizationContext={"groups"={"Nav:Write"}},
* collectionOperations={
* "get"={
* "pagination_use_output_walkers"=true,
* "security"="is_granted('ROLE_OPERATOR')"
* },
* "get_app_nav"={
* "route_name"="api_app_navs_get_app_nav_collection",
* "method"="GET",
* "pagination_enabled"=false,
* "normalization_context"={"groups"={"NavApp:Read"}, "skip_null_values"=false},
* },
* "post"={
* "security"="is_granted('ROLE_OPERATOR')"
* },
* "post_copy_to_app"={
* "route_name"="api_navs_copy_to_app_collection",
* "method"="POST",
* "denormalization_context"={"groups"={"Nav:CopyToApp"}},
* "security"="is_granted('ROLE_OPERATOR')"
* },
* },
* itemOperations={
* "get"={"security"="is_granted('IS_CO_OPR', object)"},
* "put"={"security"="is_granted('IS_CO_OPR', object)"},
* "patch"={"security"="is_granted('IS_CO_OPR', object)"},
* "patch_change_ord"={
* "path"="/navs/{id}/change-ord",
* "method"="PATCH",
* "denormalization_context"={"groups"={"Nav:ChangeOrd"}},
* "security"="is_granted('IS_CO_OPR', object)"
* },
* "delete"={"security"="is_granted('IS_CO_OPR', object)"}
* }
* )
* @ApiFilter(SearchFilter::class, properties={"id": "exact", "container.id": "exact", "parent.id": "exact", "type": "exact", "url": "partial", "translations.title": "partial"})
* @ApiFilter(BooleanFilter::class, properties={"isActive", "isWeb", "isApp"})
* @ApiFilter(OrderFilter::class, properties={"ord": "ASC", "translations.title", "id"})
* @ORM\Entity(repositoryClass=NavRepository::class)
*/
class Nav extends AbstractTranslatable implements ContainerMappedInterface, OrderMappedInterface
{
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
/**
* @ORM\OneToMany(targetEntity="NavTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
*
* @Groups({"Nav:Write", "NavTranslationGroup"})
* @Assert\Valid()
*/
protected $translations;
/**
* @Groups({"Nav:Read", "NavApp:Read"})
*/
private $title;
/**
* @Groups({"Nav:Read", "NavApp:Read"})
*/
private $externalUrl;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"Nav:Read", "NavApp:Read"})
*/
private $id;
/**
* @ORM\Column(type="string", length=32)
* @Groups({"Nav:Read", "Nav:Write", "NavApp:Read"})
*/
private $type;
public const TYPE_INTERNAL = "INTERNAL";
public const TYPE_INFO_PAGE = "INFO_PAGE";
public const TYPE_EXTERNAL = "EXTERNAL";
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"Nav:Read", "Nav:Write", "NavApp:Read"})
*/
private $url;
/**
* @ORM\Column(type="string", length=32, nullable=true)
* @Groups({"Nav:Read", "Nav:Write", "NavApp:Read"})
*/
private $icon;
/**
* @ORM\Column(type="array", nullable=true)
* @Groups({"Nav:Read", "Nav:Write"})
*/
private $roles = [];
/**
* @ORM\Column(type="boolean", nullable=true)
* @Groups({"Nav:Read", "Nav:Write", "NavApp:Read"})
*/
private $isOptionInNewWindow;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Groups({"Nav:Read", "Nav:Write", "NavApp:Read"})
*/
private $isActive;
/**
* @ORM\ManyToOne(targetEntity=Container::class)
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
* @Groups({"Nav:Write"})
*/
private $container;
/**
* @ORM\ManyToOne(targetEntity=Nav::class, inversedBy="children")
* @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
* @ApiProperty(readableLink=false, writableLink=false)
* @Groups({"Nav:Read", "Nav:Write"})
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity=Nav::class, mappedBy="parent")
* @Groups({"Nav:Read", "NavApp:Read"})
*/
private $children;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups({"Nav:Read", "Nav:Write", "NavApp:Read"})
*/
private $ord;
/**
* @Groups({"Nav:ChangeOrd"})
*/
private int $newOrd;
private $ordChangeDirection;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Groups({"Nav:Read", "Nav:Write", "NavApp:Read"})
*/
private $isWeb;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Groups({"Nav:Read", "Nav:Write", "NavApp:Read"})
*/
private $isApp;
/**
* @ORM\ManyToMany(targetEntity=UserGroup::class, inversedBy="navs")
* @Groups({"Nav:Read", "Nav:Write", "NavApp:Read"})
*/
private $userGroups;
/**
* @ORM\Column(type="string", length=32, nullable=true)
* @Groups({"Nav:Read", "Nav:Write", "NavApp:Read"})
*/
private $newsfeedPostType;
/**
* @ORM\ManyToMany(targetEntity=CommonCategory::class)
* @Groups({"Nav:Read", "Nav:Write", "NavApp:Read"})
*/
private $newsfeedCategories;
/**
* @ORM\ManyToMany(targetEntity=NewsfeedTag::class)
* @Groups({"Nav:Read", "Nav:Write", "NavApp:Read"})
*/
private $newsfeedTags;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Groups({"Nav:Read", "Nav:Write", "NavApp:Read"})
*/
private $isExcludeCategory;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Groups({"Nav:Read", "Nav:Write", "NavApp:Read"})
*/
private $isExcludeTag;
/**
* @ORM\Column(type="array", nullable=true)
* @Groups({"Nav:Read", "Nav:Write", "NavApp:Read"})
*/
private $excludeNewsfeedCategories = [];
/**
* @ORM\Column(type="array", nullable=true)
* @Groups({"Nav:Read", "Nav:Write", "NavApp:Read"})
*/
private $excludeNewsfeedTags = [];
/**
* @Groups({"Nav:CopyToApp"})
*/
private int $copyId;
protected function createTranslation(): TranslationInterface
{
return new NavTranslation();
}
public function getTitle(): ?string
{
return $this->getTranslation()->getTitle();
}
public function setTitle(string $title): self
{
$this->getTranslation()->setTitle($title);
return $this;
}
public function getExternalUrl(): ?string
{
return $this->getTranslation()->getExternalUrl();
}
public function setExternalUrl(string $externalUrl): self
{
$this->getTranslation()->setExternalUrl($externalUrl);
return $this;
}
public function __construct()
{
parent::__construct();
$this->children = new ArrayCollection();
$this->userGroups = new ArrayCollection();
$this->newsfeedCategories = new ArrayCollection();
$this->newsfeedTags = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
public function getUrl(): ?string
{
return $this->url;
}
public function setUrl(?string $url): self
{
$this->url = $url;
return $this;
}
public function getIcon(): ?string
{
return $this->icon;
}
public function setIcon(?string $icon): self
{
$this->icon = $icon;
return $this;
}
public function getRoles(): ?array
{
return $this->roles;
}
public function setRoles(?array $roles): self
{
$this->roles = $roles;
return $this;
}
public function getIsOptionInNewWindow(): ?bool
{
return $this->isOptionInNewWindow;
}
public function setIsOptionInNewWindow(?bool $isOptionInNewWindow): self
{
$this->isOptionInNewWindow = $isOptionInNewWindow;
return $this;
}
public function getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(?bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getContainer(): ?Container
{
return $this->container;
}
public function setContainer(?Container $container): self
{
$this->container = $container;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection<int, self>
*/
public function getChildren(): Collection
{
return $this->children;
}
public function addChild(self $child): self
{
if (!$this->children->contains($child)) {
$this->children[] = $child;
$child->setParent($this);
}
return $this;
}
public function removeChild(self $child): self
{
if ($this->children->removeElement($child)) {
// set the owning side to null (unless already changed)
if ($child->getParent() === $this) {
$child->setParent(null);
}
}
return $this;
}
public function getOrd(): ?int
{
return $this->ord;
}
public function setOrd(?int $ord): self
{
$this->ord = $ord;
return $this;
}
public function getOrdParents(): array
{
return [
'container' => $this->getContainer(),
'parent' => $this->getParent(),
'isWeb' => $this->getIsWeb()
];
}
public function getOrdChangeDirection()
{
return $this->ordChangeDirection;
}
public function getNewOrd(): int
{
return $this->newOrd ?? 0;
}
public function setNewOrd(int $newOrd): self
{
$oldOrd = $this->ord;
$this->newOrd = $newOrd;
$this->ord = $newOrd;
$this->ordChangeDirection = ($this->newOrd < $oldOrd) ? 'desc' : 'asc';
return $this;
}
public function getIsWeb(): ?bool
{
return $this->isWeb;
}
public function setIsWeb(?bool $isWeb): self
{
if ($isWeb) {
$this->setIsApp(!$isWeb);
}
$this->isWeb = $isWeb;
return $this;
}
public function getIsApp(): ?bool
{
return $this->isApp;
}
public function setIsApp(?bool $isApp): self
{
if ($isApp) {
$this->setIsWeb(!$isApp);
}
$this->isApp = $isApp;
return $this;
}
/**
* @return Collection<int, UserGroup>
*/
public function getUserGroups(): Collection
{
return $this->userGroups;
}
public function addUserGroup(UserGroup $userGroup): self
{
if (!$this->userGroups->contains($userGroup)) {
$this->userGroups[] = $userGroup;
}
return $this;
}
public function removeUserGroup(UserGroup $userGroup): self
{
$this->userGroups->removeElement($userGroup);
return $this;
}
public function removeAllUserGroups(): self
{
foreach ($this->userGroups as $userGroup) {
$this->removeUserGroup($userGroup);
}
return $this;
}
public function getNewsfeedPostType(): ?string
{
return $this->newsfeedPostType;
}
public function setNewsfeedPostType(?string $newsfeedPostType): self
{
$this->newsfeedPostType = $newsfeedPostType;
return $this;
}
/**
* @return Collection<int, CommonCategory>
*/
public function getNewsfeedCategories(): Collection
{
return $this->newsfeedCategories;
}
public function addNewsfeedCategory(CommonCategory $newsfeedCategory): self
{
if (!$this->newsfeedCategories->contains($newsfeedCategory)) {
$this->newsfeedCategories[] = $newsfeedCategory;
}
return $this;
}
public function removeNewsfeedCategory(CommonCategory $newsfeedCategory): self
{
$this->newsfeedCategories->removeElement($newsfeedCategory);
return $this;
}
/**
* @return Collection<int, NewsfeedTag>
*/
public function getNewsfeedTags(): Collection
{
return $this->newsfeedTags;
}
public function addNewsfeedTag(NewsfeedTag $newsfeedTag): self
{
if (!$this->newsfeedTags->contains($newsfeedTag)) {
$this->newsfeedTags[] = $newsfeedTag;
}
return $this;
}
public function removeNewsfeedTag(NewsfeedTag $newsfeedTag): self
{
$this->newsfeedTags->removeElement($newsfeedTag);
return $this;
}
public function getIsExcludeCategory(): ?bool
{
return $this->isExcludeCategory;
}
public function setIsExcludeCategory(?bool $isExcludeCategory): self
{
$this->isExcludeCategory = $isExcludeCategory;
return $this;
}
public function getIsExcludeTag(): ?bool
{
return $this->isExcludeTag;
}
public function setIsExcludeTag(?bool $isExcludeTag): self
{
$this->isExcludeTag = $isExcludeTag;
return $this;
}
public function getExcludeNewsfeedCategories(): ?array
{
return $this->excludeNewsfeedCategories;
}
public function setExcludeNewsfeedCategories(?array $excludeNewsfeedCategories): self
{
$this->excludeNewsfeedCategories = $excludeNewsfeedCategories;
return $this;
}
public function getExcludeNewsfeedTags(): ?array
{
return $this->excludeNewsfeedTags;
}
public function setExcludeNewsfeedTags(?array $excludeNewsfeedTags): self
{
$this->excludeNewsfeedTags = $excludeNewsfeedTags;
return $this;
}
/**
* Get the value of copyId
*/
public function getCopyId()
{
return $this->copyId;
}
/**
* Set the value of copyId
*
* @return self
*/
public function setCopyId($copyId)
{
$this->copyId = $copyId;
return $this;
}
}