Skip to content

Commit 49d3ccf

Browse files
authored
Merge pull request #250 from magefan/2178-import-aw-blog-m2
2178-import-aw-blog-m2 add aw2 controller, model, layout, change temp…
2 parents 3bb5759 + 346ad97 commit 49d3ccf

File tree

8 files changed

+303
-4
lines changed

8 files changed

+303
-4
lines changed

Block/Adminhtml/Import/Aheadworks/Form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ protected function _prepareForm()
191191
$data['dbhost'] = 'localhost';
192192
}
193193

194-
$data['type'] = 'aw';
194+
$data['type'] = $this->getRequest()->getActionName();
195195

196196
$form->setValues($data);
197197

Controller/Adminhtml/Import/Aw.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function execute()
2626
$this->_addBreadcrumb($title, $title);
2727

2828
$config = new \Magento\Framework\DataObject(
29-
(array)$this->_getSession()->getData('import_aheadworks_form_data', true) ?: []
29+
(array)$this->_getSession()->getData('import_aw_form_data', true) ?: []
3030
);
3131

3232
$this->_objectManager->get(\Magento\Framework\Registry::class)->register('import_config', $config);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Controller\Adminhtml\Import;
10+
11+
/**
12+
* Blog aw2 import controller
13+
*/
14+
class Aw2 extends \Magento\Backend\App\Action
15+
{
16+
/**
17+
* Prepare aw import
18+
* @return \Magento\Framework\Controller\ResultInterface
19+
*/
20+
public function execute()
21+
{
22+
$this->_view->loadLayout();
23+
$this->_setActiveMenu('Magefan_Blog::import');
24+
$title = __('Blog Import from AheadWorks M2');
25+
$this->_view->getPage()->getConfig()->getTitle()->prepend($title);
26+
$this->_addBreadcrumb($title, $title);
27+
28+
$config = new \Magento\Framework\DataObject(
29+
(array)$this->_getSession()->getData('import_aw2_form_data', true) ?: []
30+
);
31+
32+
$this->_objectManager->get(\Magento\Framework\Registry::class)->register('import_config', $config);
33+
34+
$this->_view->renderLayout();
35+
}
36+
37+
/**
38+
* Check is allowed access
39+
*
40+
* @return bool
41+
*/
42+
protected function _isAllowed()
43+
{
44+
return $this->_authorization->isAllowed('Magefan_Blog::import');
45+
}
46+
}

Model/Import/Aw2.php

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
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+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magefan ([email protected]). All rights reserved.
5+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
6+
*
7+
* Glory to Ukraine! Glory to the heroes!
8+
*/
9+
-->
10+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
11+
<update handle="blog_import_aw"/>
12+
</page>

view/adminhtml/templates/import.phtml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@
4242
<tr>
4343
</table>
4444
</td>
45+
<td class="item">
46+
<table>
47+
<tr>
48+
<td>
49+
<div class="itm-hld" onclick="window.location='<?= $block->escapeUrl($block->getUrl('blog/import/aw2')) ?>'">
50+
<img class="img" src="<?= $block->escapeUrl($this->getViewFileUrl('Magefan_Blog::images/aw2_blog.png')) ?>" />
51+
<br/>
52+
<span class="lable">Import from AheadWorks<br/><br/></span>
53+
</div>
54+
</td>
55+
<tr>
56+
</table>
57+
</td>
4558
<td class="item">
4659
<table>
4760
<tr>
@@ -72,6 +85,7 @@
7285
<tr>
7386
</table>
7487
</td>
88+
<tr>
7589
<td class="item">
7690
<table>
7791
<tr>
@@ -85,8 +99,7 @@
8599
<tr>
86100
</table>
87101
</td>
88-
</tr>
89-
<tr>
102+
</td>
90103
<td class="item">
91104
<table>
92105
<tr>
2.64 KB
Loading
15 Bytes
Loading

0 commit comments

Comments
 (0)