123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- /**
- * Copyright © 2015 Magento. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Smartwave\Porto\Model\Import;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- use Magento\Framework\DataObject;
- use Magento\Store\Model\ScopeInterface;
- use Magento\Cms\Model\ResourceModel\Block\CollectionFactory as BlockCollectionFactory;
- use Magento\Cms\Model\BlockFactory as BlockFactory;
- use Magento\Cms\Model\ResourceModel\Block as BlockResourceBlock;
- use Magento\Cms\Model\ResourceModel\Page\CollectionFactory as PageCollectionFactory;
- use Magento\Cms\Model\PageFactory as PageFactory;
- use Magento\Cms\Model\ResourceModel\Page as PageResourceBlock;
- class Cms
- {
- /**
- * @var ScopeConfigInterface
- */
- protected $_scopeConfig;
-
- protected $_storeManager;
-
- private $_importPath;
-
- protected $_parser;
-
- protected $_blockCollectionFactory;
- protected $_blockRepository;
-
- protected $_blockFactory;
-
- protected $_pageCollectionFactory;
- protected $_pageRepository;
-
- protected $_pageFactory;
-
- public function __construct(
- ScopeConfigInterface $scopeConfig,
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- BlockCollectionFactory $blockCollectionFactory,
- \Magento\Cms\Api\BlockRepositoryInterface $blockRepository,
- BlockFactory $blockFactory,
- PageCollectionFactory $pageCollectionFactory,
- \Magento\Cms\Api\PageRepositoryInterface $pageRepository,
- PageFactory $pageFactory
- ) {
- $this->_scopeConfig = $scopeConfig;
- $this->_storeManager = $storeManager;
- $this->_blockCollectionFactory = $blockCollectionFactory;
- $this->_blockFactory = $blockFactory;
- $this->_blockRepository = $blockRepository;
- $this->_pageCollectionFactory = $pageCollectionFactory;
- $this->_pageFactory = $pageFactory;
- $this->_pageRepository = $pageRepository;
- $this->_importPath = BP . '/app/code/Smartwave/Porto/etc/import/';
- $this->_parser = new \Magento\Framework\Xml\Parser();
- }
- public function importCms($type, $demo_version)
- {
- // Default response
- $gatewayResponse = new DataObject([
- 'is_valid' => false,
- 'import_path' => '',
- 'request_success' => false,
- 'request_message' => __('Error during Import CMS Sample Datas.'),
- ]);
- try {
- $xmlPath = $this->_importPath . $type . '.xml';
- $demoCMSxmlPath = $this->_importPath . 'demo_cms.xml';
-
- $overwrite = false;
-
- if($this->_scopeConfig->getValue("porto_settings/install/overwrite_".$type)) {
- $overwrite = true;
- }
-
- if (!is_readable($xmlPath) || !is_readable($demoCMSxmlPath))
- {
- throw new \Exception(
- __("Can't get the data file for import cms blocks/pages: ".$xmlPath)
- );
- }
- $data = $this->_parser->load($xmlPath)->xmlToArray();
- $cms_data = $this->_parser->load($demoCMSxmlPath)->xmlToArray();
-
- $arr = array();
- if($demo_version != "0") {
- foreach($cms_data['root']['demos'][$demo_version][$type]['item'] as $item) {
- if(!is_array($item)) {
- $arr[] = $item;
- } else {
- foreach($item as $__item) {
- $arr[] = $__item;
- }
- }
- }
- }
- $cms_collection = null;
- $conflictingOldItems = array();
-
- $i = 0;
- foreach($data['root'][$type]['cms_item'] as $_item) {
- $exist = false;
- if($demo_version == "0" || in_array($_item['identifier'],$arr)){
- if($type == "blocks") {
- $cms_collection = $this->_blockCollectionFactory->create()->addFieldToFilter('identifier', $_item['identifier']);
- if(count($cms_collection) > 0)
- $exist = true;
-
- }else {
- $cms_collection = $this->_pageCollectionFactory->create()->addFieldToFilter('identifier', $_item['identifier']);
- if(count($cms_collection) > 0)
- $exist = true;
-
- }
- if($overwrite) {
- if($exist) {
- $conflictingOldItems[] = $_item['identifier'];
- if($type == "blocks")
- $this->_blockRepository->deleteById($_item['identifier']);
- else
- $this->_pageRepository->deleteById($_item['identifier']);
- }
- } else {
- if($exist) {
- $conflictingOldItems[] = $_item['identifier'];
- continue;
- }
- }
- $_item['stores'] = [0];
- if($type == "blocks") {
- $this->_blockFactory->create()->setData($_item)->save();
- } else {
- $this->_pageFactory->create()->setData($_item)->save();
- }
- $i++;
- }
- }
- $message = "";
- if ($i)
- $message = $i." item(s) was(were) imported.";
- else
- $message = "No items were imported.";
-
- $gatewayResponse->setIsValid(true);
- $gatewayResponse->setRequestSuccess(true);
-
- if ($gatewayResponse->getIsValid()) {
- if ($overwrite){
- if($conflictingOldItems){
- $message .= "Items (".count($conflictingOldItems).") with the following identifiers were overwritten:<br/>".implode(', ', $conflictingOldItems);
- }
- } else {
- if($conflictingOldItems){
- $message .= "<br/>Unable to import items (".count($conflictingOldItems).") with the following identifiers (they already exist in the database):<br/>".implode(', ', $conflictingOldItems);
- }
- }
- }
- $gatewayResponse->setRequestMessage(__($message));
- } catch (\Exception $exception) {
- $gatewayResponse->setIsValid(false);
- $gatewayResponse->setRequestMessage($exception->getMessage());
- }
- return $gatewayResponse;
- }
- }
|