/**
  * Step 6 - Import Votes
  * @param array $xml
  * @param string $file_name
  * @return bool
  */
 function step6($h, $xml, $file_name)
 {
     echo "<b>Table:</b> Votes...<br /><br />";
     $this_table = "postvotes";
     $h->db->query($h->db->prepare("TRUNCATE " . DB_PREFIX . 'postvotes'));
     $h->db->query($h->db->prepare("TRUNCATE " . DB_PREFIX . 'commentvotes'));
     echo "<i>Number of records added:</i> ";
     $count = 0;
     foreach ($xml->children() as $child) {
         // Skip all comment votes...
         if ($child->vote_type == 'links') {
             $count++;
             if ($child->vote_value > 0) {
                 $rating = $child->vote_value;
             } else {
                 $rating = -999;
                 // buried/flagged
             }
             $columns = "vote_post_id, vote_user_id, vote_user_ip, vote_date, vote_type, vote_rating, vote_reason, vote_updateby";
             $sql = "INSERT IGNORE " . DB_PREFIX . $this_table . " (" . $columns . ") VALUES(%d, %d, %s, %s, %s, %d, %d, %d)";
             $lks = new PliggImp2();
             $comms = new PliggImp3();
             $usr = new PliggImp5();
             // Insert into postvotes table
             $h->db->query($h->db->prepare($sql, $lks->get_new_link_id($h, $child->vote_link_id), $usr->get_new_user_id($h, $child->vote_user_id), $child->vote_ip, $child->vote_date, 'vote', $rating, 0, $h->currentUser->id));
         }
         // Now do comment votes...
         if ($child->vote_type == 'comments') {
             $count++;
             if ($child->vote_value > 0) {
                 $rating = 10;
             } else {
                 $rating = -10;
             }
             $columns = "cvote_post_id, cvote_comment_id, cvote_user_id, cvote_user_ip, cvote_date, cvote_rating, cvote_reason, cvote_updateby";
             $sql = "INSERT IGNORE " . DB_PREFIX . "commentvotes (" . $columns . ") VALUES(%d, %d, %d, %s, %s, %d, %d, %d)";
             // Insert into commentvotes table
             $h->db->query($h->db->prepare($sql, 0, $comms->get_new_comment_id($h, $child->vote_link_id), $usr->get_new_user_id($h, $child->vote_user_id), $child->vote_ip, $child->vote_date, $rating, 0, $h->currentUser->id));
         }
     }
     //Output the number of records added
     echo $count . " minus duplicate entries.<br /><br />";
     echo "<span style='color: green;'><b>Votes table imported successfully!</b></span><br /><br />";
     echo "<a class='pliggimp_next' href='" . BASEURL . "admin_index.php?page=plugin_settings&amp;plugin=pligg_importer&amp;step=7'>Continue</a>";
     return true;
 }
 /**
  * Step 3 - Import Comments
  *
  * @param array $xml
  * @param string $file_name
  * @return bool
  */
 function step3($h, $xml, $file_name)
 {
     echo "<b>Table:</b> Comments...<br /><br />";
     $this_table = "comments";
     $h->db->query($h->db->prepare("TRUNCATE " . DB_PREFIX . $this_table));
     echo "<i>Number of records added:</i> ";
     $count = 0;
     foreach ($xml->children() as $child) {
         // Skip any record that has no title
         if ($child->comment_content != "") {
             $count++;
             // Store old id. We need this to work out authors and categories later...
             $comment[$count]['old_id'] = $child->comment_id;
             $comment[$count]['old_id']['parent'] = $child->comment_parent;
             $comment[$count]['old_id']['post_id'] = $child->comment_link_id;
             $comment[$count]['old_id']['author'] = $child->comment_author_id;
             if (!isset($child->comment_subscribe)) {
                 $child->comment_subscribe = 0;
             }
             if ($child->comment_votes < 0) {
                 $cvotes_up = 0;
                 $cvotes_down = abs($child->comment_votes);
             } else {
                 $cvotes_up = $child->comment_votes;
                 $cvotes_down = 0;
             }
             $columns = "comment_post_id, comment_user_id, comment_parent, comment_date, comment_content, comment_votes_up, comment_votes_down, comment_subscribe, comment_updateby";
             $sql = "INSERT INTO " . DB_PREFIX . $this_table . " (" . $columns . ") VALUES(%d, %d, %d, %s, %s, %d, %d, %d, %d)";
             $lks = new PliggImp2();
             // Insert into Comments table
             $h->db->query($h->db->prepare($sql, $lks->get_new_link_id($h, $child->comment_link_id), $child->comment_user_id, $child->comment_parent, $child->comment_date, urlencode(trim($child->comment_content)), $cvotes_up, $cvotes_down, $child->comment_subscribe, $h->currentUser->id));
             // Grab the ID of the last insert.
             $comment[$count]['old_id']['new_id'] = $h->db->get_var($h->db->prepare("SELECT LAST_INSERT_ID()"));
             $sql = "REPLACE INTO " . DB_PREFIX . "pliggimp_temp (pliggimp_setting, pliggimp_old_value, pliggimp_new_value) VALUES(%s, %d, %d)";
             $h->db->query($h->db->prepare($sql, 'comment_id', $comment[$count]['old_id'], $comment[$count]['old_id']['new_id']));
         }
     }
     $this->update_comment_parent_ids($h, $this_table);
     //Output the number of records added
     echo $count . "<br /><br />";
     echo "<span style='color: green;'><b>Comments table imported successfully!</b></span><br /><br />";
     echo "<a class='pliggimp_next' href='" . BASEURL . "admin_index.php?page=plugin_settings&amp;plugin=pligg_importer&amp;step=4'>Continue</a>";
     return true;
 }
 /**
  * Step 4 - Import Tags
  *
  * @param array $xml
  * @param string $file_name
  * @return bool
  */
 function step4($h, $xml, $file_name)
 {
     echo "<b>Table:</b> Tags...<br /><br />";
     $this_table = "tags";
     $h->db->query($h->db->prepare("TRUNCATE " . DB_PREFIX . $this_table));
     echo "<i>Number of records added:</i> ";
     $count = 0;
     foreach ($xml->children() as $child) {
         // Skip any record that has no words
         if ($child->tag_words != "") {
             $count++;
             $columns = "tags_post_id, tags_date, tags_word, tags_updateby";
             $sql = "INSERT IGNORE " . DB_PREFIX . $this_table . " (" . $columns . ") VALUES(%d, %s, %s, %d)";
             $lks = new PliggImp2();
             // Insert into tags table
             $h->db->query($h->db->prepare($sql, $lks->get_new_link_id($h, $child->tag_link_id), $child->tag_date, urlencode(str_replace(' ', '_', trim($child->tag_words))), $h->currentUser->id));
         }
     }
     //Output the number of records added
     echo $count . " minus duplicate entries.<br /><br />";
     echo "<span style='color: green;'><b>Tags table imported successfully!</b></span><br /><br />";
     echo "<a class='pliggimp_next' href='" . BASEURL . "admin_index.php?page=plugin_settings&amp;plugin=pligg_importer&amp;step=5'>Continue</a>";
     return true;
 }
 /**
  * Import data into the database from an XML file
  *
  * @param int $step
  * @param str $file_name
  */
 public function process_file($h, $step = 0, $file_name = '')
 {
     $uploads_folder = PLUGINS . "pligg_importer/uploads/";
     $xml = simplexml_load_file($uploads_folder . $file_name);
     echo "<h2>Importing data from <i>" . $xml->getName() . "</i></h2>";
     switch ($step) {
         case 1:
             $this->create_temp_table($h);
             $cats = new PliggImp1();
             $cats->step1($h, $xml, $file_name);
             break;
         case 2:
             $links = new PliggImp2();
             $links->step2($h, $xml, $file_name);
             break;
         case 3:
             $comms = new PliggImp3();
             $comms->step3($h, $xml, $file_name);
             break;
         case 4:
             $tags = new PliggImp4();
             $tags->step4($h, $xml, $file_name);
             break;
         case 5:
             $users = new PliggImp5();
             $users->step5($h, $xml, $file_name);
             break;
         case 6:
             $votes = new PliggImp6();
             $votes->step6($h, $xml, $file_name);
             break;
         default:
             break;
     }
     echo "<br /><br />";
 }