Skip to content

Commit

Permalink
Merge branch '2.10.x' into 2.11.x
Browse files Browse the repository at this point in the history
  • Loading branch information
romainruaud committed May 25, 2023
2 parents 9490458 + 8b13743 commit 39607f9
Show file tree
Hide file tree
Showing 12 changed files with 666 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCatalog
* @author Vadym Honcharuk <[email protected]>
* @copyright 2023 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteCatalog\Controller\Adminhtml\Product\Attribute;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Filesystem;
use Magento\Framework\View\Result\PageFactory;
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
use Smile\ElasticsuiteCatalog\Model\Import\ProductAttribute as ProductAttributeImport;

/**
* Product attribute export to CSV controller.
*
* @category Smile
* @package Smile\ElasticsuiteCatalog
* @author Vadym Honcharuk <[email protected]>
*/
class ExportProductAttributeCsv extends Action
{
/**
* @var PageFactory
*/
protected $resultPageFactory;

/**
* @var FileFactory
*/
protected $fileFactory;

/**
* @var CollectionFactory
*/
protected $attributeCollectionFactory;

/**
* @var Filesystem
*/
protected $filesystem;

/**
* @var array
*/
private $columns;

/**
* Constructor.
*
* @param Context $context Application context.
* @param PageFactory $resultPageFactory Result Page factory.
* @param FileFactory $fileFactory File Factory.
* @param Filesystem $filesystem File System.
* @param CollectionFactory $attributeCollectionFactory Attribute Collection Factory.
* @param ProductAttributeImport $productAttributeImport Product Attribute Import Model.
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory,
FileFactory $fileFactory,
Filesystem $filesystem,
CollectionFactory $attributeCollectionFactory,
ProductAttributeImport $productAttributeImport
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
$this->fileFactory = $fileFactory;
$this->filesystem = $filesystem;
$this->attributeCollectionFactory = $attributeCollectionFactory;
$this->columns = $productAttributeImport->getValidColumnNames();
}

/**
* Execute.
*
* @return Redirect|ResponseInterface|ResultInterface|void
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function execute()
{
// Prepare product attributes grid collection.
$attributeCollectionFactory = $this->attributeCollectionFactory->create();
$attributeCollection = $attributeCollectionFactory->addVisibleFilter()
->setOrder('attribute_code', 'ASC');

$content = [];

// Add header row.
$header = [];
foreach ($this->columns as $column) {
$header[] = $column;
}
$content[] = $header;

// Add content row.
foreach ($attributeCollection as $attribute) {
$row = [
$attribute->getAttributeCode(),
$attribute->getDefaultFrontendLabel(),
$attribute->getIsSearchable(),
$attribute->getSearchWeight(),
$attribute->getIsUsedInSpellcheck(),
$attribute->getIsDisplayedInAutocomplete(),
$attribute->getIsFilterable(),
$attribute->getIsFilterableInSearch(),
$attribute->getIsUsedForPromoRules(),
$attribute->getUsedForSortBy(),
$attribute->getIsDisplayRelNofollow(),
$attribute->getFacetMaxSize(),
$attribute->getFacetSortOrder(),
$attribute->getFacetMinCoverageRate(),
$attribute->getFacetBooleanLogic(),
$attribute->getPosition(),
$attribute->getDefaultAnalyzer(),
$attribute->getNormsDisabled(),
$attribute->getIsSpannable(),
$attribute->getIncludeZeroFalseValues(),
];
$content[] = $row;
}

// Prepare and send the CSV file to the browser.
$date = date('Ymd_His');
$fileName = 'elasticsuite_product_attribute-' . $date . '.csv';
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
$stream = $directory->openFile($fileName, 'w+');
foreach ($content as $line) {
$stream->writeCsv($line);
}
$stream->close();

$this->fileFactory->create(
$fileName,
[
'type' => 'filename',
'value' => $fileName,
'rm' => true,
],
DirectoryList::VAR_DIR,
'application/csv'
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
attribute_code,attribute_label,is_searchable,search_weight,is_used_in_spellcheck,is_displayed_in_autocomplete,is_filterable,is_filterable_in_search,is_used_for_promo_rules,used_for_sort_by,is_display_rel_nofollow,facet_max_size,facet_sort_order,facet_min_coverage_rate,facet_boolean_logic,position,default_analyzer,norms_disabled,is_spannable,include_zero_false_values
activity,Activity,0,1,1,0,1,0,1,0,0,10,_count,90,1,0,standard,0,0,0
allow_open_amount,Open Amount,0,1,1,0,0,0,0,0,0,10,_count,90,0,0,standard,0,0,0
Loading

0 comments on commit 39607f9

Please sign in to comment.