AbstractImport.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * Copyright © 2016 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Model\Import;
  9. /**
  10. * Abstract import model
  11. */
  12. abstract class AbstractImport extends \Magento\Framework\Model\AbstractModel
  13. {
  14. /**
  15. * Connect to bd
  16. */
  17. protected $_connect;
  18. /**
  19. * @var array
  20. */
  21. protected $_requiredFields = [];
  22. /**
  23. * @var \Magefan\Blog\Model\PostFactory
  24. */
  25. protected $_postFactory;
  26. /**
  27. * @var \Magefan\Blog\Model\CategoryFactory
  28. */
  29. protected $_categoryFactory;
  30. /**
  31. * @var \Magefan\Blog\Model\TagFactory
  32. */
  33. protected $_tagFactory;
  34. /**
  35. * @var integer
  36. */
  37. protected $_importedPostsCount = 0;
  38. /**
  39. * @var integer
  40. */
  41. protected $_importedCategoriesCount = 0;
  42. /**
  43. * @var integer
  44. */
  45. protected $_importedTagsCount = 0;
  46. /**
  47. * @var array
  48. */
  49. protected $_skippedPosts = [];
  50. /**
  51. * @var array
  52. */
  53. protected $_skippedCategories = [];
  54. /**
  55. * @var array
  56. */
  57. protected $_skippedTags = [];
  58. /**
  59. * @var \Magento\Store\Model\StoreManagerInterface
  60. */
  61. protected $_storeManager;
  62. /**
  63. * Initialize dependencies.
  64. *
  65. * @param \Magento\Framework\Model\Context $context
  66. * @param \Magento\Framework\Registry $registry
  67. * @param \Magefan\Blog\Model\PostFactory $postFactory,
  68. * @param \Magefan\Blog\Model\CategoryFactory $categoryFactory,
  69. * @param \Magento\Store\Model\StoreManagerInterface $storeManager,
  70. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  71. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  72. * @param array $data
  73. */
  74. public function __construct(
  75. \Magento\Framework\Model\Context $context,
  76. \Magento\Framework\Registry $registry,
  77. \Magefan\Blog\Model\PostFactory $postFactory,
  78. \Magefan\Blog\Model\CategoryFactory $categoryFactory,
  79. \Magefan\Blog\Model\TagFactory $tagFactory,
  80. \Magento\Store\Model\StoreManagerInterface $storeManager,
  81. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  82. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  83. array $data = []
  84. ) {
  85. $this->_postFactory = $postFactory;
  86. $this->_categoryFactory = $categoryFactory;
  87. $this->_tagFactory = $tagFactory;
  88. $this->_storeManager = $storeManager;
  89. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  90. }
  91. /**
  92. * Retrieve import statistic
  93. * @return \Magento\Framework\DataObject
  94. */
  95. public function getImportStatistic()
  96. {
  97. return new \Magento\Framework\DataObject([
  98. 'imported_posts_count' => $this->_importedPostsCount,
  99. 'imported_categories_count' => $this->_importedCategoriesCount,
  100. 'skipped_posts' => $this->_skippedPosts,
  101. 'skipped_categories' => $this->_skippedCategories,
  102. 'imported_count' => $this->_importedPostsCount + $this->_importedCategoriesCount + $this->_importedTagsCount,
  103. 'skipped_count' => count($this->_skippedPosts) + count($this->_skippedCategories) + count($this->_skippedTags),
  104. 'imported_tags_count' => $this->_importedTagsCount,
  105. 'skipped_tags' => $this->_skippedTags,
  106. ]);
  107. }
  108. /**
  109. * Prepare import data
  110. * @param array $data
  111. * @return $this
  112. */
  113. public function prepareData($data)
  114. {
  115. if (!is_array($data)) {
  116. $data = (array) $data;
  117. }
  118. foreach($this->_requiredFields as $field) {
  119. if (empty($data[$field])) {
  120. throw new \Exception(__('Parameter %1 is required', $field), 1);
  121. }
  122. }
  123. foreach($data as $field => $value) {
  124. if (!in_array($field, $this->_requiredFields)) {
  125. unset($data[$field]);
  126. }
  127. }
  128. $this->setData($data);
  129. return $this;
  130. }
  131. /**
  132. * Execute mysql query
  133. */
  134. protected function _mysqliQuery($sql)
  135. {
  136. $result = mysqli_query($this->_connect, $sql);
  137. if (!$result) {
  138. throw new \Exception(
  139. __('Mysql error: %1.', mysqli_error($this->_connect))
  140. );
  141. }
  142. return $result;
  143. }
  144. }