Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
01/04/2011 12:33:48 PM (16 years ago)
Author:
boonebgorges
Message:

Ensures that datebox profile field types have their values displayed properly on the profile edit screen. Cleans us bp_get_the_profile_field_options() and reworks the way that default values work in the case of multiselect and checkbox fields. Fixes #2947. Props r-a-y, cnorris23

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-xprofile/bp-xprofile-templatetags.php

    r3560 r3650  
    129129                $field = $this->next_field();
    130130
    131                 if ( !empty( $field->data->value ) ) {
     131                $value = !empty( $field->data ) && !empty( $field->data->value ) ? maybe_unserialize( $field->data->value ) : false;
     132
     133                if ( !empty( $value ) ) {
    132134                        $this->field_has_data = true;
    133                 }
    134                 else {
     135                } else {
    135136                        $this->field_has_data = false;
    136137                }
    … …  
    188189                        $css_classes[] = sanitize_title( esc_attr( $class ) );
    189190
    190                 /* Set a class with the field ID */
     191                // Set a class with the field ID
    191192                $css_classes[] = 'field_' . $profile_template->field->id;
    192193
    193                 /* Set a class with the field name (sanitized) */
     194                // Set a class with the field name (sanitized)
    194195                $css_classes[] = 'field_' . sanitize_title( $profile_template->field->name );
    195196
    … …  
    360361        }
    361362
     363/**
     364 * bp_the_profile_field_options()
     365 *
     366 * Displays field options HTML for field types of 'selectbox', 'multiselectbox',
     367 * 'radio', 'checkbox', and 'datebox'.
     368 *
     369 * @package BuddyPress Xprofile
     370 * @since 1.1
     371 *
     372 * @uses bp_get_the_profile_field_options()
     373 *
     374 * @param array $args Specify type for datebox. Allowed 'day', 'month', 'year'.
     375 */
    362376function bp_the_profile_field_options( $args = '' ) {
    363377        echo bp_get_the_profile_field_options( $args );
    364378}
     379        /**
     380         * bp_get_the_profile_field_options()
     381         *
     382         * Retrieves field options HTML for field types of 'selectbox', 'multiselectbox',
     383         * 'radio', 'checkbox', and 'datebox'.
     384         *
     385         * @package BuddyPress Xprofile
     386         * @since 1.1
     387         *
     388         * @uses BP_XProfile_Field::get_children()
     389         * @uses BP_XProfile_ProfileData::get_value_byid()
     390         *
     391         * @param array $args Specify type for datebox. Allowed 'day', 'month', 'year'.
     392         */
    365393        function bp_get_the_profile_field_options( $args = '' ) {
    366394                global $field;
    … …  
    378406                $options = $field->get_children();
    379407
     408                // Setup some defaults
     409                $html     = '';
     410                $selected = '';
     411
    380412                switch ( $field->type ) {
    381413
    … …  
    385417
    386418                                for ( $k = 0; $k < count($options); $k++ ) {
    387                                         $option_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $options[$k]->parent_id ) );
    388                                         $option_values = (array)$option_values;
    389 
    390                                         /* Check for updated posted values, but errors preventing them from being saved first time */
    391                                         foreach( (array)$option_values as $i => $option_value ) {
     419                                        $original_option_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $options[$k]->parent_id ) );
     420                                        $option_values = (array) $original_option_values;
     421
     422                                        // Check for updated posted values, but errors preventing them from being saved first time
     423                                        foreach( $option_values as $i => $option_value ) {
    392424                                                if ( isset( $_POST['field_' . $field->id] ) && $_POST['field_' . $field->id] != $option_value ) {
    393425                                                        if ( !empty( $_POST['field_' . $field->id] ) )
    … …  
    396428                                        }
    397429
    398                                         if ( in_array( $options[$k]->name, (array)$option_values ) || $options[$k]->is_default_option ) {
     430                                        $selected = '';
     431                                       
     432                                        // First, check to see whether the user-entered value
     433                                        // matches
     434                                        if ( in_array( $options[$k]->name, (array) $option_values ) )
    399435                                                $selected = ' selected="selected"';
    400                                         } else {
    401                                                 $selected = '';
    402                                         }
    403 
    404                                         $html .= apply_filters( 'bp_get_the_profile_field_options_select', '<option' . $selected . ' value="' . stripslashes( esc_attr( $options[$k]->name ) ) . '">' . stripslashes( esc_attr( $options[$k]->name ) ) . '</option>', $options[$k] );
     436
     437                                        // Then, if the user has not provided a value, check for
     438                                        // defaults
     439                                        if ( !is_array( $original_option_values ) && empty( $option_values ) & $options[$k]->is_default_option )
     440                                                $selected = ' selected="selected"';
     441
     442                                        $html .= apply_filters( 'bp_get_the_profile_field_options_select', '<option' . $selected . ' value="' . esc_attr( stripslashes( $options[$k]->name ) ) . '">' . esc_attr( stripslashes( $options[$k]->name ) ) . '</option>', $options[$k] );
    405443                                }
    406444                                break;
    407445
    408446                        case 'radio':
    409                                 $html = '<div id="field_' . $field->id . '">';
     447                                $html .= '<div id="field_' . $field->id . '">';
    410448
    411449                                for ( $k = 0; $k < count($options); $k++ ) {
    412450                                        $option_value = BP_XProfile_ProfileData::get_value_byid($options[$k]->parent_id);
    413451
    414                                         /* Check for updated posted values, but errors preventing them from being saved first time */
     452                                        // Check for updated posted values, but errors preventing them from being saved first time
    415453                                        if ( isset( $_POST['field_' . $field->id] ) && $option_value != $_POST['field_' . $field->id] ) {
    416454                                                if ( !empty( $_POST['field_' . $field->id] ) )
    417455                                                        $option_value = $_POST['field_' . $field->id];
    418456                                        }
    419 
    420                                         if ( $option_value == $options[$k]->name || $value == $options[$k]->name || ( empty( $option_value ) && $options[$k]->is_default_option ) ) {
     457                                       
     458                                        $selected = '';
     459                                        if ( $option_value == $options[$k]->name || !empty( $value ) && $value == $options[$k]->name || ( empty( $option_value ) && $options[$k]->is_default_option ) )
    421460                                                $selected = ' checked="checked"';
    422                                         } else {
    423                                                 $selected = '';
    424                                         }
    425 
    426                                         $html .= apply_filters( 'bp_get_the_profile_field_options_radio', '<label><input' . $selected . ' type="radio" name="field_' . $field->id . '" id="option_' . $options[$k]->id . '" value="' . stripslashes( esc_attr( $options[$k]->name ) ) . '"> ' . stripslashes( esc_attr( $options[$k]->name ) ) . '</label>', $options[$k] );
     461
     462                                        $html .= apply_filters( 'bp_get_the_profile_field_options_radio', '<label><input' . $selected . ' type="radio" name="field_' . $field->id . '" id="option_' . $options[$k]->id . '" value="' . esc_attr( stripslashes( $options[$k]->name ) ) . '"> ' . esc_attr( stripslashes( $options[$k]->name ) ) . '</label>', $options[$k] );
    427463                                }
    428464
    … …  
    433469                                $option_values = BP_XProfile_ProfileData::get_value_byid($options[0]->parent_id);
    434470
    435                                 /* Check for updated posted values, but errors preventing them from being saved first time */
     471                                // Check for updated posted values, but errors preventing them from being saved first time
    436472                                if ( isset( $_POST['field_' . $field->id] ) && $option_values != maybe_serialize( $_POST['field_' . $field->id] ) ) {
    437473                                        if ( !empty( $_POST['field_' . $field->id] ) )
    … …  
    440476
    441477                                $option_values = maybe_unserialize($option_values);
    442 
    443                                 $html = '';
     478                               
    444479                                for ( $k = 0; $k < count($options); $k++ ) {
    445480                                        $selected = '';
     481                                       
     482                                        // First, check to see whether the user's saved values
     483                                        // match the option
    446484                                        for ( $j = 0; $j < count($option_values); $j++ ) {
    447                                                 if ( $option_values[$j] == $options[$k]->name || @in_array( $options[$k]->name, $value ) || $options[$k]->is_default_option ) {
     485                                                if ( $option_values[$j] == $options[$k]->name || @in_array( $options[$k]->name, $value ) ) {
    448486                                                        $selected = ' checked="checked"';
    449487                                                        break;
    450488                                                }
    451489                                        }
    452 
    453                                         $html .= apply_filters( 'bp_get_the_profile_field_options_checkbox', '<label><input' . $selected . ' type="checkbox" name="field_' . $field->id . '[]" id="field_' . $options[$k]->id . '_' . $k . '" value="' . stripslashes( esc_attr( $options[$k]->name ) ) . '"> ' . stripslashes( esc_attr( $options[$k]->name ) ) . '</label>', $options[$k] );
    454                                         $selected = '';
     490                                       
     491                                        // If the user has not yet supplied a value for this field,
     492                                        // check to see whether there is a default value available
     493                                        if ( !is_array( $option_values ) && empty( $option_values ) && !$selected && $options[$k]->is_default_option) {
     494                                                $selected = ' checked="checked"';
     495                                        }
     496
     497                                        $html .= apply_filters( 'bp_get_the_profile_field_options_checkbox', '<label><input' . $selected . ' type="checkbox" name="field_' . $field->id . '[]" id="field_' . $options[$k]->id . '_' . $k . '" value="' . esc_attr( stripslashes( $options[$k]->name ) ) . '"> ' . esc_attr( stripslashes( $options[$k]->name ) ) . '</label>', $options[$k] );
    455498                                }
    456499                                break;
    457500
    458501                        case 'datebox':
    459 
    460                                 if ( !empty( $field->data->value ) ) {
    461                                         $day = date("j", $field->data->value);
    462                                         $month = date("F", $field->data->value);
    463                                         $year = date("Y", $field->data->value);
    464                                         $default_select = ' selected="selected"';
    465                                 }
    466 
    467                                 /* Check for updated posted values, but errors preventing them from being saved first time */
     502                                $date = BP_XProfile_ProfileData::get_value_byid( $field->id );
     503
     504                                // Set day, month, year defaults
     505                                $day   = '';
     506                                $month = '';
     507                                $year  = '';
     508
     509                                if ( !empty( $date ) ) {
     510                                        $day   = date( 'j', $date );
     511                                        $month = date( 'F', $date );
     512                                        $year  = date( 'Y', $date );
     513                                }
     514
     515                                // Check for updated posted values, but errors preventing them from being saved first time
    468516                                if ( !empty( $_POST['field_' . $field->id . '_day'] ) ) {
    469517                                        if ( $day != $_POST['field_' . $field->id . '_day'] )
    … …  
    483531                                switch ( $type ) {
    484532                                        case 'day':
    485                                                 $html .= '<option value=""' . esc_attr( $default_select ) . '>--</option>';
     533                                                $html .= '<option value=""' . selected( $day, '', false ) . '>--</option>';
    486534
    487535                                                for ( $i = 1; $i < 32; $i++ ) {
    488                                                         if ( $day == $i ) {
    489                                                                 $selected = ' selected = "selected"';
    490                                                         } else {
    491                                                                 $selected = '';
    492                                                         }
    493                                                         $html .= '<option value="' . $i .'"' . $selected . '>' . $i . '</option>';
     536                                                        $html .= '<option value="' . $i .'"' . selected( $day, $i, false ) . '>' . $i . '</option>';
    494537                                                }
    495538                                                break;
    … …  
    504547                                                                );
    505548
    506                                                 $html .= '<option value=""' . esc_attr( $default_select ) . '>------</option>';
     549                                                $html .= '<option value=""' . selected( $month, '', false ) . '>------</option>';
    507550
    508551                                                for ( $i = 0; $i < 12; $i++ ) {
    509                                                         if ( $month == $eng_months[$i] ) {
    510                                                                 $selected = ' selected = "selected"';
    511                                                         } else {
    512                                                                 $selected = '';
    513                                                         }
    514 
    515                                                         $html .= '<option value="' . $eng_months[$i] . '"' . $selected . '>' . $months[$i] . '</option>';
     552                                                        $html .= '<option value="' . $eng_months[$i] . '"' . selected( $month, $eng_months[$i], false ) . '>' . $months[$i] . '</option>';
    516553                                                }
    517554                                                break;
    518555
    519556                                        case 'year':
    520                                                 $html .= '<option value=""' . esc_attr( $default_select ) . '>----</option>';
    521 
    522                                                 for ( $i = date( 'Y', time() ); $i > 1899; $i-- ) {
    523                                                         if ( $year == $i ) {
    524                                                                 $selected = ' selected = "selected"';
    525                                                         } else {
    526                                                                 $selected = '';
    527                                                         }
    528 
    529                                                         $html .= '<option value="' . $i .'"' . $selected . '>' . $i . '</option>';
     557                                                $html .= '<option value=""' . selected( $year, '', false ) . '>----</option>';
     558
     559                                                for ( $i = date( 'Y' ); $i > 1899; $i-- ) {
     560                                                        $html .= '<option value="' . $i .'"' . selected( $year, $i, false ) . '>' . $i . '</option>';
    530561                                                }
    531562                                                break;
    532563                                }
    533564
    534                                 apply_filters( 'bp_get_the_profile_field_datebox', $html, $day, $month, $year, $default_select );
     565                                $html = apply_filters( 'bp_get_the_profile_field_datebox', $html, $type, $day, $month, $year );
    535566
    536567                                break;
Note: See TracChangeset for help on using the changeset viewer.