Home › Forums › Calendar Products › Community Events › Start Time Changes When Error Message is Returned
- This topic has 5 replies, 2 voices, and was last updated 10 years, 4 months ago by
Brian.
-
AuthorPosts
-
December 4, 2015 at 6:15 pm #1035830
Sean
ParticipantHi,
We are using some code you supplied in snippets to alter the default Start Time and End Time for events on the Community Events: Submit an Event form. It works fine, except that when an error message is returned (because the user left a required field blank, etc), the Start Time is changed to be the same as the End Time that was entered when the user first tried to submit the form.
We’ve confirmed that the issue only occurs with this snippet activated. How can we modify this so that when error messages are returned on the Community Events: Submit an Event form, the Start Time that was originally entered is not altered?
/** * Helps to change the default start/end times for new event. */ class Change_Tribe_Default_Event_Times { const TWELVEHOUR = 12; const TWENTYFOURHOUR = 24; protected $start = 9; protected $end = 11; protected $mode = self::TWELVEHOUR; protected $mode_set = false; /** * Provide the desired default start and end hours in 24hr format (ie 15 = 3pm). * * @param $start_hour * @param $end_hour */ public function __construct( $start_hour, $end_hour ) { $this->settings( $start_hour, $end_hour ); $this->add_filters(); } protected function settings( $start_hour, $end_hour ) { $this->start = $this->safe_hour( $start_hour ); $this->end = $this->safe_hour( $end_hour ); } protected function add_filters() { add_filter( 'tribe_get_hour_options', array( $this, 'change_default_time' ), 10, 3 ); add_filter( 'tribe_get_meridian_options', array( $this, 'change_default_meridian' ), 10, 3 ); } protected function set_mode() { if ( $this->mode_set ) return; if ( strstr( get_option('time_format', $this->time_format() ), 'H' ) ) $this->mode = self::TWENTYFOURHOUR; $this->mode_set = true; } protected function safe_hour( $hour ) { $hour = absint( $hour ); if ( $hour < 0 ) $hour = 0; if ( $hour > 23 ) $hour = 23; return $hour; } public function change_default_time( $hour, $date, $isStart ) { $this->set_mode(); if ( 'post-new.php' !== $GLOBALS['pagenow'] ) return $hour; // Only intervene if it's a new event if ( $isStart ) return $this->corrected_time( $this->start ); else return $this->corrected_time( $this->end ); } /** * If working in the 12hr clock, converts the hour appropriately. * * @param $hour * @return int */ protected function corrected_time( $hour ) { $this->set_mode(); if ( self::TWENTYFOURHOUR === $this->mode ) return $hour; if ( $hour > 12 ) return $hour - 12; return $hour; } public function change_default_meridian( $meridian, $date, $isStart ) { if ( 'post-new.php' !== $GLOBALS['pagenow'] ) return $meridian; // Only intervene if it's a new event $meridian = 'am'; if ( $isStart && 12 <= $this->start ) $meridian = 'pm'; if ( ! $isStart && 12 <= $this->end ) $meridian = 'pm'; if ( strstr( get_option('time_format', $this->time_format() ), 'A' ) ) $meridian = strtoupper( $meridian ); return $meridian; } protected function time_format() { if ( class_exists( 'Tribe__Events__Date_Utils' ) ) return Tribe__Events__Date_Utils::TIMEFORMAT; if ( class_exists( 'TribeDateUtils') ) return TribeDateUtils::TIMEFORMAT; return 'g:i A'; } } new Change_Tribe_Default_Event_Times(0, 0); // Change Start Time on Community Events Submit an Event form add_action( 'tribe_community_events_form_start_time_selector', 'comm_default_start_time', 10, 2 ); function comm_default_start_time( $output, $event_id ) { if ( !$event_id ) { $new_time = strtotime( '12am' ); $start_date = tribe_event_format_date( $new_time, true, Tribe__Events__Date_Utils::DBDATETIMEFORMAT ); $start_minutes = Tribe__Events__View_Helpers::getMinuteOptions( $start_date, true ); $start_hours = Tribe__Events__View_Helpers::getHourOptions( $is_all_day == 'yes' ? null : $start_date, true ); $start_meridian = Tribe__Events__View_Helpers::getMeridianOptions( $start_date, true ); $output = ''; $output .= sprintf( '<select name="EventStartHour">%s</select>', $start_hours ); $output .= sprintf( '<select name="EventStartMinute">%s</select>', $start_minutes ); if ( ! tribe_community_events_use_24hr_format() ) { $output .= sprintf( '<select name="EventStartMeridian">%s</select>', $start_meridian ); } } return $output; } add_action( 'tribe_community_events_form_end_time_selector', 'comm_default_end_time', 10, 2 ); function comm_default_end_time( $output, $event_id ) { if ( !$event_id ) { $new_time = strtotime( '12am' ); $start_date = tribe_event_format_date( $new_time, true, Tribe__Events__Date_Utils::DBDATETIMEFORMAT ); $start_minutes = Tribe__Events__View_Helpers::getMinuteOptions( $start_date, true ); $start_hours = Tribe__Events__View_Helpers::getHourOptions( $is_all_day == 'yes' ? null : $start_date, true ); $start_meridian = Tribe__Events__View_Helpers::getMeridianOptions( $start_date, true ); $output = ''; $output .= sprintf( '<select name="EventStartHour">%s</select>', $start_hours ); $output .= sprintf( '<select name="EventStartMinute">%s</select>', $start_minutes ); if ( ! tribe_community_events_use_24hr_format() ) { $output .= sprintf( '<select name="EventStartMeridian">%s</select>', $start_meridian ); } } return $output; }December 7, 2015 at 8:20 am #1036938Brian
MemberHi,
The patch we are adding to fix it so all fields retain their value on a required field error might resolve this.
I do not think changing the snippet itself can help with the issue.
Did this work in 3.12?
Thanks
December 7, 2015 at 10:42 am #1037039Sean
ParticipantOkay that’s good to hear a fix is on the way. Is this fix (so all fields retain their value on a required field error) set to be released in the next maintenance release, 4.0.1?
I’m not sure if it occurred in 3.12 as we didn’t start testing/QA until 4.0 beta was available.
December 7, 2015 at 2:46 pm #1037187Brian
MemberOkay that’s good to hear a fix is on the way. Is this fix (so all fields retain their value on a required field error) set to be released in the next maintenance release, 4.0.1?
It is being worked on with a hopefully release within days, but that could change if something comes up.
December 9, 2015 at 1:51 pm #1038474Sean
ParticipantOkay sounds good Brian. Thank you.
December 10, 2015 at 8:59 am #1038942Brian
MemberI am going to close this ticket to new replies, but the issue is still open and we will update this ticket once we release a fix.
If you have any new questions or issues please create a new ticket and reference this one.
Thanks
-
AuthorPosts
- The topic ‘Start Time Changes When Error Message is Returned’ is closed to new replies.
