<?php
namespace App\Entity\PointsCatalog;
use App\Annotation\SiteAware;
use App\Entity\AbstractBase;
use App\Entity\Interfaces\SiteInterface;
use App\Entity\MiniAbstractBase;
use App\Entity\Traits\DescriptionTrait;
use App\Entity\Traits\HasImageTrait;
use App\Entity\Traits\NameTrait;
use App\Entity\Traits\OnlyAdminTrait;
use App\Entity\Traits\PublishedTrait;
use App\Entity\Traits\SiteTrait;
use App\Entity\User;
use App\Enum\UserRolesEnum;
use App\Persistence\Model\UserGroupAccessType;
use App\Repository\PointsCatalog\CatalogGiftRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Money\Money;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Table(name="vulco_points_catalog_gift", indexes={@ORM\Index(name="catalog_gift_site_idx", columns={"site"})})
*
* @ORM\Entity(repositoryClass=CatalogGiftRepository::class)
*
* @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
*
* @SiteAware(siteFieldName="site")
*
* @Vich\Uploadable
*/
class CatalogGift extends AbstractBase implements SiteInterface
{
use DescriptionTrait;
use HasImageTrait;
use NameTrait;
use OnlyAdminTrait;
use PublishedTrait;
use SiteTrait;
/**
* @Vich\UploadableField(mapping="catalog_gift_image", fileNameProperty="imageName")
*/
private ?File $image = null;
/**
* @ORM\Column(type="string", length=255)
*
* @Assert\NotBlank()
*/
private string $name;
/**
* @ORM\Column(type="text", length=10000, nullable=true)
*/
private ?string $description = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $homepageName = null;
/**
* @ORM\Column(type="string", length=255)
*
* @Assert\NotBlank()
*/
private string $code;
/**
* @ORM\Column(type="integer")
*
* @Assert\NotBlank()
*/
private int $pointsValue;
/**
* @ORM\Column(type="boolean")
*/
private bool $showInHomepage;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private ?bool $showInHomepageClub = null;
/**
* @ORM\Column(type="boolean", options={"default": 0})
*/
private bool $onlyAdminVisible = false;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\PointsCatalog\CatalogGiftCategory", fetch="EAGER")
*
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*
* @Assert\NotBlank()
*/
private ?CatalogGiftCategory $category;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\PointsCatalog\CatalogSizeGroup")
*
* @ORM\JoinColumn(name="size_group_id", referencedColumnName="id")
*/
private ?CatalogSizeGroup $sizeGroup;
/**
* @ORM\OneToMany(targetEntity="App\Entity\PointsCatalog\CatalogGiftHasProvider", mappedBy="gift", cascade={"persist"}, orphanRemoval=true)
*
* @Assert\Valid()
*
* @Assert\Count(min=1, max=1)
*/
private ?Collection $providers;
public function __construct()
{
$this->providers = new ArrayCollection();
}
public function getCategory(): ?CatalogGiftCategory
{
return $this->category;
}
public function setCategory(CatalogGiftCategory $category): self
{
$this->category = $category;
return $this;
}
public function getNameAndSizeGroupString(): string
{
return $this->getName().($this->getSizeGroup() ? ' ('.$this->getSizeGroup()->getName().')' : '');
}
public function getSizeGroup(): ?CatalogSizeGroup
{
return $this->sizeGroup;
}
public function setSizeGroup(CatalogSizeGroup $sizeGroup): self
{
$this->sizeGroup = $sizeGroup;
return $this;
}
public function getHomepageName(): ?string
{
return $this->homepageName;
}
public function setHomepageName(?string $homepageName): self
{
$this->homepageName = $homepageName;
return $this;
}
public function getCode(): string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function getPointsValue(): int
{
return $this->pointsValue;
}
public function getPointsValueString(): string
{
return MiniAbstractBase::getIntegerAsString($this->pointsValue);
}
public function setPointsValue(int $pointsValue): self
{
$this->pointsValue = $pointsValue;
return $this;
}
public function isShowInHomepage(): bool
{
return $this->showInHomepage;
}
public function getShowInHomepage(): bool
{
return $this->isShowInHomepage();
}
public function setShowInHomepage(bool $showInHomepage): self
{
$this->showInHomepage = $showInHomepage;
return $this;
}
public function isShowInHomepageClub(): ?bool
{
return $this->showInHomepageClub;
}
public function setShowInHomepageClub(?bool $showInHomepageClub): self
{
$this->showInHomepageClub = $showInHomepageClub;
return $this;
}
public function getProviders(): ?Collection
{
return $this->providers;
}
public function setProviders(?Collection $providers): self
{
$this->providers = $providers;
return $this;
}
public function addProvider(CatalogGiftHasProvider $provider): self
{
if (!$this->providers->contains($provider)) {
$provider->setGift($this);
$this->providers->add($provider);
}
return $this;
}
public function removeProvider(CatalogGiftHasProvider $provider): self
{
$this->providers->removeElement($provider);
return $this;
}
public function firstProvider(): ?CatalogGiftProvider
{
return $this->getProviders() && $this->getProviders()->isEmpty() ? null : $this->getProviders()->first()->getProvider();
}
public function price(): ?Money
{
return $this->getProviders() && $this->getProviders()->first() ? $this->getProviders()->first()->getPrice() : null;
}
public function userHasAccess(UserInterface $user): bool
{
/** @var User $user */
$category = $this->getCategory();
// coordinator must be the first condition to evaluate because coordinators have the associated role as well
if ($user->hasRole(UserRolesEnum::ROLE_COORDINATOR_LONG)) {
switch ($category->getUserAccessType()) {
case UserGroupAccessType::ENABLE:
return $user->belongsToUsers($category->getUsers());
case UserGroupAccessType::DISABLE:
return !$user->belongsToUsers($category->getUsers());
case UserGroupAccessType::IGNORE:
default:
return true;
}
} elseif ($user->hasRole(UserRolesEnum::ROLE_ASSOCIATED_LONG)) {
switch ($category->getUserGroupAccessType()) {
case UserGroupAccessType::ENABLE:
return $user->belongsToUserGroup($category->getUserGroups());
case UserGroupAccessType::DISABLE:
return !$user->belongsToUserGroup($category->getUserGroups());
case UserGroupAccessType::IGNORE:
default:
return true;
}
} else {
return true;
}
}
public static function userHasAccessClosure(UserInterface $user): \Closure
{
return static function (CatalogGift $catalogGift) use ($user) {
$category = $catalogGift->getCategory();
// coordinator must be the first condition to evaluate because coordinators have the associated role as well
if ($user->hasRole(UserRolesEnum::ROLE_COORDINATOR_LONG)) {
switch ($category->getUserAccessType()) {
case UserGroupAccessType::ENABLE:
return $user->belongsToUsers($category->getUsers());
case UserGroupAccessType::DISABLE:
return !$user->belongsToUsers($category->getUsers());
case UserGroupAccessType::IGNORE:
default:
return true;
}
} elseif ($user->hasRole(UserRolesEnum::ROLE_ASSOCIATED_LONG)) {
switch ($category->getUserGroupAccessType()) {
case UserGroupAccessType::ENABLE:
return $user->belongsToUserGroup($category->getUserGroups());
case UserGroupAccessType::DISABLE:
return !$user->belongsToUserGroup($category->getUserGroups());
case UserGroupAccessType::IGNORE:
default:
return true;
}
} else {
return true;
}
};
}
public static function getCatalogGiftsUserHasAccess(UserInterface $user, iterable $catalogGifts): iterable
{
$catalogGiftsUserHasAccess = [];
if (!empty($catalogGifts)) {
// get only catalogGifts that current user has access
/** @var CatalogGift $catalogGift */
foreach ($catalogGifts as $catalogGift) {
if ($catalogGift->userHasAccess($user)) {
$catalogGiftsUserHasAccess[] = $catalogGift;
}
}
}
return $catalogGiftsUserHasAccess;
}
public function __toString(): string
{
return $this->id ? $this->name : MiniAbstractBase::DEFAULT_EMPTY_STRING;
}
}