/** * Add fieldsets * * @return $this * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function _prepareForm() { /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(['data' => ['id' => 'edit_form', 'action' => $this->getUrl('adminhtml/*/validate'), 'method' => 'post', 'enctype' => 'multipart/form-data']]); // base fieldset $fieldsets['base'] = $form->addFieldset('base_fieldset', ['legend' => __('Import Settings')]); $fieldsets['base']->addField('entity', 'select', ['name' => 'entity', 'title' => __('Entity Type'), 'label' => __('Entity Type'), 'required' => true, 'onchange' => 'varienImport.handleEntityTypeSelector();', 'values' => $this->_entityFactory->create()->toOptionArray(), 'after_element_html' => $this->getDownloadSampleFileHtml()]); // add behaviour fieldsets $uniqueBehaviors = $this->_importModel->getUniqueEntityBehaviors(); foreach ($uniqueBehaviors as $behaviorCode => $behaviorClass) { $fieldsets[$behaviorCode] = $form->addFieldset($behaviorCode . '_fieldset', ['legend' => __('Import Behavior'), 'class' => 'no-display']); /** @var $behaviorSource \Magento\ImportExport\Model\Source\Import\AbstractBehavior */ $fieldsets[$behaviorCode]->addField($behaviorCode, 'select', ['name' => 'behavior', 'title' => __('Import Behavior'), 'label' => __('Import Behavior'), 'required' => true, 'disabled' => true, 'values' => $this->_behaviorFactory->create($behaviorClass)->toOptionArray(), 'class' => $behaviorCode, 'onchange' => 'varienImport.handleImportBehaviorSelector();', 'note' => ' ']); $fieldsets[$behaviorCode]->addField($behaviorCode . \Magento\ImportExport\Model\Import::FIELD_NAME_VALIDATION_STRATEGY, 'select', ['name' => \Magento\ImportExport\Model\Import::FIELD_NAME_VALIDATION_STRATEGY, 'title' => __(' '), 'label' => __(' '), 'required' => true, 'class' => $behaviorCode, 'disabled' => true, 'values' => [ProcessingErrorAggregatorInterface::VALIDATION_STRATEGY_STOP_ON_ERROR => 'Stop on Error', ProcessingErrorAggregatorInterface::VALIDATION_STRATEGY_SKIP_ERRORS => 'Skip error entries'], 'after_element_html' => $this->getDownloadSampleFileHtml()]); $fieldsets[$behaviorCode]->addField($behaviorCode . '_' . \Magento\ImportExport\Model\Import::FIELD_NAME_ALLOWED_ERROR_COUNT, 'text', ['name' => \Magento\ImportExport\Model\Import::FIELD_NAME_ALLOWED_ERROR_COUNT, 'label' => __('Allowed Errors Count'), 'title' => __('Allowed Errors Count'), 'required' => true, 'disabled' => true, 'value' => 10, 'class' => $behaviorCode . ' validate-number validate-greater-than-zero input-text', 'note' => __('Please specify number of errors to halt import process')]); $fieldsets[$behaviorCode]->addField($behaviorCode . '_' . \Magento\ImportExport\Model\Import::FIELD_FIELD_SEPARATOR, 'text', ['name' => \Magento\ImportExport\Model\Import::FIELD_FIELD_SEPARATOR, 'label' => __('Field separator'), 'title' => __('Field separator'), 'required' => true, 'disabled' => true, 'class' => $behaviorCode, 'value' => ',']); $fieldsets[$behaviorCode]->addField($behaviorCode . \Magento\ImportExport\Model\Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR, 'text', ['name' => \Magento\ImportExport\Model\Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR, 'label' => __('Multiple value separator'), 'title' => __('Multiple value separator'), 'required' => true, 'disabled' => true, 'class' => $behaviorCode, 'value' => ',']); } // fieldset for file uploading $fieldsets['upload'] = $form->addFieldset('upload_file_fieldset', ['legend' => __('File to Import'), 'class' => 'no-display']); $fieldsets['upload']->addField(\Magento\ImportExport\Model\Import::FIELD_NAME_SOURCE_FILE, 'file', ['name' => \Magento\ImportExport\Model\Import::FIELD_NAME_SOURCE_FILE, 'label' => __('Select File to Import'), 'title' => __('Select File to Import'), 'required' => true, 'class' => 'input-file']); $fieldsets['upload']->addField(\Magento\ImportExport\Model\Import::FIELD_NAME_IMG_FILE_DIR, 'text', ['name' => \Magento\ImportExport\Model\Import::FIELD_NAME_IMG_FILE_DIR, 'label' => __('Images File Directory'), 'title' => __('Images File Directory'), 'required' => false, 'class' => 'input-text', 'note' => __('For Type "Local Server" use relative path to Magento installation, e.g. var/export, var/import, var/export/some/dir')]); $form->setUseContainer(true); $this->setForm($form); return parent::_prepareForm(); }
/** * Add fieldsets * * @return $this * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function _prepareForm() { /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(['data' => ['id' => 'edit_form', 'action' => $this->getUrl('adminhtml/*/validate'), 'method' => 'post', 'enctype' => 'multipart/form-data']]); // base fieldset $fieldsets['base'] = $form->addFieldset('base_fieldset', ['legend' => __('Import Settings')]); $fieldsets['base']->addField('entity', 'select', ['name' => 'entity', 'title' => __('Entity Type'), 'label' => __('Entity Type'), 'required' => true, 'onchange' => 'varienImport.handleEntityTypeSelector();', 'values' => $this->_entityFactory->create()->toOptionArray()]); // add behaviour fieldsets $uniqueBehaviors = $this->_importModel->getUniqueEntityBehaviors(); foreach ($uniqueBehaviors as $behaviorCode => $behaviorClass) { $fieldsets[$behaviorCode] = $form->addFieldset($behaviorCode . '_fieldset', ['legend' => __('Import Behavior'), 'class' => 'no-display']); /** @var $behaviorSource \Magento\ImportExport\Model\Source\Import\AbstractBehavior */ $fieldsets[$behaviorCode]->addField($behaviorCode, 'select', ['name' => 'behavior', 'title' => __('Import Behavior'), 'label' => __('Import Behavior'), 'required' => true, 'disabled' => true, 'values' => $this->_behaviorFactory->create($behaviorClass)->toOptionArray(), 'class' => $behaviorCode]); $fieldsets[$behaviorCode]->addField($behaviorCode . \Magento\ImportExport\Model\Import::FIELD_FIELD_SEPARATOR, 'text', ['name' => \Magento\ImportExport\Model\Import::FIELD_FIELD_SEPARATOR, 'label' => __('Field separator'), 'title' => __('Field separator'), 'required' => true, 'disabled' => true, 'class' => $behaviorCode, 'value' => ',']); $fieldsets[$behaviorCode]->addField($behaviorCode . \Magento\ImportExport\Model\Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR, 'text', ['name' => \Magento\ImportExport\Model\Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR, 'label' => __('Multiple value separator'), 'title' => __('Multiple value separator'), 'required' => true, 'disabled' => true, 'class' => $behaviorCode, 'value' => ',']); } // fieldset for file uploading $fieldsets['upload'] = $form->addFieldset('upload_file_fieldset', ['legend' => __('File to Import'), 'class' => 'no-display']); $fieldsets['upload']->addField(\Magento\ImportExport\Model\Import::FIELD_NAME_SOURCE_FILE, 'file', ['name' => \Magento\ImportExport\Model\Import::FIELD_NAME_SOURCE_FILE, 'label' => __('Select File to Import'), 'title' => __('Select File to Import'), 'required' => true, 'class' => 'input-file']); $fieldsets['upload']->addField(\Magento\ImportExport\Model\Import::FIELD_NAME_IMG_FILE_DIR, 'text', ['name' => \Magento\ImportExport\Model\Import::FIELD_NAME_IMG_FILE_DIR, 'label' => __('Images File Directory'), 'title' => __('Images File Directory'), 'required' => false, 'class' => 'input-text', 'note' => __('For Type "Local Server" use relative path to Magento installation, e.g. var/export, var/import, var/export/some/dir')]); $form->setUseContainer(true); $this->setForm($form); return parent::_prepareForm(); }
/** * {@inheritdoc} */ public function importSource() { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'importSource'); if (!$pluginInfo) { return parent::importSource(); } else { return $this->___callPlugins('importSource', func_get_args(), $pluginInfo); } }
/** * Add fieldsets * * @return $this */ protected function _prepareForm() { /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(array('data' => array('id' => 'edit_form', 'action' => $this->getUrl('adminhtml/*/validate'), 'method' => 'post', 'enctype' => 'multipart/form-data'))); // base fieldset $fieldsets['base'] = $form->addFieldset('base_fieldset', array('legend' => __('Import Settings'))); $fieldsets['base']->addField('entity', 'select', array('name' => 'entity', 'title' => __('Entity Type'), 'label' => __('Entity Type'), 'required' => true, 'onchange' => 'varienImport.handleEntityTypeSelector();', 'values' => $this->_entityFactory->create()->toOptionArray())); // add behaviour fieldsets $uniqueBehaviors = $this->_importModel->getUniqueEntityBehaviors(); foreach ($uniqueBehaviors as $behaviorCode => $behaviorClass) { $fieldsets[$behaviorCode] = $form->addFieldset($behaviorCode . '_fieldset', array('legend' => __('Import Behavior'), 'class' => 'no-display')); /** @var $behaviorSource \Magento\ImportExport\Model\Source\Import\AbstractBehavior */ $fieldsets[$behaviorCode]->addField($behaviorCode, 'select', array('name' => 'behavior', 'title' => __('Import Behavior'), 'label' => __('Import Behavior'), 'required' => true, 'disabled' => true, 'values' => $this->_behaviorFactory->create($behaviorClass)->toOptionArray())); } // fieldset for file uploading $fieldsets['upload'] = $form->addFieldset('upload_file_fieldset', array('legend' => __('File to Import'), 'class' => 'no-display')); $fieldsets['upload']->addField(\Magento\ImportExport\Model\Import::FIELD_NAME_SOURCE_FILE, 'file', array('name' => \Magento\ImportExport\Model\Import::FIELD_NAME_SOURCE_FILE, 'label' => __('Select File to Import'), 'title' => __('Select File to Import'), 'required' => true, 'class' => 'input-file')); $form->setUseContainer(true); $this->setForm($form); return parent::_prepareForm(); }
/** * Start import process action * * @return \Magento\Framework\Controller\ResultInterface */ public function executeInternal() { $data = $this->getRequest()->getPostValue(); if ($data) { /** @var \Magento\Framework\View\Result\Layout $resultLayout */ $resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT); /** @var $resultBlock \Magento\ImportExport\Block\Adminhtml\Import\Frame\Result */ $resultBlock = $resultLayout->getLayout()->getBlock('import.frame.result'); $resultBlock->addAction('show', 'import_validation_container')->addAction('innerHTML', 'import_validation_container_header', __('Status'))->addAction('hide', ['edit_form', 'upload_button', 'messages']); $this->importModel->setData($data); $this->importModel->importSource(); $errorAggregator = $this->importModel->getErrorAggregator(); if ($this->importModel->getErrorAggregator()->hasToBeTerminated()) { $resultBlock->addError(__('Maximum error count has been reached or system error is occurred!')); $this->addErrorMessages($resultBlock, $errorAggregator); } else { $this->importModel->invalidateIndex(); $this->addErrorMessages($resultBlock, $errorAggregator); $resultBlock->addSuccess(__('Import successfully done')); } return $resultLayout; } /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $resultRedirect->setPath('adminhtml/*/index'); return $resultRedirect; }
public function uploadSource() { $result = null; if ($sourceType = $this->getImportSource()) { $source = $this->_helper->getSourceModelByType($sourceType); $source->setData($this->getData()); try { $result = $source->uploadSource(); } catch (\Exception $e) { throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage())); } } if ($result) { return $result; } return parent::uploadSource(); }
/** * Process validation results * * @param \Magento\ImportExport\Model\Import $import * @param \Magento\ImportExport\Block\Adminhtml\Import\Frame\Result $resultBlock * @return void */ protected function processValidationError(Import $import, ImportResultBlock $resultBlock) { if ($import->getProcessedRowsCount() == $import->getInvalidRowsCount()) { $resultBlock->addNotice(__('This file is invalid. Please fix errors and re-upload the file.')); } elseif ($import->getErrorsCount() >= $import->getErrorsLimit()) { $resultBlock->addNotice(__('You\'ve reached an error limit (%1). Please fix errors and re-upload the file.', $import->getErrorsLimit())); } else { if ($import->isImportAllowed()) { $resultBlock->addNotice(__('Please fix errors and re-upload the file. Or press "Import" to skip rows with errors.'), true); } else { $resultBlock->addNotice(__('The file is partially valid, but we can\'t import it for some reason.'), false); } } // errors info foreach ($import->getErrors() as $errorCode => $rows) { $error = $errorCode . ' ' . __('in rows:') . ' ' . implode(', ', $rows); $resultBlock->addError($error); } }
/** * Process validation results * * @param \Magento\ImportExport\Model\Import $import * @param \Magento\ImportExport\Block\Adminhtml\Import\Frame\Result $resultBlock * @return void */ protected function processValidationError(Import $import, ImportResultBlock $resultBlock) { if ($import->getProcessedRowsCount() == $import->getInvalidRowsCount()) { $resultBlock->addNotice(__('File is totally invalid. Please fix errors and re-upload file.')); } elseif ($import->getErrorsCount() >= $import->getErrorsLimit()) { $resultBlock->addNotice(__('Errors limit (%1) reached. Please fix errors and re-upload file.', $import->getErrorsLimit())); } else { if ($import->isImportAllowed()) { $resultBlock->addNotice(__('Please fix errors and re-upload file or simply press "Import" button to skip rows with errors'), true); } else { $resultBlock->addNotice(__('File is partially valid, but import is not possible'), false); } } // errors info foreach ($import->getErrors() as $errorCode => $rows) { $error = $errorCode . ' ' . __('in rows:') . ' ' . implode(', ', $rows); $resultBlock->addError($error); } }
/** * Cover createHistoryReport(). * * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage Source file coping failed */ public function testCreateHistoryReportThrowException() { $sourceFileRelative = null; $entity = ''; $extension = ''; $result = ''; $gmtTimestamp = 1234567; $this->import->expects($this->once())->method('isReportEntityType')->with($entity)->willReturn(true); $this->_varDirectory->expects($this->never())->method('getRelativePath'); $phrase = $this->getMock('\\Magento\\Framework\\Phrase', [], [], '', false); $this->_driver->expects($this->any())->method('fileGetContents')->willReturnCallback(function () use($phrase) { throw new \Magento\Framework\Exception\FileSystemException($phrase); }); $this->dateTime->expects($this->once())->method('gmtTimestamp')->willReturn($gmtTimestamp); $args = [$sourceFileRelative, $entity, $extension, $result]; $actualResult = $this->invokeMethod($this->import, 'createHistoryReport', $args); $this->assertEquals($this->import, $actualResult); }
public function testImportSource() { $entityTypeCode = 'code'; $this->_importData->expects($this->any())->method('getEntityTypeCode')->will($this->returnValue($entityTypeCode)); $behaviour = 'behaviour'; $this->_importData->expects($this->once())->method('getBehavior')->will($this->returnValue($behaviour)); $this->import->expects($this->any())->method('getDataSourceModel')->will($this->returnValue($this->_importData)); $this->import->expects($this->any())->method('setData')->withConsecutive(['entity', $entityTypeCode], ['behavior', $behaviour]); $phraseClass = '\\Magento\\Framework\\Phrase'; $this->import->expects($this->any())->method('addLogComment')->with($this->isInstanceOf($phraseClass)); $this->_entityAdapter->expects($this->once())->method('importData')->will($this->returnSelf()); $this->import->expects($this->once())->method('_getEntityAdapter')->will($this->returnValue($this->_entityAdapter)); $importOnceMethodsReturnNull = ['getEntity', 'getBehavior', 'getProcessedRowsCount', 'getProcessedEntitiesCount', 'getInvalidRowsCount', 'getErrorsCount']; foreach ($importOnceMethodsReturnNull as $method) { $this->import->expects($this->once())->method($method)->will($this->returnValue(null)); } $this->import->importSource(); }
/** * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage Entity is unknown */ public function testGetEntityEntityIsNotSet() { $this->_model->getEntity(); }
/** * Return json-encoded list of existing behaviors * * @return string */ public function getUniqueBehaviors() { $uniqueBehaviors = $this->_importModel->getUniqueEntityBehaviors(); return $this->_jsonEncoder->encode(array_keys($uniqueBehaviors)); }
protected function processBehaviorGetter($behavior) { $dataSource = $this->getMock('Magento\\ImportExport\\Model\\ResourceModel\\Import\\Data', [], [], '', false); $dataSource->expects($this->once())->method('getBehavior')->will($this->returnValue($behavior)); $this->import->expects($this->once())->method('getDataSourceModel')->will($this->returnValue($dataSource)); }
/** * {@inheritdoc} */ public function offsetGet($offset) { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'offsetGet'); if (!$pluginInfo) { return parent::offsetGet($offset); } else { return $this->___callPlugins('offsetGet', func_get_args(), $pluginInfo); } }
/** * Attach Attributes By Id * * @param string $attributeSetName * @param array $attributeIds * @return void */ protected function attachAttributesById($attributeSetName, $attributeIds) { foreach ($this->_prodAttrColFac->create()->addFieldToFilter('main_table.attribute_id', ['in' => $attributeIds]) as $attribute) { $attributeCode = $attribute->getAttributeCode(); $attributeId = $attribute->getId(); if ($attribute->getIsVisible() || in_array($attributeCode, $this->_forcedAttributesCodes)) { self::$commonAttributesCache[$attributeId] = ['id' => $attributeId, 'code' => $attributeCode, 'is_global' => $attribute->getIsGlobal(), 'is_required' => $attribute->getIsRequired(), 'is_unique' => $attribute->getIsUnique(), 'frontend_label' => $attribute->getFrontendLabel(), 'is_static' => $attribute->isStatic(), 'apply_to' => $attribute->getApplyTo(), 'type' => \Magento\ImportExport\Model\Import::getAttributeType($attribute), 'default_value' => strlen($attribute->getDefaultValue()) ? $attribute->getDefaultValue() : null, 'options' => $this->_entityModel->getAttributeOptions($attribute, $this->_indexValueAttributes)]; self::$attributeCodeToId[$attributeCode] = $attributeId; $this->_addAttributeParams($attributeSetName, self::$commonAttributesCache[$attributeId], $attribute); } } }
/** * Import behavior getter. * * @return string */ public function getBehavior() { if (!isset($this->_parameters['behavior']) || $this->_parameters['behavior'] != \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND && $this->_parameters['behavior'] != \Magento\ImportExport\Model\Import::BEHAVIOR_REPLACE && $this->_parameters['behavior'] != \Magento\ImportExport\Model\Import::BEHAVIOR_DELETE) { return \Magento\ImportExport\Model\Import::getDefaultBehavior(); } return $this->_parameters['behavior']; }
public function getLogTrace() { return $this->importModel->getFormatedLogTrace(); }
/** * Initialize entity attributes * * @return $this */ protected function _initAttributes() { /** @var $attribute \Magento\Eav\Model\Attribute */ foreach ($this->_attributeCollection as $attribute) { $this->_attributes[$attribute->getAttributeCode()] = ['id' => $attribute->getId(), 'code' => $attribute->getAttributeCode(), 'table' => $attribute->getBackend()->getTable(), 'is_required' => $attribute->getIsRequired(), 'is_static' => $attribute->isStatic(), 'rules' => $attribute->getValidateRules() ? unserialize($attribute->getValidateRules()) : null, 'type' => \Magento\ImportExport\Model\Import::getAttributeType($attribute), 'options' => $this->getAttributeOptions($attribute)]; $this->validColumnNames[] = $attribute->getAttributeCode(); } return $this; }
/** * Initialize attribute option values and types. * * @return $this */ protected function initAttributes() { foreach ($this->getAttributeCollection() as $attribute) { $this->_attributeValues[$attribute->getAttributeCode()] = $this->getAttributeOptions($attribute); $this->_attributeTypes[$attribute->getAttributeCode()] = \Magento\ImportExport\Model\Import::getAttributeType($attribute); } return $this; }
/** * Get import summary * * @param \Magento\ImportExport\Model\Import $import * @return string */ public function getSummaryStats(\Magento\ImportExport\Model\Import $import) { $message = __('Created: %1, Updated: %2, Deleted: %3', $import->getCreatedItemsCount(), $import->getUpdatedItemsCount(), $import->getDeletedItemsCount()); return $message; }
/** * Initialize attributes parameters for all attributes' sets. * * @return $this */ protected function _initAttributes() { // temporary storage for attributes' parameters to avoid double querying inside the loop $attributesCache = []; foreach ($this->_attrSetColFac->create()->setEntityTypeFilter($this->_entityModel->getEntityTypeId()) as $attributeSet) { foreach ($this->_prodAttrColFac->create()->setAttributeSetFilter($attributeSet->getId()) as $attribute) { $attributeCode = $attribute->getAttributeCode(); $attributeId = $attribute->getId(); if ($attribute->getIsVisible() || in_array($attributeCode, $this->_forcedAttributesCodes)) { if (!isset($attributesCache[$attributeId])) { $attributesCache[$attributeId] = ['id' => $attributeId, 'code' => $attributeCode, 'is_global' => $attribute->getIsGlobal(), 'is_required' => $attribute->getIsRequired(), 'is_unique' => $attribute->getIsUnique(), 'frontend_label' => $attribute->getFrontendLabel(), 'is_static' => $attribute->isStatic(), 'apply_to' => $attribute->getApplyTo(), 'type' => \Magento\ImportExport\Model\Import::getAttributeType($attribute), 'default_value' => strlen($attribute->getDefaultValue()) ? $attribute->getDefaultValue() : null, 'options' => $this->_entityModel->getAttributeOptions($attribute, $this->_indexValueAttributes)]; } $this->_addAttributeParams($attributeSet->getAttributeSetName(), $attributesCache[$attributeId], $attribute); } } } return $this; }
/** * @return MagentoImport\ErrorProcessing\ProcessingError[] */ public function getErrors() { return $this->importModel->getErrorAggregator()->getAllErrors(); }