<?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\Level4\DTO\Level4Input as Input;
use QSL\Make\API\Endpoint\Level4\DTO\Level4Output as Output;
/**
* @ORM\Entity (repositoryClass="\QSL\Make\Model\Repo\Level4")
* @ORM\Table (name="level4",
* indexes={
* @ORM\Index (name="enabled", columns={"enabled"}),
* @ORM\Index (name="level3", columns={"level3_id"}),
* @ORM\Index (name="showOnFrontPage", columns={"showOnFrontPage"})
* }
* )
* @ApiPlatform\ApiResource(
* shortName="Make/Model/Year - Level4",
* input=Input::class,
* output=Output::class,
* itemOperations={
* "get"={
* "method"="GET",
* "path"="/qsl/make/level4/{id}.{_format}",
* "identifiers"={"id"},
* "requirements"={"id"="\d+"}
* },
* "put"={
* "method"="PUT",
* "path"="/qsl/make/level4/{id}.{_format}",
* "identifiers"={"id"},
* "requirements"={"id"="\d+"}
* },
* "delete"={
* "method"="DELETE",
* "path"="/qsl/make/level4/{id}.{_format}",
* "identifiers"={"id"},
* "requirements"={"id"="\d+"}
* }
* },
* collectionOperations={
* "get"={
* "method"="GET",
* "path"="/qsl/make/level4.{_format}",
* "identifiers"={}
* },
* "post"={
* "method"="POST",
* "path"="/qsl/make/level4.{_format}",
* "identifiers"={},
* "controller"="xcart.api.qsl.make.level4.controller"
* }
* },
* attributes={
* "pagination_items_per_page" = 100
* }
* )
*/
class Level4 extends ALevel
{
/**
* Level3
*
* @var \QSL\Make\Model\Level3
*
* @ORM\ManyToOne (targetEntity="QSL\Make\Model\Level3", inversedBy="level4", cascade={"merge","detach"})
* @ORM\JoinColumn (name="level3_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $level3;
/**
* Relation to a ModelProduct entities
*
* @var \Doctrine\Common\Collections\ArrayCollection
*
* @ORM\OneToMany (targetEntity="QSL\Make\Model\Level4Product", mappedBy="level", cascade={"all"})
* @ORM\OrderBy ({"position" = "ASC"})
*/
protected $level4Product;
/**
* One-to-one relation with image
*
* @var \QSL\Make\Model\Image\Level4
*
* @ORM\OneToMany (targetEntity="QSL\Make\Model\Image\Level4", mappedBy="level", cascade={"all"})
*/
protected $images;
/**
* @var \QSL\Make\Model\Image\Level4Logo
*
* @ORM\OneToOne (targetEntity="QSL\Make\Model\Image\Level4Logo", mappedBy="level", cascade={"all"})
*/
protected $logo;
/**
* Clean URLs
*
* @var \Doctrine\Common\Collections\ArrayCollection
*
* @ORM\OneToMany (targetEntity="XLite\Model\CleanURL", mappedBy="level4", cascade={"all"})
* @ORM\OrderBy ({"id" = "ASC"})
*/
protected $cleanURLs;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany (targetEntity="QSL\Make\Model\Level4Translation", 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
*
* @param \XLite\Model\CleanURL $cleanURL
*/
public function addCleanURLs(\XLite\Model\CleanURL $cleanURL)
{
$this->cleanURLs[] = $cleanURL;
}
/**
* @param $value
*
* @return $this
*/
public function setImages($value)
{
$this->images = $value;
return $this;
}
/**
* @return Image\Level4
*/
public function getImages()
{
return $this->images;
}
/**
* @return \QSL\Make\Model\Image\Level4Logo
*/
public function getLogo()
{
return $this->logo;
}
/**
* @param \QSL\Make\Model\Image\Level4Logo $value
*/
public function setLogo($value)
{
$this->logo = $value;
}
/**
* @return string
*/
public function getCustomerTitle()
{
return $this->getTitle() ?: $this->getLevel3()->getCustomerTitle();
}
/**
* @return string
*/
public function getCustomerValue()
{
return $this->getLevel3()->getCustomerValue() . ' ' . $this->getValue();
}
/**
* @return string
*/
public function getDisplayDescription()
{
return $this->getDescription() ?: $this->getLevel3()->getDisplayDescription();
}
/**
* @return Image\Level4
*/
public function getCustomerImage()
{
return $this->getImages()->count() ? $this->getImages() : $this->getLevel3()->getCustomerImage();
}
/**
* @return mixed
*/
public function getLevel3()
{
return $this->level3;
}
/**
* @param mixed $value
*
* @return \QSL\Make\Model\Level4
*/
public function setLevel3($value)
{
$this->level3 = $value;
return $this;
}
/**
* @return mixed
*/
public function getLevel4Product()
{
return $this->level4Product;
}
/**
* @param mixed $value
*
* @return \QSL\Make\Model\Level4
*/
public function addLevel4Product($value)
{
$this->level4Product[] = $value;
return $this;
}
/**
* @param mixed $value
*
* @return \QSL\Make\Model\Level4
*/
public function setLevelProduct($value)
{
return $this->addLevel4Product($value);
}
/**
* @return string
*/
public function getLevelName($delimiter = ' ')
{
return $this->getLevel3()->getLevelName($delimiter) . $delimiter . $this->getValue();
}
public function getPathData(): array
{
$result = $this->getLevel3()->getPathData();
$result[] = ['id' => $this->getId(), 'name' => $this->getValue()];
return $result;
}
/**
* @return array
*/
public function getLevelSelectors()
{
return array_merge(
$this->getLevel3()->getLevelSelectors(),
[
\XLite\Core\Config::getInstance()->QSL->Make->level4name => [
'id' => $this->getId(),
'value' => $this->getValue(),
],
]
);
}
/**
* Constructor
*
* @param array $data Entity properties OPTIONAL
*/
public function __construct(array $data = [])
{
$this->level4Product = 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\Level4Product')
->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->getLevel4Product()->toArray()
);
}
/**
* @param integer $productId
*
* @return boolean
*/
public function hasProduct($productId)
{
return in_array(
$productId,
array_map(
static function ($product) {
return $product->getProductId();
},
$this->getProducts()
)
);
}
/**
* @return string
*/
public function getLevelPath()
{
return $this->getLevel3()->getLevelPath() . '-' . $this->getValue();
}
/**
* @return string
*/
public function getUrl()
{
return LC_USE_CLEAN_URLS
? $this->getCleanURL()
: \XLite\Core\Converter::buildURL('level_front_page', '', ['id' => $this->getId()]);
}
/**
* @return \QSL\Make\Model\Level3
*/
public function getParentLevel()
{
return $this->getLevel3();
}
public function getDepth(): int
{
return 4;
}
}