-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathAw.php
249 lines (215 loc) · 9.5 KB
/
Aw.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*
* Glory to Ukraine! Glory to the heroes!
*/
namespace Magefan\Blog\Model\Import;
use Magento\Framework\Config\ConfigOptionsListConstants;
/**
* Aw import model
*/
class Aw extends AbstractImport
{
protected $_requiredFields = ['dbname', 'uname', 'dbhost'];
public function execute()
{
$adapter = $this->getDbAdapter();
$_pref = $this->getPrefix();
$sql = 'SELECT * FROM '.$_pref.'aw_blog_cat LIMIT 1';
try {
$adapter->query($sql)->execute();
} catch (\Exception $e) {
throw new \Exception(__('AheadWorks Blog Extension not detected.'), 1);
}
$storeIds = array_keys($this->_storeManager->getStores(true));
$categories = [];
$oldCategories = [];
/* Import categories */
$sql = 'SELECT
t.cat_id as old_id,
t.title as title,
t.identifier as identifier,
t.sort_order as position,
t.meta_keywords as meta_keywords,
t.meta_description as meta_description
FROM '.$_pref.'aw_blog_cat t';
$result = $adapter->query($sql)->execute();
foreach ($result as $data) {
/* Prepare category data */
/* Find store ids */
$data['store_ids'] = [];
$s_sql = 'SELECT store_id FROM '.$_pref.'aw_blog_cat_store WHERE cat_id = "'.$data['old_id'].'"';
$s_result = $adapter->query($s_sql)->execute();
foreach ($s_result as $s_data) {
$data['store_ids'][] = $s_data['store_id'];
}
foreach ($data['store_ids'] as $key => $id) {
if (!in_array($id, $storeIds)) {
unset($data['store_ids'][$key]);
}
}
if (empty($data['store_ids']) || in_array(0, $data['store_ids'])) {
$data['store_ids'] = 0;
}
$data['is_active'] = 1;
$data['path'] = 0;
$data['identifier'] = trim(strtolower($data['identifier']));
if (strlen($data['identifier']) == 1) {
$data['identifier'] .= $data['identifier'];
}
$category = $this->_categoryFactory->create();
try {
/* Initial saving */
$category->setData($data)->save();
$this->_importedCategoriesCount++;
$categories[$category->getId()] = $category;
$oldCategories[$category->getOldId()] = $category;
} catch (\Magento\Framework\Exception\LocalizedException $e) {
unset($category);
$this->_skippedCategories[] = $data['title'];
$this->_logger->debug('Blog Category Import [' . $data['title'] . ']: '. $e->getMessage());
}
}
/* Import tags */
$tags = [];
$oldTags = [];
$existingTags = [];
$sql = 'SELECT
t.id as old_id,
t.tag as title
FROM '.$_pref.'aw_blog_tags t';
$result = $adapter->query($sql)->execute();
foreach ($result as $data) {
/* Prepare tag data */
/*
foreach (['title'] as $key) {
$data[$key] = mb_convert_encoding($data[$key], 'HTML-ENTITIES', 'UTF-8');
}
*/
if (!$data['title']) {
continue;
}
$data['title'] = trim($data['title']);
try {
/* Initial saving */
if (!isset($existingTags[$data['title']])) {
$tag = $this->_tagFactory->create();
$tag->setData($data)->save();
$this->_importedTagsCount++;
$tags[$tag->getId()] = $tag;
$oldTags[$tag->getOldId()] = $tag;
$existingTags[$tag->getTitle()] = $tag;
} else {
$tag = $existingTags[$data['title']];
$oldTags[$data['old_id']] = $tag;
}
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->_skippedTags[] = $data['title'];
$this->_logger->debug('Blog Tag Import [' . $data['title'] . ']: '. $e->getMessage());
}
}
/* Import posts */
$sql = 'SELECT * FROM '.$_pref.'aw_blog';
$result = $adapter->query($sql)->execute();
foreach ($result as $data) {
/* Find post categories*/
$postCategories = [];
$c_sql = 'SELECT cat_id as category_id FROM '.
$_pref.'aw_blog_post_cat WHERE post_id = "'.$data['post_id'].'"';
$c_result = $adapter->query($c_sql)->execute();
foreach ($c_result as $c_data) {
$oldId = $c_data['category_id'];
if (isset($oldCategories[$oldId])) {
$id = $oldCategories[$oldId]->getId();
$postCategories[$id] = $id;
}
}
/* Find store ids */
$data['store_ids'] = [];
$s_sql = 'SELECT store_id FROM '.$_pref.'aw_blog_store WHERE post_id = "'.$data['post_id'].'"';
$s_result = $adapter->query($s_sql)->execute();
foreach ($s_result as $s_data) {
$data['store_ids'][] = $s_data['store_id'];
}
foreach ($data['store_ids'] as $key => $id) {
if (!in_array($id, $storeIds)) {
unset($data['store_ids'][$key]);
}
}
if (empty($data['store_ids']) || in_array(0, $data['store_ids'])) {
$data['store_ids'] = 0;
}
/* Prepare post data */
$data = [
'old_id' => $data['post_id'],
'store_ids' => $data['store_ids'],
'title' => $data['title'],
'meta_keywords' => $data['meta_keywords'],
'meta_description' => $data['meta_description'],
'identifier' => $data['identifier'],
'content_heading' => '',
'content' => str_replace('<!--more-->', '<!-- pagebreak -->', $data['post_content']),
'short_content' => $data['short_content'],
'creation_time' => strtotime((string)$data['created_time']),
'update_time' => strtotime((string)$data['update_time']),
'publish_time' => strtotime((string)$data['created_time']),
'is_active' => (int)($data['status'] == 1),
'categories' => $postCategories,
'featured_img' => !empty($data['featured_image']) ? 'magefan_blog/' . $data['featured_image'] : '',
];
$data['identifier'] = trim(strtolower($data['identifier']));
$post = $this->_postFactory->create();
try {
/* Post saving */
$post->setData($data)->save();
/* find post comment s*/
$sql = 'SELECT * FROM '.$_pref.'aw_blog_comment WHERE `post_id` = ' . $post->getOldId();
$resultComments = $adapter->query($sql)->execute();
foreach ($resultComments as $comments) {
$commentParentId = 0;
$commentData = [
'parent_id' => $commentParentId,
'post_id' => $post->getPostId(),
'status' => ($comments['status'] == 2) ?
\Magefan\Blog\Model\Config\Source\CommentStatus::APPROVED :
\Magefan\Blog\Model\Config\Source\CommentStatus::NOT_APPROVED,
'author_type' => \Magefan\Blog\Model\Config\Source\AuthorType::GUEST,
'author_nickname' => $comments['user'],
'author_email' => $comments['email'],
'text' => $comments['comment'],
'creation_time' => $comments['created_time'],
];
/*
foreach (['text'] as $key) {
$commentData[$key] = mb_convert_encoding($commentData[$key], 'HTML-ENTITIES', 'UTF-8');
}
*/
if (!$commentData['text']) {
continue;
}
$comment = $this->_commentFactory->create($commentData);
try {
/* saving */
$comment->setData($commentData)->save();
$this->_importedCommentsCount++;
} catch (\Exception $e) {
if (!isset($commentData['title'])) {
$commentData['title'] = 'Undefined';
}
$this->_skippedComments[] = $commentData['title'];
unset($comment);
}
}
$this->_importedPostsCount++;
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->_skippedPosts[] = $data['title'];
$this->_logger->debug('Blog Post Import [' . $data['title'] . ']: '. $e->getMessage());
}
unset($post);
}
/* end */
$adapter->getDriver()->getConnection()->disconnect();
}
}