View difference between Paste ID: EK86mCR7 and Lh3Xs6iP
SHOW: | | - or go back to the newest paste.
1
<?php
2
/*** code à copier dans le functions.php du thème actif à partir d'ici ***/
3
4
5
/**
6
* on utilise ici un hook disponible dans le template archive.php de bp-default
7
* en cas de thème différent, il suffit d'ajouter <?php do_action( 'bp_before_archive' );?> 
8
* dans le template category.php ou archive.php
9
*/
10
11
add_action( 'bp_before_archive', 'chritchan_list_user_for_category');
12
13
function chritchan_list_user_for_category() {
14
	if( !is_category() )
15
		return false;
16
	
17
	/* 
18
	d'abord on ne sait jamais.. si un post était attaché à plusieurs catégories..
19
	autant utiliser un tableau pour stocker tous les ids de catégorie.
20
	*/
21
	foreach( ( get_the_category() ) as $category) { 
22
	    $cat_args[] = $category->cat_ID; 
23
	}
24
		
25
	if( !is_array( $cat_args ) && count( $cat_args ) < 1 )
26
		return false;
27
	
28
	$cats = implode(',', $cat_args );
29
	
30
	// on définit les arguments à passer au members loop
31
	$member_args = array( 'meta_key' => 'chritchan_dept', 'meta_value' => $cats );
32
	
33
	?>
34
	<div id="chritchan-members">
35
	
36
		<?php if ( bp_has_members( $member_args ) ) : ?>
37
38
			<ul id="members-list" class="item-list" role="main">
39
40
			<?php while ( bp_members() ) : bp_the_member(); ?>
41
42
				<li>
43
					<div class="item-avatar">
44
						<a href="<?php bp_member_permalink(); ?>"><?php bp_member_avatar(); ?></a>
45
					</div>
46
47
					<div class="item">
48
						<a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?></a>
49
					</div>
50
51
					<div class="clear"></div>
52
				</li>
53
54
			<?php endwhile; ?>
55
56
			</ul>
57
58
		<?php else: ?>
59
60
			<!-- on affiche rien ! -->
61
			
62
		<?php endif; ?>
63
		
64
	</div>
65
	<?php
66
}
67
68
/**
69
* on modifie la requête de la fonction BP_Core_User::get_users
70
*********************************************************************
71
* Attention cette méthode sera dépréciée à partir de BuddyPress 1.7 *
72
*********************************************************************
73
* voir ce code pour un exemple des modifications à envisager à partir de la 1.7 :
74
* https://github.com/imath/bp-mystery-man-hunt/blob/master/includes/cubepoints.php#L125
75
*/
76
77
add_filter('bp_core_get_paged_users_sql', 'bpmmh_cubepoints_umeta_filter_select', 10, 2);
78
79
function bpmmh_cubepoints_umeta_filter_select($query, $sql) {
80
	/* check the meta before modifying the query...*/
81
	if( !empty($sql['where_meta']) && strpos( $sql['where_meta'], 'chritchan_dept') && strpos( $sql['where_meta'], ',') ) {
82
		
83
		preg_match( '/umm.meta_value \= \'([0-9,]+)\'/', $sql['where_meta'], $meta_match );
84
		
85
		$sql['where_meta'] = str_replace( $meta_match[0], 'umm.meta_value IN ('. $meta_match[1] .')', $sql['where_meta']);
86
		
87
		$query = join( ' ', (array)$sql );
88
	}
89
90
	return $query;
91
92
}
93
94
95
96
/*** fin de l'ajout des membres dans template category ***/
97
98
99
100
/*** pour mémoire l'ancien code ***/
101
function chritchan_get_select_cat( $selected_id = false ) {
102
103
	$args = array( 
104
			'orderby'    => 'name', /* ou id ou count .. */
105
			'order'      => 'ASC', /* ou DESC */
106
			'hide_empty' => 0 /* ou 1 si on ne veut que des catégories ayant des articles rattachés */
107
		);
108
		
109
	// si custom post type utiliser l'identifiant de la custom taxo au lieu de category
110
	$cats = get_terms( 'category', $args );
111
	
112
	$select = '<select name="_dept_chritchan"><option value="0">Choisir</option>';
113
	
114
	foreach( $cats as $term ) {
115
		$selected = false;
116
		
117
		if( $selected_id == $term->term_id )
118
			$selected = 'selected';
119
		
120
		$select .= '<option value="'.$term->term_id.'" '.$selected.' >'.$term->name.'</option>';
121
	}
122
	
123
	$select .= '</select>';
124
	
125
	// si on veut modifier par un filtre le select box
126
	return apply_filters('chritchan_get_select_cat', $select, $cats );
127
}
128
129
function edit_chritchan_select_cat() {
130
	global $bp;
131
	
132
	$user_id = $bp->displayed_user->id;
133
	
134
	$selected_id = get_user_meta( $user_id, 'chritchan_dept' ,true );
135
	
136
	if( bp_is_user_profile_edit() ) {
137
		?>
138
		
139
		<label for="_dept_chritchan">Votre département</label>
140
		<?php echo chritchan_get_select_cat( $selected_id );?>
141
		<?php
142
		
143
	} else {
144
		
145
		$term_id = get_user_meta( $user_id, 'chritchan_dept' ,true );
146
		
147
		if( empty( $term_id ) )
148
			return false;
149
150
		// changer category pour l'identifiant de la custom taxo
151
		$cat = get_term_by( 'id', $term_id, 'category' );
152
153
		?>
154
		<table class="profile-fields">
155
			
156
			<tr<?php bp_field_css_class(); ?>>
157
158
				<td class="label">Les articles de votre département</td>
159
160
				<td class="data"><a href="<?php echo get_term_link( $cat, 'category' );?>"><?php echo $cat->name?></a></td>
161
162
			</tr>
163
		</table>
164
		<?php
165
	}
166
	
167
}
168
169
add_action( 'bp_after_profile_field_content', 'edit_chritchan_select_cat');
170
171
function save_chritchan_select_cat( $user_id, $posted_field_ids, $errors) {
172
	if( !empty( $_POST['_dept_chritchan'] ) )
173
		update_user_meta($user_id, 'chritchan_dept', intval( $_POST['_dept_chritchan'] ) );
174
		
175
	if( $_POST['_dept_chritchan'] == 0 )
176
		delete_user_meta( $user_id, 'chritchan_dept' );
177
}
178
179
add_action('xprofile_updated_profile', 'save_chritchan_select_cat', 10, 3);
180
181
/*** fin de la copie ***/
182
183
?>