From f28159916ab4f5fd008984e9c7f7f1f8bd400dc8 Mon Sep 17 00:00:00 2001 From: briandunnigan Date: Fri, 18 Mar 2016 15:46:15 -0400 Subject: [PATCH] Updating people sorting (again) --- functions.php | 87 ++++++++++++++++++++++++-------------------- js/custom.js | 8 ++-- js/min/custom.min.js | 2 +- 3 files changed, 53 insertions(+), 44 deletions(-) diff --git a/functions.php b/functions.php index a21a53c..ccbf753 100644 --- a/functions.php +++ b/functions.php @@ -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 ){ @@ -51,7 +51,7 @@ 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' => '' ); @@ -59,36 +59,34 @@ function processPosts( $queryPosts, $queryHavePosts, $expertsFlag = false ){ $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; @@ -102,6 +100,7 @@ function neag_get_people_callback(){ $args = array( 'post_type' => array( 'person' ), 'fields' => 'ids', + 'posts_per_page' => -1, ); if( $categoryID != 0 ){ @@ -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(); } @@ -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(); } diff --git a/js/custom.js b/js/custom.js index deaf390..b54aa94 100644 --- a/js/custom.js +++ b/js/custom.js @@ -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 @@ -120,7 +120,7 @@ jQuery(document).ready(function($) { if( displayExpertiseFlag ){ var expertiseDiv = $('
', { class: 'directory-expertise', - text: v.expertise + text: ((v.expertise) ? v.expertise: '') }).appendTo( colNameTitle ); $('', { class: 'expertise', @@ -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 ); @@ -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 ); diff --git a/js/min/custom.min.js b/js/min/custom.min.js index 2c62a65..a856d30 100644 --- a/js/min/custom.min.js +++ b/js/min/custom.min.js @@ -1 +1 @@ -jQuery(document).ready(function(e){if(e("#directory-dropdown").length>0){var t=!1;screen.width>767&&(t=!0,e(".directory-face-wrap").each(function(t,a){var i=e(a).height(),o=e(a).find(".directory-name-title-wrap"),r=o.height();i>r&&o.css("padding-top",(i-r)/2)}));var a=function(){function a(a,i,o){i.empty(),e.each(a,function(a,r){var n=e("
",{id:r.divid,"class":"directory-person"}).appendTo(i),c=e("
",{"class":"row"}).appendTo(n),s=e("
",{"class":"col-sm-5 directory-face-wrap"}).appendTo(c),d=e("
",{"class":"directory-photo"}).appendTo(s);e("",{href:r.permalink,html:r.photo}).appendTo(d);var p=e("
",{"class":"directory-name-title-wrap"}).appendTo(s),l=e("
",{"class":"directory-name"}).appendTo(p);if(e("",{href:r.permalink,text:r.name}).appendTo(l),e("
",{"class":"directory-title",text:r.title}).appendTo(p),o){var h=e("
",{"class":"directory-expertise",text:r.expertise}).appendTo(p);e("",{"class":"expertise",text:"Expertise: "}).prependTo(h)}var u=e("
",{"class":"col-sm-3 directory-contact-wrap"}).appendTo(c);e("
",{"class":"directory-phone",text:r.phone}).appendTo(u);var f=e("
",{"class":"directory-email"}).appendTo(u),v=(e("",{href:"mailto:"+r.email,text:r.email}).appendTo(f),e("
",{"class":"col-sm-4 directory-office-wrap"}).appendTo(c));if(e("
",{"class":"directory-officeLabel",text:"Office"}).appendTo(v),e("
",{"class":"directory-office",text:r.office_location}).appendTo(v),t){var y=s.height(),g=p.height();y>g&&p.css("padding-top",(y-g)/2)}})}var i={},o=e("#directory-list"),r=e("#directory-list-loading-text"),n={type:"POST",url:neag_object.ajaxurl,timeout:1e4,beforeSend:function(){r.show()},complete:function(e,t){"success"===t?r.hide():"timeout"===t?r.text("Request has timed out, please refresh this page"):r.text("Error: "+t)}};return i.setFaculty=function(t,i){n.data={action:"neag_get_people",categoryID:t},n.success=function(e){e=JSON.parse(e),0!=Object.keys(e).length&&(-1!=i.indexOf("Faculty Expert")?a(e,o,!0):a(e,o))},e.ajax(n)},i.setFacultyExperts=function(){n.data={action:"neag_get_experts"},n.success=function(e){e=JSON.parse(e),0!=Object.keys(e).length&&a(e,o,!0)},e.ajax(n)},i}(),i=window.location.hash;if(i.length>1){i=i.replace(/_/g," ").substr(1);var o=e("#directory-dropdownMenu").children("option:contains('"+i+"')");if(0!=o.length){e("#directory-dropdownMenu")[0].selectedIndex=o[0].index;var r=o[0].value;0!=r&&("faculty-expert"==r?a.setFacultyExperts():a.setFaculty(r,i))}}e("#directory-dropdownMenu").on("change",function(e){e.preventDefault();var t=this.options[this.options.selectedIndex].text;window.location.hash=0!=this.value?"#"+t.replace(/\s/g,"_"):"","faculty-expert"==this.value?a.setFacultyExperts():a.setFaculty(this.value,t)})}var n=new Date,c=n.getMonth()+1,s=n.getFullYear();e(".widget_archives").append("

Archives ›

"),e("#page-news .panel-grid:nth-child(5) .panel-grid-cell:nth-child(1) .widget_siteorigin-panels-postloop").append("

News Archives ›

"),e("h4.panel-title a").addClass("accordion-closed"),e("h4.panel-title a").click(function(){e(this).addClass("accordion-open")})}); \ No newline at end of file +jQuery(document).ready(function(e){if(e("#directory-dropdown").length>0){var t=!1;screen.width>767&&(t=!0,e(".directory-face-wrap").each(function(t,a){var i=e(a).height(),o=e(a).find(".directory-name-title-wrap"),r=o.height();i>r&&o.css("padding-top",(i-r)/2)}));var a=function(){function a(a,i,o){i.empty(),e.each(a,function(a,r){var n=e("
",{id:r.divid,"class":"directory-person"}).appendTo(i),c=e("
",{"class":"row"}).appendTo(n),s=e("
",{"class":"col-sm-5 directory-face-wrap"}).appendTo(c),d=e("
",{"class":"directory-photo"}).appendTo(s);e("",{href:r.permalink,html:r.photo}).appendTo(d);var p=e("
",{"class":"directory-name-title-wrap"}).appendTo(s),l=e("
",{"class":"directory-name"}).appendTo(p);if(e("",{href:r.permalink,text:r.name}).appendTo(l),e("
",{"class":"directory-title",text:r.title}).appendTo(p),o){var h=e("
",{"class":"directory-expertise",text:r.expertise?r.expertise:""}).appendTo(p);e("",{"class":"expertise",text:"Expertise: "}).prependTo(h)}var u=e("
",{"class":"col-sm-3 directory-contact-wrap"}).appendTo(c);e("
",{"class":"directory-phone",text:r.phone}).appendTo(u);var f=e("
",{"class":"directory-email"}).appendTo(u),v=(e("",{href:"mailto:"+r.email,text:r.email}).appendTo(f),e("
",{"class":"col-sm-4 directory-office-wrap"}).appendTo(c));if(e("
",{"class":"directory-officeLabel",text:"Office"}).appendTo(v),e("
",{"class":"directory-office",text:r.office_location}).appendTo(v),t){var y=s.height(),x=p.height();y>x&&p.css("padding-top",(y-x)/2)}})}var i={},o=e("#directory-list"),r=e("#directory-list-loading-text"),n={type:"POST",url:neag_object.ajaxurl,timeout:1e4,beforeSend:function(){r.show()},complete:function(e,t){"success"===t?r.hide():"timeout"===t?r.text("Request has timed out, please refresh this page"):r.text("Error: "+t)}};return i.setFaculty=function(t,i){n.data={action:"neag_get_people",categoryID:t},n.success=function(e){e=JSON.parse(e),0!=Object.keys(e).length&&(-1!=i.indexOf("Faculty Expert")?a(e,o,!0):a(e,o))},e.ajax(n)},i.setFacultyExperts=function(){n.data={action:"neag_get_experts"},n.success=function(e){e=JSON.parse(e),0!=Object.keys(e).length&&a(e,o,!0)},e.ajax(n)},i}(),i=window.location.hash;if(i.length>1){i=i.replace(/_/g," ").substr(1);var o=e("#directory-dropdownMenu").children("option:contains('"+i+"')");if(0!=o.length){e("#directory-dropdownMenu")[0].selectedIndex=o[0].index;var r=o[0].value;0!=r&&("faculty-expert"===r?a.setFacultyExperts():a.setFaculty(r,i))}}e("#directory-dropdownMenu").on("change",function(e){e.preventDefault();var t=this.options[this.options.selectedIndex].text;window.location.hash=0!=this.value?"#"+t.replace(/\s/g,"_"):"","faculty-expert"===this.value?a.setFacultyExperts():a.setFaculty(this.value,t)})}var n=new Date,c=n.getMonth()+1,s=n.getFullYear();e(".widget_archives").append("

Archives ›

"),e("#page-news .panel-grid:nth-child(5) .panel-grid-cell:nth-child(1) .widget_siteorigin-panels-postloop").append("

News Archives ›

"),e("h4.panel-title a").addClass("accordion-closed"),e("h4.panel-title a").click(function(){e(this).addClass("accordion-open")})}); \ No newline at end of file