Newer
Older
<?php
namespace NicolasBejean\CategoryWidget\Block\Widget;
class CategoryWidget extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
{
/**
* Category helper
*
* @var \Magento\Catalog\Helper\Category
/**
* Category Flat Config
*
* @var \Magento\Catalog\Model\Indexer\Category\Flat\State
*/
/**
* Top Menu
*
* @var \Magento\Theme\Block\Html\Topmenu
*/
/**
* Category factory
*
* @var \Magento\Catalog\Model\CategoryFactory
*/
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
*/
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
) {
$this->_categoryHelper = $categoryHelper;
$this->categoryFlatConfig = $categoryFlatState;
$this->_categoryFactory = $categoryFactory;
$this->_imageFactory = $imageFactory;
/**
* 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
committed
return $this->getData('widget_title');
}
/**
* Récupère le contenu du widget
*/
public function getWidgetContent()
{

Nicolas
committed
return $this->getData('widget_content');
}
/**
* 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
committed
public function getTemplate()

Nicolas
committed
return $this->getData('template');
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* 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;
endif;
endif;
endif;
if(is_null($width)) :
$width = $this->getCategoryImageWidth();
endif;
if(is_null($height)):
$height = $this->getCategoryImageHeight();
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;
}