<?php
namespace Roothirsch\PimBundle\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use App\Api\ReportInjectOperation;
use App\Api\UpdatePositionContainerSorting;
use App\Filter\NotFilter;
use Roothirsch\CoreBundle\Behaviors\Attributable\Attribute\AttributeInterface;
use Roothirsch\CoreBundle\Behaviors\Attributable\AttributeValue\AttributeValueAwareInterface;
use Roothirsch\CoreBundle\Behaviors\Attributable\AttributeValue\AttributeValueInterface;
use Roothirsch\CoreBundle\Behaviors\Attributable\MappedSuperclass\AbstractAttributable;
use Roothirsch\CoreBundle\Behaviors\Translatable\TranslatableInterface;
use Roothirsch\CoreBundle\Entity\Group;
use Roothirsch\PimBundle\Repository\ProductRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\Ignore;
use Symfony\Component\Serializer\Annotation\SerializedName;
/**
* @ApiResource(
* normalizationContext={"groups"={"list"}, "enable_max_depth"=true},
* denormalizationContext={"groups"={"list"}},
* shortName="Pim/Product"
* )
* @ApiFilter(SearchFilter::class, properties={
* "name": "partial",
* "categories.title": "partial",
* "categories.id": "exact"
* })
* @ORM\Entity(repositoryClass=ProductRepository::class)
* @ORM\Table(name="pim_product"))
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="type", type="string")
*/
class Product extends AbstractAttributable implements TranslatableInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"list"})
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
* @Gedmo\Translatable
* @Groups({"list"})
*/
protected $title;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"list"})
*/
protected $name;
/**
* @ORM\Column(type="string", length=1024, nullable=true)
* @Gedmo\Translatable
* @Groups({"list"})
*/
protected $description;
/**
* @ORM\Column(type="string", nullable=true)
* @Groups({"list"})
*/
protected $preview;
/**
* @ORM\Column(type="string", nullable=true)
*/
protected $cartDisplayMode = 'product';
/**
* @ORM\OneToMany(targetEntity=Article::class, mappedBy="product", orphanRemoval=true)
* @Groups({"list"})
*/
protected $articles;
/**
* @ORM\ManyToMany(targetEntity=Category::class, inversedBy="products")
* @ORM\JoinTable(name="pim_product_categories")
* @Groups({"list"})
*/
protected $categories;
/**
* @ORM\OneToMany(targetEntity=AttributeValue::class, mappedBy="product", orphanRemoval=true, cascade={"persist"})
*/
protected $attributeValues;
/**
* @ORM\ManyToMany(targetEntity=Group::class)
* @ORM\JoinTable(name="pim_product_groups")
* @Groups({"list"})
*/
protected $groups;
public function __construct()
{
$this->attributeValues = new ArrayCollection();
$this->articles = new ArrayCollection();
$this->categories = new ArrayCollection();
$this->groups = new ArrayCollection();
}
public function attributes() {
$attributes = new ArrayCollection();
foreach($this->categories as $category) {
/** @var Category $category */
foreach($category->getProductAttributeGroups() as $attributeGroup) {
foreach($attributeGroup->getAttributes() as $attribute) {
$attributes->add($attribute);
}
}
}
return $attributes;
}
public function newValue(AttributeInterface $attribute): AttributeValueInterface
{
return new AttributeValue($attribute);
}
public function getAttributeValues(): Collection
{
return $this->attributeValues;
}
public function setAttributeValues(Collection $collection): AttributeValueAwareInterface
{
$this->attributeValues = $collection;
return $this;
}
public function __toString()
{
return strval($this->title);
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
/**
* @return Collection|Article[]
*/
public function getArticles(): Collection
{
return $this->articles;
}
public function addArticle(Article $article): self
{
if (!$this->articles->contains($article)) {
$this->articles[] = $article;
$article->setProduct($this);
}
return $this;
}
public function removeArticle(Article $article): self
{
if ($this->articles->removeElement($article)) {
// set the owning side to null (unless already changed)
if ($article->getProduct() === $this) {
$article->setProduct(null);
}
}
return $this;
}
/**
* @return Collection|Attribute[]
*/
public function getAttributes(): Collection
{
return $this->attributes;
}
public function addAttribute(Attribute $attribute): self
{
if (!$this->attributes->contains($attribute)) {
$this->attributes[] = $attribute;
}
return $this;
}
public function removeAttribute(Attribute $attribute): self
{
$this->attributes->removeElement($attribute);
return $this;
}
/**
* @return Collection|Category[]
*/
public function getCategories(): Collection
{
return $this->categories;
}
public function addCategory(Category $category): self
{
if (!$this->categories->contains($category)) {
$this->categories[] = $category;
}
return $this;
}
public function removeCategory(Category $category): self
{
$this->categories->removeElement($category);
return $this;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name): void
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getPreview()
{
return $this->preview;
}
/**
* @param mixed $preview
*/
public function setPreview($preview): void
{
$this->preview = $preview;
}
/**
* @return ArrayCollection
*/
public function getGroups()
{
return $this->groups;
}
public function hasGroup($groupName)
{
return array_search($groupName, $this->getGroups()) !== false;
}
public function addGroup(Group $group): self
{
if (!$this->groups->contains($group)) {
$this->groups[] = $group;
}
return $this;
}
public function removeGroup(Group $group): self
{
$this->groups->removeElement($group);
return $this;
}
/**
* {@inheritdoc}
*/
public function getRoles()
{
$roles = [];
foreach ($this->groups as $group) {
$roles[] = $group->getName();
}
return $roles;
}
/**
* @return mixed
*/
public function getDescription()
{
return $this->description;
}
/**
* @param mixed $description
*/
public function setDescription($description): void
{
$this->description = $description;
}
/**
* Get the value of cartDisplayMode
*/
public function getCartDisplayMode()
{
return $this->cartDisplayMode;
}
/**
* Set the value of cartDisplayMode
*
* @return self
*/
public function setCartDisplayMode($cartDisplayMode)
{
$this->cartDisplayMode = $cartDisplayMode;
return $this;
}
public function getArticleByName($name): ?Article {
foreach($this->articles as $article) {
if ($article->getName() == $name) {
return $article;
}
}
return null;
}
public function getTranslatableFields(): Collection
{
return $this->attributes();
}
}