<?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\CategoryTv')) {
/**
* CategoryTv
*
* @ORM\Table(name="dtb_category_tv")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255)
* @ORM\HasLifecycleCallbacks()
* @ORM\Entity(repositoryClass="Customize\Repository\CategoryTvRepository")
*/
class CategoryTv 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\CategoryTv
*/
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 = [];
$CategoryTv = $this;
$max = 10;
while ($max--) {
$path[] = $CategoryTv;
$CategoryTv = $CategoryTv->getParent();
if (!$CategoryTv || !$CategoryTv->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 $ChildCategoryTv) {
$DescendantCategories[$ChildCategoryTv->getId()] = $ChildCategoryTv;
$DescendantCategories2 = $ChildCategoryTv->getDescendants();
foreach ($DescendantCategories2 as $DescendantCategoryTv) {
$DescendantCategories[$DescendantCategoryTv->getId()] = $DescendantCategoryTv;
}
}
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->ProductCategoryTvs->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\ProductCategoryTv", mappedBy="CategoryTv", fetch="EXTRA_LAZY")
*/
private $ProductCategoryTvs;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Customize\Entity\CategoryTv", mappedBy="Parent")
* @ORM\OrderBy({
* "sort_no"="DESC"
* })
*/
private $Children;
/**
* @var \Customize\Entity\CategoryTv
*
* @ORM\ManyToOne(targetEntity="Customize\Entity\CategoryTv", 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 int
*
* @ORM\Column(name="no", type="integer", nullable=true)
*/
private $no;
/**
* @var boolean
*
* @ORM\Column(name="reserve_flg", type="boolean", options={"default":false})
*/
private $reserve_flg = false;
/**
* @var \DateTime
*
* @ORM\Column(name="reserve_date", type="datetimetz", nullable=true)
*/
private $reserve_date;
/**
* @var string|null
*
* @ORM\Column(name="contents", type="text", nullable=true)
*/
private $contents;
/**
* Constructor
*/
public function __construct()
{
$this->ProductCategoryTvs = 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 CategoryTv
*/
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 CategoryTv
*/
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 CategoryTv
*/
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 CategoryTv
*/
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 CategoryTv
*/
public function setUpdateDate($updateDate)
{
$this->update_date = $updateDate;
return $this;
}
/**
* Get updateDate.
*
* @return \DateTime
*/
public function getUpdateDate()
{
return $this->update_date;
}
/**
* Add productCategoryTv.
*
* @param \Customize\Entity\ProductCategoryTv $productCategoryTv
*
* @return Category
*/
public function addProductCategoryTv(ProductCategoryTv $productCategoryTv)
{
$this->ProductCategoryTvs[] = $productCategoryTv;
return $this;
}
/**
* Remove productCategoryTv.
*
* @param \Customize\Entity\ProductCategoryTv $productCategoryTv
*
* @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeProductCategoryTv(ProductCategoryTv $productCategoryTv)
{
return $this->ProductCategoryTvs->removeElement($productCategoryTv);
}
/**
* Get productCategoryTvs.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProductCategoryTvs()
{
return $this->ProductCategoryTvs;
}
/**
* Add child.
*
* @param \Customize\Entity\CategoryTv $child
*
* @return CategoryTv
*/
public function addChild(CategoryTv $child)
{
$this->Children[] = $child;
return $this;
}
/**
* Remove child.
*
* @param \Customize\Entity\CategoryTv $child
*
* @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeChild(CategoryTv $child)
{
return $this->Children->removeElement($child);
}
/**
* Get children.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getChildren()
{
return $this->Children;
}
/**
* Set parent.
*
* @param \Customize\Entity\CategoryTv|null $parent
*
* @return CategoryTv
*/
public function setParent(CategoryTv $parent = null)
{
$this->Parent = $parent;
return $this;
}
/**
* Get parent.
*
* @return \Customize\Entity\CategoryTv|null
*/
public function getParent()
{
return $this->Parent;
}
/**
* Set creator.
*
* @param \Eccube\Entity\Member|null $creator
*
* @return CategoryTv
*/
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 no.
*
* @param int $no
*
* @return CategoryTv
*/
public function setNo($no)
{
$this->no = $no;
return $this;
}
/**
* Get no.
*
* @return int
*/
public function getNo()
{
return $this->no;
}
/**
* Set reserve_flg.
*
* @param boolean $reserve_flg
*
* @return CategoryTv
*/
public function setReserveFlg($reserve_flg)
{
$this->reserve_flg = $reserve_flg;
return $this;
}
/**
* Get reserve_flg.
*
* @return boolean
*/
public function isReserveFlg()
{
return $this->reserve_flg;
}
/**
* Set reserveDate.
*
* @param \DateTime $reserveDate
*
* @return CategoryTv
*/
public function setReserveDate($reserveDate)
{
$this->reserve_date = $reserveDate;
return $this;
}
/**
* Get reserveDate.
*
* @return \DateTime
*/
public function getReserveDate()
{
return $this->reserve_date;
}
/**
* @return string|null
*/
public function getContents()
{
return $this->contents;
}
/**
* @param string|null $contents
*
* @return $this
*/
public function setContents($contents = null)
{
$this->contents = $contents;
return $this;
}
}
}