Add date selector to calendars

This commit is contained in:
Brian Miyaji
2014-06-18 23:01:53 +10:00
parent a9a47decd5
commit 51b115377e
5 changed files with 148 additions and 74 deletions

View File

@@ -80,7 +80,8 @@ class SP_Meta_Box_Calendar_Data {
</thead> </thead>
<tbody> <tbody>
<?php <?php
if ( is_array( $data ) && sizeof( $data ) > 0 ): if ( is_array( $data ) ):
if ( sizeof( $data ) > 0 ):
$main_result = get_option( 'sportspress_primary_result', null ); $main_result = get_option( 'sportspress_primary_result', null );
$i = 0; $i = 0;
foreach ( $data as $event ): foreach ( $data as $event ):
@@ -156,6 +157,15 @@ class SP_Meta_Box_Calendar_Data {
<?php <?php
$i++; $i++;
endforeach; endforeach;
else:
?>
<tr class="sp-row alternate">
<td colspan="6">
<?php _e( 'No results found.', 'sportspress' ); ?>
</td>
</tr>
<?php
endif;
else: else:
?> ?>
<tr class="sp-row alternate"> <tr class="sp-row alternate">

View File

@@ -20,6 +20,7 @@ class SP_Meta_Box_Calendar_Details {
*/ */
public static function output( $post ) { public static function output( $post ) {
$status = get_post_meta( $post->ID, 'sp_status', true ); $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 ); $league_id = sp_get_the_term_id( $post->ID, 'sp_league', 0 );
$season_id = sp_get_the_term_id( $post->ID, 'sp_season', 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 ); $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_dropdown_statuses( $args );
?> ?>
</p> </p>
<p><strong><?php _e( 'Date', 'sportspress' ); ?></strong></p>
<p>
<?php
$args = array(
'name' => 'sp_date',
'id' => 'sp_date',
'selected' => $date,
);
sp_dropdown_dates( $args );
?>
</p>
<p><strong><?php _e( 'League', 'sportspress' ); ?></strong></p> <p><strong><?php _e( 'League', 'sportspress' ); ?></strong></p>
<p> <p>
<?php <?php
@@ -117,6 +129,7 @@ class SP_Meta_Box_Calendar_Details {
wp_set_post_terms( $post_id, sp_array_value( $_POST, 'sp_season', 0 ), 'sp_season' ); wp_set_post_terms( $post_id, sp_array_value( $_POST, 'sp_season', 0 ), 'sp_season' );
wp_set_post_terms( $post_id, sp_array_value( $_POST, 'sp_venue', 0 ), 'sp_venue' ); wp_set_post_terms( $post_id, sp_array_value( $_POST, 'sp_venue', 0 ), 'sp_venue' );
update_post_meta( $post_id, 'sp_status', sp_array_value( $_POST, 'sp_status', 0 ) ); update_post_meta( $post_id, 'sp_status', sp_array_value( $_POST, 'sp_status', 0 ) );
update_post_meta( $post_id, 'sp_date', sp_array_value( $_POST, 'sp_date', 0 ) );
update_post_meta( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', 0 ) ); update_post_meta( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', 0 ) );
update_post_meta( $post_id, 'sp_order', sp_array_value( $_POST, 'sp_order', array() ) ); update_post_meta( $post_id, 'sp_order', sp_array_value( $_POST, 'sp_order', array() ) );
} }

View File

@@ -12,10 +12,13 @@
*/ */
class SP_Calendar extends SP_Custom_Post { class SP_Calendar extends SP_Custom_Post {
/** @var array The events status. */ /** @var string The events status. */
public $status; public $status;
/** @var array The events order. */ /** @var string The date filter for events. */
public $date;
/** @var string The events order. */
public $order; public $order;
/** /**
@@ -34,11 +37,15 @@ class SP_Calendar extends SP_Custom_Post {
endif; endif;
$this->status = $this->__get( 'status' ); $this->status = $this->__get( 'status' );
$this->date = $this->__get( 'date' );
$this->order = $this->__get( 'order' ); $this->order = $this->__get( 'order' );
if ( ! $this->status ) if ( ! $this->status )
$this->status = 'any'; $this->status = 'any';
if ( ! $this->date )
$this->date = 0;
if ( ! $this->order ) if ( ! $this->order )
$this->order = 'ASC'; $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 ( $pagenow != 'post-new.php' ):
if ( $this->ID ): if ( $this->ID ):
$leagues = get_the_terms( $this->ID, 'sp_league' ); $leagues = get_the_terms( $this->ID, 'sp_league' );
@@ -121,7 +138,7 @@ class SP_Calendar extends SP_Custom_Post {
$events = get_posts( $args ); $events = get_posts( $args );
else: else:
$events = array(); $events = null;
endif; endif;
return $events; return $events;

View File

@@ -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( '<select name="%s" class="postform %s">', $args['name'], $args['class'] );
if ( $args['show_option_default'] ):
printf( '<option value="default">%s</option>', $args['show_option_default'] );
endif;
$dates = apply_filters( 'sportspress_dates', array(
0 => __( 'All', 'sportspress' ),
'w' => __( 'This week', 'sportspress' ),
'day' => __( 'Today', 'sportspress' )
));
foreach ( $dates as $value => $label ):
printf( '<option value="%s" %s>%s</option>', $value, selected( $value, $args['selected'], false ), $label );
endforeach;
print( '</select>' );
return true;
}
}
if ( !function_exists( 'sp_dropdown_taxonomies' ) ) { if ( !function_exists( 'sp_dropdown_taxonomies' ) ) {
function sp_dropdown_taxonomies( $args = array() ) { function sp_dropdown_taxonomies( $args = array() ) {
$defaults = array( $defaults = array(

View File

@@ -34,6 +34,9 @@ if ( isset( $id ) ):
foreach ( $events as $event ): foreach ( $events as $event ):
$event_ids[] = $event->ID; $event_ids[] = $event->ID;
endforeach; endforeach;
if ( empty( $event_ids ) )
$in = 'AND 1 = 0'; // False logic to prevent SQL error
else
$in = 'AND ID IN (' . implode( ', ', $event_ids ) . ')'; $in = 'AND ID IN (' . implode( ', ', $event_ids ) . ')';
else: else:
$in = ''; $in = '';