Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidwolfpaw/d7f08e1fe001d6028bdc13ca587f9c67 to your computer and use it in GitHub Desktop.
Save davidwolfpaw/d7f08e1fe001d6028bdc13ca587f9c67 to your computer and use it in GitHub Desktop.

Revisions

  1. David created this gist Aug 23, 2016.
    118 changes: 118 additions & 0 deletions dynamic_date_dropdown_gravityforms.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,118 @@
    <?php
    /*
    * Dynamically generate list of dates in Gravity Forms Dropdown and remove past and previously selected dates
    */


    add_filter( 'gform_pre_render', 'dl_populate_dropdown' );

    //Note: when changing drop down values, we also need to use the gform_pre_validation so that the new values are available when validating the field.
    add_filter( 'gform_pre_validation', 'dl_populate_dropdown' );

    //Note: when changing drop down values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.
    add_filter( 'gform_admin_pre_render', 'dl_populate_dropdown' );

    //Note: this will allow for the labels to be used during the submission process in case values are enabled
    add_filter( 'gform_pre_submission_filter', 'dl_populate_dropdown' );

    function dl_populate_dropdown( $form ) {

    // Set Form ID and field ID to 6 and 4 respectively
    $form_id = 6;
    $field_id = 4;

    //only populating drop down for form id 6
    if ( $form['id'] != $form_id ) {
    return $form;
    }


    // Create array of available dates (Updated Manually until I think of something better)
    $dates = array(
    '20 August 2016',
    '21 August 2016',
    '22 August 2016',
    '23 August 2016',
    '24 August 2016',
    '25 August 2016',
    '26 August 2016',
    '2 September 2016',
    '16 September 2016',
    '23 September 2016',
    '30 September 2016',
    '14 October 2016',
    '21 October 2016',
    '28 October 2016',
    );


    // Create array for Unix timestamped dates
    $unix_dates[] = array( 'text' => '', 'value' => '' );


    // Convert dates to array of Unix timestamps (I don't want to do this myself!)
    foreach ( $dates as $date ) {
    $unix_dates[] = array( 'value' => strtotime( $date ), 'text' => $date );
    }

    // Get the past 20 entries to this form to compare and remove used dates
    $entries = GFAPI::get_entries( $form_id );

    // Create array of booked dates
    $booked_dates = array();
    foreach ( $entries as $entry ) {
    $booked_dates[] = ( $entry[ $field_id ] );
    }

    // Update array to remove any dates that have been booked already
    foreach ( $unix_dates as $unix_date => $date_value ) {

    foreach ( $date_value as $key => $value ) {

    foreach ( $booked_dates as $booked_date ) {

    // If the date is booked, remove it from the list
    if ( ( $key == 'value' ) && ( $value == $booked_date ) ) {
    unset( $unix_dates[$unix_date] );
    }

    }

    }

    }

    // Get current Unix timestamp
    $current_time = time();

    // Update array to remove any dates that have passed or will pass in 12 hours
    // I want to only allow people to schedule up to noon the day before to avoid surprises
    foreach ( $unix_dates as $unix_date => $date_value ) {

    foreach ( $date_value as $key => $value ) {

    // Current time plus twelve hours
    $time_plus_twelve = $current_time + (60*60*12);

    // See if time has passed, and if so, unset date
    if ( ( $key == 'value' ) && ( $time_plus_twelve >= $value ) ) {
    unset( $unix_dates[$unix_date] );
    }

    }

    }


    // Adding items to field
    foreach ( $form['fields'] as &$field ) {

    if ( $field->id == $field_id ) {
    $field->choices = $unix_dates;
    }

    }


    return $form;
    }