Skip to content
Permalink
135b674836
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
341 lines (302 sloc) 13.4 KB
<?php
Class UC_People_Widget extends WP_Widget {
private $fields = array('title','department','about','file','email','phone','phone_(alternate)','fax','mailing_address','office_location','campus','office_hours','courses','url');
public function __construct(){
parent::__construct( 'uc_people_widget', 'UC People', array( 'classname' => __CLASS__, 'description' => 'A widget to display People' ) );
}
private static function sort_by_last_then_first($a,$b) {
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 ){
$r = strnatcasecmp( iconv('UTF-8', 'ASCII//TRANSLIT', get_field('first_name', $a)), iconv('UTF-8', 'ASCII//TRANSLIT', get_field('first_name', $b)) );
}
return $r;
}
private static function sort_group_by_order( $a, $b ){
// Getting the order for groups $a and $b
$groupA = get_term_by('slug', $a, 'group')->term_id;
$array_orderGroupA = get_option( "taxonomy_{$groupA}" );
$orderGroupA = $array_orderGroupA['group_order'];
$groupB = get_term_by('slug', $b, 'group')->term_id;
$array_orderGroupB = get_option( "taxonomy_{$groupB}" );
$orderGroupB = $array_orderGroupB['group_order'];
$r = strnatcasecmp( $orderGroupA, $orderGroupB );
if( $r === 0 ){
$r = strnatcasecmp( $a, $b );
}
return $r;
}
private function createTextCell( $field, $information_to_display, $id ){
$cell = '';
if (in_array($field, $information_to_display)) {
$cell .= '<td class="person-'.$field.'">'.get_field($field, $id ).'</td>';
}
return $cell;
}
private function get_people_grid( $args, $information_to_display ){
$the_query = new WP_Query( $args );
$out = '';
if( $the_query->have_posts() ){
$out = '<div class="row">';
$person_count = 1;
$people = $the_query->get_posts();
usort( $people, array('UC_People_Widget', 'sort_by_last_then_first') );
foreach( $people as $id ){
$out .= '<div class="col-sm-3 personcount-'.$person_count.'" id="person-'.$id.'">';
$out .= '<div class="person">';
$out .= '<a href="'.get_the_permalink( $id ).'" class="person-permalink">';
if (in_array('photo', $information_to_display)) {
$out .= '<div class="person-thumbnail">'.get_the_post_thumbnail( $id, 'large').'</div>';
};
if (in_array('first_name', $information_to_display) || in_array('middle_name', $information_to_display) || in_array('last_name', $information_to_display)){
$out .= '<h4 class="person-name">';
if (in_array('first_name', $information_to_display)) {
$out .= get_field('first_name', $id ).' ';
};
if (in_array('middle_name', $information_to_display)) {
$out .= get_field('middle_name', $id ).' ';
};
if (in_array('last_name', $information_to_display)) {
$out .= get_field('last_name', $id );
};
$out .= '</h4>';
};
$out .= '</a>';
foreach( $this->fields as $f ){
if( $f == 'file' && in_array('file', $information_to_display) ){
$file = get_field('file', $id );
$out .= '<p class="person-file"><a href="'.$file[url].'">'.$file[title].'</a>'.'</p>';
} elseif( $f == 'email' && in_array( 'email', $information_to_display ) ){
$out .= '<p class="person-email"><a href="mailto:'.get_field('email', $id ).'">'.get_field('email', $id ).'</a>'.'</p>';
} else if( $f == 'title' && in_array( 'title', $information_to_display ) ){
$out .= '<p class="person-title">';
$titlePieces = explode(";", get_field('title', $id ));
foreach( $titlePieces as $t ){
$out .= ( !empty($t) ? "<em>{$t}</em><br>": "" );
}
$out .= '</p>';
} else if( $f == 'department' && in_array( 'department', $information_to_display ) ){
$out .= '<p class="person-department">';
$departmentPieces = explode(";", get_field('department', $id ));
foreach( $departmentPieces as $d ){
$out .= ( !empty($d) ? "<em>{$d}</em><br>": "" );
}
$out .= '</p>';
} else if( $f == 'mailing_address' && in_array( 'mailing_address', $information_to_display ) ){
$out .= '<p class="person-'.$f.'">'.str_replace(';','<br>',get_field($f, $id )).'</p>';
} elseif( $f == 'url' && in_array( 'url', $information_to_display ) ){
$out .= '<p class="person-url"><a href="'.get_field('url', $id ).'">'.get_field('url', $id ).'</a>'.'</p>';
} else {
if ( in_array($f, $information_to_display) ) {
$out .= '<p class="person-'.$f.'">'.get_field($f, $id ).'</p>';
};
}
}
$out .= '</div></div>'; // div.person and div.col-sm-3
// check to see if we need to close this row...
if ($person_count % 4 == 0){
// if this is the 4th, 8th, 16th, etc person...
$out .= '</div><!-- /row --><div class="row">';
}
$person_count++;
}
$out .= '</div>'; // div.row
} else {
} //end of if posts
return $out;
}
private function get_people_table( $args, $information_to_display ){
$the_query = new WP_Query( $args );
$out = '';
if ( $the_query->have_posts() ) {
$people = $the_query->get_posts();
usort( $people, array('UC_People_Widget', 'sort_by_last_then_first') );
// how many columns? Based on what they chose to display.
$num_cols = count($information_to_display);
$out .= '<table class="table table-striped">';
$out.= '<legend class="sr-only">List of People</legend>';
$out .= '<thead><tr>';
$nameCell = false;
foreach($information_to_display as $value){
if ($value == 'first_name' || $value == 'middle_name' || $value == 'last_name'){
if ($nameCell == false){
$out .= '<th>Name</th>';
$nameCell = true;
};
} elseif ($value == 'photo') {
$out .= '<th>&nbsp;</th>';
} else {
$arr = get_field_object('field_'.$value);
$label = $arr['label'];
$out .= '<th>'.$label.'</th>';
}
}
$out .= '</tr></thead>';
$out .= '<tbody>';
foreach( $people as $id ){
$out .= '<tr>';
if (in_array('photo', $information_to_display)) {
$out .= '<td class="person-thumbnail"><a href="'.get_the_permalink( $id ).'">'.get_the_post_thumbnail($id, array('65', '65')).'</a></td>';
}
if (in_array('first_name', $information_to_display) || in_array('middle_name', $information_to_display) || in_array('last_name', $information_to_display)){
$out .= '<td class="person-name"><a href="'.get_the_permalink( $id ).'">';
if (in_array('first_name', $information_to_display)) {
$out .= get_field('first_name', $id );
$out .= ' ';
};
if (in_array('middle_name', $information_to_display)) {
$out .= get_field('middle_name', $id );
$out .= ' ';
};
if (in_array('last_name', $information_to_display)) {
$out .= get_field('last_name', $id );
};
$out .= '</a></td>';
}
foreach( $this->fields as $f ){
if( $f == 'file' && in_array('file', $information_to_display) ){
$file = get_field('file', $id );
$out .= '<td class="person-file"><a href="'.$file[url].'">'.$file[title].'</a></td>';
} else if( $f == 'email' && in_array('email', $information_to_display) ){
$out .= '<td class="person-email"><a href="mailto:'.get_field('email', $id ).'">'.get_field('email', $id ).'</a></td>';
} else if( $f == 'title' && in_array('title', $information_to_display) ){
$out .= '<td class="person-title">';
$titlePieces = explode(";", get_field('title', $id ));
foreach( $titlePieces as $t ){
$out .= ( !empty($t) ? "<strong>{$t}</strong><br>": "" );
}
$out .= '</td>';
} else if( $f == 'department' && in_array('department', $information_to_display) ){
$out .= '<td class="person-department">';
$departmentPieces = explode(";", get_field('department', $id ));
foreach( $departmentPieces as $d ){
$out .= ( !empty($d) ? "<strong>{$d}</strong><br>": "" );
}
$out .= '</td>';
} else if( $f == 'mailing_address' && in_array('mailing_address', $information_to_display) ){
$out .= '<td class="person-'.$f.'">'.str_replace(';','<br>',get_field($f, $id )).'</td>';
// If the value($f) is a phone number than we should replace any hyphens with the html entity for a non-break hyphen (&#8209;)
} elseif ($f == 'phone' && in_array('phone', $information_to_display) ) {
$phoneStrReplace = str_replace('-', '&#8209;', get_field('phone', $id ));
$out .= '<td>'.$phoneStrReplace.'</td>';
} elseif( $f == 'url' && in_array( 'url', $information_to_display ) ){
$out .= '<td class="person-url"><a href="'.get_field('url', $id ).'">'.get_field('url', $id ).'</a>'.'</td>';
} else {
$out .= $this->createTextCell($f, $information_to_display, $id );
}
}
$out .= '</tr>';
}
$out .= '</tbody>';
$out .= '</table>';
} else {
// no posts found
}
return $out;
}
public function widget( $args, $instance ){
$settings = array( 'layout' => '', 'specific_people' => '', 'specific_groups' => '', 'specific_tags' => '', 'break_into_groups' => '', 'information_to_display' => '');
foreach( $settings as $k => $v ){
$settings[$k] = get_field( $k, 'widget_' . $args['widget_id'] );
}
$wpQueryArgs = array(
'posts_per_page' => -1,
'post_type' => 'person',
'post__in' => ( ( !is_null($settings['specific_people']) ) ? $settings['specific_people'] : array() ),
'fields' => 'ids'
);
if ( $settings['specific_groups'] != false || $settings['specific_tags'] != false ){
$tax_query = array();
$isGroupSet = false;
$isTagSet = false;
if ( $settings['specific_groups'] != false ) {
$isGroupSet = true;
$taxGroupArray = array(
'taxonomy' => 'group',
'field' => 'term_id',
'terms' => $settings['specific_groups']
);
$tax_query[] = $taxGroupArray;
}
if ( $settings['specific_tags'] != false ) {
$isTagSet = true;
$taxTagArray = array(
'taxonomy' => 'persontag',
'field' => 'term_id',
'terms' => $settings['specific_tags']
);
$tax_query[] = $taxTagArray;
}
$wpQueryArgs['tax_query'] = $tax_query;
}
echo '<div class="widget uc-people-widget">';
if( $settings['break_into_groups'] != false ){
// get all the people
$the_query = new WP_Query( $wpQueryArgs );
// focusing on just the people, not the rest of the query.
$people = $the_query->posts;
// look at each person, and see each group used, use that to create a list of terms.
$groups_in_use = array();
foreach($people as $value){
//$id = $value->ID;
$id = $value;
$terms = get_the_terms($id, 'group');
if (is_array($terms)){
foreach($terms as $key => $v){
//duplicates will overwrite themselves...
$slug = $v->slug;
$groups_in_use[$slug]= $slug;
}
};
}
// sort the groups in use alphabetically
//sort($groups_in_use);
usort($groups_in_use, array('UC_People_Widget', 'sort_group_by_order') );
// for each item in the list of terms, do a new query, but this time we're going to limit it to ONLY items from that one group.
foreach($groups_in_use as $value){
// this sets up the query we'll need, and will overwrite any group queries from the original settings.
$tax_query = array();
$group_query = array(
'taxonomy' => 'group',
'field' => 'slug',
'terms' => $value
);
array_push($tax_query, $group_query);
if( $isTagSet != false ){
array_push($tax_query, $taxTagArray);
}
$wpQueryArgs['tax_query'] = $tax_query;
// with $arg updated, let's do a new query.
echo '<h3 class="group-title">'.get_term_by('slug', $value, 'group')->name.'</h3>';
if( $settings['layout'] == 'grid' ){
echo $this->get_people_grid( $wpQueryArgs, $settings['information_to_display'] );
} else if( $settings['layout'] == 'table' ){
echo $this->get_people_table( $wpQueryArgs, $settings['information_to_display'] );
}
}
} else {
if( $settings['layout'] == 'grid' ){
echo $this->get_people_grid( $wpQueryArgs, $settings['information_to_display'] );
} else if( $settings['layout'] == 'table' ){
echo $this->get_people_table( $wpQueryArgs, $settings['information_to_display'] );
}
}
echo '</div>';
}
public function form( $instance ){
//Form is handled by ACF, code can be found in wp-content/plugins/uc-people/acf-export.php
}
public function update( $new_instance, $old_instance ){
//Update is handled by ACF
}
}
function register_uc_people_widget(){
register_widget( 'UC_People_Widget' );
}
add_action( 'widgets_init', 'register_uc_people_widget' );
function remove_uc_people_from_sopanels( $widgets ){
unset($widgets['UC_People_Widget']);
return $widgets;
}
add_filter( 'siteorigin_panels_widgets', 'remove_uc_people_from_sopanels', 11);
?>