adding sql import logic to extract, sort and import forum data into
authorAnthony Talarico <talarico@gaslightmedia.com>
Thu, 12 Jan 2017 21:28:22 +0000 (16:28 -0500)
committerAnthony Talarico <talarico@gaslightmedia.com>
Thu, 12 Jan 2017 21:28:22 +0000 (16:28 -0500)
bbpress. added script tag in importer view file to trigger a click
so that the next button doesn't have to be pressed more than 50 times

models/admin/management/csvimport.php
views/admin/management/csvimport.html

index 9c1904b..5775ab2 100644 (file)
@@ -1,5 +1,4 @@
 <?php
-
 /**
  * Gaslight Media Members Database
  * Admin Development
@@ -47,7 +46,6 @@ class GlmMembersAdmin_management_csvimport
      */
     public function __construct ($wpdb, $config)
     {
-
         // Save WordPress Database object
         $this->wpdb = $wpdb;
 
@@ -98,6 +96,7 @@ class GlmMembersAdmin_management_csvimport
      */
     public function modelAction ($actionData = false)
     {
+         
 
         $resultMessage = '';
         $success       = false;
@@ -137,6 +136,7 @@ class GlmMembersAdmin_management_csvimport
         case 'importBoard':
             // check for members.csv
             $upload_dir = wp_upload_dir();
+
             if ( is_file( $upload_dir['basedir'].'/Members.csv' ) && ( $fh = fopen( $upload_dir['basedir'].'/Members.csv', 'r' ) ) !== false ) {
                 $resultMessage .= '<p>Member file found</p>';
                 $row = 0;
@@ -150,22 +150,87 @@ class GlmMembersAdmin_management_csvimport
                 fclose( $fh );
                 $resultMessage .= '<pre>$Members: ' . print_r( $Members, true ) . '</pre>';
             }
-            if ( is_file( $upload_dir['basedir'].'/MessageBoards.csv' ) && ( $fh = fopen( $upload_dir['basedir'].'/MessageBoards.csv', 'r' ) ) !== false ) {
-                $resultMessage .= '<p>Forum file found</p>';
-                $row = 0;
-                while( ( $data = fgetcsv( $fh, 4000, '|' ) ) !== false ) {
-                    if ( $row === 0 ) {
-                        // First row grab as headers.
-                        $file_headers = $data;
-                    } else {
-                        // All other rows are data.
-                        $file_data[] = array_combine( $file_headers, $data );
-                    }
-                    $row++;
+            
+               
+            $url = get_site_url() . '/wp-admin/admin.php?page=glm-members-admin-menu-management&glm_action=csvimport&option=importBoard';
+            $limit = 110;
+
+            if ( isset( $_REQUEST['start'] ) ) {
+                $start = filter_var( $_REQUEST['start'], FILTER_VALIDATE_INT );
+            } else {
+                $start = 0;
+            }
+            global $wpdb;
+
+            $sql = "SELECT * FROM MessageBoards ORDER BY ReplyID ASC LIMIT $limit OFFSET $start";
+            $forum = $wpdb->get_results( $sql, ARRAY_A );
+//                echo '<pre>', print_r($forum), '</pre>';
+
+            foreach($forum as $key => $fd){
+
+                if ( isset( $Members[$fd['PostedBy']] ) ){
+                    $name = $Members[$fd['PostedBy']];
+
+                    $new_id         = get_user_by('slug',$name);
+                    $post_author    = $new_id->ID;
+                } else {
+                    $post_author    = $fd['PostedBy'];
+                }
+                $old_post_id        = $fd['MessageID'];
+                $post_content       = $fd['Message'];
+                $post_title         = $fd['Subject'];
+                $post_date          = $fd['PostedDate'];
+                $reply              = $fd['ReplyID'];
+
+                if( strpos( $post_title, 'Re:') !== false  ){
+                    $args = array(
+                        'post_author'  => $post_author,
+                        'post_content' => $post_content,
+                        'post_title'   => $post_title,
+                        'post_date'    => $post_date,
+                        'post_name'    => $old_post_id,
+                        'post_type'    => 'reply',
+                        'post_status'  => 'publish',
+                    );  
+
+                }else if( strpos( $post_title, 'Re:') === false ){
+                    $args = array(
+                        'post_author'  => $post_author,
+                        'post_content' => $post_content,
+                        'post_title'   => $post_title,
+                        'post_date'    => $post_date,
+                        'post_name'    => $old_post_id,
+                        'post_type'    => 'topic',
+                        'post_status'  => 'publish',
+                        'post_parent'  => 28850
+                    );
+                }
+                $new_post_id = wp_insert_post($args);
+                add_post_meta($new_post_id, '_bbp_forum_id', 28850);
+                add_post_meta($new_post_id, '_bbp_last_active_time', $post_date);    
+
+
+                if($reply === '0'){
+                    add_post_meta($new_post_id, '_bbp_topic_id', $new_post_id);
+                } else if($reply !== '0'){
+                    $args = array(
+                        'name'        => $reply,
+                        'post_type'   => 'topic',
+                        'post_status' => 'publish',
+                        'numberposts' => 1
+                    );
+                    $topic = get_posts($args);
+                    add_post_meta( $new_post_id, '_bbp_topic_id', $topic[0]->ID );
+//                    $post_update = "UPDATE glm_posts SET post_parent= $topic[0]->ID WHERE ID = $new_post_id";
+//                    wp_update_post( array( 'ID' => $new_post_id,'post_parent' => $topic[0]->ID ) );
+                    $wpdb->update('glm_posts', array('post_parent' => $topic[0]->ID),array('ID' => $new_post_id));
                 }
-                fclose( $fh );
             }
-            $resultMessage .= '<pre>$file_data: ' . print_r( $file_data, true ) . '</pre>';
+
+        $resultMessage =  "<pre>Start: $start</pre>";
+        $start += $limit;
+        $resultMessage .= "<p><a class='next-import-btn' href=\"". $url . "&limit=$limit&start=$start\">Next</a></p>";
             break;
 
         default:
index 0809dd1..bca7bed 100644 (file)
 {/if}
 
 {include file='admin/footer.html'}
+<script>
+jQuery(document).ready(function($){
+    $(window).on('load', function(){
+        var url = window.location.href;
+        if(url.indexOf('start=') > -1){
+            var regex_start = /start=(\d*)/;
+            var start = regex_start.exec(url);
+            start = parseInt(start[1]);
+        }
+
+        function clickNext(){
+            if(start && start < 5432){
+              $('.next-import-btn')[0].click();
+            }
+        }
+        window.onload = clickNext();
+    });
+});   
+    
+</script>