diff --git a/acf-export.php b/acf-export.php index 34d0a2f..a8bce2b 100644 --- a/acf-export.php +++ b/acf-export.php @@ -28,7 +28,7 @@ if(function_exists("register_field_group")) 'name' => 'specific_people', 'type' => 'relationship', 'instructions' => 'Choose specific people to display. Leave right side blank to display all People.', - 'return_format' => 'object', + 'return_format' => 'id', 'post_type' => array ( 0 => 'person', ), @@ -120,6 +120,13 @@ if(function_exists("register_field_group")) 'group_no' => 0, ), ), + array ( + array ( + 'param' => 'widget', + 'operator' => '==', + 'value' => 'uc_people_widget', + ), + ), ), 'options' => array ( 'position' => 'normal', @@ -131,7 +138,7 @@ if(function_exists("register_field_group")) )); } -//adds boxes to the "People" pages. +//adds boxes to the "People" pages. if(function_exists("register_field_group")) { register_field_group(array ( diff --git a/uc-people-widget.php b/uc-people-widget.php new file mode 100644 index 0000000..e33a72b --- /dev/null +++ b/uc-people-widget.php @@ -0,0 +1,257 @@ + __CLASS__, 'description' => 'A widget to display People' ) ); + } + + private function get_people_grid( $args, $information_to_display ){ + $the_query = new WP_Query( $args ); + $out = ''; + if( $the_query->have_posts() ){ + $out = '
'; + $person_count = 1; + + while( $the_query->have_posts() ){ + $the_query->the_post(); + $out .= '
'; + $out .= '
'; + + $out .= ''; + if (in_array('photo', $information_to_display)) { + $out .= '
'.get_the_post_thumbnail('large').'
'; + }; + if (in_array('first_name', $information_to_display) || in_array('middle_name', $information_to_display) || in_array('last_name', $information_to_display)){ + $out .= '

'; + if (in_array('first_name', $information_to_display)) { + $out .= get_field('first_name').' '; + }; + if (in_array('middle_name', $information_to_display)) { + $out .= get_field('middle_name').' '; + }; + if (in_array('last_name', $information_to_display)) { + $out .= get_field('last_name'); + }; + $out .= '

'; + }; + $out .= '
'; + + foreach( $this->fields as $f ){ + if( $f == 'file' && in_array('file', $information_to_display) ){ + $file = get_field('file'); + $out .= '

'.$file[title].''.'

'; + } elseif( $f == 'email' && in_array( 'email', $information_to_display ) ){ + $out .= '

'.get_field('email').''.'

'; + } else if( $f == 'title' && in_array( 'title', $information_to_display ) ){ + $out .= '

'.get_field('title').'

'; + } else { + if ( in_array($f, $information_to_display) ) { + $out .= '

'.get_field($f).'

'; + }; + } + } + $out .= '
'; // 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... + echo '
'; + } + $person_count++; + } //end of while posts + $out .= '
'; // div.row + } else { + + } //end of if posts + + return $out; + } + + private function createTextCell( $field, $information_to_display ){ + $cell = ''; + if (in_array($field, $information_to_display)) { + $cell .= ''.get_field($field).''; + } + return $cell; + } + + private function get_people_table( $args, $information_to_display ){ + $the_query = new WP_Query( $args ); + $out = ''; + if ( $the_query->have_posts() ) { + // how many columns? Based on what they chose to display. + $num_cols = count($information_to_display); + + $out .= ''; + $out.= 'List of People'; + $out .= ''; + + $nameCell = false; + foreach($information_to_display as $value){ + if ($value == 'first_name' || $value == 'middle_name' || $value == 'last_name'){ + if ($nameCell == false){ + $out .= ''; + $nameCell = true; + }; + } elseif ($value == 'photo') { + $out .= ''; + } else { + $arr = get_field_object('field_'.$value); + $label = $arr['label']; + $out .= ''; + } + } + $out .= ''; + $out .= ''; + + while ( $the_query->have_posts() ) { + $the_query->the_post(); + + $out .= ''; + if (in_array('photo', $information_to_display)) { + $out .= ''; + } + if (in_array('first_name', $information_to_display) || in_array('middle_name', $information_to_display) || in_array('last_name', $information_to_display)){ + $out .= ''; + } + + foreach( $this->fields as $f ){ + if( $f == 'file' && in_array('file', $information_to_display) ){ + $file = get_field('file'); + $out .= ''; + } else { + $out .= $this->createTextCell($f, $information_to_display); + } + } + $out .= ''; + } //end of posts + $out .= ''; + $out .= '
Name '.$label.'
'.the_post_thumbnail(array('65', '65')).''; + if (in_array('first_name', $information_to_display)) { + $out .= get_field('first_name'); + $out .= ' '; + }; + if (in_array('middle_name', $information_to_display)) { + $out .= get_field('middle_name'); + $out .= ' '; + }; + if (in_array('last_name', $information_to_display)) { + $out .= get_field('last_name'); + }; + $out .= ''.$file[title].''; + } else if( $f == 'email' && in_array('email', $information_to_display) ){ + $out .= ''.get_field('email').'
'; + } 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', + 'orderby' => 'menu_order title', + 'order' => 'ASC', + 'post__in' => ( ( !is_null($settings['specific_people']) ) ? $settings['specific_people'] : array() ) + ); + + if ( $settings['specific_groups'] != false || $settings['specific_tags'] != false ){ + $tax_query = array(); + + if ( $settings['specific_groups'] != false ) { + $tax_query[] = array( + 'taxonomy' => 'group', + 'field' => 'term_id', + 'terms' => $settings['specific_groups'] + ); + } + if ( $settings['specific_tags'] != false ) { + $tax_query[] = array( + 'taxonomy' => 'persontag', + 'field' => 'term_id', + 'terms' => $settings['specific_tags'] + ); + } + $wpQueryArgs['tax_query'] = $tax_query; + } + + 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); + + // 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); + $wpQueryArgs['tax_query'] = $tax_query; + + // with $arg updated, let's do a new query. + echo '

'.get_term_by('slug', $value, 'group')->name.'

'; + + 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'] ); + } + } + } + + 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' ); + +?> \ No newline at end of file diff --git a/uc-people.php b/uc-people.php index 6925df3..e86d172 100644 --- a/uc-people.php +++ b/uc-people.php @@ -12,7 +12,7 @@ if ( ! defined( 'WPINC' ) ) { } require 'acf-export.php'; require_once(WP_CONTENT_DIR . '/ldap.php'); - +include 'uc-people-widget.php'; function create_custom_custom_post_type(){ $labels = array(