Skip to content

Instantly share code, notes, and snippets.

@Apina
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Apina/ee274edad2c26fef1f87 to your computer and use it in GitHub Desktop.
Save Apina/ee274edad2c26fef1f87 to your computer and use it in GitHub Desktop.
Custom Function for EE3 event list to display all times
function deans_amazing_time( $event_id = 'NULL', $label = 1, $multi_reg = 0, $value = '' ) {
global $wpdb, $org_options;
$html = '';
//Will make the name an array and put the event id as a key so we
//know which event this belongs to
$multi_name_adjust = $multi_reg == 1 ? "[$event_id]" : '';
$SQL = "SELECT timezone_string FROM " . EVENTS_DETAIL_TABLE . " WHERE id= %d ";
$timezone_string = $wpdb->get_var( $wpdb->prepare( $SQL, $event_id ));
//This is the initial check to see if time slots are controlled by registration limits.
if ( $org_options['time_reg_limit'] == 'Y' ) {
// select everything from time table plus calculate available spaces by subtracting
// attendee count from inner query from the individual reg limits returned by the outer query
$SQL = "SELECT ESE.*, ( ESE.reg_limit - ";
// count # of attendees with good registrations where event start and end times match outer query start and end times
$SQL .= "( SELECT count(id) FROM " . EVENTS_ATTENDEE_TABLE . " ATT ";
$SQL .= "WHERE ATT.event_id= %d ";
$SQL .= "AND ATT.payment_status != 'Incomplete' ";
$SQL .= "AND ATT.event_time = ESE.start_time ";
$SQL .= "AND ATT.end_time = ESE.end_time ) ";
$SQL .= ") AS available_spaces ";
$SQL .= "FROM " . EVENTS_START_END_TABLE . " ESE ";
$SQL .= "WHERE ESE.event_id= %d ";
$SQL .= "GROUP BY ESE.id ";
$SQL .= "ORDER BY ESE.start_time";
$event_times = $wpdb->get_results( $wpdb->prepare( $SQL, $event_id, $event_id ));
} else {
$SQL = "SELECT ESE.* FROM " . EVENTS_START_END_TABLE . " ESE ";
$SQL .= "WHERE ESE.event_id= %d ";
$SQL .= "GROUP BY ESE.id ";
$SQL .= "ORDER BY ESE.start_time";
$event_times = $wpdb->get_results( $wpdb->prepare( $SQL, $event_id ));
}
//printr( $event_times, '$event_times <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
// If one result, then display the times.
if ($wpdb->num_rows == 1) {
$html .= $label == 1 ? '<span class="span_event_time_label ">' . __('Start Time:', 'event_espresso') . '</span>' : '';
foreach ($event_times as $time) {
$html .= ' <span class="span_event_time_value">' . event_date_display($time->start_time, get_option('time_format')) . '</span>';
$html .= $label == 1 ? '<br/><span class="span_event_time_label">' . __('End Time: ', 'event_espresso') . '</span>' : __(' to ', 'event_espresso');
$html .= ' <span class="span_event_time_value">' . event_date_display($time->end_time, get_option('time_format')) . '</span>';
$html .= '<input type="hidden" name="start_time_id' . $multi_name_adjust . '" id="start_time_id_' . $time->id . '" value="' . $time->id . '" />';
}
} else if ($wpdb->num_rows > 1) {//If more than one result, then display the dropdown
//print_r($event_times);
$html .= $label == 1 ? '<label class="start_time_id section-title" for="start_time_id">' . __('Available Times: ', 'event_espresso') . '</label>' : '';
$html .= '<div>';
//$html .= $label == 0 ?'<option value="">' .__('Select a Time', 'event_espresso') . '</option>':'';
foreach ($event_times as $time) {
$selected = $value == $time->id ? ' selected="selected" ' : '';
switch ( $org_options['time_reg_limit'] ) {//This checks to see if the time slots are controlled by registration limits.
case 'Y':
//If the time slot is controlled by a registration limit.
//Then we need to check if there are enough spaces available.
//if (($time->reg_limit == 0)||($time->reg_limit > 0 && $time->reg_limit >=$num_attendees))
//If enough spaces are available, then show this time slot
if ($time->available_spaces > 0)
$html .= '<p>' . event_date_display($time->start_time, get_option('time_format')) . ' - ' . event_date_display($time->end_time, get_option('time_format')) . " ($time->available_spaces " . __('available spaces', 'event_espresso') . ")" . '</p>';
break;
case 'N'://If time slots are not controlled by registration limits, then we show the default dropdown list of times.
default:
$html .= '<p>' . event_date_display($time->start_time, get_option('time_format')) . ' - ' . event_date_display($time->end_time, get_option('time_format')) . '</p>';
break;
}
}
$html .= '</div>';
}
return $html;
}
@Apina
Copy link
Author

Apina commented Mar 14, 2014

Needs to go into a file called custom_functions.php located in wp-content/uploads/espresso/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment