From 51b115377e9e994e8312731c8a8b4e294c750319 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Wed, 18 Jun 2014 23:01:53 +1000 Subject: [PATCH] Add date selector to calendars --- .../class-sp-meta-box-calendar-data.php | 150 ++++++++++-------- .../class-sp-meta-box-calendar-details.php | 13 ++ includes/class-sp-calendar.php | 23 ++- includes/sp-core-functions.php | 31 ++++ templates/event-calendar.php | 5 +- 5 files changed, 148 insertions(+), 74 deletions(-) diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-calendar-data.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-calendar-data.php index 28acfb73..f1d80110 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-calendar-data.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-calendar-data.php @@ -80,82 +80,92 @@ class SP_Meta_Box_Calendar_Data { 0 ): - $main_result = get_option( 'sportspress_primary_result', null ); - $i = 0; - foreach ( $data as $event ): - $teams = get_post_meta( $event->ID, 'sp_team' ); - $results = get_post_meta( $event->ID, 'sp_results', true ); - $video = get_post_meta( $event->ID, 'sp_video', true ); - $main_results = array(); - ?> - - - post_title; ?> - - ID ) . '">' . $team_result . ' '; - endif; - - echo $name . '
'; - endif; - endforeach; else: - echo '—'; - endif; - ?> - - - - - ID, 'sp_venue' ); ?> - - - -
- ID ) ): ?> -
- + if ( is_array( $data ) ): + if ( sizeof( $data ) > 0 ): + $main_result = get_option( 'sportspress_primary_result', null ); + $i = 0; + foreach ( $data as $event ): + $teams = get_post_meta( $event->ID, 'sp_team' ); + $results = get_post_meta( $event->ID, 'sp_results', true ); + $video = get_post_meta( $event->ID, 'sp_video', true ); + $main_results = array(); + ?> + + + post_title; ?> + post_content == null ): - _e( 'None', 'sportspress' ); - elseif ( $event->post_status == 'publish' ): - _e( 'Recap', 'sportspress' ); - else: - _e( 'Preview', 'sportspress' ); + if ( $teams ): foreach ( $teams as $team ): + $name = get_the_title( $team ); + if ( $name ): + $team_results = sp_array_value( $results, $team, null ); + + if ( $main_result ): + $team_result = sp_array_value( $team_results, $main_result, null ); + else: + if ( is_array( $team_results ) ): + end( $team_results ); + $team_result = prev( $team_results ); + else: + $team_result = null; + endif; + endif; + + if ( $team_result != null ): + $main_results[] = $team_result; + unset( $team_results['outcome'] ); + $team_results = implode( ' | ', $team_results ); + echo '
' . $team_result . ' '; + endif; + + echo $name . '
'; + endif; + endforeach; else: + echo '—'; endif; ?> - + + + + + ID, 'sp_venue' ); ?> + + + +
+ ID ) ): ?> +
+ + post_content == null ): + _e( 'None', 'sportspress' ); + elseif ( $event->post_status == 'publish' ): + _e( 'Recap', 'sportspress' ); + else: + _e( 'Preview', 'sportspress' ); + endif; + ?> +
+ + + + + + diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-calendar-details.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-calendar-details.php index 7ccf645b..cc355c1c 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-calendar-details.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-calendar-details.php @@ -20,6 +20,7 @@ class SP_Meta_Box_Calendar_Details { */ public static function output( $post ) { $status = get_post_meta( $post->ID, 'sp_status', true ); + $date = get_post_meta( $post->ID, 'sp_date', true ); $league_id = sp_get_the_term_id( $post->ID, 'sp_league', 0 ); $season_id = sp_get_the_term_id( $post->ID, 'sp_season', 0 ); $venue_id = sp_get_the_term_id( $post->ID, 'sp_venue', 0 ); @@ -38,6 +39,17 @@ class SP_Meta_Box_Calendar_Details { sp_dropdown_statuses( $args ); ?>

+

+

+ 'sp_date', + 'id' => 'sp_date', + 'selected' => $date, + ); + sp_dropdown_dates( $args ); + ?> +

status = $this->__get( 'status' ); + $this->date = $this->__get( 'date' ); $this->order = $this->__get( 'order' ); if ( ! $this->status ) $this->status = 'any'; + if ( ! $this->date ) + $this->date = 0; + if ( ! $this->order ) $this->order = 'ASC'; } @@ -64,6 +71,16 @@ class SP_Calendar extends SP_Custom_Post { ), ); + if ( $this->date !== 0 ): + $args['year'] = date('Y'); + if ( $this->date == 'w' ): + $args['w'] = date('W'); + elseif ( $this->date == 'day' ): + $args['day'] = date('j'); + $args['monthnum'] = date('n'); + endif; + endif; + if ( $pagenow != 'post-new.php' ): if ( $this->ID ): $leagues = get_the_terms( $this->ID, 'sp_league' ); @@ -121,7 +138,7 @@ class SP_Calendar extends SP_Custom_Post { $events = get_posts( $args ); else: - $events = array(); + $events = null; endif; return $events; diff --git a/includes/sp-core-functions.php b/includes/sp-core-functions.php index 9ff23fec..5f51ba53 100644 --- a/includes/sp-core-functions.php +++ b/includes/sp-core-functions.php @@ -389,6 +389,37 @@ if ( !function_exists( 'sp_dropdown_statuses' ) ) { } } +if ( !function_exists( 'sp_dropdown_dates' ) ) { + function sp_dropdown_dates( $args = array() ) { + $defaults = array( + 'show_option_default' => false, + 'name' => 'sp_date', + 'id' => null, + 'selected' => null, + 'class' => null, + ); + $args = array_merge( $defaults, $args ); + + printf( '' ); + return true; + } +} + if ( !function_exists( 'sp_dropdown_taxonomies' ) ) { function sp_dropdown_taxonomies( $args = array() ) { $defaults = array( diff --git a/templates/event-calendar.php b/templates/event-calendar.php index 4952ee23..897325fb 100644 --- a/templates/event-calendar.php +++ b/templates/event-calendar.php @@ -34,7 +34,10 @@ if ( isset( $id ) ): foreach ( $events as $event ): $event_ids[] = $event->ID; endforeach; - $in = 'AND ID IN (' . implode( ', ', $event_ids ) . ')'; + if ( empty( $event_ids ) ) + $in = 'AND 1 = 0'; // False logic to prevent SQL error + else + $in = 'AND ID IN (' . implode( ', ', $event_ids ) . ')'; else: $in = ''; endif;