Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved icon uploading for brands #4

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Block/Adminhtml/Brands/Grid/Renderer/BrandIcon.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getColumnValue($columnId, $entityId)
return '';
}

$iconPath = $this->filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath() . 'brands/' . $brandData->getBrandIcon();
$iconPath = $this->filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath() . 'brands/icon/' . ltrim($brandData->getBrandIcon(), '/');

if (!file_exists($iconPath)) {
return '';
}
return sprintf('<img src="%s" width="250"/>', $brandData->getBrandIconUrl());
}
}
}
16 changes: 5 additions & 11 deletions Controller/Adminhtml/Brand/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MageSuite\BrandManagement\Controller\Adminhtml\Brand;

class Upload extends \Magento\Framework\App\Action\Action
class Upload extends \Magento\Backend\App\Action
{
/**
* @var \MageSuite\BrandManagement\Model\Brands\Processor\UploadFactory
Expand All @@ -17,23 +17,17 @@ class Upload extends \Magento\Framework\App\Action\Action
public function __construct(
\Magento\Backend\App\Action\Context $context,
\MageSuite\BrandManagement\Model\Brands\Processor\UploadFactory $uploadProcessor
)
{
$this->uploadProcessor = $uploadProcessor;
) {
parent::__construct($context);
$this->uploadProcessor = $uploadProcessor;
}

/**
* @return \Magento\Framework\Controller\ResultFactory
*/
public function execute()
{
try {
$result = $this->uploadProcessor->create()->processUpload();
} catch (\Exception $e)
{
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
}
$result = $this->uploadProcessor->create()->processUpload();
return $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)->setData($result);
}

Expand All @@ -44,4 +38,4 @@ protected function _isAllowed()
{
return true;
}
}
}
4 changes: 2 additions & 2 deletions Model/Brands.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public function getBrandIconUrl($image = null)
->getStore()
->getBaseUrl(
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
) . 'brands/' . $icon;
) . 'brands/icon/' . ltrim($icon, '/');
}


Expand All @@ -302,7 +302,7 @@ public function getBrandAdditionalIconUrl($image = null)
->getStore()
->getBaseUrl(
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
) . 'brands/' . $icon;
) . 'brands/additional_icon/' . ltrim($icon, '/');
}

public function getBrandUrl()
Expand Down
69 changes: 48 additions & 21 deletions Model/Brands/DataProvider.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
<?php
namespace MageSuite\BrandManagement\Model\Brands;

use Magento\Framework\App\Filesystem\DirectoryList;

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
const DEFAULT_STORE_ID = 0;

/**
* Media Directory object (read)
*
* @var \Magento\Framework\Filesystem\Directory\ReadInterface
*/
protected $mediaDirectory;

/**
* @var \MageSuite\BrandManagement\Api\BrandsRepositoryInterface
*/
Expand Down Expand Up @@ -36,6 +45,7 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
* @param string $requestFieldName
* @param \MageSuite\BrandManagement\Model\ResourceModel\Brands\CollectionFactory $brandsCollectionFactory
* @param \MageSuite\BrandManagement\Api\BrandsRepositoryInterface $brandsRepository
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\Framework\App\RequestInterface $request
* @param array $meta
* @param array $data
Expand All @@ -47,12 +57,13 @@ public function __construct(
\MageSuite\BrandManagement\Model\ResourceModel\Brands\CollectionFactory $brandsCollectionFactory,
\MageSuite\BrandManagement\Api\BrandsRepositoryInterface $brandsRepository,
\MageSuite\BrandManagement\Api\Data\BrandsInterfaceFactory $brandsFactory,
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\Registry $registry,
array $meta = [],
array $data = []
) {

$this->mediaDirectory = $filesystem->getDirectoryRead(DirectoryList::MEDIA);
$this->brandsRepository = $brandsRepository;
$this->brandsFactory = $brandsFactory;
$this->collection = $brandsCollectionFactory->create();
Expand Down Expand Up @@ -147,30 +158,46 @@ public function getData()
]
];

$directory = $this->mediaDirectory;

if ($brand->getBrandIcon()) {
$name = $brand->getBrandIcon();
$url = $brand->getBrandIconUrl();
$size = file_exists('media/brands/' . $name) ? filesize('media/brands/' . $name) : 0;
$result[$brand->getEntityId()]['brand_icon'] = [
0 => [
'url' => $url,
'name' => $name,
'size' => $size
]
];
$file = $brand->getBrandIcon();
$path = 'brands/icon/' . ltrim($file, '/');
$absPath = $directory->getAbsolutePath($path);
if (file_exists($absPath)) {
$url = $brand->getBrandIconUrl();
$size = $directory->stat($directory->getRelativePath($path))['size'];
$result[$brand->getEntityId()]['brand_icon'] = [
0 => [
'url' => $url,
'file' => $file,
'size' => $size,
'name' => pathinfo($absPath, PATHINFO_BASENAME),
'type' => mime_content_type($absPath),
'exists' => true
]
];
}
}

if ($brand->getBrandAdditionalIcon()) {
$name = $brand->getBrandAdditionalIcon();
$url = $brand->getBrandAdditionalIconUrl();
$size = file_exists('media/brands/' . $name) ? filesize('media/brands/' . $name) : 0;
$result[$brand->getEntityId()]['brand_additional_icon'] = [
0 => [
'url' => $url,
'name' => $name,
'size' => $size
]
];
$file = $brand->getBrandAdditionalIcon();
$path = 'brands/additional_icon/' . ltrim($file, '/');
$absPath = $directory->getAbsolutePath($path);
if (file_exists($absPath)) {
$url = $brand->getBrandAdditionalIconUrl();
$size = $directory->stat($directory->getRelativePath($path))['size'];
$result[$brand->getEntityId()]['brand_additional_icon'] = [
0 => [
'url' => $url,
'file' => $file,
'size' => $size,
'name' => pathinfo($absPath, PATHINFO_BASENAME),
'type' => mime_content_type($absPath),
'exists' => true
]
];
}
}

return $result;
Expand Down
20 changes: 14 additions & 6 deletions Model/Brands/Processor/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,24 @@ class Save
* @var \Magento\Framework\DataObjectFactory
*/
private $dataObjectFactory;
/**
* @var \MageSuite\BrandManagement\Model\Brands\Processor\UploadFactory
*/
protected $uploadProcessor;

public function __construct(
\MageSuite\BrandManagement\Model\BrandsFactory $brandsFactory,
\MageSuite\BrandManagement\Api\BrandsRepositoryInterface $brandsRepository,
\Magento\Framework\Event\Manager $eventManager,
\Magento\Framework\DataObjectFactory $dataObjectFactory
\Magento\Framework\DataObjectFactory $dataObjectFactory,
\MageSuite\BrandManagement\Model\Brands\Processor\UploadFactory $uploadProcessor
)
{
$this->brandsFactory = $brandsFactory;
$this->brandsRepository = $brandsRepository;
$this->eventManager = $eventManager;
$this->dataObjectFactory = $dataObjectFactory;
$this->uploadProcessor = $uploadProcessor;
}

public function processSave($params) {
Expand Down Expand Up @@ -62,10 +68,11 @@ public function processSave($params) {

if(isset($params['brand_icon'])) {
if (is_array($params['brand_icon'])) {
$imagePath = $params['brand_icon'][0]['name'];
$imagePath = $params['brand_icon'][0]['file'];
} else {
$imagePath = $params['brand_icon'];
}
$this->uploadProcessor->create()->moveFileFromTmp($imagePath, 'brand_icon');
}
if($imagePath){
$brand->setBrandIcon($imagePath);
Expand All @@ -77,10 +84,11 @@ public function processSave($params) {

if(isset($params['brand_additional_icon'])) {
if (is_array($params['brand_additional_icon'])) {
$imageAdditionalPath = $params['brand_additional_icon'][0]['name'];
$imageAdditionalPath = $params['brand_additional_icon'][0]['file'];
} else {
$imageAdditionalPath = $params['brand_additional_icon'];
}
$this->uploadProcessor->create()->moveFileFromTmp($imageAdditionalPath, 'brand_additional_icon');
}
if($imageAdditionalPath){
$brand->setBrandAdditionalIcon($imageAdditionalPath);
Expand Down Expand Up @@ -118,12 +126,12 @@ public function matchParams($params)
}

if($field == 'brand_icon'){
$matchedParams[$field] = $params['brand_icon'][0]['name'];
$matchedParams[$field] = $params['brand_icon'][0]['file'];
continue;
}

if($field == 'brand_additional_icon'){
$matchedParams[$field] = $params['brand_additional_icon'][0]['name'];
$matchedParams[$field] = $params['brand_additional_icon'][0]['file'];
continue;
}

Expand All @@ -134,4 +142,4 @@ public function matchParams($params)

return $this->dataObjectFactory->create()->setData($matchedParams);
}
}
}
Loading