<?php
function import_page () {
global $zc_ms;
$all_stats = $zc_ms->get_plugin_option( 'stats' );
$last_update = $all_stats['last_update_stats'];
$last_date = $last_update['date'];
$last_count = $last_update['count'];
$last_filename = $last_update['filename'];
$total_songs = 0;
$args = array('post_type' => 'songs','post_status' => 'publish',  'fields' => 'ids', 'posts_per_page' => -1 );//, 'offset' => $x * 1000);
$query = new WP_Query($args);
if( $query->have_posts() ) {
    $total_songs = $query->post_count;
}
?>
<div class="wrap" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
    <div id="overlay" class="hidden"></div>
    <div id="icon-edit" class="icon32 icon32-posts-songs">
    </div>
    <h2>Import Songs
    </h2>
<BR />
    <?php
    $a = $total_songs;$b = 100; $c =ceil($a/$b);?>
    <input type="hidden" class="delete_csv" value="<?=$c;?>"/>
    <div class="main_block">
    <div id="stats">
        <div><b>Last update: </b><?php echo  $last_date; ?></div>
        <div><b>Filename:</b> <?php echo  $last_filename; ?></div>
        <div><b># of Songs imported:</b> <?php echo  $last_count; ?></div>
    </div><BR />
    <?php fileupload( '<b>CSV Music Import:</b>', $all_stats);?>
    </div>
    <?php };
function fileupload( $label ) { ?>
    <tr>
    <td class="left_label"> <?php
        echo $label; ?>
    </td>
    <td>
        <form name="uploadfile" id="uploadfile_form" method="POST" enctype="multipart/form-data" action="<?php echo $_SERVER['REQUEST_URI'].'#uploadfile'; ?>" accept-charset="utf-8" >
             <input type="file" name="uploadfiles[]" id="uploadfiles" size="35" class="uploadfiles" accept=".csv" />
           <p> <input class="button-primary" type="submit" name="uploadfile" id="uploadfile_btn" value="Upload"   /> <span>  <i><b>   CSV Format:</b> Title, Artist, Length, Genre, Year, Month, Sample Playlist (on)</i></span></p>
        </form>

        <b> <span>Import Instructions</span></b>
        <p style="margin-top: 0;margin-bottom: 0"> <span>1. Download and install MP3 Tag Tools at (<a target="_blank" href="http://www.mp3tag.de/en/download.html">http://www.mp3tag.de/en/download.html</a>)</span></p>
        <p style="margin-top: 0;margin-bottom: 0"> <span>2. Follow detailed instructions to turn your MP3 collection into a CSV (<a target="_blank" href="http://www.eventmasterpro.com/how-to-make-a-csv-file-of-your-song-list/">See Detailed Instructions Here</a>)</span></p>
        <p style="margin-top: 0;margin-bottom: 0"> <span>3. Use this sample CSV file and paste your songs in the correct columns (<a target="_blank" href="http://www.eventmasterpro.com/download-sample-csv/">Download Sample CSV Here</a>)</span></p>
        <p style="margin-top: 0;margin-bottom: 0"> <span>4. Make sure you convert seconds to minutes and seconds (See detailed instructions in Step 2 or in Sample CSV)</span></p>
        <p style="margin-top: 0;margin-bottom: 0"> <span>5. Import playlist, no more than 5000 songs per upload to reduce chance of errors</span></p>
    </td>
    <td><div>&nbsp;</div></td>
    <td><b>Delete All Songs:</b></td>
    <td>
        <form name="resetsongs" id="resetsongs_form" method="POST" enctype="multipart/form-data" action="<?php echo $_SERVER['REQUEST_URI'].'#resetsongs'; ?>" accept-charset="utf-8" >
            <input class="button-secondary" type="submit" name="resetsongs" id="resetsongs_btn" value="Reset Music Library"  />
        </form>
    </td>
    </tr>  <BR />
    <div class="done" style="display: none">Done!</div>
    <div id="status">
        <div><i>Processing your request...</i></div>
        <div><i>Grab a cup of coffee, as this may take a while.</i></div>
        <div id="loader"></div>
    </div><?php
}
/**
 * Handle file uploads
 *
 * @todo check nonces
 * @todo check file size
 *
 * @return none
 */

function stats_update($args = false, $all_stats = false) {

    global $zc_ms;
    if( is_array($args) ) {
        $count = $args[2];
        $stats = array(
            'filename' => $args[0],
            'date' => $args[1],
            'count' => $count
        );
        $updated = $zc_ms->update_sub_option('stats', 'last_update_stats', $stats);
        $total_count = (int)$count + (int)$all_stats['total_song_count'];
    } else if( $args === false) {
        $total_count = 0;
    }
    $updated = $zc_ms->update_sub_option('stats', 'total_song_count', $total_count);
}
function process_custom_post($song) {
    $track =  (array_key_exists(0, $song) && $song[0] != "" ?  $song[0] : 'N/A');
    $artist = (array_key_exists(1, $song) && $song[1] != ""  ?  $song[1] : 'N/A');
    $length = (array_key_exists(2, $song) && $song[2] != ""  ?  $song[2] : 'N/A');
    $genre = (array_key_exists(3, $song) && $song[3] != ""  ?  $song[3] : 'N/A');
    $year = (array_key_exists(4, $song) && $song[4] != ""  ?  $song[4] : 'N/A');
    $month = (array_key_exists(5, $song) && $song[5] != ""  ?  $song[5] : 'N/A');
    $playlist = (array_key_exists(6, $song) && $song[6] != ""  ?  $song[6] : '');
    $custom_post = array();
    $custom_post['post_type'] = 'songs';
    $custom_post['post_status'] = 'publish';
    $custom_post['post_title'] = $track;
    $post_id = wp_insert_post( $custom_post );
    $updated = update_post_meta($post_id, 'artist_name', $artist);
    $updated = update_post_meta($post_id, 'song_length', $length);
    $updated = update_post_meta($post_id, 'song_genre', $genre);
    $updated = update_post_meta($post_id, 'song_year', $year);
    $updated = update_post_meta($post_id, 'song_month', $month);
    $updated = update_post_meta($post_id, 'sample_playlist', $playlist);
    return true;
}
