99namespace Magefan \Blog \Model \Import ;
1010
1111use Magento \Framework \Config \ConfigOptionsListConstants ;
12+
1213/**
13- * Aw import model
14+ * Mageplaza import model
1415 */
1516class Mageplaza extends AbstractImport
1617{
@@ -51,6 +52,7 @@ public function execute()
5152 $ this ->getData ('dbname ' )
5253 );
5354 }
55+
5456 if (mysqli_connect_errno ()) {
5557 throw new \Exception ("Failed connect to magento database " , 1 );
5658 }
@@ -60,12 +62,7 @@ public function execute()
6062 $ config ->get ('db/table_prefix ' )
6163 );
6264
63-
64-
65-
66-
67- $ sql = 'SELECT * FROM ' .$ _pref .'mageplaza_blog_category LIMIT 1 ' ;
68-
65+ $ sql = 'SELECT * FROM ' . $ _pref . 'mageplaza_blog_category LIMIT 1 ' ;
6966 try {
7067 $ this ->_mysqliQuery ($ sql );
7168 } catch (\Exception $ e ) {
@@ -74,30 +71,34 @@ public function execute()
7471
7572 $ categories = [];
7673 $ oldCategories = [];
74+
7775 /* Import categories */
7876 $ sql = 'SELECT
7977 t.category_id as old_id,
8078 t.name as title,
8179 t.url_key as identifier,
8280 t.position as position,
81+ t.meta_title as meta_title,
8382 t.meta_keywords as meta_keywords,
8483 t.meta_description as meta_description,
8584 t.description as content,
86- t.parent_id as path,
85+ t.parent_id as parent_id,
86+ t.position as position,
8787 t.enabled as is_active,
8888 t.store_ids as store_ids
89-
90-
91- FROM ' .$ _pref .'mageplaza_blog_category t ' ;
89+ FROM ' . $ _pref . 'mageplaza_blog_category t ' ;
9290 $ result = $ this ->_mysqliQuery ($ sql );
9391 while ($ data = mysqli_fetch_assoc ($ result )) {
9492 /* Prepare category data */
9593
9694 $ data ['store_ids ' ] = explode (', ' , $ data ['store_ids ' ]);
95+ $ data ['path ' ] = 0 ;
96+ /*
9797 $data['identifier'] = trim(strtolower($data['identifier']));
9898 if (strlen($data['identifier']) == 1) {
9999 $data['identifier'] .= $data['identifier'];
100100 }
101+ */
101102 $ category = $ this ->_categoryFactory ->create ();
102103 try {
103104 /* Initial saving */
@@ -112,6 +113,41 @@ public function execute()
112113 }
113114 }
114115
116+ /* Reindexing parent categories */
117+ foreach ($ categories as $ ct ) {
118+ if ($ oldParentId = $ ct ->getData ('parent_id ' )) {
119+ if (isset ($ oldCategories [$ oldParentId ])) {
120+ $ ct ->setPath (
121+ $ parentId = $ oldCategories [$ oldParentId ]->getId ()
122+ );
123+ }
124+ }
125+ }
126+
127+ for ($ i = 0 ; $ i < 4 ; $ i ++) {
128+ $ changed = false ;
129+ foreach ($ categories as $ ct ) {
130+ if ($ ct ->getPath ()) {
131+ $ parentId = explode ('/ ' , $ ct ->getPath ())[0 ];
132+ $ pt = $ categories [$ parentId ];
133+ if ($ pt ->getPath ()) {
134+ $ ct ->setPath ($ pt ->getPath () . '/ ' . $ ct ->getPath ());
135+ $ changed = true ;
136+ }
137+ }
138+ }
139+
140+ if (!$ changed ) {
141+ break ;
142+ }
143+ }
144+ /* end*/
145+
146+ foreach ($ categories as $ ct ) {
147+ /* Final saving */
148+ $ ct ->save ();
149+ }
150+
115151 /* Import tags */
116152 $ tags = [];
117153 $ oldTags = [];
@@ -121,16 +157,21 @@ public function execute()
121157 t.tag_id as old_id,
122158 t.name as title,
123159 t.url_key as identifier,
124- t.description as content,
160+ t.description as content,
161+ t.meta_title as meta_title,
162+ t.meta_description as meta_description,
163+ t.meta_keywords as meta_keywords,
125164 t.enabled as is_active
126- FROM '. $ _pref. 'mageplaza_blog_tag t ' ;
165+ FROM ' . $ _pref . 'mageplaza_blog_tag t ' ;
127166
128167 $ result = $ this ->_mysqliQuery ($ sql );
129168 while ($ data = mysqli_fetch_assoc ($ result )) {
130169 /* Prepare tag data */
170+ /*
131171 foreach (['title'] as $key) {
132172 $data[$key] = mb_convert_encoding($data[$key], 'HTML-ENTITIES', 'UTF-8');
133173 }
174+ */
134175
135176 if (!$ data ['title ' ]) {
136177 continue ;
@@ -159,33 +200,25 @@ public function execute()
159200 }
160201
161202
162-
163-
164203 /* Import posts */
165- $ postCategories = [];
166- $ data ['store_ids ' ] = [];
167- $ sql = 'SELECT * FROM ' .$ _pref .'mageplaza_blog_post ' ;
204+ $ sql = 'SELECT * FROM ' . $ _pref . 'mageplaza_blog_post ' ;
168205 $ result = $ this ->_mysqliQuery ($ sql );
169206 while ($ data = mysqli_fetch_assoc ($ result )) {
170207 /* Find post categories*/
171- $ c_sql = ' SELECT category_id FROM ' . $ _pref . ' mageplaza_blog_post_category WHERE post_id = " ' . $ data [ ' post_id ' ]. ' " ' ;
172-
208+ $ postCategories = [] ;
209+ $ c_sql = ' SELECT category_id FROM ' . $ _pref . ' mageplaza_blog_post_category WHERE post_id = " ' . $ data [ ' post_id ' ]. ' " ' ;
173210 $ c_result = $ this ->_mysqliQuery ($ c_sql );
174-
175211 while ($ c_data = mysqli_fetch_assoc ($ c_result )) {
176-
177212 $ oldId = $ c_data ['category_id ' ];
178213 if (isset ($ oldCategories [$ oldId ])) {
179-
180214 $ id = $ oldCategories [$ oldId ]->getId ();
181215 $ postCategories [$ id ] = $ id ;
182216 }
183217 }
184218
185-
186- /* find post tags*/
219+ /* Find post tags*/
187220 $ postTags = [];
188- $ t_sql = 'SELECT tag_id FROM ' . $ _pref. 'mageplaza_blog_post_tag WHERE post_id = " ' .$ data ['post_id ' ].'" ' ;
221+ $ t_sql = 'SELECT tag_id FROM ' . $ _pref . 'mageplaza_blog_post_tag WHERE post_id = " ' .$ data ['post_id ' ].'" ' ;
189222
190223 $ t_result = $ this ->_mysqliQuery ($ t_sql );
191224
@@ -199,30 +232,30 @@ public function execute()
199232 }
200233 }
201234
202-
203-
235+ /* Find store ids */
204236 $ data ['store_ids ' ] = explode (', ' , $ data ['store_ids ' ]);
205237
238+
206239 /* Prepare post data */
207240 $ data = [
208241 'old_id ' => $ data ['post_id ' ],
209242 'store_ids ' => $ data ['store_ids ' ],
210243 'title ' => $ data ['name ' ],
244+ 'meta_title ' => $ data ['meta_title ' ],
211245 'meta_keywords ' => $ data ['meta_keywords ' ],
212246 'meta_description ' => $ data ['meta_description ' ],
213247 'identifier ' => $ data ['url_key ' ],
214- 'content_heading ' => $ data [ ' name ' ] ,
248+ 'content_heading ' => '' ,
215249 'content ' => $ data ['post_content ' ],
216250 'short_content ' => $ data ['short_description ' ],
217251 'creation_time ' => strtotime ($ data ['created_at ' ]),
218252 'update_time ' => strtotime ($ data ['updated_at ' ]),
219253 'publish_time ' => strtotime ($ data ['publish_date ' ]),
220254 'is_active ' => $ data ['enabled ' ],
221255 'categories ' => $ postCategories ,
256+ 'tags ' => $ postTags ,
222257 'featured_img ' => !empty ($ data ['image ' ]) ? 'magefan_blog/ ' . $ data ['image ' ] : '' ,
223- 'media_gallery ' => !empty ($ data ['image ' ]) ? 'magefan_blog/ ' . $ data ['image ' ] : '' ,
224- 'tags ' => $ postTags
225-
258+ 'author_id ' => '' ,
226259 ];
227260
228261
@@ -233,7 +266,7 @@ public function execute()
233266
234267
235268 /* find post comment s*/
236- $ sql = 'SELECT * FROM ' . $ _pref. 'mageplaza_blog_comment WHERE `post_id` = ' . $ post ->getOldId ();
269+ $ sql = 'SELECT * FROM ' . $ _pref . 'mageplaza_blog_comment WHERE `post_id` = ' . $ post ->getOldId ();
237270 $ resultComments = $ this ->_mysqliQuery ($ sql );
238271
239272 while ($ comments = mysqli_fetch_assoc ($ resultComments )) {
@@ -249,10 +282,11 @@ public function execute()
249282 'text ' => $ comments ['content ' ],
250283 'creation_time ' => $ comments ['created_at ' ],
251284 ];
252-
285+ /*
253286 foreach (['text'] as $key) {
254287 $commentData[$key] = mb_convert_encoding($commentData[$key], 'HTML-ENTITIES', 'UTF-8');
255288 }
289+ */
256290
257291 if (!$ commentData ['text ' ]) {
258292 continue ;
@@ -265,7 +299,7 @@ public function execute()
265299 $ comment ->setData ($ commentData )->save ();
266300 $ this ->_importedCommentsCount ++;
267301 } catch (\Exception $ e ) {
268- $ this ->_skippedComments [] = $ data ['title ' ];
302+ $ this ->_skippedComments [] = $ commentData ['title ' ];
269303 unset($ comment );
270304 }
271305 }
0 commit comments