1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace Sm\Shop4u\Model\Import;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\DataObject;
use Magento\Store\Model\ScopeInterface;
class Demo
{
/**
* @var ScopeConfigInterface
*/
protected $_scopeConfig;
protected $_storeManager;
private $_importPath;
protected $_parser;
protected $_configFactory;
protected $_objectManager;
public function __construct(
ScopeConfigInterface $scopeConfig,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\ObjectManagerInterface $objectManager,
\Magento\Framework\App\Config\ConfigResource\ConfigInterface $configFactory
) {
$this->_scopeConfig = $scopeConfig;
$this->_storeManager = $storeManager;
$this->_configFactory = $configFactory;
$this->_objectManager= $objectManager;
$this->_importPath = BP . '/app/code/Sm/Shop4u/etc/import/';
$this->_parser = new \Magento\Framework\Xml\Parser();
}
public function importDemo($demo_version,$store=NULL,$website = NULL)
{
// Default response
$gatewayResponse = new DataObject([
'is_valid' => false,
'import_path' => '',
'request_success' => false,
'request_message' => __('Error during Import '.$demo_version.'.'),
]);
try {
$xmlPath = $this->_importPath . $demo_version . '.xml';
$overwrite = true;
if (!is_readable($xmlPath))
{
throw new \Exception(
__("Can't get the data file for import ".$demo_version.": ".$xmlPath)
);
}
$data = $this->_parser->load($xmlPath)->xmlToArray();
$scope = "default";
$scope_id = 0;
if ($store && $store > 0) // store level
{
$scope = "stores";
$scope_id = $store;
}
elseif ($website && $website > 0) // website level
{
$scope = "websites";
$scope_id = $website;
}
foreach($data['root']['config'] as $b_name => $b){
foreach($b as $c_name => $c){
foreach($c as $d_name => $d){
$this->_configFactory->saveConfig($b_name.'/'.$c_name.'/'.$d_name,$d,$scope,$scope_id);
}
}
}
//$gatewayResponse->setData("import_path",$config);
$gatewayResponse->setIsValid(true);
$gatewayResponse->setRequestSuccess(true);
if ($gatewayResponse->getIsValid()) {
$gatewayResponse->setRequestMessage(__('Success to Import '.$demo_version.'.'));
} else {
$gatewayResponse->setRequestMessage(__('Error during Import '.$demo_version.'.'));
}
} catch (\Exception $exception) {
$gatewayResponse->setIsValid(false);
$gatewayResponse->setRequestMessage($exception->getMessage());
}
return $gatewayResponse;
}
}