Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
CategoryWidget.php 5.87 KiB
Newer Older
  • Learn to ignore specific revisions
  • Nicolas's avatar
    Nicolas committed
    <?php
    namespace NicolasBejean\CategoryWidget\Block\Widget;
    
    
    use Magento\Framework\View\Element\Template;
    use Magento\Widget\Block\BlockInterface;
    
    class CategoryWidget extends Template implements BlockInterface
    
    Nicolas's avatar
    Nicolas committed
    {
        /**
    
         * Category helper
         *
         * @var \Magento\Catalog\Helper\Category
    
    Nicolas's avatar
    Nicolas committed
         */
        protected $_categoryHelper;
    
    
        /**
         * Category Flat Config
         *
         * @var \Magento\Catalog\Model\Indexer\Category\Flat\State
         */
    
    Nicolas's avatar
    Nicolas committed
        protected $categoryFlatConfig;
    
    
        /**
         * Top Menu
         *
         * @var \Magento\Theme\Block\Html\Topmenu
         */
    
    Nicolas's avatar
    Nicolas committed
        protected $topMenu;
    
    
        /**
         * Category factory
         *
         * @var \Magento\Catalog\Model\CategoryFactory
         */
    
    Nicolas's avatar
    Nicolas committed
        protected $_categoryFactory;
    
        protected $widgetTitle;
        protected $widgetContent;
    
        /**
         * @param \Magento\Framework\View\Element\Template\Context $context
         * @param \Magento\Catalog\Helper\Category $categoryHelper
    
         * @param \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState
         * @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
         * @param \Magento\Theme\Block\Html\Topmenu $topMenu
    
         * @param \Magento\Framework\Image\AdapterFactory $imageFactory
    
    Nicolas's avatar
    Nicolas committed
         */
        public function __construct(
            \Magento\Framework\View\Element\Template\Context $context,
            \Magento\Catalog\Helper\Category $categoryHelper,
            \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
            \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    
            \Magento\Theme\Block\Html\Topmenu $topMenu,
            \Magento\Framework\Image\AdapterFactory $imageFactory
    
    Nicolas's avatar
    Nicolas committed
        ) {
            $this->_categoryHelper = $categoryHelper;
            $this->categoryFlatConfig = $categoryFlatState;
            $this->_categoryFactory = $categoryFactory;
    
            $this->topMenu = $topMenu;
    
            $this->_imageFactory = $imageFactory;
    
    
    Nicolas's avatar
    Nicolas committed
            parent::__construct($context);
        }
    
    Nicolas's avatar
    Nicolas committed
        /**
         * Return categories helper
         */
        public function getCategoryHelper()
        {
            return $this->_categoryHelper;
        }
    
        public function getCategoryModel($id)
        {
             $_category = $this->_categoryFactory->create();
             $_category->load($id);
             return $_category;
        }
    
        /**
         * Retrieve current store categories
         *
         * @param bool|string $sorted
         * @param bool $asCollection
         * @param bool $toLoad
         * @return \Magento\Framework\Data\Tree\Node\Collection|\Magento\Catalog\Model\Resource\Category\Collection|array
         */
    
        /**
         * Récupère le titre du widget
        */
        public function getWidgetTitle()
        {
    
    Nicolas's avatar
    Nicolas committed
        }
    
        /**
         * Récupère le contenu du widget
         */
        public function getWidgetContent()
        {
    
            return $this->getData('widget_content');
    
    Nicolas's avatar
    Nicolas committed
        }
    
        /**
         * Récupère la sélection de catégories
         */
        public function getCategorySelection()
        {
            $rootCat = $this->getData('category_selection');
    
    
            $category = $this->_categoryFactory->create();
            $collection = $category
                ->getCollection()
                ->addAttributeToSelect('image')
                ->addIdFilter($rootCat);
            return $collection;
        }
    
        /**
         * Récupère le template du widget
         */
    
    Nicolas's avatar
    Nicolas committed
        {
    
            if ($this->hasData('template')) {
                $this->setTemplate($this->getData('template'));
            }
            return $this->_template;
    
    Nicolas's avatar
    Nicolas committed
        }
    
    
        /**
         * Récupère la largeur de l'image du widget
         */
        public function getCategoryImageWidth()
        {
            return $this->getData('category_image_width');
        }
    
        /**
         * Récupère la hauteur de l'image du widget
         */
        public function getCategoryImageHeight()
        {
            return $this->getData('category_image_height');
        }
    
        /**
         * Permet d'optimiser les images de la catégorie
         */
        public function getResizeImage($image, $width = null, $height = null)
        {
            if ($image) :
                if (is_string($image)) :
                    $isRelativeUrl = substr($image, 0, 1) === '/';
                    if ($isRelativeUrl) :
                        $image = ltrim($image, '/media/');
                    else :
    
                        $image = 'catalog/category/' . $image;
    
            if (substr($image,-3) != 'jpg'):
    
                return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).$image;
    
            if (is_null($width)) :
                if (is_null($this->getCategoryImageWidth())) :
                    $width = 450;
                else:
                    $width = $this->getCategoryImageWidth();
                endif;
    
                if (is_null($this->getCategoryImageHeight())) :
                    $height = 450;
                else:
                    $height = $this->getCategoryImageHeight();
                endif;
    
            endif;
    
            $absolutePath = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath('').$image;
    
            $imageResized = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath('resized/'.$width.'/').$image;
    
            /**
             * Créé imageFactory
             */
            $imageResize = $this->_imageFactory->create();
            $imageResize->open($absolutePath);
            $imageResize->constrainOnly(TRUE);
            $imageResize->keepTransparency(TRUE);
            $imageResize->keepFrame(FALSE);
            $imageResize->keepAspectRatio(TRUE);
    
            $imageResize->resize($width,$height);
    
            /**
             * Enregistre l'image dans le dossier de destination
             */
            $destination = $imageResized ;
            $imageResize->save($destination);
    
            $resizedURL = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'resized/'.$width.'/'.$image;
    
            return $resizedURL;
        }
    
    Nicolas's avatar
    Nicolas committed
    }