From 1b31adf97ee52f0e9ae300a1c352f38b4f52bfcc Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Thu, 1 Sep 2016 13:25:16 +1000 Subject: [PATCH] Add fixtures importer --- dummy-data/fixtures-sample.csv | 6 + includes/admin/class-sp-admin-importers.php | 18 + .../importers/class-sp-fixture-importer.php | 335 ++++++++++++++++++ 3 files changed, 359 insertions(+) create mode 100644 dummy-data/fixtures-sample.csv create mode 100644 includes/admin/importers/class-sp-fixture-importer.php diff --git a/dummy-data/fixtures-sample.csv b/dummy-data/fixtures-sample.csv new file mode 100644 index 00000000..492fa080 --- /dev/null +++ b/dummy-data/fixtures-sample.csv @@ -0,0 +1,6 @@ +Date,Time,Venue,Home,Away +2017/01/20,14:30:00,Little Park,Eagles,Sharks +2017/01/24,16:00:00,Big Stadium,Kangaroos,Eagles +2017/02/14,16:15:00,Little Park,Sharks,Eagles +2017/03/05,13:00:00,Little Park,Eagles,Kangaroos +2017/04/27,12:45:00,Big Stadium,Sharks,Kangaroos \ No newline at end of file diff --git a/includes/admin/class-sp-admin-importers.php b/includes/admin/class-sp-admin-importers.php index 22a2094a..2816795d 100644 --- a/includes/admin/class-sp-admin-importers.php +++ b/includes/admin/class-sp-admin-importers.php @@ -34,6 +34,11 @@ class SP_Admin_Importers { 'description' => __( 'Import events from a csv file.', 'sportspress'), 'callback' => array( $this, 'events_importer' ), ), + 'sp_fixture_csv' => array( + 'name' => __( 'SportsPress Fixtures (CSV)', 'sportspress' ), + 'description' => __( 'Import fixtures from a csv file.', 'sportspress'), + 'callback' => array( $this, 'fixtures_importer' ), + ), 'sp_team_csv' => array( 'name' => __( 'SportsPress Teams (CSV)', 'sportspress' ), 'description' => __( 'Import teams from a csv file.', 'sportspress'), @@ -69,6 +74,19 @@ class SP_Admin_Importers { $importer->dispatch(); } + /** + * Add menu item + */ + public function fixtures_importer() { + $this->includes(); + + require 'importers/class-sp-fixture-importer.php'; + + // Dispatch + $importer = new SP_Fixture_Importer(); + $importer->dispatch(); + } + /** * Add menu item */ diff --git a/includes/admin/importers/class-sp-fixture-importer.php b/includes/admin/importers/class-sp-fixture-importer.php new file mode 100644 index 00000000..cc48bdc8 --- /dev/null +++ b/includes/admin/importers/class-sp-fixture-importer.php @@ -0,0 +1,335 @@ +import_page = 'sp_fixture_csv'; + $this->import_label = __( 'Import Fixtures', 'sportspress' ); + $this->columns = array( + 'post_date' => __( 'Date', 'sportspress' ), + 'post_time' => __( 'Time', 'sportspress' ), + 'sp_venue' => __( 'Venue', 'sportspress' ), + 'sp_home' => __( 'Home', 'sportspress' ), + 'sp_away' => __( 'Away', 'sportspress' ), + ); + } + + /** + * import function. + * + * @access public + * @param array $array + * @param array $columns + * @return void + */ + function import( $array = array(), $columns = array( 'post_title' ) ) { + $this->imported = $this->skipped = 0; + + if ( ! is_array( $array ) || ! sizeof( $array ) ): + $this->footer(); + die(); + endif; + + $rows = array_chunk( $array, sizeof( $columns ) ); + + // Get event format, league, and season from post vars + $event_format = ( empty( $_POST['sp_format'] ) ? false : $_POST['sp_format'] ); + $league = ( sp_array_value( $_POST, 'sp_league', '-1' ) == '-1' ? false : $_POST['sp_league'] ); + $season = ( sp_array_value( $_POST, 'sp_season', '-1' ) == '-1' ? false : $_POST['sp_season'] ); + $date_format = ( empty( $_POST['sp_date_format'] ) ? 'yyyy/mm/dd' : $_POST['sp_date_format'] ); + + foreach ( $rows as $row ): + + $row = array_filter( $row ); + + if ( empty( $row ) ) continue; + + $meta = array(); + + foreach ( $columns as $index => $key ): + $meta[ $key ] = sp_array_value( $row, $index ); + endforeach; + + // Get event details + $event = array( + sp_array_value( $meta, 'post_date' ), + sp_array_value( $meta, 'post_time' ), + sp_array_value( $meta, 'sp_venue' ), + ); + + $teams = array( + sp_array_value( $meta, 'sp_home' ), + sp_array_value( $meta, 'sp_away' ), + ); + + // Add new event if date is given + if ( sizeof( $event ) > 0 && ! empty( $event[0] ) ): + + // List event columns + list( $date, $time, $venue ) = $event; + + // Format date + $date = str_replace( '/', '-', trim( $date ) ); + $date_array = explode( '-', $date ); + switch ( $date_format ): + case 'dd/mm/yyyy': + $date = substr( str_pad( sp_array_value( $date_array, 2, '0000' ), 4, '0', STR_PAD_LEFT ), 0, 4 ) . '-' . + substr( str_pad( sp_array_value( $date_array, 1, '00' ), 2, '0', STR_PAD_LEFT ), 0, 2 ) . '-' . + substr( str_pad( sp_array_value( $date_array, 0, '00' ), 2, '0', STR_PAD_LEFT ), 0, 2 ); + break; + case 'mm/dd/yyyy': + $date = substr( str_pad( sp_array_value( $date_array, 2, '0000' ), 4, '0', STR_PAD_LEFT ), 0, 4 ) . '-' . + substr( str_pad( sp_array_value( $date_array, 0, '00' ), 2, '0', STR_PAD_LEFT ), 0, 2 ) . '-' . + substr( str_pad( sp_array_value( $date_array, 1, '00' ), 2, '0', STR_PAD_LEFT ), 0, 2 ); + break; + default: + $date = substr( str_pad( sp_array_value( $date_array, 0, '0000' ), 4, '0', STR_PAD_LEFT ), 0, 4 ) . '-' . + substr( str_pad( sp_array_value( $date_array, 1, '00' ), 2, '0', STR_PAD_LEFT ), 0, 2 ) . '-' . + substr( str_pad( sp_array_value( $date_array, 2, '00' ), 2, '0', STR_PAD_LEFT ), 0, 2 ); + endswitch; + + // Add time to date if given + if ( ! empty( $time ) ): + $date .= ' ' . trim( $time ); + endif; + + // Define post type args + $args = array( 'post_type' => 'sp_event', 'post_status' => 'publish', 'post_date' => $date, 'post_title' => __( 'Event', 'sportspress' ) ); + + // Insert event + $id = wp_insert_post( $args ); + + // Flag as import + update_post_meta( $id, '_sp_import', 1 ); + + // Update event format + if ( $event_format ): + update_post_meta( $id, 'sp_format', $event_format ); + endif; + + // Update league + if ( $league ): + wp_set_object_terms( $id, $league, 'sp_league', false ); + endif; + + // Update season + if ( $season ): + wp_set_object_terms( $id, $season, 'sp_season', false ); + endif; + + // Update venue + wp_set_object_terms( $id, $venue, 'sp_venue', false ); + + // Increment + $this->imported ++; + + endif; + + // Add teams to event + if ( sizeof( $teams ) > 0 ): + + foreach ( $teams as $team_name ): + + if ( '' !== $team_name ): + + // Find out if team exists + $team_object = get_page_by_title( stripslashes( $team_name ), OBJECT, 'sp_team' ); + + // Get or insert team + if ( $team_object ): + + // Make sure team is published + if ( $team_object->post_status != 'publish' ): + wp_update_post( array( 'ID' => $team_object->ID, 'post_status' => 'publish' ) ); + endif; + + // Get team ID + $team_id = $team_object->ID; + + else: + + // Insert team + $team_id = wp_insert_post( array( 'post_type' => 'sp_team', 'post_status' => 'publish', 'post_title' => wp_strip_all_tags( $team_name ) ) ); + + // Flag as import + update_post_meta( $team_id, '_sp_import', 1 ); + + endif; + + // Update league + if ( $league ): + wp_set_object_terms( $team_id, $league, 'sp_league', true ); + endif; + + // Update season + if ( $season ): + wp_set_object_terms( $team_id, $season, 'sp_season', true ); + endif; + + // Add to event if exists + if ( isset( $id ) ): + + // Add team to event + add_post_meta( $id, 'sp_team', $team_id ); + + // Get event name + $title = get_the_title( $id ); + + // Initialize event name + if ( __( 'Event', 'sportspress' ) === $title ) { + $title = ''; + } else { + $title .= ' ' . get_option( 'sportspress_event_teams_delimiter', 'vs' ) . ' '; + } + + // Append team name to event name + $title .= $team_name; + + // Update event with new name + $post = array( + 'ID' => $id, + 'post_title' => $title, + 'post_name' => $id, + ); + wp_update_post( $post ); + + endif; + + else: + + // Add empty team to event + add_post_meta( $id, 'sp_team', -1 ); + + endif; + + endforeach; + + endif; + + endforeach; + + // Show Result + echo '

+ '.sprintf( __( 'Import complete - imported %s events and skipped %s.', 'sportspress' ), $this->imported, $this->skipped ).' +

'; + + $this->import_end(); + } + + /** + * Performs post-import cleanup of files and the cache + */ + function import_end() { + echo '

' . __( 'All done!', 'sportspress' ) . ' ' . __( 'View Fixtures', 'sportspress' ) . '' . '

'; + + do_action( 'import_end' ); + } + + /** + * greet function. + * + * @access public + * @return void + */ + function greet() { + echo '
'; + echo '

' . __( 'Hi there! Choose a .csv file to upload, then click "Upload file and import".', 'sportspress' ).'

'; + echo '

' . sprintf( __( 'Fixtures need to be defined with columns in a specific order (4+ columns). Click here to download a sample.', 'sportspress' ), plugin_dir_url( SP_PLUGIN_FILE ) . 'dummy-data/fixtures-sample.csv' ) . '

'; + wp_import_upload_form( 'admin.php?import=sp_fixture_csv&step=1' ); + echo '
'; + } + + /** + * options function. + * + * @access public + * @return void + */ + function options() { + ?> + + + + + + + + + + + + + + + + + + + +

+
+
    +
  • +
  • +
    +
+

'sp_league', + 'name' => 'sp_league', + 'values' => 'slug', + 'show_option_none' => __( '— Not set —', 'sportspress' ), + ); + if ( ! sp_dropdown_taxonomies( $args ) ): + echo '

' . __( 'None', 'sportspress' ) . '

'; + sp_taxonomy_adder( 'sp_league', 'sp_team', __( 'Add New', 'sportspress' ) ); + endif; + ?>

'sp_season', + 'name' => 'sp_season', + 'values' => 'slug', + 'show_option_none' => __( '— Not set —', 'sportspress' ), + ); + if ( ! sp_dropdown_taxonomies( $args ) ): + echo '

' . __( 'None', 'sportspress' ) . '

'; + sp_taxonomy_adder( 'sp_season', 'sp_team', __( 'Add New', 'sportspress' ) ); + endif; + ?>
+ + +
+
    +
  • + +
  • +
  • + +
  • +
  • + +
  • +
+
+
+