<?php
namespace App\Entity;
use App\Service\FileHandlerService;
use App\Repository\ProductRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Serializer\Annotation\SerializedName;
use App\Annotation\DashboardAnnotation as Dashboard;
use App\Annotation\FormBuildAnnotation as FormBuild;
/**
* @ORM\Entity(repositoryClass=ProductRepository::class)
* @Dashboard(generation=true, name="Продукция")
* @ORM\Table(indexes={
* @ORM\Index(name="product_title_idx", columns={"title"})
* })
*/
class Product
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Serializer\Groups({"collection", "selection/data", "selection/id"})
* @Dashboard(label="Номер", only=true)
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Serializer\Groups({"collection", "selection/data", "selection/id"})
* @Dashboard(label="Название продукции")
* @FormBuild(component="input")
*/
private $title;
/**
* @ORM\ManyToOne(targetEntity=Type::class, inversedBy="products")
* @ORM\JoinColumn(nullable=true)
* @Serializer\Groups({"collection", "selection/data", "selection/id"})
* @Dashboard(label="Тип")
* @FormBuild(component="autocomplete", bindField="type", field="title", params={"nullable": true})
*/
private $type;
/**
* @ORM\ManyToOne(targetEntity=Season::class, inversedBy="products")
* @ORM\JoinColumn(nullable=true)
* @Serializer\Groups({"collection", "selection/data", "selection/id"})
* @Dashboard(label="Сезон")
* @FormBuild(component="autocomplete", bindField="season", field="title", params={"nullable": true})
*/
private $season;
/**
* @ORM\ManyToOne(targetEntity=Category::class, inversedBy="products")
* @ORM\JoinColumn(nullable=true)
* @Serializer\Groups({"collection", "selection/id"})
* @Dashboard(label="Категория")
* @FormBuild(component="autocomplete", bindField="category", field="title", params={"nullable": true})
*/
private $category;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @FormBuild(component="files", multiple=false, accept="image/*")
* @Dashboard(label="Превью", only=true, head={"type":"image", "value":"file.path"})
*/
public $file;
/**
* @Serializer\Groups({"collection", "selection/data", "selection/id"})
*/
public function getFile() {
$fileHandler = new FileHandlerService();
if (!$this->file)
return null;
return [
'name' => $this->file,
'path' => '/'.$fileHandler->getGlobalPath().'/product/'.$this->file
];
}
/**
* @ORM\Column(type="boolean")
* @Serializer\Groups({"collection"})
* @FormBuild(component="switch")
* @Dashboard(label="Скрыть на сайте", only=true, inverse=true)
*/
private $public = true;
/**
* @Serializer\Groups({"collection", "selection/data"})
* @SerializedName("articles")
* @ORM\OneToMany(targetEntity=ProductArticle::class, mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\OrderBy({"title" = "ASC"})
*/
private $productArticles;
/**
* @ORM\OneToMany(targetEntity=ProductOem::class, mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $productOems;
public function __construct()
{
$this->productArticles = new ArrayCollection();
$this->selections = new ArrayCollection();
$this->productOems = new ArrayCollection();
}
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;
}
public function isPublic(): ?bool
{
return $this->public;
}
public function setPublic(bool $public): self
{
$this->public = $public;
return $this;
}
public function getType(): ?Type
{
return $this->type;
}
public function setType(?Type $type): self
{
$this->type = $type;
return $this;
}
public function getSeason(): ?Season
{
return $this->season;
}
public function setSeason(?Season $season): self
{
$this->season = $season;
return $this;
}
public function getFileName(): ?string
{
return $this->file;
}
public function setFile(?string $file): self
{
$this->file = $file;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
/**
* @return Collection<int, ProductArticle>
*/
public function getProductArticles(): Collection
{
return $this->productArticles;
}
public function addProductArticle(ProductArticle $productArticle): self
{
if (!$this->productArticles->contains($productArticle)) {
$this->productArticles[] = $productArticle;
$productArticle->setProduct($this);
}
return $this;
}
public function removeProductArticle(ProductArticle $productArticle): self
{
if ($this->productArticles->removeElement($productArticle)) {
// set the owning side to null (unless already changed)
if ($productArticle->getProduct() === $this) {
$productArticle->setProduct(null);
}
}
return $this;
}
public function clearArticles():self
{
$this->productArticles = new ArrayCollection();
return $this;
}
/**
* @return Collection<int, ProductOem>
*/
public function getProductOems(): Collection
{
return $this->productOems;
}
public function addProductOem(ProductOem $productOem): self
{
if (!$this->productOems->contains($productOem)) {
$this->productOems[] = $productOem;
$productOem->setProduct($this);
}
return $this;
}
public function getProductOemsAsArray(): array
{
$result = [];
foreach($this->getProductOems() as $oem) {
$result[] = $oem->getOem();
}
return $result;
}
public function removeProductOem(ProductOem $productOem): self
{
if ($this->productOems->removeElement($productOem)) {
// set the owning side to null (unless already changed)
if ($productOem->getProduct() === $this) {
$productOem->setProduct(null);
}
}
return $this;
}
public function clearOems(): self
{
$this->productOems = new ArrayCollection();
return $this;
}
public function clearArticlesAndOems()
{
return $this->clearOems()->clearArticles();
}
/**
* @Serializer\Groups({"selection/id"})
* @SerializedName("article")
*/
public function getOneProductArticle(): ?ProductArticle
{
foreach($this->getProductArticles() as $article) {
return $article;
}
return null;
}
}