<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Criteria;
/**
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
*/
class Category
{
const TYPE_POST_CATEGORY = 1;
const TYPE_CLASSIFIED_GROUP = 2;
const TYPE_CUSTOMER_LEVEL = 3;
const STATUS_ACTIVE = 1;
const STATUS_INACTIVE = 0;
const SITE_RCS = 1;
const SITE_AAR = 2;
const SITE_MCS = 3;
const SITE_CCS = 4;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="bigint")
*/
private $id;
/**
* @ORM\Version @ORM\Column(type="integer")
*/
private $version;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Site", inversedBy="categories")
*/
private $site;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $description;
/**
* @ORM\Column(type="string", length=255)
*/
private $slug;
/**
* @ORM\Column(type="integer")
*/
private $type;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="children")
* @ORM\JoinColumn(nullable=true)
*/
private $prnt;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Category", mappedBy="prnt")
*/
private $children;
/**
* @ORM\Column(type="datetime")
*/
private $modified_at;
/**
* @ORM\Column(type="datetime")
*/
private $created_at;
/**
* @ORM\Column(type="integer")
* ... not needed
*/
private $sort;
/**
* @ORM\Column(type="integer")
*/
private $status;
/**
* @ O R M\OneToMany(targetEntity="App\Entity\Customer", mappedBy="customer_type")
* /
private $customers;
*/
/**
* @ORM\OneToMany(targetEntity="App\Entity\Content", mappedBy="category")
*/
private $content;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Content", mappedBy="secondary_categories", cascade={"detach"})
*/
private $secondary_content;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="category")
*/
private $products;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Product", mappedBy="secondary_categories", cascade={"persist"})
*/
private $secondary_products;
/**
* @ORM\Column(type="string", length=255)
*/
private $taxonomy;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Customer", mappedBy="categories")
*/
private $customers;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Media", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
private $media;
public function __construct ()
{
$this->sort = 0;
$this->status = 1;
$this->type = 0;
$this->description = "";
$this->site = new ArrayCollection();
$this->modified_at = new \DateTime("now");
$this->created_at = new \DateTime("now");
$this->customer_level = new ArrayCollection();
$this->customers = new ArrayCollection();
$this->content = new ArrayCollection();
$this->secondary_content = new ArrayCollection();
$this->products = new ArrayCollection();
$this->secondary_products = new ArrayCollection();
$this->children = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function getVersion() {
return $this->version;
}
/*
public function getSite(): ?int
{
return $this->site;
}
public function setSite(int $site): self
{
$this->site = $site;
return $this;
}
*/
/**
* @return Collection|Site[]
*/
public function getSite(): Collection
{
return $this->site;
}
public function getSites(): Collection
{
return $this->site;
}
public function addSite(Site $site): self
{
if (!$this->site->contains($site)) {
$this->site[] = $site;
$site->addCategory($this);
}
return $this;
}
public function resetSite(): self
{
$this->site = new ArrayCollection();
return $this;
}
public function removeSite(Site $site): self
{
if ($this->site->contains($site)) {
$this->site->removeElement($site);
$site->removeCategory($this);
}
return $this;
}
public function getSiteTextShort()
{
$site_texts = [];
foreach($this->getSite() as $s) {
$site_texts[] = $s->getSiteTextShort();
}
return implode(", ", $site_texts);
}
public function getSiteIds()
{
$site_ids = [];
foreach($this->getSite() as $s) {
$site_ids[] = $s->getId();
}
return $site_ids;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title = null): self
{
$this->title = $title ? $title : "";
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description = null): self
{
$this->description = $description ? $description : "";
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug = null): self
{
$this->slug = $slug ? $slug : "";
return $this;
}
public function getType(): ?int
{
return $this->type;
}
public function setType(int $type = null): self
{
$this->type = $type ? $type : 0;
return $this;
}
public function getPrnt(): ?self
{
return $this->prnt;
}
public function setPrnt(?self $prnt): self
{
$this->prnt = $prnt;
return $this;
}
public function getModifiedAt(): ?\DateTimeInterface
{
return $this->modified_at;
}
public function setModifiedAt(\DateTimeInterface $modified_at): self
{
$this->modified_at = $modified_at;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->created_at;
}
public function setCreatedAt(\DateTimeInterface $created_at): self
{
$this->created_at = $created_at;
return $this;
}
public function getSort(): ?int
{
return $this->sort;
}
public function setSort(int $sort): self
{
$this->sort = $sort;
return $this;
}
public function getStatus(): ?int
{
return $this->status;
}
public function setStatus(int $status): self
{
$this->status = $status;
return $this;
}
/**
* @return Collection|Customer[]
* /
public function getCustomers(): Collection
{
return $this->customers;
}
public function addCustomer(Customer $customer): self
{
if (!$this->customers->contains($customer)) {
$this->customers[] = $customer;
$customer->setCustomerType($this);
}
return $this;
}
public function removeCustomer(Customer $customer): self
{
if ($this->customers->contains($customer)) {
$this->customers->removeElement($customer);
// set the owning side to null (unless already changed)
if ($customer->getCustomerType() === $this) {
$customer->setCustomerType(null);
}
}
return $this;
}
*/
/**
* @return Collection|Content[]
*/
public function getContent(): Collection
{
return $this->content;
}
public function addContent(Content $content): self
{
if (!$this->content->contains($content)) {
$this->content[] = $content;
$content->setCategory($this);
}
return $this;
}
public function removeContent(Content $content): self
{
if ($this->content->contains($content)) {
$this->content->removeElement($content);
// set the owning side to null (unless already changed)
if ($content->getCategory() === $this) {
$content->setCategory(null);
}
}
return $this;
}
public function getSecondaryContent (): Collection
{
return $this->secondary_content;
}
public function addSecondaryContent (Content $content)
{
if (!$this->secondary_content->contains($content)) {
$this->secondary_content[] = $content;
$content->addSecondaryCategory($this);
}
return $this;
}
public function removeSecondaryContent (Content $content): self
{
if ($this->secondary_content->contains($content)) {
$this->secondary_content->removeElement($content);
$content->removeSecondaryCategory($this);
}
return $this;
}
public function getPinnedEbook($site = "") {
$criteria = Criteria::create()
->where(Criteria::expr()->eq("status", "1"))
->orderBy(["id" => Criteria::ASC])
;
$content = $this->getSecondaryContent()->matching($criteria);
//$content = $this->getSecondaryContent();
if(!$content) {
return null;
}
if($site) {
$tempContent = $content;
$content = [];
foreach($tempContent as $c) {
foreach($c->getSites() as $s) {
if($s->getId() == $site) {
$content[] = $c;
}
}
}
if(!$content) {
return null;
}
}
$pinnedContent = [];
foreach($content as $c) {
if($c->getEbookPinned()) {
$pinnedContent[] = $c;
}
}
if(!empty($pinnedContent)) {
return $pinnedContent[array_rand($pinnedContent)];
}
//return $content->first();
return $content[0];
}
/**
* @return Collection|Product[]
*/
public function getProducts(): Collection
{
return $this->products;
}
public function addProduct(Product $product): self
{
if (!$this->products->contains($product)) {
$this->products[] = $product;
$product->setCategory($this);
}
return $this;
}
public function removeProduct(Product $product): self
{
if ($this->products->contains($product)) {
$this->products->removeElement($product);
// set the owning side to null (unless already changed)
if ($product->getCategory() === $this) {
$product->setCategory(null);
}
}
return $this;
}
public function getSecondaryProducts (): Collection
{
return $this->secondary_products;
}
public function addSecondaryProduct (Product $product)
{
if (!$this->secondary_products->contains($product)) {
$this->secondary_products[] = $product;
$product->addSecondaryCategory($this);
}
return $this;
}
public function removeSecondaryProduct (Product $product): self
{
if ($this->secondary_products->contains($product)) {
$this->secondary_products->removeElement($product);
$product->removeSecondaryCategory($this);
}
return $this;
}
public function getTaxonomy(): ?string
{
return $this->taxonomy;
}
public function setTaxonomy(string $taxonomy = null): self
{
$this->taxonomy = $taxonomy ? $taxonomy : "";
return $this;
}
/**
* @return Collection|Customer[]
*/
public function getCustomers(): Collection
{
return $this->customers;
}
public function addCustomer(Customer $customer): self
{
if (!$this->customers->contains($customer)) {
$this->customers[] = $customer;
$customer->addCategory($this);
}
return $this;
}
public function removeCustomer(Customer $customer): self
{
if ($this->customers->contains($customer)) {
$this->customers->removeElement($customer);
$customer->removeCategory($this);
}
return $this;
}
public function getChildren ()
{
return $this->children;
}
public function addChild (Category $child) {
if (!$this->children->contains($child)) {
$this->children[] = $child;
$child->setPrnt($this);
}
return $this;
}
public function removeChild (Category $children): self
{
if ($this->children->contains($children)) {
$this->children->removeElement($children);
// set the owning side to null (unless already changed)
if ($children->getPrnt() === $this) {
$children->setPrnt(null);
}
}
return $this;
}
public function getURL ()
{
// grab URL based on category taxonomy
if ($this->taxonomy && strtolower($this->taxonomy) != "category") {
return "/taxonomy/{$this->taxonomy}/{$this->slug}";
}
return "/category/{$this->slug}";
}
public function getSiteMapURL()
{
if ($this->taxonomy && strtolower($this->taxonomy) == "ebook-category") {
return "/category/ebooks/{$this->slug}";
}
return "/category/{$this->slug}";
}
public function getMedia (): ?Media
{
return $this->media;
}
public function setMedia (Media $media = null): self
{
$this->media = $media;
return $this;
}
public function __toString ()
{
return $this->title ? $this->title : "";
}
}