Spamworldpro Mini Shell
Spamworldpro


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/app/code/Cnc/CsBlock/Controller/FindMyPart/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/CsBlock/Controller/FindMyPart/SelectorCategories.php
<?php
/**
 * Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved.
 * https://opensource.org/licenses/OSL-3.0  Open Software License (OSL 3.0)
 * Cnc
 * Radosław Stępień <[email protected]> <[email protected]>
 */
namespace Cnc\CsBlock\Controller\FindMyPart;

use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;

class SelectorCategories extends Action
{
    /**
     * @var JsonFactory
     */
    private $jsonFactory;

    /**
     * @var CollectionFactory
     */
    private $categoryCollectionFactory;

    /**
     * SelectorCategories constructor.
     * @param Context $context
     * @param JsonFactory $jsonFactory
     * @param CollectionFactory $categoryCollectionFactory
     */
    public function __construct(
        Context $context,
        JsonFactory $jsonFactory,
        CollectionFactory $categoryCollectionFactory
    ) {
        parent::__construct($context);
        $this->jsonFactory = $jsonFactory;
        $this->categoryCollectionFactory = $categoryCollectionFactory;
    }

    /**
     * @return ResponseInterface|Json|ResultInterface
     * @throws LocalizedException
     */
    public function execute()
    {
        $result = $this->jsonFactory->create();
        $resultData = false;
        $isButtonEnabled = false;
        $childrenCategories = [];
        $categoryId = $this->getRequest()->getParam('categoryId');
        $selectorLevel = $this->getRequest()->getParam('selectorLevel');
        $placeholder = $this->getRequest()->getParam('placeholder');
        if (is_numeric($categoryId)) {
            if ($selectorLevel != 3) {
                /** @var CategoryInterface[] $childrenCategories */
                $childrenCategories = $this->categoryCollectionFactory->create()
                    ->addAttributeToSelect('name')
                    ->addAttributeToFilter('parent_id', ['eq' => $categoryId])
                    ->setOrder('name', 'asc')
                    ->getItems();
            }
            if ($selectorLevel == 3 || !count($childrenCategories)) {
                $isButtonEnabled = true;
                /** @var CategoryInterface $category */
                $category = $this->categoryCollectionFactory->create()
                    ->addAttributeToFilter('entity_id', ['eq' => $categoryId])
                    ->setOrder('name', 'asc')
                    ->getFirstItem();
                $resultData = $category->getUrl();
            } elseif (count($childrenCategories)) {
                //Build content for next select field with categories based on previous select choice
                if ($placeholder) {
                    $resultData .= '<option selected="selected">'
                        . $placeholder . '</option>';
                }
                foreach ($childrenCategories as $childrenCategory) {
                    $resultData .= '<option value="' . $childrenCategory->getId() . '">'
                        . $childrenCategory->getName() . '</option>';
                }
            }
        }
        return $result->setData(
            [
                'enable_button' => $isButtonEnabled,
                'content' => $resultData
            ]
        );
    }
}

Spamworldpro Mini