· 9 years ago · Jan 31, 2017, 11:36 AM
1<?php
2namespace Digitalwerk\DwArticle\Controller;
3
4/***************************************************************
5 *
6 * Copyright notice
7 *
8 * (c) 2016 peter eliades <peter@digitalwerk.agency>, DigitalWerk Slovakia
9 *
10 * All rights reserved
11 *
12 * This script is part of the TYPO3 project. The TYPO3 project is
13 * free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * The GNU General Public License can be found at
19 * http://www.gnu.org/copyleft/gpl.html.
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28
29/**
30 * LocationController
31 */
32class ImportController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
33
34 const TYPO3_PRESS_DOKTYPE = 99;
35 private $db, $wpdb;
36 private $mapping = array(
37 'at' => array(
38 'id' => 6,
39 'from_date' => '2016-01-01',
40 'categories' => array(
41 array(
42 'typo3' => array(
43 963 => 'Sommer'
44 ),
45 'wp' => array(
46 314 => 'Sommer',
47 319 => 'Sommer Kulinarik',
48 318 => 'Sommer Kultur',
49 317 => 'Sommer Natur',
50 316 => 'Sommer Unterkünfte',
51 315 => 'Sommer Wellness & Gesundheit'
52 )
53 ),
54 array(
55 'typo3' => array(
56 964 => 'Winter'
57 ),
58 'wp' => array(
59 320 => 'Winter',
60 325 => 'Winter Kulinarik',
61 324 => 'Winter Kultur',
62 323 => 'Winter Natur',
63 322 => 'Winter Unterkünfte',
64 321 => 'Winter Wellness & Gesundheit'
65 )
66 ),
67 array(
68 'typo3' => array(
69 992 => 'Häufig gefragt'
70 ),
71 'wp' => array(
72 329 => 'Häufig gefragt',
73 409 => 'austriantime'
74 )
75 )
76 )
77 ),
78 /*'de' => array(
79 'id' => 7,
80 'from_date' => '2016-01-01',
81 'categories' => array( )
82 ),
83 'ch' => array(
84 'id' => 11,
85 'from_date' => '2016-01-01',
86 'categories' => array( )
87 ),
88 'fr' => array(
89 'id' => 29,
90 'from_date' => '2016-01-01',
91 'categories' => array( )
92 ),
93 'it' => array(
94 'id' => 8,
95 'from_date' => '2016-01-01',
96 'categories' => array( )
97 ),
98 'uk' => array(
99 'id' => 9,
100 'from_date' => '2016-01-01',
101 'categories' => array( )
102 ),
103 'us' => array(
104 'id' => 10,
105 'from_date' => '2016-01-01',
106 'categories' => array( )
107 )*/
108 );
109
110 /**
111 * Import
112 *
113 * @return void
114 */
115 public function import() {
116 $this->init();
117 $this->processDataImport( true );
118 }
119
120 /**
121 * Init
122 *
123 * @return void
124 */
125 private function init() {
126 $this->db = &$GLOBALS['TYPO3_DB'];
127 $this->wpdb = new \mysqli( 'localhost', 'root', '', 'blognetwork' );
128 }
129
130 /**
131 * Process data import
132 *
133 * @param bool $import
134 * @return void
135 */
136 private function processDataImport( $import = true ) {
137 # For each site
138 foreach ( $this->mapping as $site_key => $site ) {
139 # Print country code and set some variables
140 echo '<hr><strong>' . $site_key . ' - prepare & check</strong><hr>';
141 $all_posts = array();
142 $duplicates = array();
143 # For each category binding (1**Typo3 => n**WP)
144 foreach ( $site['categories'] as $category ) {
145 # Set new category name and ID
146 $typo3_category_id = array_keys( $category['typo3'] )[0];
147 $typo3_category_name = array_values( $category['typo3'] )[0];
148 # Prepare array with Post IDs
149 $all_posts[$typo3_category_id]['category_name'] = $typo3_category_name;
150 $all_posts[$typo3_category_id]['posts'] = array();
151 # Print new category name
152 echo '<br><strong>Typo3 category [' . $typo3_category_id . '] "' . $typo3_category_name . '":</strong><br>';
153 # For each WP category
154 foreach ( $category['wp'] as $wp_category_id => $wp_category_name ) {
155 # Query all Posts from a WP category
156 $post_ids = $this->wpdb->query( "SELECT TREL.object_id FROM oew_" . $site['id'] . "_term_relationships AS TREL INNER JOIN oew_" . $site['id'] . "_posts AS POSTS ON TREL.object_id = POSTS.ID WHERE TREL.term_taxonomy_id = " . $wp_category_id . " AND POSTS.post_date > '" . $site['from_date'] . "'" );
157 # Push Post ID into array
158 while ( $post_id = $post_ids->fetch_assoc() ) {
159 array_push( $all_posts[$typo3_category_id]['posts'], $post_id['object_id'] );
160 }
161 # Print the number of posts from the current WP category
162 echo 'Category [WP:' . $wp_category_id . '] "' . $wp_category_name . '" has ' . $post_ids->num_rows . ' post(s).<br>';
163 }
164 # Merge duplicate Post IDs
165 echo 'Total number of posts: ' . sizeof( $all_posts[$typo3_category_id]['posts'] ) . '<br>';
166 $all_posts[$typo3_category_id]['posts'] = array_unique( $all_posts[$typo3_category_id]['posts'] );
167 echo 'Number of unique posts: ' . sizeof( $all_posts[$typo3_category_id]['posts'] ) . '<br>';
168 }
169 # Check for posts with multiple categories
170 foreach ( $all_posts as $category_id => $categories_posts ) {
171 foreach ( $categories_posts['posts'] as $key => $post_id ) {
172 foreach ( $site['categories'] as $category ) {
173 # Set new category name and ID
174 $typo3_category_id = array_keys( $category['typo3'] )[0];
175 # Check for duplicate post IDs and make sure that the category ID is not the same
176 if ( $category_id != $typo3_category_id and array_search( $post_id, $all_posts[$typo3_category_id]['posts'] ) !== false ) {
177 if ( ! array_key_exists( $post_id, $duplicates ) ) {
178 # This is the first occurrence, so just set the array
179 $duplicates[$post_id] = array();
180 } else {
181 # Delete duplicate
182 unset( $all_posts[$category_id]['posts'][$key] );
183 }
184 array_push( $duplicates[$post_id], $categories_posts['category_name'] );
185 }
186 }
187 }
188 }
189 # Print posts with multiple categories and number of posts that will be imported
190 echo '<br>Number of posts with <strong>multiple categories</strong>: ' . sizeof( $duplicates ) . '<br>';
191 foreach ( $duplicates as $post_id => $categories ) {
192 echo 'Post <strong>' . $post_id . '</strong> has two or more categories: ';
193 foreach ( $categories as $i => $category ) {
194 if ( $i != 0 ) {
195 echo ', ';
196 }
197 echo $category;
198 }
199 echo '<br>';
200 }
201 foreach ( $all_posts as $categories_posts ) {
202 echo '<br>Number of posts that will be imported in Typo3 category "' . $categories_posts['category_name'] . '": ' . sizeof( $categories_posts['posts'] );
203 }
204 # Import
205 if ( $import ) {
206 echo '<br><br><hr><strong>' . $site_key . ' - import</strong><hr>';
207 # Loop categories
208 foreach ( $all_posts as $category_id => $categories_posts ) {
209 # Loop posts in category
210 foreach ( $categories_posts['posts'] as $post_id ) {
211 $post = $this->wpdb->query( "SELECT ID, post_title, post_content, post_date FROM oew_" . $site['id'] . "_posts WHERE ID = " . $post_id )->fetch_assoc();
212 if ( $post ) {
213 //var_dump( $post );
214 # Prepare page for Typo3
215 $tce_page_id = 'NEW' . md5( $post['ID'] );
216 $data = array();
217 $data['pages'][$tce_page_id] = array(
218 'pid' => $category_id,
219 'doktype' => self::TYPO3_PRESS_DOKTYPE,
220 'hidden' => 0,
221 'title' => $post['post_title'],
222 'tstamp' => strtotime( $post['post_date'] ),
223 'date' => date( 'Y-m-d', strtotime( $post['post_date'] ) )
224 );
225 var_dump( $data );
226 # Search for related images
227 $images = $this->wpdb->query( "SELECT guid FROM oew_" . $site['id'] . "_posts WHERE post_parent = " . $post_id . " AND post_type = 'attachment'" );
228 while ( $image = $images->fetch_assoc() ) {
229 //var_dump( $image );
230 }
231 # Insert into Typo3
232 $this->processDatamap( $data );
233 }
234 }
235 }
236 }
237 }
238 }
239
240 //while ( $row = $post_ids->fetch_assoc() ) {
241
242 /**
243 * Start import of locations into db
244 *
245 * @return void
246 */
247 private function processDataImport2() {
248 //example query
249 //$sql = "SELECT * FROM PAGES";
250 //$this->db->sql_query($sql);
251
252 $this->test();
253
254 // todo: set values
255 $oldUniqueID = '';
256 $title = 'test title';
257 $pid = 963;
258
259 //check if location already exists -> if yes update record
260 $existingUid = false;
261
262
263// if ($existingUid) {
264// $tceUid = $existingUid;
265// } else {
266// $tceUid = 'NEW'.md5($oldUniqueID);
267// }
268
269
270 for ($i = 1; $i < 20; $i++) {
271 $tceUid = 'NEW'.md5($i);
272
273 $data = array();
274 $data['pages'][$tceUid] = array(
275 'pid' => $pid,
276 'doktype' => self::TYPO3_PRESS_DOKTYPE,
277 'hidden' => 0,
278 'title' => $title . ' - id:' . $i,
279 // 'external_uid' => $oldUniqueID,
280 );
281
282 // todo create new field for table pages: imported_image_url
283
284 // set field date. this already exists in db
285
286 $ttContentUid = 'NEW'.md5(-$i);
287
288 $data['tt_content'][$ttContentUid] = array(
289 'pid' => $tceUid,
290 'hidden' => 0,
291 'CType' => 'dce_dceuid1',
292 'pi_flexform' => '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
293 <T3FlexForms>
294 <data>
295 <sheet index="sheet.tabGeneral">
296 <language index="lDEF">
297 <field index="settings.title">
298 <value index="vDEF">'.$title.'</value>
299 </field>
300 <field index="settings.titleType">
301 <value index="vDEF">h1</value>
302 </field>
303 <field index="settings.useSeoTitle">
304 <value index="vDEF">1</value>
305 </field>
306 <field index="settings.introText">
307 <value index="vDEF"></value>
308 </field>
309 <field index="settings.text">
310 <value index="vDEF">THIS IS THE IMPORTED POST TEXT</value>
311 </field>
312 <field index="settings.slimText">
313 <value index="vDEF">0</value>
314 </field>
315 </language>
316 </sheet>
317 </data>
318 </T3FlexForms>'
319
320 );
321
322 $this->processDatamap($data);
323 }
324
325 //print_r($data); die();
326
327 }
328
329
330 /**
331 * Run TCE
332 *
333 * @return void
334 */
335 private function processDatamap( $data ) {
336 $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
337 $tce->stripslashes_values = 0;
338 $tce->start( $data, array() );
339 $tce->process_datamap();
340 }
341
342}