Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Updating people sorting (again)
  • Loading branch information
briandunnigan authored and briandunnigan committed Mar 18, 2016
1 parent 1c5f8ba commit f281599
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 44 deletions.
87 changes: 48 additions & 39 deletions functions.php
Expand Up @@ -42,7 +42,7 @@ if ( function_exists('register_sidebar') ) {
) );
}

function neag_sort_by_last_then_first( $a, $b ){
function neag_sort_by_last_then_first( $a, $b ){ // a and b are people IDs
setlocale(LC_CTYPE, 'en_US.UTF8'); // Using setlocale and iconv incase a name has an accent or other similar characters in them
$r = strnatcasecmp( iconv('UTF-8', 'ASCII//TRANSLIT', get_field('last_name', $a)), iconv('UTF-8', 'ASCII//TRANSLIT', get_field('last_name', $b)) );
if( $r === 0 ){
Expand All @@ -51,44 +51,42 @@ function neag_sort_by_last_then_first( $a, $b ){
return $r;
}

function processPosts( $queryPosts, $queryHavePosts, $expertsFlag = false ){
function processPosts( $queryPosts, $expertsFlag = false ){
usort( $queryPosts, 'neag_sort_by_last_then_first' );

$fields = array( 'first_name' => '', 'middle_name' => '', 'last_name' => '', 'title' => '', 'email' => '', 'phone' => '', 'office_location' => '', 'expertise' => '' );

$alphabet = array_fill_keys( range('A', 'Z'), '' );

$people = array();
if( $queryHavePosts ){
foreach( $queryPosts as $i=>$id ){
$people[$i] = array();

foreach( $fields as $k=>$v ){
$fields[$k] = get_field( $k, $id );
}

$people[$i]['name'] = "{$fields['first_name']} {$fields['middle_name']} {$fields['last_name']}";
$people[$i]['permalink'] = get_permalink( $id );
$people[$i]['phone'] = $fields['phone'];
$people[$i]['title'] = $fields['title'];
$people[$i]['email'] = $fields['email'];
$people[$i]['office_location'] = $fields['office_location'];
$people[$i]['photo'] = get_the_post_thumbnail( $id, array(115, 115) );
$people[$i]['expertise'] = $fields['expertise'];

$nameLetter = substr( $fields['last_name'], 0, 1 );

$setDivIDFlag = false;
if( empty( $alphabet[$nameLetter] ) ){
$setDivIDFlag = true;
$alphabet[$nameLetter] = true;
}
if( $setDivIDFlag == true ){
$people[$i]['divid'] = $nameLetter;
} else {
$people[$i]['divid'] = '';
}
}
foreach( $queryPosts as $i=>$id ){
$people[$i] = array();

foreach( $fields as $k=>$v ){
$fields[$k] = get_field( $k, $id );
}

$people[$i]['name'] = "{$fields['first_name']} {$fields['middle_name']} {$fields['last_name']}";
$people[$i]['permalink'] = get_permalink( $id );
$people[$i]['phone'] = $fields['phone'];
$people[$i]['title'] = $fields['title'];
$people[$i]['email'] = $fields['email'];
$people[$i]['office_location'] = $fields['office_location'];
$people[$i]['photo'] = get_the_post_thumbnail( $id, array(115, 115) );
$people[$i]['expertise'] = $fields['expertise'];

$nameLetter = $fields['last_name'][0]; // $fields['last_name'] is a string

$setDivIDFlag = false;
if( empty( $alphabet[$nameLetter] ) ){
$setDivIDFlag = true;
$alphabet[$nameLetter] = true;
}
if( $setDivIDFlag == true ){
$people[$i]['divid'] = $nameLetter;
} else {
$people[$i]['divid'] = '';
}
}

return $people;
Expand All @@ -102,6 +100,7 @@ function neag_get_people_callback(){
$args = array(
'post_type' => array( 'person' ),
'fields' => 'ids',
'posts_per_page' => -1,
);

if( $categoryID != 0 ){
Expand All @@ -117,9 +116,14 @@ function neag_get_people_callback(){

$query = new WP_Query( $args );
$queryPosts = $query->posts;
$queryHavePosts = $query->have_posts();

echo wp_json_encode( processPosts( $queryPosts, $queryHavePosts ) );
$people = array();

if( $query->have_posts() ){
$people = processPosts( $queryPosts );
}

echo wp_json_encode( $people );

wp_die();
}
Expand All @@ -130,17 +134,22 @@ function neag_get_experts_callback(){
$args = array(
'post_type' => array( 'person' ),
'fields' => 'ids',
'posts_per_page' => -1,
'meta_key' => 'expertise',
'meta_compare' => '!=',
'meta_value' => ' ',
'posts_per_page' => -1,
);

$query = new WP_Query( $args );
$queryPosts = $query->posts;
$queryHavePosts = $query->have_posts();

echo wp_json_encode( processPosts( $queryPosts, $queryHavePosts, true ) );
$queryPosts = $query->posts;

$people = array();

if( $query->have_posts() ){
$people = processPosts( $queryPosts, true );
}

echo wp_json_encode( $people );

wp_die();
}
Expand Down
8 changes: 4 additions & 4 deletions js/custom.js
Expand Up @@ -39,7 +39,7 @@ jQuery(document).ready(function($) {
},
};

mod.setFaculty = function ( categoryID, categoryName ){
mod.setFaculty = function ( categoryID, categoryName ){
ajaxReqSettings.data = {
'action': 'neag_get_people',
'categoryID': categoryID
Expand Down Expand Up @@ -120,7 +120,7 @@ jQuery(document).ready(function($) {
if( displayExpertiseFlag ){
var expertiseDiv = $('<div />', {
class: 'directory-expertise',
text: v.expertise
text: ((v.expertise) ? v.expertise: '')
}).appendTo( colNameTitle );
$('<span />', {
class: 'expertise',
Expand Down Expand Up @@ -183,7 +183,7 @@ jQuery(document).ready(function($) {
var categoryID = category[0].value;

if( categoryID != 0 ){ // categoryID == 0 is All Faculty & Staff, which loads by default
if( categoryID == "faculty-expert" ){
if( categoryID === "faculty-expert" ){
directory.setFacultyExperts();
} else {
directory.setFaculty( categoryID, hash );
Expand All @@ -203,7 +203,7 @@ jQuery(document).ready(function($) {
window.location.hash = "";
}

if( this.value == "faculty-expert" ){
if( this.value === "faculty-expert" ){
directory.setFacultyExperts();
} else {
directory.setFaculty( this.value, categoryName );
Expand Down
2 changes: 1 addition & 1 deletion js/min/custom.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f281599

Please sign in to comment.