<?php
/**
* Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
* See https://www.x-cart.com/license-agreement.html for license details.
*/
namespace QSL\Make\Model;
use ApiPlatform\Core\Annotation as ApiPlatform;
use Doctrine\ORM\Mapping as ORM;
use QSL\Make\API\Endpoint\Level3\DTO\Level3Input as Input;
use QSL\Make\API\Endpoint\Level3\DTO\Level3Output as Output;
/**
* @ORM\Entity (repositoryClass="\QSL\Make\Model\Repo\Level3")
* @ORM\Table (name="level3",
* indexes={
* @ORM\Index (name="enabled", columns={"enabled"}),
* @ORM\Index (name="level2", columns={"level2_id"}),
* @ORM\Index (name="showOnFrontPage", columns={"showOnFrontPage"})
* }
* )
* @ApiPlatform\ApiResource(
* shortName="Make/Model/Year - Level3",
* input=Input::class,
* output=Output::class,
* itemOperations={
* "get"={
* "method"="GET",
* "path"="/qsl/make/level3/{id}.{_format}",
* "identifiers"={"id"},
* "requirements"={"id"="\d+"}
* },
* "put"={
* "method"="PUT",
* "path"="/qsl/make/level3/{id}.{_format}",
* "identifiers"={"id"},
* "requirements"={"id"="\d+"}
* },
* "delete"={
* "method"="DELETE",
* "path"="/qsl/make/level3/{id}.{_format}",
* "identifiers"={"id"},
* "requirements"={"id"="\d+"}
* }
* },
* collectionOperations={
* "get"={
* "method"="GET",
* "path"="/qsl/make/level3.{_format}",
* "identifiers"={}
* },
* "post"={
* "method"="POST",
* "path"="/qsl/make/level3.{_format}",
* "identifiers"={},
* "controller"="xcart.api.qsl.make.level3.controller"
* }
* },
* attributes={
* "pagination_items_per_page" = 100
* }
* )
*/
class Level3 extends ALevel
{
/**
* Level2
*
* @var \QSL\Make\Model\Level2
*
* @ORM\ManyToOne (targetEntity="QSL\Make\Model\Level2", inversedBy="level3", cascade={"merge","detach"})
* @ORM\JoinColumn (name="level2_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $level2;
/**
* Level4
*
* @var \Doctrine\Common\Collections\ArrayCollection
*
* @ORM\OneToMany (targetEntity="QSL\Make\Model\Level4", mappedBy="level3", cascade={"all"})
* @ORM\OrderBy ({"value" = "ASC"})
*/
protected $level4;
/**
* Relation to a ModelProduct entities
*
* @var \Doctrine\Common\Collections\ArrayCollection
*
* @ORM\OneToMany (targetEntity="QSL\Make\Model\Level3Product", mappedBy="level", cascade={"all"})
* @ORM\OrderBy ({"position" = "ASC"})
*/
protected $level3Product;
/**
* One-to-one relation with image
*
* @var \QSL\Make\Model\Image\Level3
*
* @ORM\OneToMany (targetEntity="QSL\Make\Model\Image\Level3", mappedBy="level", cascade={"all"})
*/
protected $images;
/**
* @var \QSL\Make\Model\Image\Level3Logo
*
* @ORM\OneToOne (targetEntity="QSL\Make\Model\Image\Level3Logo", mappedBy="level", cascade={"all"})
*/
protected $logo;
/**
* Clean URLs
*
* @var \Doctrine\Common\Collections\ArrayCollection
*
* @ORM\OneToMany (targetEntity="XLite\Model\CleanURL", mappedBy="level3", cascade={"all"})
* @ORM\OrderBy ({"id" = "ASC"})
*/
protected $cleanURLs;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany (targetEntity="QSL\Make\Model\Level3Translation", mappedBy="owner", cascade={"all"})
*/
protected $translations;
/**
* @return \Doctrine\Common\Collections\ArrayCollection
*/
public function getCleanURLs()
{
return $this->cleanURLs;
}
/**
* @@param \Doctrine\Common\Collections\ArrayCollection
*/
public function setCleanURLs($value)
{
$this->cleanURLs = $value;
}
/**
* Add cleanURL
*
* @return $this
*/
public function addCleanURLs(\XLite\Model\CleanURL $cleanURL)
{
$this->cleanURLs[] = $cleanURL;
}
/**
* @param $value
*/
public function setImages($value)
{
$this->images = $value;
}
/**
* @return Image\Level3
*/
public function getImages()
{
return $this->images;
}
/**
* @return \QSL\Make\Model\Image\Level3Logo
*/
public function getLogo()
{
return $this->logo;
}
/**
* @param \QSL\Make\Model\Image\Level3Logo $value
*/
public function setLogo($value)
{
$this->logo = $value;
}
/**
* @return string
*/
public function getCustomerTitle()
{
return $this->getTitle() ?: $this->getLevel2()->getCustomerTitle();
}
/**
* @return string
*/
public function getCustomerValue()
{
return $this->getLevel2()->getCustomerValue() . ' ' . $this->getValue();
}
/**
* @return string
*/
public function getDisplayDescription()
{
return $this->getDescription() ?: $this->getLevel2()->getDisplayDescription();
}
/**
* @return Image\Level3
*/
public function getCustomerImage()
{
return $this->getImages()->count() ? $this->getImages() : $this->getLevel2()->getCustomerImage();
}
/**
* @return mixed
*/
public function getLevel2()
{
return $this->level2;
}
/**
* @param \QSL\Make\Model\Level2 $value
*
* @return \QSL\Make\Model\Level3
*/
public function setLevel2($value)
{
$this->level2 = $value;
}
/**
* Level4 getter
*
* @return \QSL\Make\Model\Level4
*/
public function getLevel4()
{
return $this->level4;
}
/**
* @param \QSL\Make\Model\Level4 $value
*/
public function addLevel4($value)
{
$this->level4[] = $value;
}
/**
* @return mixed
*/
public function getLevel3Product()
{
return $this->level3Product;
}
/**
* @param \QSL\Make\Model\Level3Product $value
*
* @return \QSL\Make\Model\Level3
*/
public function addLevel3Product($value)
{
$this->level3Product[] = $value;
}
/**
* @param \QSL\Make\Model\Level3Product $value
*
* @return \QSL\Make\Model\Level3
*/
public function setLevelProduct($value)
{
return $this->addLevel3Product($value);
}
/**
* @return string
*/
public function getLevelName($delimiter = ' ')
{
return $this->getLevel2()->getLevelName($delimiter) . $delimiter . $this->getValue();
}
public function getPathData(): array
{
$result = $this->getLevel2()->getPathData();
$result[] = ['id' => $this->getId(), 'name' => $this->getValue()];
return $result;
}
/**
* @return array
*/
public function getLevelSelectors()
{
return array_merge(
$this->getLevel2()->getLevelSelectors(),
[
\XLite\Core\Config::getInstance()->QSL->Make->level3name => [
'id' => $this->getId(),
'value' => $this->getValue(),
],
]
);
}
/**
* Constructor
*
* @param array $data Entity properties OPTIONAL
*/
public function __construct(array $data = [])
{
$this->level4 = new \Doctrine\Common\Collections\ArrayCollection();
$this->level3Product = new \Doctrine\Common\Collections\ArrayCollection();
$this->cleanURLs = new \Doctrine\Common\Collections\ArrayCollection();
parent::__construct($data);
}
/**
* @return integer
*/
public function getProductsCount()
{
return \XLite\Core\Database::getRepo('QSL\Make\Model\Level3Product')
->search(new \XLite\Core\CommonCell(['level' => $this]), \XLite\Model\Repo\ARepo::SEARCH_MODE_COUNT);
}
/**
* @return array
*/
public function getProducts()
{
return array_map(
static function ($modelProduct) {
return $modelProduct->getProduct();
},
$this->getLevel3Product()->toArray()
);
}
/**
* @param integer $productId
*
* @return boolean
*/
public function hasProduct($productId)
{
return in_array(
$productId,
array_map(
static function ($product) {
return $product->getProductId();
},
$this->getProducts()
),
true
);
}
/**
* @return string
*/
public function getLevelPath()
{
return $this->getLevel2()->getLevelPath() . '-' . $this->getValue();
}
/**
* @return \QSL\Make\Model\Level2
*/
public function getParentLevel()
{
return $this->getLevel2();
}
public function getDepth(): int
{
return 3;
}
}