app/Customize/Entity/CategoryArtist.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 Customize\Entity;
  13. use Doctrine\Common\Collections\Criteria;
  14. use Doctrine\ORM\Mapping as ORM;
  15. if (!class_exists('\Customize\Entity\CategoryArtist')) {
  16.     /**
  17.      * CategoryArtist
  18.      *
  19.      * @ORM\Table(name="dtb_category_artist")
  20.      * @ORM\InheritanceType("SINGLE_TABLE")
  21.      * @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255)
  22.      * @ORM\HasLifecycleCallbacks()
  23.      * @ORM\Entity(repositoryClass="Customize\Repository\CategoryArtistRepository")
  24.      */
  25.     class CategoryArtist extends \Eccube\Entity\AbstractEntity
  26.     {
  27.         /**
  28.          * @return string
  29.          */
  30.         public function __toString()
  31.         {
  32.             return (string) $this->getName();
  33.         }
  34.         /**
  35.          * @return integer
  36.          */
  37.         public function countBranches()
  38.         {
  39.             $count 1;
  40.             foreach ($this->getChildren() as $Child) {
  41.                 $count += $Child->countBranches();
  42.             }
  43.             return $count;
  44.         }
  45.         /**
  46.          * @param  \Doctrine\ORM\EntityManager $em
  47.          * @param  integer                     $sortNo
  48.          *
  49.          * @return \Customize\Entity\CategoryArtist
  50.          */
  51.         public function calcChildrenSortNo(\Doctrine\ORM\EntityManager $em$sortNo)
  52.         {
  53.             $this->setSortNo($this->getSortNo() + $sortNo);
  54.             $em->persist($this);
  55.             foreach ($this->getChildren() as $Child) {
  56.                 $Child->calcChildrenSortNo($em$sortNo);
  57.             }
  58.             return $this;
  59.         }
  60.         public function getParents()
  61.         {
  62.             $path $this->getPath();
  63.             array_pop($path);
  64.             return $path;
  65.         }
  66.         public function getPath()
  67.         {
  68.             $path = [];
  69.             $CategoryArtist $this;
  70.             $max 10;
  71.             while ($max--) {
  72.                 $path[] = $CategoryArtist;
  73.                 $CategoryArtist $CategoryArtist->getParent();
  74.                 if (!$CategoryArtist || !$CategoryArtist->getId()) {
  75.                     break;
  76.                 }
  77.             }
  78.             return array_reverse($path);
  79.         }
  80.         public function getNameWithLevel()
  81.         {
  82.             return str_repeat(' '$this->getHierarchy() - 1).$this->getName();
  83.         }
  84.         public function getDescendants()
  85.         {
  86.             $DescendantCategories = [];
  87.             $ChildCategories $this->getChildren();
  88.             foreach ($ChildCategories as $ChildCategoryArtist) {
  89.                 $DescendantCategories[$ChildCategoryArtist->getId()] = $ChildCategoryArtist;
  90.                 $DescendantCategories2 $ChildCategoryArtist->getDescendants();
  91.                 foreach ($DescendantCategories2 as $DescendantCategoryArtist) {
  92.                     $DescendantCategories[$DescendantCategoryArtist->getId()] = $DescendantCategoryArtist;
  93.                 }
  94.             }
  95.             return $DescendantCategories;
  96.         }
  97.         public function getSelfAndDescendants()
  98.         {
  99.             return array_merge([$this], $this->getDescendants());
  100.         }
  101.         /**
  102.          * カテゴリに紐づく商品があるかどうかを調べる.
  103.          *
  104.          * ProductCategoriesはExtra Lazyのため, lengthやcountで評価した際にはCOUNTのSQLが発行されるが,
  105.          * COUNT自体が重いので, LIMIT 1で取得し存在チェックを行う.
  106.          *
  107.          * @see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#filtering-collections
  108.          *
  109.          * @return bool
  110.          */
  111.         public function hasProductCategories()
  112.         {
  113.             $criteria Criteria::create()
  114.             ->orderBy(['category_id' => Criteria::ASC])
  115.             ->setFirstResult(0)
  116.             ->setMaxResults(1);
  117.             return $this->ProductCategoryArtists->matching($criteria)->count() > 0;
  118.         }
  119.         /**
  120.          * @var int
  121.          *
  122.          * @ORM\Column(name="id", type="integer", options={"unsigned":true})
  123.          * @ORM\Id
  124.          * @ORM\GeneratedValue(strategy="IDENTITY")
  125.          */
  126.         private $id;
  127.         /**
  128.          * @var string
  129.          *
  130.          * @ORM\Column(name="category_name", type="string", length=255)
  131.          */
  132.         private $name;
  133.         /**
  134.          * @var int
  135.          *
  136.          * @ORM\Column(name="hierarchy", type="integer", options={"unsigned":true})
  137.          */
  138.         private $hierarchy;
  139.         /**
  140.          * @var int
  141.          *
  142.          * @ORM\Column(name="sort_no", type="integer")
  143.          */
  144.         private $sort_no;
  145.         /**
  146.          * @var \DateTime
  147.          *
  148.          * @ORM\Column(name="create_date", type="datetimetz")
  149.          */
  150.         private $create_date;
  151.         /**
  152.          * @var \DateTime
  153.          *
  154.          * @ORM\Column(name="update_date", type="datetimetz")
  155.          */
  156.         private $update_date;
  157.         /**
  158.          * @var \Doctrine\Common\Collections\Collection
  159.          *
  160.          * @ORM\OneToMany(targetEntity="Customize\Entity\ProductCategoryArtist", mappedBy="CategoryArtist", fetch="EXTRA_LAZY")
  161.          */
  162.         private $ProductCategoryArtists;
  163.         /**
  164.          * @var \Doctrine\Common\Collections\Collection
  165.          *
  166.          * @ORM\OneToMany(targetEntity="Customize\Entity\CategoryArtist", mappedBy="Parent")
  167.          * @ORM\OrderBy({
  168.          *     "sort_no"="DESC"
  169.          * })
  170.          */
  171.         private $Children;
  172.         /**
  173.          * @var \Customize\Entity\CategoryArtist
  174.          *
  175.          * @ORM\ManyToOne(targetEntity="Customize\Entity\CategoryArtist", inversedBy="Children")
  176.          * @ORM\JoinColumns({
  177.          *   @ORM\JoinColumn(name="parent_category_id", referencedColumnName="id")
  178.          * })
  179.          */
  180.         private $Parent;
  181.         /**
  182.          * @var \Eccube\Entity\Member
  183.          *
  184.          * @ORM\ManyToOne(targetEntity="Eccube\Entity\Member")
  185.          * @ORM\JoinColumns({
  186.          *   @ORM\JoinColumn(name="creator_id", referencedColumnName="id")
  187.          * })
  188.          */
  189.         private $Creator;
  190.         /**
  191.          * @var string
  192.          *
  193.          * @ORM\Column(name="category_kana", type="string", length=255, nullable=true)
  194.          */
  195.         private $category_kana;
  196.         /**
  197.          * @var string
  198.          *
  199.          * @ORM\Column(name="category_likename", type="string", length=3000, nullable=true)
  200.          */
  201.         private $category_likename;
  202.         /**
  203.          * @var string|null
  204.          *
  205.          * @ORM\Column(name="category_content", type="text", nullable=true)
  206.          */
  207.         private $category_content;
  208.         /**
  209.          * @var \Customize\Entity\Master\ArtistTemplate
  210.          *
  211.          * @ORM\ManyToOne(targetEntity="Customize\Entity\Master\ArtistTemplate")
  212.          * @ORM\JoinColumns({
  213.          *   @ORM\JoinColumn(name="template_id", referencedColumnName="id")
  214.          * })
  215.          */
  216.         private $ArtistTemplate;
  217.         /**
  218.          * @var \Customize\Entity\Master\CategoryLabel
  219.          *
  220.          * @ORM\ManyToOne(targetEntity="Customize\Entity\Master\CategoryLabel")
  221.          * @ORM\JoinColumns({
  222.          *   @ORM\JoinColumn(name="category_label", referencedColumnName="id", nullable=true)
  223.          * })
  224.          */
  225.         private $CategoryLabel;
  226.         /**
  227.          * Constructor
  228.          */
  229.         public function __construct()
  230.         {
  231.             $this->ProductCategoryArtists = new \Doctrine\Common\Collections\ArrayCollection();
  232.             $this->Children = new \Doctrine\Common\Collections\ArrayCollection();
  233.         }
  234.         /**
  235.          * Get id.
  236.          *
  237.          * @return int
  238.          */
  239.         public function getId()
  240.         {
  241.             return $this->id;
  242.         }
  243.         /**
  244.          * Set name.
  245.          *
  246.          * @param string $name
  247.          *
  248.          * @return CategoryArtist
  249.          */
  250.         public function setName($name)
  251.         {
  252.             $this->name $name;
  253.             return $this;
  254.         }
  255.         /**
  256.          * Get name.
  257.          *
  258.          * @return string
  259.          */
  260.         public function getName()
  261.         {
  262.             return $this->name;
  263.         }
  264.         /**
  265.          * Set hierarchy.
  266.          *
  267.          * @param int $hierarchy
  268.          *
  269.          * @return CategoryArtist
  270.          */
  271.         public function setHierarchy($hierarchy)
  272.         {
  273.             $this->hierarchy $hierarchy;
  274.             return $this;
  275.         }
  276.         /**
  277.          * Get hierarchy.
  278.          *
  279.          * @return int
  280.          */
  281.         public function getHierarchy()
  282.         {
  283.             return $this->hierarchy;
  284.         }
  285.         /**
  286.          * Set sortNo.
  287.          *
  288.          * @param int $sortNo
  289.          *
  290.          * @return CategoryArtist
  291.          */
  292.         public function setSortNo($sortNo)
  293.         {
  294.             $this->sort_no $sortNo;
  295.             return $this;
  296.         }
  297.         /**
  298.          * Get sortNo.
  299.          *
  300.          * @return int
  301.          */
  302.         public function getSortNo()
  303.         {
  304.             return $this->sort_no;
  305.         }
  306.         /**
  307.          * Set createDate.
  308.          *
  309.          * @param \DateTime $createDate
  310.          *
  311.          * @return CategoryArtist
  312.          */
  313.         public function setCreateDate($createDate)
  314.         {
  315.             $this->create_date $createDate;
  316.             return $this;
  317.         }
  318.         /**
  319.          * Get createDate.
  320.          *
  321.          * @return \DateTime
  322.          */
  323.         public function getCreateDate()
  324.         {
  325.             return $this->create_date;
  326.         }
  327.         /**
  328.          * Set updateDate.
  329.          *
  330.          * @param \DateTime $updateDate
  331.          *
  332.          * @return CategoryArtist
  333.          */
  334.         public function setUpdateDate($updateDate)
  335.         {
  336.             $this->update_date $updateDate;
  337.             return $this;
  338.         }
  339.         /**
  340.          * Get updateDate.
  341.          *
  342.          * @return \DateTime
  343.          */
  344.         public function getUpdateDate()
  345.         {
  346.             return $this->update_date;
  347.         }
  348.         /**
  349.          * Add productCategoryArtist.
  350.          *
  351.          * @param \Customize\Entity\ProductCategoryArtist $productCategoryArtist
  352.          *
  353.          * @return Category
  354.          */
  355.         public function addProductCategoryArtist(ProductCategoryArtist $productCategoryArtist)
  356.         {
  357.             $this->ProductCategoryArtists[] = $productCategoryArtist;
  358.             return $this;
  359.         }
  360.         /**
  361.          * Remove productCategoryArtist.
  362.          *
  363.          * @param \Customize\Entity\ProductCategoryArtist $productCategoryArtist
  364.          *
  365.          * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  366.          */
  367.         public function removeProductCategoryArtist(ProductCategoryArtist $productCategoryArtist)
  368.         {
  369.             return $this->ProductCategoryArtists->removeElement($productCategoryArtist);
  370.         }
  371.         /**
  372.          * Get productCategoryArtists.
  373.          *
  374.          * @return \Doctrine\Common\Collections\Collection
  375.          */
  376.         public function getProductCategoryArtists()
  377.         {
  378.             return $this->ProductCategoryArtists;
  379.         }
  380.         /**
  381.          * Add child.
  382.          *
  383.          * @param \Customize\Entity\CategoryArtist $child
  384.          *
  385.          * @return CategoryArtist
  386.          */
  387.         public function addChild(CategoryArtist $child)
  388.         {
  389.             $this->Children[] = $child;
  390.             return $this;
  391.         }
  392.         /**
  393.          * Remove child.
  394.          *
  395.          * @param \Customize\Entity\CategoryArtist $child
  396.          *
  397.          * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  398.          */
  399.         public function removeChild(CategoryArtist $child)
  400.         {
  401.             return $this->Children->removeElement($child);
  402.         }
  403.         /**
  404.          * Get children.
  405.          *
  406.          * @return \Doctrine\Common\Collections\Collection
  407.          */
  408.         public function getChildren()
  409.         {
  410.             return $this->Children;
  411.         }
  412.         /**
  413.          * Set parent.
  414.          *
  415.          * @param \Customize\Entity\CategoryArtist|null $parent
  416.          *
  417.          * @return CategoryArtist
  418.          */
  419.         public function setParent(CategoryArtist $parent null)
  420.         {
  421.             $this->Parent $parent;
  422.             return $this;
  423.         }
  424.         /**
  425.          * Get parent.
  426.          *
  427.          * @return \Customize\Entity\CategoryArtist|null
  428.          */
  429.         public function getParent()
  430.         {
  431.             return $this->Parent;
  432.         }
  433.         /**
  434.          * Set creator.
  435.          *
  436.          * @param \Eccube\Entity\Member|null $creator
  437.          *
  438.          * @return CategoryArtist
  439.          */
  440.         public function setCreator(\Eccube\Entity\Member $creator null)
  441.         {
  442.             $this->Creator $creator;
  443.             return $this;
  444.         }
  445.         /**
  446.          * Get creator.
  447.          *
  448.          * @return \Eccube\Entity\Member|null
  449.          */
  450.         public function getCreator()
  451.         {
  452.             return $this->Creator;
  453.         }
  454.         /**
  455.          * Set category_kana.
  456.          *
  457.          * @param string $category_kana
  458.          *
  459.          * @return CategoryArtist
  460.          */
  461.         public function setCategoryKana($category_kana)
  462.         {
  463.             $this->category_kana $category_kana;
  464.             return $this;
  465.         }
  466.         /**
  467.          * Get category_kana.
  468.          *
  469.          * @return string
  470.          */
  471.         public function getCategoryKana()
  472.         {
  473.             return $this->category_kana;
  474.         }
  475.         /**
  476.          * Set category_likename.
  477.          *
  478.          * @param string $category_likename
  479.          *
  480.          * @return CategoryArtist
  481.          */
  482.         public function setCategoryLikename($category_likename)
  483.         {
  484.             $this->category_likename $category_likename;
  485.             return $this;
  486.         }
  487.         /**
  488.          * Get category_likename.
  489.          *
  490.          * @return string
  491.          */
  492.         public function getCategoryLikename()
  493.         {
  494.             return $this->category_likename;
  495.         }
  496.         /**
  497.          * Set category_content.
  498.          *
  499.          * @param string $category_content
  500.          *
  501.          * @return CategoryArtist
  502.          */
  503.         public function setCategoryContent($category_content)
  504.         {
  505.             $this->category_content $category_content;
  506.             return $this;
  507.         }
  508.         /**
  509.          * Get category_content.
  510.          *
  511.          * @return string
  512.          */
  513.         public function getCategoryContent()
  514.         {
  515.             return $this->category_content;
  516.         }
  517.         /**
  518.          * Set artistTemplate.
  519.          *
  520.          * @param \Customize\Entity\Master\ArtistTemplate|null $artistTemplate
  521.          *
  522.          * @return CategoryArtist
  523.          */
  524.         public function setArtistTemplate(\Customize\Entity\Master\ArtistTemplate $artistTemplate null)
  525.         {
  526.             $this->ArtistTemplate $artistTemplate;
  527.             return $this;
  528.         }
  529.         /**
  530.          * Get artistTemplate.
  531.          *
  532.          * @return \Customize\Entity\Master\ArtistTemplate|null
  533.          */
  534.         public function getArtistTemplate()
  535.         {
  536.             return $this->ArtistTemplate;
  537.         }
  538.         /**
  539.          * Set categoryLabel.
  540.          *
  541.          * @param \Customize\Entity\Master\CategoryLabel|null $categoryLabel
  542.          *
  543.          * @return CategoryArtist
  544.          */
  545.         public function setCategoryLabel(\Customize\Entity\Master\CategoryLabel $categoryLabel null)
  546.         {
  547.             $this->CategoryLabel $categoryLabel;
  548.             return $this;
  549.         }
  550.         /**
  551.          * Get categoryLabel.
  552.          *
  553.          * @return \Customize\Entity\Master\CategoryLabel|null
  554.          */
  555.         public function getCategoryLabel()
  556.         {
  557.             return $this->CategoryLabel;
  558.         }
  559.         public function getCategoryLabelUrl()
  560.         {
  561.             $urls = [
  562.                 => '/rock',
  563.                 => '/metal',
  564.                 => '/products/list',
  565.                 => '/products/list',
  566.             ];
  567.             return $urls[$this->CategoryLabel->getId()] ?? '/rock';
  568.         }
  569.     }
  570. }