diff --git a/dummy-data/event-performance-sample.csv b/dummy-data/event-performance-sample.csv new file mode 100644 index 00000000..8d7aec70 --- /dev/null +++ b/dummy-data/event-performance-sample.csv @@ -0,0 +1 @@ +Squad Number,Player,Goals,Assists,Yellow Cards,Red Cards Dee Callery,0,1,0,0 Romeo Melvin,2,0,1,0 Ty Carrell,0,2,0,0 Alonzo Ohalloran,1,1,0,0 Nelson Giddings,1,0,0,1 Ellis Nicholls,0,1,0,0 Corey Kirk,0,0,1,0 Spencer Duncan,0,0,0,1 \ 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 e49cf29a..e628d125 100644 --- a/includes/admin/class-sp-admin-importers.php +++ b/includes/admin/class-sp-admin-importers.php @@ -36,9 +36,14 @@ class SP_Admin_Importers { ), 'sp_fixture_csv' => array( 'name' => __( 'SportsPress Fixtures (CSV)', 'sportspress' ), - 'description' => __( 'Import fixtures from a csv file.', 'sportspress'), + 'description' => __( 'Import upcoming events from a csv file.', 'sportspress'), 'callback' => array( $this, 'fixtures_importer' ), ), + 'sp_event_performance_csv' => array( + 'name' => __( 'SportsPress Box Score (CSV)', 'sportspress' ), + 'description' => __( 'Import event box scores from a csv file.', 'sportspress'), + 'callback' => array( $this, 'event_performance_importer' ), + ), 'sp_team_csv' => array( 'name' => __( 'SportsPress Teams (CSV)', 'sportspress' ), 'description' => __( 'Import teams from a csv file.', 'sportspress'), @@ -74,6 +79,19 @@ class SP_Admin_Importers { $importer->dispatch(); } + /** + * Add menu item + */ + public function event_performance_importer() { + $this->includes(); + + require 'importers/class-sp-event-performance-importer.php'; + + // Dispatch + $importer = new SP_Event_Performance_Importer(); + $importer->dispatch(); + } + /** * Add menu item */ diff --git a/includes/admin/class-sp-admin.php b/includes/admin/class-sp-admin.php index 801536a7..365cb08d 100644 --- a/includes/admin/class-sp-admin.php +++ b/includes/admin/class-sp-admin.php @@ -102,29 +102,43 @@ class SP_Admin { */ public function action_links() { global $pagenow, $typenow; - if ( 'edit.php' == $pagenow && in_array( $typenow, sp_primary_post_types() ) ) { + if ( in_array( $typenow, sp_primary_post_types() ) ) { if ( 'sp_event' === $typenow ) { - ?> - - + + + + - - + + import_page = 'sp_event_performance_csv'; + $this->import_label = __( 'Import Box Score', 'sportspress' ); + $this->columns = array( + 'sp_player' => __( 'Player', 'sportspress' ), + ); + $performance_labels = sp_get_var_labels( 'sp_performance' ); + if ( $performance_labels && is_array( $performance_labels ) && sizeof( $performance_labels ) ) + $this->columns = array_merge( $this->columns, $performance_labels ); + } + + /** + * import function. + * + * @access public + * @param array $array + * @param array $columns + * @return void + */ + function import( $array = array(), $columns = array( 'sp_player' ) ) { + $this->imported = $this->skipped = 0; + + if ( ! is_array( $array ) || ! sizeof( $array ) ): + $this->footer(); + die(); + endif; + + $rows = array_chunk( $array, sizeof( $columns ) ); + + // Get event ID and team ID from post vars + $event = ( empty( $_POST['sp_event'] ) ? false : $_POST['sp_event'] ); + $teams = ( empty( $_POST['sp_teams'] ) ? false : $_POST['sp_teams'] ); + $index = ( empty( $_POST['sp_index'] ) ? false : $_POST['sp_index'] ); + $team = ( empty( $_POST['sp_team'] ) ? false : $_POST['sp_team'] ); + + $team_players = array( 0 ); + $team_performance = array(); + $name_index = (int) array_search( 'sp_player', $columns ); + + foreach ( $rows as $row ): + + $row = array_filter( $row ); + + if ( empty( $row ) ) continue; + + $player_name = sp_array_value( $row, $name_index ); + if ( ! $player_name ) continue; + + $player_object = get_page_by_title( stripslashes( $player_name ), OBJECT, 'sp_player' ); + + if ( ! $player_object ) continue; + $player_id = $player_object->ID; + + $team_players[] = $player_id; + $player = array(); + + foreach ( $columns as $i => $key ): + if ( 'sp_player' === $key ) continue; + $player[ $key ] = sp_array_value( $row, $i, '' ); + endforeach; + + $team_performance[ $player_id ] = $player; + + endforeach; + + if ( $event && $team ): + $the_players = get_post_meta( $event, 'sp_player', false ); + $players = array(); + for ( $i = 0; $i < $teams; $i++ ): + if ( $index == $i ): + array_push( $players, $team_players ); + else: + array_push( $players, sp_array_between( $the_players, 0, $i ) ); + endif; + endfor; + sp_update_post_meta_recursive( $event, 'sp_player', $players ); + + $this->imported = sizeof( $team_players ) - 1; + + $performance = (array) get_post_meta( $event, 'sp_players', true ); + $performance = array_filter( $performance ); + $performance[ $team ] = $team_performance; + update_post_meta( $event, 'sp_players', $performance ); + endif; + + // Show Result + echo '
+ '.sprintf( __( 'Import complete - imported %s rows and skipped %s.', 'sportspress' ), $this->imported, $this->skipped ).' +
' . __( 'All done!', 'sportspress' ) . ' ' . __( 'View Event', 'sportspress' ) . '' . '
'; + + do_action( 'import_end' ); + } + + /** + * greet function. + * + * @access public + * @return void + */ + function greet() { + $args = array_merge( $_REQUEST, array( 'import' => 'sp_event_performance_csv', 'step' => '1' ) ); + + echo '' . __( 'Hi there! Choose a .csv file to upload, then click "Upload file and import".', 'sportspress' ).'
'; + echo '' . sprintf( __( 'Box scores need to be defined with columns in a specific order. Click here to download a sample.', 'sportspress' ), plugin_dir_url( SP_PLUGIN_FILE ) . 'dummy-data/event-performance-sample.csv' ) . '
'; + wp_import_upload_form( add_query_arg( $args, 'admin.php' ) ); + echo '| + + + + + + + | +|
| + 'sp_team', + 'name' => 'sp_team', + 'values' => 'ID', + 'selected' => $team, + 'include' => $include, + ); + sp_dropdown_pages( $args ); + ?> + | +