Skip to:
Content

BuddyPress.org

Changeset 4869


Ignore:
Timestamp:
07/26/2011 05:26:30 PM (15 years ago)
Author:
boonebgorges
Message:

Adds hide_empty_fields parameter to bp_has_profile() and the rest of the profile template chain, to make zebra striping easier in the profile component. Fixes #3074. Props johnjamesjacoby for help with the patch

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-template.php

    r4847 r4869  
    651651        $is_current_component = false;
    652652
     653        // Backward compatibility: 'xprofile' should be read as 'profile'
     654        if ( 'xprofile' == $component )
     655                $component = 'profile';
     656
    653657        if ( !empty( $bp->current_component ) ) {
    654658
     
    10231027        if ( bp_is_current_component( 'xprofile' ) && bp_is_current_action( 'edit' ) )
    10241028                return true;
    1025 
     1029               
    10261030        return false;
    10271031}
  • trunk/bp-xprofile/bp-xprofile-classes.php

    r4827 r4869  
    100100         *              'user_id' - Required if you want to load a specific user's data
    101101         *              'hide_empty_groups' - Hide groups without any fields
     102         *              'hide_empty_fields' - Hide fields where the user has not provided data
    102103         *              'fetch_fields' - Load each group's fields
    103104         *              'fetch_field_data' - Load each field's data. Requires a user_id
     
    114115                        'user_id'           => $bp->displayed_user->id,
    115116                        'hide_empty_groups' => false,
     117                        'hide_empty_fields' => false,
    116118                        'fetch_fields'      => false,
    117119                        'fetch_field_data'  => false,
     
    162164                                $field_ids[] = $field->id;
    163165
    164                         $field_ids = implode( ',', (array) $field_ids );
     166                        $field_ids_sql = implode( ',', (array) $field_ids );
    165167
    166168                        if ( !empty( $field_ids ) )
    167                                 $field_data = $wpdb->get_results( $wpdb->prepare( "SELECT field_id, value FROM {$bp->profile->table_name_data} WHERE field_id IN ( {$field_ids} ) AND user_id = %d", $user_id ) );
    168 
    169                         if ( !empty( $field_data ) ) {
     169                                $field_data = $wpdb->get_results( $wpdb->prepare( "SELECT field_id, value FROM {$bp->profile->table_name_data} WHERE field_id IN ( {$field_ids_sql} ) AND user_id = %d", $user_id ) );
     170                               
     171                        // Remove data-less fields, if necessary
     172                        if ( $hide_empty_fields ) {
     173                       
     174                                // Loop through the results and find the fields that have data.
     175                                foreach( (array)$field_data as $data ) {
     176                                        if ( false !== $key = array_search( $data->field_id, $field_ids ) ) {
     177                                                // Fields that have data get removed from the list
     178                                                unset( $field_ids[$key] );
     179                                        }
     180                                }
     181                               
     182                                // The remaining members of $field_ids are empty. Remove them.
     183                                foreach( $fields as $field_key => $field ) {
     184                                        if ( in_array( $field->id, $field_ids ) ) {
     185                                                unset( $fields[$field_key] );
     186                                        }
     187                                }
     188                               
     189                                // Reset indexes
     190                                $fields = array_values( $fields );
     191                               
     192                        }
     193                       
     194                        // Field data was found
     195                        if ( !empty( $field_data ) && !is_wp_error( $field_data ) ) {
     196                               
     197                                // Loop through fields
    170198                                foreach( (array)$fields as $field_key => $field ) {
     199                                       
     200                                        // Loop throught the data in each field
    171201                                        foreach( (array)$field_data as $data ) {
     202                                       
     203                                                // Assign correct data value to the field
    172204                                                if ( $field->id == $data->field_id )
    173205                                                        $fields[$field_key]->data->value = $data->value;
    174                                         }
    175                                 }
     206                                        }       
     207                                }       
    176208                        }
    177209                }
    178210
    179211                // Merge the field array back in with the group array
    180                 foreach( (array)$groups as $group_key => $group ) {
    181                         foreach( (array)$fields as $field ) {
     212                foreach( (array) $groups as $group_key => $group ) {
     213                        foreach( (array) $fields as $field ) {
    182214                                if ( $group->id == $field->group_id )
    183215                                        $groups[$group_key]->fields[] = $field;
    184216                        }
     217                       
     218                        // When we unset fields above, we may have created empty groups.
     219                        // Remove them, if necessary.
     220                        if ( empty( $group->fields ) && $hide_empty_groups ) {
     221                                unset( $groups[$group_key] );
     222                        }
     223                       
     224                        // Reset indexes
     225                        $groups = array_values( $groups );
    185226                }
    186227
  • trunk/bp-xprofile/bp-xprofile-template.php

    r4840 r4869  
    2121        var $user_id;
    2222
    23         function bp_xprofile_data_template( $user_id, $profile_group_id, $hide_empty_groups = false, $fetch_fields = false, $fetch_field_data = false, $exclude_groups = false, $exclude_fields = false ) {
    24                 $this->__construct( $user_id, $profile_group_id, $hide_empty_groups, $fetch_fields, $fetch_field_data, $exclude_groups, $exclude_fields );
    25         }
    26 
    27         function __construct( $user_id, $profile_group_id, $hide_empty_groups = false, $fetch_fields = false, $fetch_field_data = false, $exclude_groups = false, $exclude_fields = false ) {
     23        function bp_xprofile_data_template( $user_id, $profile_group_id, $hide_empty_groups = false, $fetch_fields = false, $fetch_field_data = false, $exclude_groups = false, $exclude_fields = false, $hide_empty_fields = false ) {
     24                $this->__construct( $user_id, $profile_group_id, $hide_empty_groups, $fetch_fields, $fetch_field_data, $exclude_groups, $exclude_fields, $hide_empty_fields );
     25        }
     26
     27        function __construct( $user_id, $profile_group_id, $hide_empty_groups = false, $fetch_fields = false, $fetch_field_data = false, $exclude_groups = false, $exclude_fields = false, $hide_empty_fields = false ) {
    2828                $this->groups = BP_XProfile_Group::get( array(
    2929                        'profile_group_id'  => $profile_group_id,
    3030                        'user_id'           => $user_id,
    3131                        'hide_empty_groups' => $hide_empty_groups,
     32                        'hide_empty_fields' => $hide_empty_fields,
    3233                        'fetch_fields'      => $fetch_fields,
    3334                        'fetch_field_data'  => $fetch_field_data,
     
    153154        global $bp, $profile_template;
    154155
     156        // Only show empty fields if we're on the Dashboard, or on a user's profile edit page
     157        $hide_empty_fields = ( !is_network_admin() && !is_admin() && !bp_is_user_profile_edit() );
     158
    155159        $defaults = array(
    156160                'user_id' => $bp->displayed_user->id,
    157161                'profile_group_id'  => false,
    158162                'hide_empty_groups' => true,
     163                'hide_empty_fields' => $hide_empty_fields,
    159164                'fetch_fields'      => true,
    160165                'fetch_field_data'  => true,
     
    166171        extract( $r, EXTR_SKIP );
    167172
    168         $profile_template = new BP_XProfile_Data_Template( $user_id, $profile_group_id, $hide_empty_groups, $fetch_fields, $fetch_field_data, $exclude_groups, $exclude_fields );
     173        $profile_template = new BP_XProfile_Data_Template( $user_id, $profile_group_id, $hide_empty_groups, $fetch_fields, $fetch_field_data, $exclude_groups, $exclude_fields, $hide_empty_fields );
    169174        return apply_filters( 'bp_has_profile', $profile_template->has_groups(), $profile_template );
    170175}
     
    190195        function bp_get_field_css_class( $class = false ) {
    191196                global $profile_template;
    192 
     197               
    193198                $css_classes = array();
    194199
Note: See TracChangeset for help on using the changeset viewer.