|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magefan ([email protected]). All rights reserved. |
| 4 | + * Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement). |
| 5 | + * |
| 6 | + * Glory to Ukraine! Glory to the heroes! |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Magefan\Blog\Model\Import; |
| 10 | + |
| 11 | +use Magento\Framework\Config\ConfigOptionsListConstants; |
| 12 | + |
| 13 | +/** |
| 14 | + * Aw2 import model |
| 15 | + */ |
| 16 | +class Aw2 extends AbstractImport |
| 17 | +{ |
| 18 | + protected $_requiredFields = ['dbname', 'uname', 'dbhost']; |
| 19 | + |
| 20 | + public function execute() |
| 21 | + { |
| 22 | + $host = $this->getData('dbhost') ?: $this->getData('host'); |
| 23 | + if (false !== strpos($host, '.sock')) { |
| 24 | + $con = $this->_connect = mysqli_connect( |
| 25 | + 'localhost', |
| 26 | + $this->getData('uname'), |
| 27 | + $this->getData('pwd'), |
| 28 | + $this->getData('dbname'), |
| 29 | + null, |
| 30 | + $host |
| 31 | + ); |
| 32 | + } else { |
| 33 | + $con = $this->_connect = mysqli_connect( |
| 34 | + $this->getData('dbhost'), |
| 35 | + $this->getData('uname'), |
| 36 | + $this->getData('pwd'), |
| 37 | + $this->getData('dbname') |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + if (mysqli_connect_errno()) { |
| 42 | + throw new \Exception("Failed connect to magento database", 1); |
| 43 | + } |
| 44 | + |
| 45 | + mysqli_set_charset($con, "utf8"); |
| 46 | + |
| 47 | + $_pref = mysqli_real_escape_string($con, $this->getData('prefix')); |
| 48 | + |
| 49 | + $sql = 'SELECT * FROM '.$_pref.'aw_blog_category LIMIT 1'; |
| 50 | + try { |
| 51 | + $this->_mysqliQuery($sql); |
| 52 | + } catch (\Exception $e) { |
| 53 | + throw new \Exception(__('AheadWorks Blog Extension not detected.'), 1); |
| 54 | + } |
| 55 | + |
| 56 | + $storeIds = array_keys($this->_storeManager->getStores(true)); |
| 57 | + |
| 58 | + $categories = []; |
| 59 | + $oldCategories = []; |
| 60 | + |
| 61 | + /* Import categories */ |
| 62 | + $sql = 'SELECT |
| 63 | + t.id as old_id, |
| 64 | + t.name as title, |
| 65 | + t.url_key as identifier, |
| 66 | + t.sort_order as position, |
| 67 | + t.meta_description as meta_description |
| 68 | + FROM '.$_pref.'aw_blog_category t'; |
| 69 | + |
| 70 | + $result = $this->_mysqliQuery($sql); |
| 71 | + while ($data = mysqli_fetch_assoc($result)) { |
| 72 | + /* Prepare category data */ |
| 73 | + |
| 74 | + /* Find store ids */ |
| 75 | + $data['store_ids'] = []; |
| 76 | + $s_sql = 'SELECT store_id FROM '.$_pref.'aw_blog_category_store WHERE category_id = "'.$data['old_id'].'"'; |
| 77 | + $s_result = $this->_mysqliQuery($s_sql); |
| 78 | + while ($s_data = mysqli_fetch_assoc($s_result)) { |
| 79 | + $data['store_ids'][] = $s_data['store_id']; |
| 80 | + } |
| 81 | + |
| 82 | + foreach ($data['store_ids'] as $key => $id) { |
| 83 | + if (!in_array($id, $storeIds)) { |
| 84 | + unset($data['store_ids'][$key]); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (empty($data['store_ids']) || in_array(0, $data['store_ids'])) { |
| 89 | + $data['store_ids'] = 0; |
| 90 | + } |
| 91 | + |
| 92 | + $data['is_active'] = 1; |
| 93 | + $data['path'] = 0; |
| 94 | + $data['identifier'] = trim(strtolower($data['identifier'])); |
| 95 | + if (strlen($data['identifier']) == 1) { |
| 96 | + $data['identifier'] .= $data['identifier']; |
| 97 | + } |
| 98 | + |
| 99 | + $category = $this->_categoryFactory->create(); |
| 100 | + try { |
| 101 | + /* Initial saving */ |
| 102 | + $category->setData($data)->save(); |
| 103 | + $this->_importedCategoriesCount++; |
| 104 | + $categories[$category->getId()] = $category; |
| 105 | + $oldCategories[$category->getOldId()] = $category; |
| 106 | + } catch (\Magento\Framework\Exception\LocalizedException $e) { |
| 107 | + unset($category); |
| 108 | + $this->_skippedCategories[] = $data['title']; |
| 109 | + $this->_logger->addDebug('Blog Category Import [' . $data['title'] . ']: '. $e->getMessage()); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /* Import tags */ |
| 114 | + $tags = []; |
| 115 | + $oldTags = []; |
| 116 | + $existingTags = []; |
| 117 | + |
| 118 | + $sql = 'SELECT |
| 119 | + t.id as old_id, |
| 120 | + t.name as title |
| 121 | + FROM '.$_pref.'aw_blog_tag t'; |
| 122 | + |
| 123 | + $result = $this->_mysqliQuery($sql); |
| 124 | + while ($data = mysqli_fetch_assoc($result)) { |
| 125 | + /* Prepare tag data */ |
| 126 | + foreach (['title'] as $key) { |
| 127 | + $data[$key] = mb_convert_encoding($data[$key], 'HTML-ENTITIES', 'UTF-8'); |
| 128 | + } |
| 129 | + |
| 130 | + if (!$data['title']) { |
| 131 | + continue; |
| 132 | + } |
| 133 | + |
| 134 | + $data['title'] = trim($data['title']); |
| 135 | + |
| 136 | + |
| 137 | + try { |
| 138 | + /* Initial saving */ |
| 139 | + if (!isset($existingTags[$data['title']])) { |
| 140 | + $tag = $this->_tagFactory->create(); |
| 141 | + $tag->setData($data)->save(); |
| 142 | + $this->_importedTagsCount++; |
| 143 | + $tags[$tag->getId()] = $tag; |
| 144 | + $oldTags[$tag->getOldId()] = $tag; |
| 145 | + $existingTags[$tag->getTitle()] = $tag; |
| 146 | + } else { |
| 147 | + $tag = $existingTags[$data['title']]; |
| 148 | + $oldTags[$data['old_id']] = $tag; |
| 149 | + } |
| 150 | + } catch (\Magento\Framework\Exception\LocalizedException $e) { |
| 151 | + $this->_skippedTags[] = $data['title']; |
| 152 | + $this->_logger->addDebug('Blog Tag Import [' . $data['title'] . ']: '. $e->getMessage()); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | + /* Import posts */ |
| 158 | + $sql = 'SELECT * FROM '.$_pref.'aw_blog_post'; |
| 159 | + $result = $this->_mysqliQuery($sql); |
| 160 | + |
| 161 | + while ($data = mysqli_fetch_assoc($result)) { |
| 162 | + /* Find post categories*/ |
| 163 | + $postCategories = []; |
| 164 | + $c_sql = 'SELECT category_id FROM '.$_pref.'aw_blog_post_category WHERE post_id = "'.$data['id'].'"'; |
| 165 | + $c_result = $this->_mysqliQuery($c_sql); |
| 166 | + while ($c_data = mysqli_fetch_assoc($c_result)) { |
| 167 | + $oldId = $c_data['category_id']; |
| 168 | + if (isset($oldCategories[$oldId])) { |
| 169 | + $id = $oldCategories[$oldId]->getId(); |
| 170 | + $postCategories[$id] = $id; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /* Find store ids */ |
| 175 | + $data['store_ids'] = []; |
| 176 | + $s_sql = 'SELECT store_id FROM '.$_pref.'aw_blog_post_store WHERE post_id = "'.$data['id'].'"'; |
| 177 | + $s_result = $this->_mysqliQuery($s_sql); |
| 178 | + while ($s_data = mysqli_fetch_assoc($s_result)) { |
| 179 | + $data['store_ids'][] = $s_data['store_id']; |
| 180 | + } |
| 181 | + |
| 182 | + foreach ($data['store_ids'] as $key => $id) { |
| 183 | + if (!in_array($id, $storeIds)) { |
| 184 | + unset($data['store_ids'][$key]); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + if (empty($data['store_ids']) || in_array(0, $data['store_ids'])) { |
| 189 | + $data['store_ids'] = 0; |
| 190 | + } |
| 191 | + |
| 192 | + /* Prepare post data */ |
| 193 | + $data = [ |
| 194 | + 'old_id' => $data['id'], |
| 195 | + 'store_ids' => $data['store_ids'], |
| 196 | + 'title' => $data['title'], |
| 197 | + 'meta_description' => $data['meta_description'], |
| 198 | + 'identifier' => $data['url_key'], |
| 199 | + 'content_heading' => '', |
| 200 | + 'content' => str_replace('<!--more-->', '<!-- pagebreak -->', $data['content']), |
| 201 | + 'short_content' => $data['short_content'], |
| 202 | + 'creation_time' => strtotime($data['created_at']), |
| 203 | + 'update_time' => strtotime($data['updated_at']), |
| 204 | + 'publish_time' => strtotime($data['publish_date']), |
| 205 | + 'is_active' => (int)($data['status'] == 'publication'), |
| 206 | + 'categories' => $postCategories, |
| 207 | + 'featured_img' => !empty($data['featured_image']) ? 'magefan_blog/' . $data['featured_image'] : '', |
| 208 | + ]; |
| 209 | + $data['identifier'] = trim(strtolower($data['identifier'])); |
| 210 | + |
| 211 | + $post = $this->_postFactory->create(); |
| 212 | + try { |
| 213 | + /* Post saving */ |
| 214 | + $post->setData($data)->save(); |
| 215 | + |
| 216 | + $this->_importedPostsCount++; |
| 217 | + } catch (\Magento\Framework\Exception\LocalizedException $e) { |
| 218 | + $this->_skippedPosts[] = $data['title']; |
| 219 | + $this->_logger->addDebug('Blog Post Import [' . $data['title'] . ']: '. $e->getMessage()); |
| 220 | + } |
| 221 | + |
| 222 | + unset($post); |
| 223 | + } |
| 224 | + /* end */ |
| 225 | + |
| 226 | + mysqli_close($con); |
| 227 | + } |
| 228 | +} |
0 commit comments