TabsSelect.php 2.97 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
<?php
/**
 *
 * SM Listing Tabs - Version 2.5.0
 * Copyright (c) 2017 YouTech Company. All Rights Reserved.
 * @license - Copyrighted Commercial Software
 * Author: YouTech Company
 * Websites: http://www.magentech.com
 */
 
namespace Sm\ListingTabs\Model\Config\Source;

use \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;

class TabsSelect implements \Magento\Framework\Option\ArrayInterface
{
	protected $_categoryCollectionFactory;

	public function __construct(\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $objectManager)
	{
		$this->_categoryCollectionFactory = $objectManager;
	}

	public function toOptionArray()
	{
		$collection = $this->_categoryCollectionFactory->create();
		$collection->addAttributeToSelect('name')
			->addPathFilter('^1/[0-9/]+')
			->addIsActiveFilter()
			->load();

		$options = [];
		$opt_cats = [];
		$cats = [];

		foreach ($collection as $category) {
			$c = new \stdClass();
			$c->label = $category->getName();
			$c->value = $category->getId();
			$c->level = $category->getLevel();
			$c->parentid = $category->getParentId();
			$cats[$c->value] = $c;
		}

		foreach ($cats as $id => $c) {
			if (isset($cats[$c->parentid])) {
				if (!isset($cats[$c->parentid]->child)) {
					$cats[$c->parentid]->child = array();
				}
				$cats[$c->parentid]->child[] =& $cats[$id];
			}
		}
		foreach ($cats as $id => $c) {
			if (!isset($cats[$c->parentid])) {
				$stack = array($cats[$id]);
				while (count($stack) > 0) {
					$opt = array_pop($stack);
					$option = array(
						/*'label' => ($opt->level > 1 ? str_repeat('- - ', $opt->level - 1) : '') . $opt->label,*/
						'label' => '- - ' . $opt->label,
						'value' => $opt->value
					);
					array_push($opt_cats, $option);
					if (isset($opt->child) && count($opt->child)) {
						foreach (array_reverse($opt->child) as $child) {
							array_push($stack, $child);
						}
					}
				}
			}
		}
		unset($cats);
		$group_field =  [
                            'label' => '____________ Field Product ____________',
                            'value' => [
								['value'=>'name', 'label'=>__('- - Name')],
								['value'=>'entity_id', 'label'=>__('- - Id')],
								['value'=>'created_at', 'label'=>__('- - Date Created')],
								['value'=>'price', 'label'=>__('- - Price')],
								['value'=>'lastest_product', 'label'=>__('- - Lastest Product')],
								['value'=>'top_rating', 'label'=>__('- - Top Rating')],
								['value'=>'most_reviewed', 'label'=>__('- - Most Reviews')],
								['value'=>'most_viewed', 'label'=>__('- - Most Viewed')],
								['value'=>'best_sales', 'label'=>__('- - Most Selling')],
								['value'=>'random', 'label'=>__('- - Random')]
							]
                        ];
						
		$group_cats = [
					'label' => '____________ Select Categories ____________',
					'value' => $opt_cats
				];				
		array_push($options, $group_field, $group_cats);
		return $options;
	}
}