<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use App\Entity\CategoryDescription;
/**
* Categories
*
* @ORM\Table(name="categories")
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
* @ORM\HasLifecycleCallbacks
*/
class Category extends TranslatedEntity
{
protected $tranlatedEntity = 'CategoryDescription';
public const SEARCH_INDICES = [
'fr' => 'fr-categories',
'en' => 'en-categories',
];
public const DISCOUNT_UNIVERSE = 242;
/**
* @var int
*
* @ORM\Column(name="categories_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string|null
*
* @ORM\Column(name="categories_image", type="string", length=64, nullable=true)
*/
private $image;
/**
* @var \App\Entity\Category
*
* @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="categories_id", nullable=true)
*/
private $parent;
/**
* @var array
*
* @ORM\OneToMany(targetEntity="App\Entity\Category", mappedBy="parent")
* @ORM\OrderBy({"sortOrder" = "ASC"})
*/
private $children;
/**
* @ORM\OneToMany(targetEntity="App\Entity\CategoryDescription", mappedBy="category")
*/
private $descriptions = [];
/**
* @ORM\OneToMany(targetEntity="App\Entity\CategoryToExtraField", mappedBy="category")
*/
private $extraFields = [];
/**
* @ORM\OneToMany(targetEntity="App\Entity\PictureCategory", mappedBy="category")
*/
private $pictures = [];
/**
* @var int
*
* @ORM\Column(name="sort_order", type="integer", nullable=false)
*/
private $sortOrder = '0';
/**
* @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="categories_best", type="integer", nullable=true)
*/
private $categoriesBest;
/**
* @var int
*
* @ORM\Column(name="categories_affichage", type="integer", nullable=false, options={"default"="1"})
*/
private $affichage = '1';
/**
* @var int
*
* @ORM\Column(name="categories_sousmenu", type="integer", nullable=false, options={"default"="1"})
*/
private $sousmenu = '1';
/**
* @var int
*
* @ORM\Column(name="categories_index", type="integer", nullable=false, options={"default"="1"})
*/
private $index = '1';
/**
* @var int
*
* @ORM\Column(name="categories_hide_children", type="integer", nullable=false)
*/
private $hideChildren = '0';
/**
* @var string
*
* @ORM\Column(name="categories_position", type="string", length=64, nullable=true)
*/
private $position = '0';
/**
* @var ?boolean
*
* @ORM\Column(name="categories_menu_spacer", type="boolean", nullable=true)
*/
private $menuSpacer = '0';
private $file;
private $temp;
private $currentPath;
public function __construct() {
$this->pictures = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getId() {
return $this->id;
}
public function getName() {
if(!empty($this->languageId)){
$description = null;
foreach($this->descriptions as $d){
if($d->getLanguage()->getId()==$this->languageId){
$description = $d;
break;
}
}
}
if(empty($description) && count($this->descriptions)){
$description = $this->descriptions[0];
}
return empty($description)?'':$description->getName();
}
public function getUrl() {
if(!empty($this->languageId)){
$description = null;
foreach($this->descriptions as $d){
if($d->getLanguage()->getId()==$this->languageId){
$description = $d;
break;
}
}
}
if(empty($description) && count($this->descriptions)){
$description = $this->descriptions[0];
}
return empty($description)?'':$description->getUrl();
}
public function getDescription() {
if(!empty($this->languageId)){
$description = null;
foreach($this->descriptions as $d){
if($d->getLanguage()->getId()==$this->languageId){
$description = $d;
break;
}
}
}
if(empty($description) && count($this->descriptions)){
$description = $this->descriptions[0];
}
return empty($description)?'':$description->getDescription();
}
public function getDescriptions() {
return $this->descriptions;
}
public function getExtraFields() {
return $this->extraFields;
}
public function getImage() {
return $this->image;
}
public function getParent(): ?\App\Entity\Category {
return $this->parent;
}
public function getChildren() {
return $this->children;
}
public function getSortOrder() {
return $this->sortOrder;
}
public function getDateAdded(): ?\DateTime {
return $this->dateAdded;
}
public function getLastModified(): ?\DateTime {
return $this->lastModified;
}
public function getCategoriesBest() {
return $this->categoriesBest;
}
public function getAffichage() {
return $this->affichage;
}
public function getSousmenu() {
return $this->sousmenu;
}
public function getIndex() {
return $this->index;
}
public function getHideChildren() {
return $this->hideChildren;
}
public function getCategoryDescription($lang='fr') : ?CategoryDescription {
foreach($this->getDescriptions() as $desc){
if($desc->getLanguage()->getCode() == $lang)
return $desc;
}
return null;
}
public function getPictures() {
return $this->pictures;
}
public function getPosition(): ?string {
return $this->position;
}
public function getMenuSpacer() {
return $this->menuSpacer;
}
public function getCurrentPath() {
return $this->currentPath;
}
public function setPictures($pictures): void {
$this->pictures = $pictures;
}
public function setDescriptions($descriptions) {
$this->descriptions = $descriptions;
}
public function setExtraFields($extraFields): void {
$this->extraFields = $extraFields;
}
public function setImage($image) {
$this->image = $image;
}
public function setParent(?\App\Entity\Category $parent) {
$this->parent = $parent;
}
public function setChildren($children) {
$this->children = $children;
}
public function setSortOrder($sortOrder) {
$this->sortOrder = $sortOrder;
}
public function setDateAdded(\DateTime $dateAdded) {
$this->dateAdded = $dateAdded;
}
public function setLastModified(\DateTime $lastModified) {
$this->lastModified = $lastModified;
}
public function setCategoriesBest($categoriesBest) {
$this->categoriesBest = $categoriesBest;
}
public function setAffichage($affichage) {
$this->affichage = $affichage;
}
public function setSousmenu($sousmenu) {
$this->sousmenu = $sousmenu;
}
public function setIndex($index) {
$this->index = $index;
}
public function setHideChildren($hideChildren) {
$this->hideChildren = $hideChildren;
}
public function setPosition(?string $position): void {
$this->position = $position;
}
public function setMenuSpacer($menuSpacer): void {
$this->menuSpacer = $menuSpacer;
}
public function setCurrentPath($currentPath): void {
$this->currentPath = $currentPath;
}
public function isRoot() {
return empty($this->parent);
}
public function isActive() {
return $this->getAffichage() == 1;
}
public function isVisibleInMenu() {
return $this->isActive() && ($this->getSousmenu() == 1);
}
public function hasChildren() {
return !empty($this->children) && (count($this->children)>0);
}
public function toArray() : array{
$output = [
'id' => $this->getId(),
'leaf' => count($this->children)?false:true,
'children' => [],
'name' => $this->getName(),
'languageId' => $this->getLanguageId()
];
foreach($this->children as $child){
$output['children'][] = $child->toArray();
}
return $output;
}
/**
* @todo Remove once 0 values in the table are converted to NULL.
*/
public function loadDescriptions(\Doctrine\ORM\EntityManager $em)
{
$conn = $em->getConnection();
$sql = $conn->prepare('select * from categories_description where categories_id = '.$this->getId());
$sql->execute();
$rows = $sql->fetchAll();
$descriptions = [];
foreach($rows as $desc){
$description = new CategoryDescription();
$description->setName($desc['categories_name']);
$description->setDescription($desc['categories_description']);
$description->setLanguage($em->getRepository('App:Language')->find($desc['language_id']));
$descriptions[] = $description;
}
$this->setDescriptions($descriptions);
}
/**
* @ORM\PostLoad
*
* @todo Remove once 0 values in the table are converted to NULL.
*/
public function postLoad(\Doctrine\ORM\Event\LifecycleEventArgs $args) {
if ($this->parent && $this->parent->getId() == 0) {
$this->parent = null;
}
// $this->loadDescriptions($args->getObjectManager());
// $this->translate();
}
public static function getSearchIndex($lang = 'fr')
{
return self::SEARCH_INDICES[$lang];
}
}