Your IP : 127.0.0.1
<?php
/**
* @copyright: Copyright © 2019 Firebear Studio. All rights reserved.
* @author : Firebear Studio <fbeardev@gmail.com>
*/
namespace Firebear\ImportExport\Model\Export;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\ImportExport\Model\Export\AbstractEntity;
use Magento\ImportExport\Model\Export\Factory as CollectionFactory;
use Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection;
use Symfony\Component\Console\Output\ConsoleOutput;
use Psr\Log\LoggerInterface;
use Firebear\ImportExport\Traits\Export\Entity as ExportTrait;
use Firebear\ImportExport\Model\ExportJob\Processor;
use Firebear\ImportExport\Helper\Data as Helper;
/**
* UrlRewrite export
*/
class UrlRewrite extends AbstractEntity implements EntityInterface
{
use ExportTrait;
/**
* Entity collection
*
* @var \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
*/
protected $_entityCollection;
/**
* Collection factory
*
* @var \Magento\ImportExport\Model\Export\Factory
*/
protected $_collectionFactory;
/**
* Helper
*
* @var \Firebear\ImportExport\Helper\Data
*/
protected $_helper;
/**
* Initialize export
*
* @param ScopeConfigInterface $scopeConfig
* @param StoreManagerInterface $storeManager
* @param CollectionFactory $collectionFactory
* @param CollectionByPagesIteratorFactory $resourceColFactory
* @param LoggerInterface $logger
* @param ConsoleOutput $output
* @param Helper $helper
* @param array $data
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
StoreManagerInterface $storeManager,
CollectionFactory $collectionFactory,
CollectionByPagesIteratorFactory $resourceColFactory,
LoggerInterface $logger,
ConsoleOutput $output,
Helper $helper,
array $data = []
) {
$this->_logger = $logger;
$this->output = $output;
$this->_helper = $helper;
$this->_collectionFactory = $collectionFactory;
parent::__construct(
$scopeConfig,
$storeManager,
$collectionFactory,
$resourceColFactory,
$data
);
}
/**
* Export process
*
* @return array
*/
public function export()
{
//Execution time may be very long
set_time_limit(0);
$this->addLogWriteln(__('Begin Export'), $this->output);
$this->addLogWriteln(__('Scope Data'), $this->output);
$collection = $this->_getEntityCollection();
$this->_prepareEntityCollection($collection);
$this->_exportCollectionByPages($collection);
// create export file
return [
$this->getWriter()->getContents(),
$this->_processedEntitiesCount,
$this->lastEntityId
];
}
/**
* Export one item
*
* @param \Magento\Framework\Model\AbstractModel $item
* @return void
*/
public function exportItem($item)
{
$this->lastEntityId = $item->getId();
$row = $this->changeRow($item->toArray());
$this->getWriter()->writeRow($row);
$this->_processedEntitiesCount++;
}
/**
* Entity type code getter
*
* @return string
*/
public function getEntityTypeCode()
{
return 'url_rewrite';
}
/**
* Retrieve header columns
*
* @return array
*/
protected function _getHeaderColumns()
{
return $this->changeHeaders(
array_keys($this->describeTable())
);
}
/**
* Apply filter to collection
*
* @param AbstractCollection $collection
* @return \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
*/
protected function _prepareEntityCollection(AbstractCollection $collection)
{
if (!empty($this->_parameters[Processor::LAST_ENTITY_ID]) &&
$this->_parameters[Processor::LAST_ENTITY_SWITCH] > 0
) {
$collection->addFieldToFilter(
'main_table.url_rewrite_id',
['gt' => $this->_parameters[Processor::LAST_ENTITY_ID]]
);
}
if (!isset($this->_parameters[Processor::EXPORT_FILTER_TABLE]) ||
!is_array($this->_parameters[Processor::EXPORT_FILTER_TABLE])) {
$exportFilter = [];
} else {
$exportFilter = $this->_parameters[Processor::EXPORT_FILTER_TABLE];
}
$filters = [];
$entity = $this->getEntityTypeCode();
foreach ($exportFilter as $data) {
if ($data['entity'] == $entity) {
$filters[$data['field']] = $data['value'];
}
}
$fields = [];
$columns = $this->getFieldColumns();
foreach ($columns['url_rewrite'] as $field) {
$fields[$field['field']] = $field['type'];
}
foreach ($filters as $key => $value) {
if (isset($fields[$key])) {
$type = $fields[$key];
if ('text' == $type) {
if (is_scalar($value)) {
trim($value);
}
$collection->addFieldToFilter($key, ['like' => "%{$value}%"]);
} elseif ('select' == $type) {
$collection->addFieldToFilter($key, ['eq' => $value]);
} elseif ('int' == $type) {
if (is_array($value) && count($value) == 2) {
$from = array_shift($value);
$to = array_shift($value);
if (is_numeric($from)) {
$collection->addFieldToFilter($key, ['from' => $from]);
}
if (is_numeric($to)) {
$collection->addFieldToFilter($key, ['to' => $to]);
}
}
} elseif ('date' == $type) {
if (is_array($value) && count($value) == 2) {
$from = array_shift($exportFilter[$value]);
$to = array_shift($exportFilter[$value]);
if (is_scalar($from) && !empty($from)) {
$date = (new \DateTime($from))->format('m/d/Y');
$collection->addFieldToFilter($key, ['from' => $date, 'date' => true]);
}
if (is_scalar($to) && !empty($to)) {
$date = (new \DateTime($to))->format('m/d/Y');
$collection->addFieldToFilter($key, ['to' => $date, 'date' => true]);
}
}
}
}
}
return $collection;
}
/**
* Retrieve entity collection
*
* @return \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
*/
protected function _getEntityCollection()
{
if (null === $this->_entityCollection) {
$this->_entityCollection = $this->_collectionFactory->create(
UrlRewriteCollection::class
);
}
return $this->_entityCollection;
}
/**
* Retrieve entity field columns
*
* @return array
*/
public function getFieldColumns()
{
$options = [];
foreach ($this->describeTable() as $key => $field) {
$select = [];
$type = $this->_helper->convertTypesTables($field['DATA_TYPE']);
if ('int' == $type && (
'is_' == substr($field['COLUMN_NAME'], 0, 3)
)) {
$select[] = ['label' => __('Yes'), 'value' => 1];
$select[] = ['label' => __('No'), 'value' => 0];
$type = 'select';
}
$options[$this->getEntityTypeCode()][] = ['field' => $key, 'type' => $type, 'select' => $select];
}
return $options;
}
/**
* Retrieve entity field for filter
*
* @return array
*/
public function getFieldsForFilter()
{
$options = [];
foreach ($this->getFieldsForExport() as $field) {
if ($field != 'metadata') {
$options[] = [
'label' => $field,
'value' => $field
];
}
}
return [$this->getEntityTypeCode() => $options];
}
/**
* Retrieve entity field for export
*
* @return array
*/
public function getFieldsForExport()
{
return array_keys($this->describeTable());
}
/**
* Retrieve the column descriptions for a table, include additional table
*
* @return array
*/
protected function describeTable()
{
$resource = $this->_getEntityCollection()->getResource();
$fields = $resource->getConnection()->describeTable($resource->getMainTable());
unset($fields['url_rewrite_id']);
return $fields;
}
/**
* Retrieve attributes codes which are appropriate for export
*
* @return array
*/
protected function _getExportAttrCodes()
{
return [];
}
}