<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\Entity;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
if (!class_exists('\Customize\Entity\CategoryArtist')) {
/**
* CategoryArtist
*
* @ORM\Table(name="dtb_category_artist")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255)
* @ORM\HasLifecycleCallbacks()
* @ORM\Entity(repositoryClass="Customize\Repository\CategoryArtistRepository")
*/
class CategoryArtist extends \Eccube\Entity\AbstractEntity
{
/**
* @return string
*/
public function __toString()
{
return (string) $this->getName();
}
/**
* @return integer
*/
public function countBranches()
{
$count = 1;
foreach ($this->getChildren() as $Child) {
$count += $Child->countBranches();
}
return $count;
}
/**
* @param \Doctrine\ORM\EntityManager $em
* @param integer $sortNo
*
* @return \Customize\Entity\CategoryArtist
*/
public function calcChildrenSortNo(\Doctrine\ORM\EntityManager $em, $sortNo)
{
$this->setSortNo($this->getSortNo() + $sortNo);
$em->persist($this);
foreach ($this->getChildren() as $Child) {
$Child->calcChildrenSortNo($em, $sortNo);
}
return $this;
}
public function getParents()
{
$path = $this->getPath();
array_pop($path);
return $path;
}
public function getPath()
{
$path = [];
$CategoryArtist = $this;
$max = 10;
while ($max--) {
$path[] = $CategoryArtist;
$CategoryArtist = $CategoryArtist->getParent();
if (!$CategoryArtist || !$CategoryArtist->getId()) {
break;
}
}
return array_reverse($path);
}
public function getNameWithLevel()
{
return str_repeat(' ', $this->getHierarchy() - 1).$this->getName();
}
public function getDescendants()
{
$DescendantCategories = [];
$ChildCategories = $this->getChildren();
foreach ($ChildCategories as $ChildCategoryArtist) {
$DescendantCategories[$ChildCategoryArtist->getId()] = $ChildCategoryArtist;
$DescendantCategories2 = $ChildCategoryArtist->getDescendants();
foreach ($DescendantCategories2 as $DescendantCategoryArtist) {
$DescendantCategories[$DescendantCategoryArtist->getId()] = $DescendantCategoryArtist;
}
}
return $DescendantCategories;
}
public function getSelfAndDescendants()
{
return array_merge([$this], $this->getDescendants());
}
/**
* カテゴリに紐づく商品があるかどうかを調べる.
*
* ProductCategoriesはExtra Lazyのため, lengthやcountで評価した際にはCOUNTのSQLが発行されるが,
* COUNT自体が重いので, LIMIT 1で取得し存在チェックを行う.
*
* @see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#filtering-collections
*
* @return bool
*/
public function hasProductCategories()
{
$criteria = Criteria::create()
->orderBy(['category_id' => Criteria::ASC])
->setFirstResult(0)
->setMaxResults(1);
return $this->ProductCategoryArtists->matching($criteria)->count() > 0;
}
/**
* @var int
*
* @ORM\Column(name="id", type="integer", options={"unsigned":true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="category_name", type="string", length=255)
*/
private $name;
/**
* @var int
*
* @ORM\Column(name="hierarchy", type="integer", options={"unsigned":true})
*/
private $hierarchy;
/**
* @var int
*
* @ORM\Column(name="sort_no", type="integer")
*/
private $sort_no;
/**
* @var \DateTime
*
* @ORM\Column(name="create_date", type="datetimetz")
*/
private $create_date;
/**
* @var \DateTime
*
* @ORM\Column(name="update_date", type="datetimetz")
*/
private $update_date;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Customize\Entity\ProductCategoryArtist", mappedBy="CategoryArtist", fetch="EXTRA_LAZY")
*/
private $ProductCategoryArtists;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Customize\Entity\CategoryArtist", mappedBy="Parent")
* @ORM\OrderBy({
* "sort_no"="DESC"
* })
*/
private $Children;
/**
* @var \Customize\Entity\CategoryArtist
*
* @ORM\ManyToOne(targetEntity="Customize\Entity\CategoryArtist", inversedBy="Children")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="parent_category_id", referencedColumnName="id")
* })
*/
private $Parent;
/**
* @var \Eccube\Entity\Member
*
* @ORM\ManyToOne(targetEntity="Eccube\Entity\Member")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="creator_id", referencedColumnName="id")
* })
*/
private $Creator;
/**
* @var string
*
* @ORM\Column(name="category_kana", type="string", length=255, nullable=true)
*/
private $category_kana;
/**
* @var string
*
* @ORM\Column(name="category_likename", type="string", length=3000, nullable=true)
*/
private $category_likename;
/**
* @var string|null
*
* @ORM\Column(name="category_content", type="text", nullable=true)
*/
private $category_content;
/**
* @var \Customize\Entity\Master\ArtistTemplate
*
* @ORM\ManyToOne(targetEntity="Customize\Entity\Master\ArtistTemplate")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="template_id", referencedColumnName="id")
* })
*/
private $ArtistTemplate;
/**
* @var \Customize\Entity\Master\CategoryLabel
*
* @ORM\ManyToOne(targetEntity="Customize\Entity\Master\CategoryLabel")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="category_label", referencedColumnName="id", nullable=true)
* })
*/
private $CategoryLabel;
/**
* Constructor
*/
public function __construct()
{
$this->ProductCategoryArtists = new \Doctrine\Common\Collections\ArrayCollection();
$this->Children = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name.
*
* @param string $name
*
* @return CategoryArtist
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set hierarchy.
*
* @param int $hierarchy
*
* @return CategoryArtist
*/
public function setHierarchy($hierarchy)
{
$this->hierarchy = $hierarchy;
return $this;
}
/**
* Get hierarchy.
*
* @return int
*/
public function getHierarchy()
{
return $this->hierarchy;
}
/**
* Set sortNo.
*
* @param int $sortNo
*
* @return CategoryArtist
*/
public function setSortNo($sortNo)
{
$this->sort_no = $sortNo;
return $this;
}
/**
* Get sortNo.
*
* @return int
*/
public function getSortNo()
{
return $this->sort_no;
}
/**
* Set createDate.
*
* @param \DateTime $createDate
*
* @return CategoryArtist
*/
public function setCreateDate($createDate)
{
$this->create_date = $createDate;
return $this;
}
/**
* Get createDate.
*
* @return \DateTime
*/
public function getCreateDate()
{
return $this->create_date;
}
/**
* Set updateDate.
*
* @param \DateTime $updateDate
*
* @return CategoryArtist
*/
public function setUpdateDate($updateDate)
{
$this->update_date = $updateDate;
return $this;
}
/**
* Get updateDate.
*
* @return \DateTime
*/
public function getUpdateDate()
{
return $this->update_date;
}
/**
* Add productCategoryArtist.
*
* @param \Customize\Entity\ProductCategoryArtist $productCategoryArtist
*
* @return Category
*/
public function addProductCategoryArtist(ProductCategoryArtist $productCategoryArtist)
{
$this->ProductCategoryArtists[] = $productCategoryArtist;
return $this;
}
/**
* Remove productCategoryArtist.
*
* @param \Customize\Entity\ProductCategoryArtist $productCategoryArtist
*
* @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeProductCategoryArtist(ProductCategoryArtist $productCategoryArtist)
{
return $this->ProductCategoryArtists->removeElement($productCategoryArtist);
}
/**
* Get productCategoryArtists.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProductCategoryArtists()
{
return $this->ProductCategoryArtists;
}
/**
* Add child.
*
* @param \Customize\Entity\CategoryArtist $child
*
* @return CategoryArtist
*/
public function addChild(CategoryArtist $child)
{
$this->Children[] = $child;
return $this;
}
/**
* Remove child.
*
* @param \Customize\Entity\CategoryArtist $child
*
* @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeChild(CategoryArtist $child)
{
return $this->Children->removeElement($child);
}
/**
* Get children.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getChildren()
{
return $this->Children;
}
/**
* Set parent.
*
* @param \Customize\Entity\CategoryArtist|null $parent
*
* @return CategoryArtist
*/
public function setParent(CategoryArtist $parent = null)
{
$this->Parent = $parent;
return $this;
}
/**
* Get parent.
*
* @return \Customize\Entity\CategoryArtist|null
*/
public function getParent()
{
return $this->Parent;
}
/**
* Set creator.
*
* @param \Eccube\Entity\Member|null $creator
*
* @return CategoryArtist
*/
public function setCreator(\Eccube\Entity\Member $creator = null)
{
$this->Creator = $creator;
return $this;
}
/**
* Get creator.
*
* @return \Eccube\Entity\Member|null
*/
public function getCreator()
{
return $this->Creator;
}
/**
* Set category_kana.
*
* @param string $category_kana
*
* @return CategoryArtist
*/
public function setCategoryKana($category_kana)
{
$this->category_kana = $category_kana;
return $this;
}
/**
* Get category_kana.
*
* @return string
*/
public function getCategoryKana()
{
return $this->category_kana;
}
/**
* Set category_likename.
*
* @param string $category_likename
*
* @return CategoryArtist
*/
public function setCategoryLikename($category_likename)
{
$this->category_likename = $category_likename;
return $this;
}
/**
* Get category_likename.
*
* @return string
*/
public function getCategoryLikename()
{
return $this->category_likename;
}
/**
* Set category_content.
*
* @param string $category_content
*
* @return CategoryArtist
*/
public function setCategoryContent($category_content)
{
$this->category_content = $category_content;
return $this;
}
/**
* Get category_content.
*
* @return string
*/
public function getCategoryContent()
{
return $this->category_content;
}
/**
* Set artistTemplate.
*
* @param \Customize\Entity\Master\ArtistTemplate|null $artistTemplate
*
* @return CategoryArtist
*/
public function setArtistTemplate(\Customize\Entity\Master\ArtistTemplate $artistTemplate = null)
{
$this->ArtistTemplate = $artistTemplate;
return $this;
}
/**
* Get artistTemplate.
*
* @return \Customize\Entity\Master\ArtistTemplate|null
*/
public function getArtistTemplate()
{
return $this->ArtistTemplate;
}
/**
* Set categoryLabel.
*
* @param \Customize\Entity\Master\CategoryLabel|null $categoryLabel
*
* @return CategoryArtist
*/
public function setCategoryLabel(\Customize\Entity\Master\CategoryLabel $categoryLabel = null)
{
$this->CategoryLabel = $categoryLabel;
return $this;
}
/**
* Get categoryLabel.
*
* @return \Customize\Entity\Master\CategoryLabel|null
*/
public function getCategoryLabel()
{
return $this->CategoryLabel;
}
public function getCategoryLabelUrl()
{
$urls = [
1 => '/rock',
2 => '/metal',
3 => '/products/list',
4 => '/products/list',
];
return $urls[$this->CategoryLabel->getId()] ?? '/rock';
}
}
}