src/Eccube/Entity/Category.php line 29

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Entity;
  13. use Doctrine\Common\Collections\Criteria;
  14. use Doctrine\ORM\Mapping as ORM;
  15. if (!class_exists('\Eccube\Entity\Category')) {
  16.     /**
  17.      * Category
  18.      *
  19.      * @ORM\Table(name="dtb_category")
  20.      * @ORM\InheritanceType("SINGLE_TABLE")
  21.      * @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255)
  22.      * @ORM\HasLifecycleCallbacks()
  23.      * @ORM\Entity(repositoryClass="Eccube\Repository\CategoryRepository")
  24.      */
  25.     class Category extends \Eccube\Entity\AbstractEntity
  26.     {
  27.         public static $CATEGORY_TYPE_ROUTER_TERMINAL_ID 1001;
  28.         public static $CATEGORY_TYPE_GIGABIT_ADDITION_ID 1002;
  29.         public static $CATEGORY_TYPE_GIGABIT_ADDITION_OVERSEA_ID 1003;
  30.         public static function getCategoryPageName($categoryId) {
  31.             if ($categoryId == self::$CATEGORY_TYPE_ROUTER_TERMINAL_ID) {
  32.                 return trans('front.product.router_terminal.page_name');
  33.             } elseif ($categoryId == self::$CATEGORY_TYPE_GIGABIT_ADDITION_ID) {
  34.                 return trans('front.product.gigabit_addition.page_name');
  35.             } elseif ($categoryId == self::$CATEGORY_TYPE_GIGABIT_ADDITION_OVERSEA_ID) {
  36.                 return trans('front.product.gigabit_addition_oversea.page_name');
  37.             }
  38.             return '';
  39.         }
  40.         /**
  41.          * @return string
  42.          */
  43.         public function __toString()
  44.         {
  45.             return (string) $this->getName();
  46.         }
  47.         /**
  48.          * @return integer
  49.          */
  50.         public function countBranches()
  51.         {
  52.             $count 1;
  53.             foreach ($this->getChildren() as $Child) {
  54.                 $count += $Child->countBranches();
  55.             }
  56.             return $count;
  57.         }
  58.         /**
  59.          * @param  \Doctrine\ORM\EntityManager $em
  60.          * @param  integer                     $sortNo
  61.          *
  62.          * @return \Eccube\Entity\Category
  63.          */
  64.         public function calcChildrenSortNo(\Doctrine\ORM\EntityManager $em$sortNo)
  65.         {
  66.             $this->setSortNo($this->getSortNo() + $sortNo);
  67.             $em->persist($this);
  68.             foreach ($this->getChildren() as $Child) {
  69.                 $Child->calcChildrenSortNo($em$sortNo);
  70.             }
  71.             return $this;
  72.         }
  73.         public function getParents()
  74.         {
  75.             $path $this->getPath();
  76.             array_pop($path);
  77.             return $path;
  78.         }
  79.         public function getPath()
  80.         {
  81.             $path = [];
  82.             $Category $this;
  83.             $max 10;
  84.             while ($max--) {
  85.                 $path[] = $Category;
  86.                 $Category $Category->getParent();
  87.                 if (!$Category || !$Category->getId()) {
  88.                     break;
  89.                 }
  90.             }
  91.             return array_reverse($path);
  92.         }
  93.         public function getNameWithLevel()
  94.         {
  95.             return str_repeat(' '$this->getHierarchy() - 1).$this->getName();
  96.         }
  97.         public function getDescendants()
  98.         {
  99.             $DescendantCategories = [];
  100.             $ChildCategories $this->getChildren();
  101.             foreach ($ChildCategories as $ChildCategory) {
  102.                 $DescendantCategories[$ChildCategory->getId()] = $ChildCategory;
  103.                 $DescendantCategories2 $ChildCategory->getDescendants();
  104.                 foreach ($DescendantCategories2 as $DescendantCategory) {
  105.                     $DescendantCategories[$DescendantCategory->getId()] = $DescendantCategory;
  106.                 }
  107.             }
  108.             return $DescendantCategories;
  109.         }
  110.         public function getSelfAndDescendants()
  111.         {
  112.             return array_merge([$this], $this->getDescendants());
  113.         }
  114.         /**
  115.          * カテゴリに紐づく商品があるかどうかを調べる.
  116.          *
  117.          * ProductCategoriesはExtra Lazyのため, lengthやcountで評価した際にはCOUNTのSQLが発行されるが,
  118.          * COUNT自体が重いので, LIMIT 1で取得し存在チェックを行う.
  119.          *
  120.          * @see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#filtering-collections
  121.          *
  122.          * @return bool
  123.          */
  124.         public function hasProductCategories()
  125.         {
  126.             $criteria Criteria::create()
  127.             ->orderBy(['category_id' => Criteria::ASC])
  128.             ->setFirstResult(0)
  129.             ->setMaxResults(1);
  130.             return $this->ProductCategories->matching($criteria)->count() > 0;
  131.         }
  132.         /**
  133.          * @var int
  134.          *
  135.          * @ORM\Column(name="id", type="integer", options={"unsigned":true})
  136.          * @ORM\Id
  137.          * @ORM\GeneratedValue(strategy="IDENTITY")
  138.          */
  139.         private $id;
  140.         /**
  141.          * @var string
  142.          *
  143.          * @ORM\Column(name="category_name", type="string", length=255)
  144.          */
  145.         private $name;
  146.         /**
  147.          * @var int
  148.          *
  149.          * @ORM\Column(name="hierarchy", type="integer", options={"unsigned":true})
  150.          */
  151.         private $hierarchy;
  152.         /**
  153.          * @var int
  154.          *
  155.          * @ORM\Column(name="sort_no", type="integer")
  156.          */
  157.         private $sort_no;
  158.         /**
  159.          * @var \DateTime
  160.          *
  161.          * @ORM\Column(name="create_date", type="datetimetz")
  162.          */
  163.         private $create_date;
  164.         /**
  165.          * @var \DateTime
  166.          *
  167.          * @ORM\Column(name="update_date", type="datetimetz")
  168.          */
  169.         private $update_date;
  170.         /**
  171.          * @var \Doctrine\Common\Collections\Collection
  172.          *
  173.          * @ORM\OneToMany(targetEntity="Eccube\Entity\ProductCategory", mappedBy="Category", fetch="EXTRA_LAZY")
  174.          */
  175.         private $ProductCategories;
  176.         /**
  177.          * @var \Doctrine\Common\Collections\Collection
  178.          *
  179.          * @ORM\OneToMany(targetEntity="Eccube\Entity\Category", mappedBy="Parent")
  180.          * @ORM\OrderBy({
  181.          *     "sort_no"="DESC"
  182.          * })
  183.          */
  184.         private $Children;
  185.         /**
  186.          * @var \Eccube\Entity\Category
  187.          *
  188.          * @ORM\ManyToOne(targetEntity="Eccube\Entity\Category", inversedBy="Children")
  189.          * @ORM\JoinColumns({
  190.          *   @ORM\JoinColumn(name="parent_category_id", referencedColumnName="id")
  191.          * })
  192.          */
  193.         private $Parent;
  194.         /**
  195.          * @var \Eccube\Entity\Member
  196.          *
  197.          * @ORM\ManyToOne(targetEntity="Eccube\Entity\Member")
  198.          * @ORM\JoinColumns({
  199.          *   @ORM\JoinColumn(name="creator_id", referencedColumnName="id")
  200.          * })
  201.          */
  202.             private $Creator;
  203.         /**
  204.          * @var int
  205.          *
  206.          * @ORM\Column(name="is_searchable", type="smallint", options={"default":0})
  207.          */
  208.         private $isSearchable 0;
  209.     /**
  210.      * Constructor
  211.      */
  212.         public function __construct()
  213.         {
  214.             $this->ProductCategories = new \Doctrine\Common\Collections\ArrayCollection();
  215.             $this->Children = new \Doctrine\Common\Collections\ArrayCollection();
  216.         }
  217.         /**
  218.          * Get id.
  219.          *
  220.          * @return int
  221.          */
  222.         public function getId()
  223.         {
  224.             return $this->id;
  225.         }
  226.         /**
  227.          * Set name.
  228.          *
  229.          * @param string $name
  230.          *
  231.          * @return Category
  232.          */
  233.         public function setName($name)
  234.         {
  235.             $this->name $name;
  236.             return $this;
  237.         }
  238.         /**
  239.          * Get name.
  240.          *
  241.          * @return string
  242.          */
  243.         public function getName()
  244.         {
  245.             return $this->name;
  246.         }
  247.         /**
  248.          * Set hierarchy.
  249.          *
  250.          * @param int $hierarchy
  251.          *
  252.          * @return Category
  253.          */
  254.         public function setHierarchy($hierarchy)
  255.         {
  256.             $this->hierarchy $hierarchy;
  257.             return $this;
  258.         }
  259.         /**
  260.          * Get hierarchy.
  261.          *
  262.          * @return int
  263.          */
  264.         public function getHierarchy()
  265.         {
  266.             return $this->hierarchy;
  267.         }
  268.         /**
  269.          * Set sortNo.
  270.          *
  271.          * @param int $sortNo
  272.          *
  273.          * @return Category
  274.          */
  275.         public function setSortNo($sortNo)
  276.         {
  277.             $this->sort_no $sortNo;
  278.             return $this;
  279.         }
  280.         /**
  281.          * Get sortNo.
  282.          *
  283.          * @return int
  284.          */
  285.         public function getSortNo()
  286.         {
  287.             return $this->sort_no;
  288.         }
  289.         /**
  290.          * Set createDate.
  291.          *
  292.          * @param \DateTime $createDate
  293.          *
  294.          * @return Category
  295.          */
  296.         public function setCreateDate($createDate)
  297.         {
  298.             $this->create_date $createDate;
  299.             return $this;
  300.         }
  301.         /**
  302.          * Get createDate.
  303.          *
  304.          * @return \DateTime
  305.          */
  306.         public function getCreateDate()
  307.         {
  308.             return $this->create_date;
  309.         }
  310.         /**
  311.          * Set updateDate.
  312.          *
  313.          * @param \DateTime $updateDate
  314.          *
  315.          * @return Category
  316.          */
  317.         public function setUpdateDate($updateDate)
  318.         {
  319.             $this->update_date $updateDate;
  320.             return $this;
  321.         }
  322.         /**
  323.          * Get updateDate.
  324.          *
  325.          * @return \DateTime
  326.          */
  327.         public function getUpdateDate()
  328.         {
  329.             return $this->update_date;
  330.         }
  331.         /**
  332.          * Add productCategory.
  333.          *
  334.          * @param \Eccube\Entity\ProductCategory $productCategory
  335.          *
  336.          * @return Category
  337.          */
  338.         public function addProductCategory(ProductCategory $productCategory)
  339.         {
  340.             $this->ProductCategories[] = $productCategory;
  341.             return $this;
  342.         }
  343.         /**
  344.          * Remove productCategory.
  345.          *
  346.          * @param \Eccube\Entity\ProductCategory $productCategory
  347.          *
  348.          * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  349.          */
  350.         public function removeProductCategory(ProductCategory $productCategory)
  351.         {
  352.             return $this->ProductCategories->removeElement($productCategory);
  353.         }
  354.         /**
  355.          * Get productCategories.
  356.          *
  357.          * @return \Doctrine\Common\Collections\Collection
  358.          */
  359.         public function getProductCategories()
  360.         {
  361.             return $this->ProductCategories;
  362.         }
  363.         /**
  364.          * Add child.
  365.          *
  366.          * @param \Eccube\Entity\Category $child
  367.          *
  368.          * @return Category
  369.          */
  370.         public function addChild(Category $child)
  371.         {
  372.             $this->Children[] = $child;
  373.             return $this;
  374.         }
  375.         /**
  376.          * Remove child.
  377.          *
  378.          * @param \Eccube\Entity\Category $child
  379.          *
  380.          * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  381.          */
  382.         public function removeChild(Category $child)
  383.         {
  384.             return $this->Children->removeElement($child);
  385.         }
  386.         /**
  387.          * Get children.
  388.          *
  389.          * @return \Doctrine\Common\Collections\Collection
  390.          */
  391.         public function getChildren()
  392.         {
  393.             return $this->Children;
  394.         }
  395.         /**
  396.          * Set parent.
  397.          *
  398.          * @param \Eccube\Entity\Category|null $parent
  399.          *
  400.          * @return Category
  401.          */
  402.         public function setParent(Category $parent null)
  403.         {
  404.             $this->Parent $parent;
  405.             return $this;
  406.         }
  407.         /**
  408.          * Get parent.
  409.          *
  410.          * @return \Eccube\Entity\Category|null
  411.          */
  412.         public function getParent()
  413.         {
  414.             return $this->Parent;
  415.         }
  416.         /**
  417.          * Set creator.
  418.          *
  419.          * @param \Eccube\Entity\Member|null $creator
  420.          *
  421.          * @return Category
  422.          */
  423.         public function setCreator(Member $creator null)
  424.         {
  425.             $this->Creator $creator;
  426.             return $this;
  427.         }
  428.         /**
  429.          * Get creator.
  430.          *
  431.          * @return \Eccube\Entity\Member|null
  432.          */
  433.             public function getCreator()
  434.     {
  435.         return $this->Creator;
  436.     }
  437.     /**
  438.      * Set isSearchable.
  439.      *
  440.      * @param int $isSearchable
  441.      *
  442.      * @return Category
  443.      */
  444.     public function setIsSearchable($isSearchable)
  445.     {
  446.         $this->isSearchable $isSearchable;
  447.         return $this;
  448.     }
  449.     /**
  450.      * Get isSearchable.
  451.      *
  452.      * @return int
  453.      */
  454.     public function isSearchable()
  455.     {
  456.         return $this->isSearchable;
  457.     }
  458. }
  459. }