Add columns to players importer

This commit is contained in:
Brian Miyaji
2014-02-15 23:46:04 +11:00
parent 443c0e5def
commit c1914b7eaa
3 changed files with 54 additions and 24 deletions

View File

@@ -1,6 +1,6 @@
<?php <?php
/** /**
* Players importer - import players into SportsPress. * Player importer - import players into SportsPress.
* *
* @author ThemeBoy * @author ThemeBoy
* @category Admin * @category Admin
@@ -112,29 +112,50 @@ if ( class_exists( 'WP_Importer' ) ) {
$header = fgetcsv( $handle, 0, $this->delimiter ); $header = fgetcsv( $handle, 0, $this->delimiter );
if ( sizeof( $header ) == 4 ): if ( sizeof( $header ) == 7 ):
$loop = 0; $loop = 0;
while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== FALSE ): while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== FALSE ):
list( $name, $number, $team, $nationality ) = $row; list( $number, $name, $positions, $teams, $leagues, $seasons, $nationality ) = $row;
$nationality = trim( strtoupper( $nationality ) ); $nationality = trim( strtoupper( $nationality ) );
if ( $nationality == '*' ) if ( $nationality == '*' )
$nationality = ''; $nationality = '';
if ( ! $name ):
$this->skipped++;
continue;
endif;
$args = array( 'post_type' => 'sp_player', 'post_status' => 'publish', 'post_title' => $name ); $args = array( 'post_type' => 'sp_player', 'post_status' => 'publish', 'post_title' => $name );
$id = wp_insert_post( $args ); $id = wp_insert_post( $args );
// Flag as import // Flag as import
update_post_meta( $id, 'sp_import', 1 ); update_post_meta( $id, '_sp_import', 1 );
// Update number // Update number
update_post_meta( $id, 'sp_number', $number ); update_post_meta( $id, 'sp_number', $number );
// Update positions
$positions = explode( '|', $positions );
wp_set_object_terms( $id, $positions, 'sp_position', false );
// Update leagues
$leagues = explode( '|', $leagues );
wp_set_object_terms( $id, $leagues, 'sp_league', false );
// Update seasons
$seasons = explode( '|', $seasons );
wp_set_object_terms( $id, $seasons, 'sp_season', false );
// Update teams
$teams = (array)explode( '|', $teams );
$i = 0;
foreach ( $teams as $team ):
// Get or insert team // Get or insert team
$team_object = get_page_by_path( $team, OBJECT, 'sp_team' ); $team_object = get_page_by_path( $team, OBJECT, 'sp_team' );
if ( $team_object ): if ( $team_object ):
@@ -144,11 +165,20 @@ if ( class_exists( 'WP_Importer' ) ) {
$team_id = $team_object->ID; $team_id = $team_object->ID;
else: else:
$team_id = wp_insert_post( array( 'post_type' => 'sp_team', 'post_status' => 'publish', 'post_title' => $team ) ); $team_id = wp_insert_post( array( 'post_type' => 'sp_team', 'post_status' => 'publish', 'post_title' => $team ) );
wp_set_object_terms( $team_id, $leagues, 'sp_league', false );
wp_set_object_terms( $team_id, $seasons, 'sp_season', false );
endif; endif;
// Update team // Add team to player
update_post_meta( $id, 'sp_team', $team_id ); add_post_meta( $id, 'sp_team', $team_id );
// Update current team if first in array
if ( $i == 0 ):
update_post_meta( $id, 'sp_current_team', $team_id ); update_post_meta( $id, 'sp_current_team', $team_id );
endif;
$i++;
endforeach;
// Update nationality // Update nationality
update_post_meta( $id, 'sp_nationality', $nationality ); update_post_meta( $id, 'sp_nationality', $nationality );
@@ -231,7 +261,7 @@ if ( class_exists( 'WP_Importer' ) ) {
* @return void * @return void
*/ */
function header() { function header() {
echo '<h2>' . sprintf( __( 'Import %s', 'sportspress' ), __( 'Players', 'sportspress' ) ) . '</h2>'; echo '<div class="wrap"><h2>' . sprintf( __( 'Import %s', 'sportspress' ), __( 'Players', 'sportspress' ) ) . '</h2>';
} }
/** /**
@@ -255,7 +285,7 @@ if ( class_exists( 'WP_Importer' ) ) {
echo '<div class="narrow">'; echo '<div class="narrow">';
echo '<p>' . __( 'Hi there! Choose a .csv file to upload, then click "Upload file and import".', 'sportspress' ).'</p>'; echo '<p>' . __( 'Hi there! Choose a .csv file to upload, then click "Upload file and import".', 'sportspress' ).'</p>';
echo '<p>' . sprintf( __( 'Players need to be defined with columns in a specific order (4 columns). <a href="%s">Click here to download a sample</a>.', 'sportspress' ), SPORTSPRESS_PLUGIN_URL . 'dummy-data/sample_players.csv' ) . '</p>'; echo '<p>' . sprintf( __( 'Players need to be defined with columns in a specific order (4 columns). <a href="%s">Click here to download a sample</a>.', 'sportspress' ), SPORTSPRESS_PLUGIN_URL . 'dummy-data/players-sample.csv' ) . '</p>';
$action = 'admin.php?import=sportspress_player_csv&step=1'; $action = 'admin.php?import=sportspress_player_csv&step=1';

View File

@@ -0,0 +1,5 @@
Number,Name,Positions,Teams,Leagues,Seasons,Nationality
1,Joe Allen,Goalkeeper,Essendon Eagles,Primary League|Secondary League,2014,AU
3,Steven Gerrard,Defender,Carlton Kangaroos,Primary League,2014|2013,US
5,Daniel Sturridge,Midfielder|Forward,Southbank Sharks,Primary League|Secondary League,2014|2013,GB
6,Gabrielle Gonzalez,Forward,Carlton Kangaroos|Southbank Sharks,Primary League,2014,ES
1 Number Name Positions Teams Leagues Seasons Nationality
2 1 Joe Allen Goalkeeper Essendon Eagles Primary League|Secondary League 2014 AU
3 3 Steven Gerrard Defender Carlton Kangaroos Primary League 2014|2013 US
4 5 Daniel Sturridge Midfielder|Forward Southbank Sharks Primary League|Secondary League 2014|2013 GB
5 6 Gabrielle Gonzalez Forward Carlton Kangaroos|Southbank Sharks Primary League 2014 ES

View File

@@ -1,5 +0,0 @@
Name,Number,Team,Nationality
Mark Downer,1,Essendon Eagles,AU
Brian Jones,3,Essendon Eagles,US
Andrew Johnson,5,Southbank Sharks,GB
Gabrielle Gonzalez,6,Carlton Kangaroos,ES
1 Name Number Team Nationality
2 Mark Downer 1 Essendon Eagles AU
3 Brian Jones 3 Essendon Eagles US
4 Andrew Johnson 5 Southbank Sharks GB
5 Gabrielle Gonzalez 6 Carlton Kangaroos ES