![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/amasty/shopby/Model/Source/ |
<?php /** * @author Amasty Team * @copyright Copyright (c) Amasty (https://www.amasty.com) * @package Improved Layered Navigation Base for Magento 2 */ namespace Amasty\Shopby\Model\Source; use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; use Magento\Store\Model\StoreManager; class Category { public const SYSTEM_CATEGORY_ID = 1; public const ROOT_LEVEL = 1; /** * @var CollectionFactory */ private $collectionFactory; /** * @var bool */ private $emptyOption = true; /** * @var StoreManager */ private $storeManager; /** * Category constructor. * @param CollectionFactory $collectionFactory */ public function __construct( CollectionFactory $collectionFactory, StoreManager $storeManager ) { $this->collectionFactory = $collectionFactory; $this->storeManager = $storeManager; } /** * Options getter * * @return array */ public function toOptionArray() { $optionArray = []; $arr = $this->toArray(); foreach ($arr as $value => $label) { $optionArray[] = [ 'value' => $value, 'label' => $label ]; } return $optionArray; } /** * Get options in "key-value" format * * @return array */ public function toArray() { $options = []; if ($this->emptyOption) { $options[0] = ' '; } $options = array_replace( $options, $this->getChildren(self::SYSTEM_CATEGORY_ID, self::ROOT_LEVEL) ); return $options; } private function getChildren($parentCategoryId, $level) { $options = []; $collection = $this->collectionFactory->create(); $collection->addAttributeToSelect('name'); $collection->addAttributeToFilter('level', $level); $collection->addAttributeToFilter('parent_id', $parentCategoryId); $collection->setOrder('position', 'asc'); foreach ($collection as $category) { $options[$category->getId()] = str_repeat(". ", max(0, ($category->getLevel() - 1) * 3)) . $category->getName(); if ($category->hasChildren()) { $options = array_replace($options, $this->getChildren($category->getId(), $category->getLevel() + 1)); } } return $options; } /** * @param bool $emptyOption * @return $this */ public function setEmptyOption($emptyOption) { $this->emptyOption = $emptyOption; return $this; } }