Demo.php 3.7 KB

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