Aw.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. use Magento\Framework\Config\ConfigOptionsListConstants;
  10. /**
  11. * Aw import model
  12. */
  13. class Aw extends AbstractImport
  14. {
  15. public function execute()
  16. {
  17. $config = \Magento\Framework\App\ObjectManager::getInstance()
  18. ->get('Magento\Framework\App\DeploymentConfig');
  19. $pref = ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT . '/';
  20. $this->setData('dbhost',
  21. $config->get($pref . ConfigOptionsListConstants::KEY_HOST)
  22. )->setData('uname',
  23. $config->get($pref . ConfigOptionsListConstants::KEY_USER)
  24. )->setData('pwd',
  25. $config->get($pref . ConfigOptionsListConstants::KEY_PASSWORD)
  26. )->setData('dbname',
  27. $config->get($pref . ConfigOptionsListConstants::KEY_NAME)
  28. );
  29. $con = $this->_connect = mysqli_connect(
  30. $this->getData('dbhost'),
  31. $this->getData('uname'),
  32. $this->getData('pwd'),
  33. $this->getData('dbname')
  34. );
  35. if (mysqli_connect_errno()) {
  36. throw new \Exception("Failed connect to magento database", 1);
  37. }
  38. $_pref = mysqli_real_escape_string($con,
  39. $config->get($pref . ConfigOptionsListConstants::KEY_PREFIX)
  40. );
  41. $sql = 'SELECT * FROM '.$_pref.'aw_blog_cat LIMIT 1';
  42. try {
  43. $this->_mysqliQuery($sql);
  44. } catch (\Exception $e) {
  45. throw new \Exception(__('AheadWorks Blog Extension not detected.'), 1);
  46. }
  47. $storeIds = array_keys($this->_storeManager->getStores(true));
  48. $categories = [];
  49. $oldCategories = [];
  50. /* Import categories */
  51. $sql = 'SELECT
  52. t.cat_id as old_id,
  53. t.title as title,
  54. t.identifier as identifier,
  55. t.sort_order as position,
  56. t.meta_keywords as meta_keywords,
  57. t.meta_description as meta_description
  58. FROM '.$_pref.'aw_blog_cat t';
  59. $result = $this->_mysqliQuery($sql);
  60. while ($data = mysqli_fetch_assoc($result)) {
  61. /* Prepare category data */
  62. /* Find store ids */
  63. $data['store_ids'] = [];
  64. $s_sql = 'SELECT store_id FROM '.$_pref.'aw_blog_cat_store WHERE cat_id = "'.$data['old_id'].'"';
  65. $s_result = $this->_mysqliQuery($s_sql);
  66. while ($s_data = mysqli_fetch_assoc($s_result)) {
  67. $data['store_ids'][] = $s_data['store_id'];
  68. }
  69. foreach ($data['store_ids'] as $key => $id) {
  70. if (!in_array($id, $storeIds)) {
  71. unset($data['store_ids'][$key]);
  72. }
  73. }
  74. if (empty($data['store_ids']) || in_array(0, $data['store_ids'])) {
  75. $data['store_ids'] = 0;
  76. }
  77. $data['is_active'] = 1;
  78. $data['path'] = 0;
  79. $data['identifier'] = trim(strtolower($data['identifier']));
  80. if (strlen($data['identifier']) == 1) {
  81. $data['identifier'] .= $data['identifier'];
  82. }
  83. $category = $this->_categoryFactory->create();
  84. try {
  85. /* Initial saving */
  86. $category->setData($data)->save();
  87. $this->_importedCategoriesCount++;
  88. $categories[$category->getId()] = $category;
  89. $oldCategories[$category->getOldId()] = $category;
  90. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  91. unset($category);
  92. $this->_skippedCategories[] = $data['title'];
  93. }
  94. }
  95. /* Import posts */
  96. $sql = 'SELECT * FROM '.$_pref.'aw_blog';
  97. $result = $this->_mysqliQuery($sql);
  98. while ($data = mysqli_fetch_assoc($result)) {
  99. /* Find post categories*/
  100. $c_sql = 'SELECT cat_id as category_id FROM '.$_pref.'aw_blog_post_cat WHERE post_id = "'.$data['post_id'].'"';
  101. $c_result = $this->_mysqliQuery($c_sql);
  102. while ($c_data = mysqli_fetch_assoc($c_result)) {
  103. $oldId = $c_data['category_id'];
  104. if (isset($oldCategories[$oldId])) {
  105. $id = $oldCategories[$oldId]->getId();
  106. $postCategories[$id] = $id;
  107. }
  108. }
  109. /* Find store ids */
  110. $data['store_ids'] = [];
  111. $s_sql = 'SELECT store_id FROM '.$_pref.'aw_blog_store WHERE post_id = "'.$data['post_id'].'"';
  112. $s_result = $this->_mysqliQuery($s_sql);
  113. while ($s_data = mysqli_fetch_assoc($s_result)) {
  114. $data['store_ids'][] = $s_data['store_id'];
  115. }
  116. foreach ($data['store_ids'] as $key => $id) {
  117. if (!in_array($id, $storeIds)) {
  118. unset($data['store_ids'][$key]);
  119. }
  120. }
  121. if (empty($data['store_ids']) || in_array(0, $data['store_ids'])) {
  122. $data['store_ids'] = 0;
  123. }
  124. /* Prepare post data */
  125. $data = [
  126. 'store_ids' => $data['store_ids'],
  127. 'title' => $data['title'],
  128. 'meta_keywords' => $data['meta_keywords'],
  129. 'meta_description' => $data['meta_description'],
  130. 'identifier' => $data['identifier'],
  131. 'content_heading' => '',
  132. 'content' => str_replace('<!--more-->', '<!-- pagebreak -->', $data['post_content']),
  133. 'creation_time' => strtotime($data['created_time']),
  134. 'update_time' => strtotime($data['update_time']),
  135. 'publish_time' => strtotime($data['created_time']),
  136. 'is_active' => (int)($data['status'] == 1),
  137. 'categories' => $postCategories,
  138. ];
  139. $data['identifier'] = trim(strtolower($data['identifier']));
  140. $post = $this->_postFactory->create();
  141. try {
  142. /* Post saving */
  143. $post->setData($data)->save();
  144. $this->_importedPostsCount++;
  145. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  146. $this->_skippedPosts[] = $data['title'];
  147. }
  148. unset($post);
  149. }
  150. /* end */
  151. mysqli_close($con);
  152. }
  153. }