<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Manufacturers
*
* @ORM\Table(name="manufacturers", indexes={@ORM\Index(name="IDX_MANUFACTURERS_NAME", columns={"manufacturers_name"})})
* @ORM\Entity
*/
class Manufacturer extends TranslatedEntity
{
protected $tranlatedEntity = 'ManufactureryDescription';
public const SEARCH_INDICES = [
'fr' => 'fr-manufactuers',
'en' => 'en-manufactuers',
];
/**
* @var int
*
* @ORM\Column(name="manufacturers_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="manufacturers_name", type="string", length=32, nullable=false)
*/
private $name;
/**
* @var string|null
*
* @ORM\Column(name="manufacturers_image", type="string", length=64, nullable=true)
*/
private $image;
/**
* @var \DateTime|null
*
* @ORM\Column(name="date_added", type="datetime", nullable=true)
*/
private $dateAdded;
/**
* @var \DateTime|null
*
* @ORM\Column(name="last_modified", type="datetime", nullable=true)
*/
private $lastModified;
/**
* @var int|null
*
* @ORM\Column(name="manufacturers_statuts", type="integer", nullable=true)
*/
private $status;
/**
* @var int
*
* @ORM\Column(name="manufacturers_order", type="integer", nullable=false)
*/
private $order = 0;
/**
* @ORM\OneToMany(targetEntity="App\Entity\ManufacturerDescription", mappedBy="manufacturer")
*/
private $descriptions = [];
/**
* @ORM\OneToMany(targetEntity="App\Entity\ManufacturerCountry", mappedBy="manufacturer")
*/
private $countryRestrictions = [];
/**
* @var int
*
* @ORM\Column(name="manufacturers_locked_price", type="integer", nullable=true)
*/
private $lockedPrice = 0;
/**
* @var int
*
* @ORM\Column(name="manufacturers_welcome", type="integer", nullable=true)
*/
private $welcome = 0;
/**
* @var int
*
* @ORM\Column(name="manufacturers_export", type="integer", nullable=false)
*/
private $export = 0;
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function getUrl($lang = null) {
if(!empty($lang)){
$description = $this->getManufacturerDescription($lang);
if($description){
$url = $description->getUrl();
if(!empty($url)){
return \App\Helpers\Encoder::formatUrl($url);
}
}
}
return \App\Helpers\Encoder::formatUrl($this->name);
}
public function getLogo() {
if(empty($this->image))
return $this->image;
return 'assets/'. str_replace('fournisseur/', 'manufacturers/', $this->image);
}
public function getImage() {
return $this->image;
}
public function getDateAdded(): \DateTime {
return is_null($this->dateAdded) ? new \DateTime() : $this->dateAdded;
}
public function getLastModified(): \DateTime {
return is_null($this->lastModified) ? new \DateTime() : $this->lastModified;
}
public function getStatus() {
return $this->status;
}
public function getOrder() {
return $this->order;
}
public function getDescriptions() {
return $this->descriptions;
}
public function getExport(): int {
return $this->export;
}
public function getCountryRestrictions() {
return $this->countryRestrictions;
}
public function getCountries() {
$restrictions = $this->getCountryRestrictions();
$output = [];
foreach($restrictions as $r){
$output[] = $r->getCountry();
}
return $output;
}
public function getLockedPrice(): ?int {
return empty($this->lockedPrice)?0:1;
}
public function getWelcome(): ?int {
return $this->welcome;
}
public function setName($name) {
$this->name = $name;
}
public function setImage($image) {
$this->image = $image;
}
public function setDateAdded(\DateTime $dateAdded) {
$this->dateAdded = $dateAdded;
}
public function setLastModified(\DateTime $lastModified) {
$this->lastModified = $lastModified;
}
public function setStatus($status) {
$this->status = $status;
}
public function setOrder($order) {
$this->order = $order;
}
public function setDescriptions($descriptions): void {
$this->descriptions = $descriptions;
}
public function setExport(int $export): void {
$this->export = $export;
}
public function setCountryRestrictions($countryRestrictions): void {
$this->countryRestrictions = $countryRestrictions;
}
public function setLockedPrice($lockedPrice): void {
$this->lockedPrice = empty($lockedPrice)?0:1;
}
public function setWelcome(int $welcome): void {
$this->welcome = $welcome;
}
public function hasRestrictions() {
return count($this->getCountryRestrictions())>0;
}
public function hasRestrictionForCountry($country) {
if($country){
if($this->hasRestrictions()){
foreach($this->getCountryRestrictions() as $restriction){
if($restriction->getCountry() == $country){
return true;
}
}
}
}
return false;
}
public function isAvailableForCountry($country) {
if($country){
if($this->hasRestrictions()){
foreach($this->getCountryRestrictions() as $restriction){
if($restriction->getCountry() == $country){
return true;
}
}
return false;
}
}
return true;
}
public function getManufacturerDescription($lang='fr') : ?ManufacturerDescription {
foreach($this->getDescriptions() as $desc){
if($desc->getLanguage()->getCode() == $lang)
return $desc;
}
return null;
}
public function toArray() : array{
$output = [
'id' => $this->getId(),
'name' => $this->getName(),
'image' => $this->getImage(),
'dateAdded' => $this->getDateAdded(),
'lastModified' => $this->getLastModified(),
'status' => $this->getStatus()
];
return $output;
}
public function isActive()
{
return $this->getStatus() != 0;
}
public function hasWelcomeDiscount()
{
return !empty($this->welcome);
}
public static function getSearchIndex($lang = 'fr')
{
return self::SEARCH_INDICES[$lang];
}
}