<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][4869] trunk: 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.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/4869">4869</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2011-07-26 17:26:30 +0000 (Tue, 26 Jul 2011)</dd>
</dl>

<h3>Log Message</h3>
<pre>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 <a href="http://buddypress.trac.wordpress.org/ticket/3074">#3074</a>. Props johnjamesjacoby for help with the patch</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpcorebpcoretemplatephp">trunk/bp-core/bp-core-template.php</a></li>
<li><a href="#trunkbpxprofilebpxprofileclassesphp">trunk/bp-xprofile/bp-xprofile-classes.php</a></li>
<li><a href="#trunkbpxprofilebpxprofiletemplatephp">trunk/bp-xprofile/bp-xprofile-template.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpcorebpcoretemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-template.php (4868 => 4869)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-template.php        2011-07-26 13:30:54 UTC (rev 4868)
+++ trunk/bp-core/bp-core-template.php        2011-07-26 17:26:30 UTC (rev 4869)
</span><span class="lines">@@ -650,6 +650,10 @@
</span><span class="cx"> 
</span><span class="cx">         $is_current_component = false;
</span><span class="cx"> 
</span><ins>+        // Backward compatibility: 'xprofile' should be read as 'profile'
+        if ( 'xprofile' == $component )
+                $component = 'profile';
+
</ins><span class="cx">         if ( !empty( $bp-&gt;current_component ) ) {
</span><span class="cx"> 
</span><span class="cx">                 // First, check to see whether $component_name and the current
</span><span class="lines">@@ -1022,7 +1026,7 @@
</span><span class="cx"> 
</span><span class="cx">         if ( bp_is_current_component( 'xprofile' ) &amp;&amp; bp_is_current_action( 'edit' ) )
</span><span class="cx">                 return true;
</span><del>-
</del><ins>+                
</ins><span class="cx">         return false;
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkbpxprofilebpxprofileclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-xprofile/bp-xprofile-classes.php (4868 => 4869)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-xprofile/bp-xprofile-classes.php        2011-07-26 13:30:54 UTC (rev 4868)
+++ trunk/bp-xprofile/bp-xprofile-classes.php        2011-07-26 17:26:30 UTC (rev 4869)
</span><span class="lines">@@ -99,6 +99,7 @@
</span><span class="cx">          *                'profile_group_id' - Limit results to a single profile group
</span><span class="cx">          *                'user_id' - Required if you want to load a specific user's data
</span><span class="cx">          *                'hide_empty_groups' - Hide groups without any fields
</span><ins>+         *                'hide_empty_fields' - Hide fields where the user has not provided data
</ins><span class="cx">          *                'fetch_fields' - Load each group's fields
</span><span class="cx">          *                'fetch_field_data' - Load each field's data. Requires a user_id
</span><span class="cx">          *                'exclude_groups' - Comma-separated list of groups to exclude
</span><span class="lines">@@ -113,6 +114,7 @@
</span><span class="cx">                         'profile_group_id'  =&gt; false,
</span><span class="cx">                         'user_id'           =&gt; $bp-&gt;displayed_user-&gt;id,
</span><span class="cx">                         'hide_empty_groups' =&gt; false,
</span><ins>+                        'hide_empty_fields' =&gt; false,
</ins><span class="cx">                         'fetch_fields'      =&gt; false,
</span><span class="cx">                         'fetch_field_data'  =&gt; false,
</span><span class="cx">                         'exclude_groups'    =&gt; false,
</span><span class="lines">@@ -161,27 +163,66 @@
</span><span class="cx">                         foreach( (array)$fields as $field )
</span><span class="cx">                                 $field_ids[] = $field-&gt;id;
</span><span class="cx"> 
</span><del>-                        $field_ids = implode( ',', (array) $field_ids );
</del><ins>+                        $field_ids_sql = implode( ',', (array) $field_ids );
</ins><span class="cx"> 
</span><span class="cx">                         if ( !empty( $field_ids ) )
</span><del>-                                $field_data = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT field_id, value FROM {$bp-&gt;profile-&gt;table_name_data} WHERE field_id IN ( {$field_ids} ) AND user_id = %d&quot;, $user_id ) );
-
-                        if ( !empty( $field_data ) ) {
</del><ins>+                                $field_data = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT field_id, value FROM {$bp-&gt;profile-&gt;table_name_data} WHERE field_id IN ( {$field_ids_sql} ) AND user_id = %d&quot;, $user_id ) );
+                                
+                        // Remove data-less fields, if necessary
+                        if ( $hide_empty_fields ) {
+                        
+                                // Loop through the results and find the fields that have data.
+                                foreach( (array)$field_data as $data ) {
+                                        if ( false !== $key = array_search( $data-&gt;field_id, $field_ids ) ) {
+                                                // Fields that have data get removed from the list
+                                                unset( $field_ids[$key] );
+                                        }
+                                }
+                                
+                                // The remaining members of $field_ids are empty. Remove them.
+                                foreach( $fields as $field_key =&gt; $field ) {
+                                        if ( in_array( $field-&gt;id, $field_ids ) ) {
+                                                unset( $fields[$field_key] );
+                                        }
+                                }
+                                
+                                // Reset indexes
+                                $fields = array_values( $fields );
+                                
+                        }
+                        
+                        // Field data was found
+                        if ( !empty( $field_data ) &amp;&amp; !is_wp_error( $field_data ) ) {
+                                
+                                // Loop through fields
</ins><span class="cx">                                 foreach( (array)$fields as $field_key =&gt; $field ) {
</span><ins>+                                        
+                                        // Loop throught the data in each field
</ins><span class="cx">                                         foreach( (array)$field_data as $data ) {
</span><ins>+                                        
+                                                // Assign correct data value to the field
</ins><span class="cx">                                                 if ( $field-&gt;id == $data-&gt;field_id )
</span><span class="cx">                                                         $fields[$field_key]-&gt;data-&gt;value = $data-&gt;value;
</span><del>-                                        }
-                                }
</del><ins>+                                        }        
+                                }        
</ins><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 // Merge the field array back in with the group array
</span><del>-                foreach( (array)$groups as $group_key =&gt; $group ) {
-                        foreach( (array)$fields as $field ) {
</del><ins>+                foreach( (array) $groups as $group_key =&gt; $group ) {
+                        foreach( (array) $fields as $field ) {
</ins><span class="cx">                                 if ( $group-&gt;id == $field-&gt;group_id )
</span><span class="cx">                                         $groups[$group_key]-&gt;fields[] = $field;
</span><span class="cx">                         }
</span><ins>+                        
+                        // When we unset fields above, we may have created empty groups.
+                        // Remove them, if necessary.
+                        if ( empty( $group-&gt;fields ) &amp;&amp; $hide_empty_groups ) {
+                                unset( $groups[$group_key] );
+                        }
+                        
+                        // Reset indexes
+                        $groups = array_values( $groups );
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 return $groups;
</span></span></pre></div>
<a id="trunkbpxprofilebpxprofiletemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-xprofile/bp-xprofile-template.php (4868 => 4869)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-xprofile/bp-xprofile-template.php        2011-07-26 13:30:54 UTC (rev 4868)
+++ trunk/bp-xprofile/bp-xprofile-template.php        2011-07-26 17:26:30 UTC (rev 4869)
</span><span class="lines">@@ -20,15 +20,16 @@
</span><span class="cx">         var $in_the_loop;
</span><span class="cx">         var $user_id;
</span><span class="cx"> 
</span><del>-        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 ) {
-                $this-&gt;__construct( $user_id, $profile_group_id, $hide_empty_groups, $fetch_fields, $fetch_field_data, $exclude_groups, $exclude_fields );
</del><ins>+        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 ) {
+                $this-&gt;__construct( $user_id, $profile_group_id, $hide_empty_groups, $fetch_fields, $fetch_field_data, $exclude_groups, $exclude_fields, $hide_empty_fields );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        function __construct( $user_id, $profile_group_id, $hide_empty_groups = false, $fetch_fields = false, $fetch_field_data = false, $exclude_groups = false, $exclude_fields = false ) {
</del><ins>+        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 ) {
</ins><span class="cx">                 $this-&gt;groups = BP_XProfile_Group::get( array(
</span><span class="cx">                         'profile_group_id'  =&gt; $profile_group_id,
</span><span class="cx">                         'user_id'           =&gt; $user_id,
</span><span class="cx">                         'hide_empty_groups' =&gt; $hide_empty_groups,
</span><ins>+                        'hide_empty_fields' =&gt; $hide_empty_fields,
</ins><span class="cx">                         'fetch_fields'      =&gt; $fetch_fields,
</span><span class="cx">                         'fetch_field_data'  =&gt; $fetch_field_data,
</span><span class="cx">                         'exclude_groups'    =&gt; $exclude_groups,
</span><span class="lines">@@ -152,10 +153,14 @@
</span><span class="cx"> function bp_has_profile( $args = '' ) {
</span><span class="cx">         global $bp, $profile_template;
</span><span class="cx"> 
</span><ins>+        // Only show empty fields if we're on the Dashboard, or on a user's profile edit page
+        $hide_empty_fields = ( !is_network_admin() &amp;&amp; !is_admin() &amp;&amp; !bp_is_user_profile_edit() );
+
</ins><span class="cx">         $defaults = array(
</span><span class="cx">                 'user_id' =&gt; $bp-&gt;displayed_user-&gt;id,
</span><span class="cx">                 'profile_group_id'  =&gt; false,
</span><span class="cx">                 'hide_empty_groups' =&gt; true,
</span><ins>+                'hide_empty_fields' =&gt; $hide_empty_fields,
</ins><span class="cx">                 'fetch_fields'      =&gt; true,
</span><span class="cx">                 'fetch_field_data'  =&gt; true,
</span><span class="cx">                 'exclude_groups'    =&gt; false, // Comma-separated list of profile field group IDs to exclude
</span><span class="lines">@@ -165,7 +170,7 @@
</span><span class="cx">         $r = wp_parse_args( $args, $defaults );
</span><span class="cx">         extract( $r, EXTR_SKIP );
</span><span class="cx"> 
</span><del>-        $profile_template = new BP_XProfile_Data_Template( $user_id, $profile_group_id, $hide_empty_groups, $fetch_fields, $fetch_field_data, $exclude_groups, $exclude_fields );
</del><ins>+        $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 );
</ins><span class="cx">         return apply_filters( 'bp_has_profile', $profile_template-&gt;has_groups(), $profile_template );
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -189,7 +194,7 @@
</span><span class="cx"> }
</span><span class="cx">         function bp_get_field_css_class( $class = false ) {
</span><span class="cx">                 global $profile_template;
</span><del>-
</del><ins>+                
</ins><span class="cx">                 $css_classes = array();
</span><span class="cx"> 
</span><span class="cx">                 if ( $class )
</span></span></pre>
</div>
</div>

</body>
</html>