<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
*/
class Category
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=100)
* @Groups({"searchable"})
*/
private $name;
/**
* @ORM\Column(type="integer", options={"default" : 1})
*/
private $placement=1;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="categories")
*/
private $subCategory;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Category", mappedBy="subCategory")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
private $categories;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Image")
*/
private $image;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Image")
*/
private $banner;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $title;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $tier1MaturinFeesPc;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $tier2MaturinFeesPc;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $tier2MaturinFeesMinprice;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $tier2MaturinFeesMaxprice;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $packingFeesAmount;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $packingSuppliesFeesAmount;
/**
* @ORM\Column(type="string", length=200, nullable=true)
*/
private $metaDescription;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="categories")
* @ORM\OrderBy({"qtyReadyToShip" = "DESC"})
*/
private $products;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="subCategory")
* @ORM\OrderBy({"qtyReadyToShip" = "DESC"})
*/
private $productsSub;
/**
* @ORM\Column(type="boolean")
*/
private $isDisplayedForBuyer=false;
public function __construct()
{
$this->categories = new ArrayCollection();
$this->products = new ArrayCollection();
$this->productsSub= new ArrayCollection();
}
public function __toString(){
if(!empty($this->name))
return $this->name;
else
return '';
}
/*************************
* All the properties set
*************************/
public function getId(){
return $this->id;
}
public function setName($name){
$this->name = $name;
}
public function getName(){
return $this->name;
}
public function getFullUrlPath(){
$tree = array();
$tree[] = $this->getUrlName();
$c = $this->getSubCategory();
while(!empty($c)){
$tree[] = $c->getUrlName();
$c = $c->getSubCategory();
}
$url = '/'.implode('/', array_reverse($tree));
return $url;
}
public function getUrlName(){
$unwanted_array = array( 'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', ","=>"", "'" => "" );
$str = strtr( $this->name, $unwanted_array );
return urlencode(str_replace(' ', '-', strtolower($str)));
}
public function setPlacement($placement){
$this->placement = $placement;
}
public function getPlacement(){
return $this->placement;
}
/*
* This is mostly from the Select of the forms
*/
public function setSubFromObj($master){
if(!empty($master))
$this->sub = $master->getId();
else
$this->sub = 0;
}
public function hasSubFromObj(){
return false;
}
public function getSubCategory(): ?Category
{
return $this->subCategory;
}
public function setSubCategory(?self $subCategory): self
{
$this->subCategory = $subCategory;
return $this;
}
public function getAllSubCategories()
{
//Start with those here
$categories = $this->recursiveCategories($this->getCategories(), array());
return $categories;
}
public function recursiveCategories($categories, $results = array()){
foreach($categories as $c){
$results[]=$c;
if(!empty($c->getCategories()) && count($c->getCategories()) > 0){
$categories = $this->recursiveCategories($c->getCategories(), $results);
}
}
return $results;
}
/**
* @return Collection|self[]
*/
public function getCategories(): Collection
{
return $this->categories;
}
public function addCategory(self $category): self
{
if (!$this->categories->contains($category)) {
$this->categories[] = $category;
$category->setSubCategory($this);
}
return $this;
}
public function removeCategory(self $category): self
{
if ($this->categories->contains($category)) {
$this->categories->removeElement($category);
// set the owning side to null (unless already changed)
if ($category->getSubCategory() === $this) {
$category->setSubCategory(null);
}
}
return $this;
}
public function getImage(): ?Image
{
return $this->image;
}
public function setImage(?Image $image): self
{
//Fix a bug in the form when not selecting a image for now
//This need to be fixed elsewhere
//@TODO
if(!empty($image))
$this->image = $image;
return $this;
}
public function getBanner(): ?Image
{
return $this->banner;
}
public function setBanner(?Image $banner): self
{
//Fix a bug in the form when not selecting a image for now
//This need to be fixed elsewhere
//@TODO
if(!empty($banner))
$this->banner = $banner;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getMetaDescription(): ?string
{
return $this->metaDescription;
}
public function setMetaDescription(?string $metaDescription): self
{
$this->metaDescription = $metaDescription;
return $this;
}
public function getProductsSub(): Collection
{
return $this->productsSub;
}
public function addProductsSub(Product $product): self
{
if (!$this->productsSub->contains($product)) {
$this->productsSub[] = $product;
$product->setCategory($this);
}
return $this;
}
public function removeProductsSub(Product $product): self
{
if ($this->productsSub->contains($product)) {
$this->productsSub->removeElement($product);
// set the owning side to null (unless already changed)
if ($product->getCategory() === $this) {
$product->setCategory(null);
}
}
return $this;
}
/**
* @return Collection|Badge[]
*/
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 getHasProducts(): bool
{
if($this->getAllProducts()->count() > 0){
return true;
}else{
$cats = $this->getCategories();
foreach($cats as $c){
if($c->getHasProducts())
return true;
}
}
return false;
}
public function getIsValid(): bool
{
return $this->isValid();
}
public function getIsDisplayedForBuyer(): ?bool
{
return $this->isDisplayedForBuyer;
}
public function setIsDisplayedForBuyer(bool $isDisplayedForBuyer): self
{
$this->isDisplayedForBuyer = $isDisplayedForBuyer;
return $this;
}
/*
* Used in the UI for building the menu
*/
public function isValid()
{
return $this->getIsDisplayedForBuyer();
}
public function getAllProducts()
{
$cats = $this->getAllSubCategories();
$clean = array();
$clean = array_merge($this->getProducts()->toArray(), $this->getProductsSub()->toArray());
foreach($cats as $c){
$clean = array_merge($clean, $c->getProducts()->toArray(), $c->getProductsSub()->toArray());
}
//return new ArrayCollection(array_merge($this->getProducts()->toArray(), $this->getProductsSub()->toArray()));
return new ArrayCollection($clean);
}
public function getTier1MaturinFeesPc(): ?string
{
return $this->tier1MaturinFeesPc;
}
public function setTier1MaturinFeesPc(?string $tier1MaturinFeesPc): self
{
$this->tier1MaturinFeesPc = $tier1MaturinFeesPc;
return $this;
}
public function getTier2MaturinFeesPc(): ?string
{
return $this->tier2MaturinFeesPc;
}
public function setTier2MaturinFeesPc(?string $tier2MaturinFeesPc): self
{
$this->tier2MaturinFeesPc = $tier2MaturinFeesPc;
return $this;
}
public function getTier2MaturinFeesMinprice(): ?string
{
return $this->tier2MaturinFeesMinprice;
}
public function setTier2MaturinFeesMinprice(?string $tier2MaturinFeesMinprice): self
{
$this->tier2MaturinFeesMinprice = $tier2MaturinFeesMinprice;
return $this;
}
public function getTier2MaturinFeesMaxprice(): ?string
{
return $this->tier2MaturinFeesMaxprice;
}
public function setTier2MaturinFeesMaxprice(?string $tier2MaturinFeesMaxprice): self
{
$this->tier2MaturinFeesMaxprice = $tier2MaturinFeesMaxprice;
return $this;
}
public function getPackingFeesAmount(): ?string
{
return $this->packingFeesAmount;
}
public function setPackingFeesAmount(?string $packingFeesAmount): self
{
$this->packingFeesAmount = $packingFeesAmount;
return $this;
}
public function getPackingSuppliesFeesAmount(): ?string
{
return $this->packingSuppliesFeesAmount;
}
public function setPackingSuppliesFeesAmount(?string $packingSuppliesFeesAmount): self
{
$this->packingSuppliesFeesAmount = $packingSuppliesFeesAmount;
return $this;
}
}