Cms.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. use Magento\Cms\Model\ResourceModel\Block\CollectionFactory as BlockCollectionFactory;
  11. use Magento\Cms\Model\BlockFactory as BlockFactory;
  12. use Magento\Cms\Model\ResourceModel\Block as BlockResourceBlock;
  13. use Magento\Cms\Model\ResourceModel\Page\CollectionFactory as PageCollectionFactory;
  14. use Magento\Cms\Model\PageFactory as PageFactory;
  15. use Magento\Cms\Model\ResourceModel\Page as PageResourceBlock;
  16. class Cms
  17. {
  18. /**
  19. * @var ScopeConfigInterface
  20. */
  21. protected $_scopeConfig;
  22. protected $_storeManager;
  23. private $_importPath;
  24. protected $_parser;
  25. protected $_blockCollectionFactory;
  26. protected $_blockRepository;
  27. protected $_blockFactory;
  28. protected $_pageCollectionFactory;
  29. protected $_pageRepository;
  30. protected $_pageFactory;
  31. public function __construct(
  32. ScopeConfigInterface $scopeConfig,
  33. \Magento\Store\Model\StoreManagerInterface $storeManager,
  34. BlockCollectionFactory $blockCollectionFactory,
  35. \Magento\Cms\Api\BlockRepositoryInterface $blockRepository,
  36. BlockFactory $blockFactory,
  37. PageCollectionFactory $pageCollectionFactory,
  38. \Magento\Cms\Api\PageRepositoryInterface $pageRepository,
  39. PageFactory $pageFactory
  40. ) {
  41. $this->_scopeConfig = $scopeConfig;
  42. $this->_storeManager = $storeManager;
  43. $this->_blockCollectionFactory = $blockCollectionFactory;
  44. $this->_blockFactory = $blockFactory;
  45. $this->_blockRepository = $blockRepository;
  46. $this->_pageCollectionFactory = $pageCollectionFactory;
  47. $this->_pageFactory = $pageFactory;
  48. $this->_pageRepository = $pageRepository;
  49. $this->_importPath = BP . '/app/code/Smartwave/Porto/etc/import/';
  50. $this->_parser = new \Magento\Framework\Xml\Parser();
  51. }
  52. public function importCms($type, $demo_version)
  53. {
  54. // Default response
  55. $gatewayResponse = new DataObject([
  56. 'is_valid' => false,
  57. 'import_path' => '',
  58. 'request_success' => false,
  59. 'request_message' => __('Error during Import CMS Sample Datas.'),
  60. ]);
  61. try {
  62. $xmlPath = $this->_importPath . $type . '.xml';
  63. $demoCMSxmlPath = $this->_importPath . 'demo_cms.xml';
  64. $overwrite = false;
  65. if($this->_scopeConfig->getValue("porto_settings/install/overwrite_".$type)) {
  66. $overwrite = true;
  67. }
  68. if (!is_readable($xmlPath) || !is_readable($demoCMSxmlPath))
  69. {
  70. throw new \Exception(
  71. __("Can't get the data file for import cms blocks/pages: ".$xmlPath)
  72. );
  73. }
  74. $data = $this->_parser->load($xmlPath)->xmlToArray();
  75. $cms_data = $this->_parser->load($demoCMSxmlPath)->xmlToArray();
  76. $arr = array();
  77. if($demo_version != "0") {
  78. foreach($cms_data['root']['demos'][$demo_version][$type]['item'] as $item) {
  79. if(!is_array($item)) {
  80. $arr[] = $item;
  81. } else {
  82. foreach($item as $__item) {
  83. $arr[] = $__item;
  84. }
  85. }
  86. }
  87. }
  88. $cms_collection = null;
  89. $conflictingOldItems = array();
  90. $i = 0;
  91. foreach($data['root'][$type]['cms_item'] as $_item) {
  92. $exist = false;
  93. if($demo_version == "0" || in_array($_item['identifier'],$arr)){
  94. if($type == "blocks") {
  95. $cms_collection = $this->_blockCollectionFactory->create()->addFieldToFilter('identifier', $_item['identifier']);
  96. if(count($cms_collection) > 0)
  97. $exist = true;
  98. }else {
  99. $cms_collection = $this->_pageCollectionFactory->create()->addFieldToFilter('identifier', $_item['identifier']);
  100. if(count($cms_collection) > 0)
  101. $exist = true;
  102. }
  103. if($overwrite) {
  104. if($exist) {
  105. $conflictingOldItems[] = $_item['identifier'];
  106. if($type == "blocks")
  107. $this->_blockRepository->deleteById($_item['identifier']);
  108. else
  109. $this->_pageRepository->deleteById($_item['identifier']);
  110. }
  111. } else {
  112. if($exist) {
  113. $conflictingOldItems[] = $_item['identifier'];
  114. continue;
  115. }
  116. }
  117. $_item['stores'] = [0];
  118. if($type == "blocks") {
  119. $this->_blockFactory->create()->setData($_item)->save();
  120. } else {
  121. $this->_pageFactory->create()->setData($_item)->save();
  122. }
  123. $i++;
  124. }
  125. }
  126. $message = "";
  127. if ($i)
  128. $message = $i." item(s) was(were) imported.";
  129. else
  130. $message = "No items were imported.";
  131. $gatewayResponse->setIsValid(true);
  132. $gatewayResponse->setRequestSuccess(true);
  133. if ($gatewayResponse->getIsValid()) {
  134. if ($overwrite){
  135. if($conflictingOldItems){
  136. $message .= "Items (".count($conflictingOldItems).") with the following identifiers were overwritten:<br/>".implode(', ', $conflictingOldItems);
  137. }
  138. } else {
  139. if($conflictingOldItems){
  140. $message .= "<br/>Unable to import items (".count($conflictingOldItems).") with the following identifiers (they already exist in the database):<br/>".implode(', ', $conflictingOldItems);
  141. }
  142. }
  143. }
  144. $gatewayResponse->setRequestMessage(__($message));
  145. } catch (\Exception $exception) {
  146. $gatewayResponse->setIsValid(false);
  147. $gatewayResponse->setRequestMessage($exception->getMessage());
  148. }
  149. return $gatewayResponse;
  150. }
  151. }