Last active
August 29, 2015 14:14
-
-
Save marushu/d0a619e306ceb913b23c to your computer and use it in GitHub Desktop.
フロントフォームから入力したユーザー(投稿者)一覧を出力する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function disease_data_list() { | |
echo get_disease_data_list(); | |
} | |
function get_disease_data_list() { | |
global $post; | |
$current_user = wp_get_current_user(); | |
$display_name = esc_html( $current_user->display_name ); | |
if( isset( $_GET[ 'post_id' ] ) || isset( $_GET[ 'user_id' ] ) ) { | |
$post_id = intval( $_GET[ 'post_id' ] ); | |
$user_id = intval( $_GET[ 'user_id' ] ); | |
} | |
//$year_num = ''; | |
if( isset( $_GET[ 'year_num' ] ) && ! empty( $_GET[ 'year_num' ] ) ) { | |
$year = $_GET[ 'year_num' ]; | |
} else { | |
$year = date_i18n( "Y" ); | |
} | |
?> | |
<h1 style="text-align: right;" id="post_title"><span>現在 <strong><?php echo $display_name; ?></strong> としてログイン中です。</span></h1> | |
<?php | |
if( is_user_logged_in() ) { // Display WordPress login form: | |
echo '<p class="input_logout">'; | |
wp_loginout( home_url( '/member-login' ) ); // Display "Log Out" link. | |
echo '</p>'; | |
} | |
?> | |
<h1><?php echo $year; ?>年集計</h1> | |
<div id="yealy"> | |
<?php | |
get_template_part( 'function/all_disease_by_year_list' ); | |
get_all_disease_data_list(); | |
?> | |
</div> | |
<?php | |
// 配列を準備 | |
$ope_for_soutaikan_num = 0; | |
// ユーザー一覧 | |
$args = array( | |
'role' => 'author', | |
'meta_key' => 'fetus_user_order', | |
'orderby' => 'meta_value', | |
'order' => 'ASC', | |
'count_total' => false, | |
'fields' => 'all', | |
); | |
$users = get_users( $args ); | |
foreach( $users as $user ) { | |
?> | |
<div class="hospital"> | |
<h2 class="entry-title"><?php echo esc_html( $user->display_name ); ?><span>表示</span></h2> | |
<?php | |
$args = array( | |
'post_type' => 'ope_summary', | |
'author' => $user->ID, | |
'posts_per_page' => -1, | |
); | |
$each_user_posts = get_posts( $args ); | |
$dis_names = array(); | |
$ope_name_wrap = array(); | |
$same_ope_name = array(); | |
$each_ope = array(); | |
$html = ''; | |
// tableヘッダ | |
$html .= '<div class="each_table"><table class="summary_datas each_hospital"><tbody><tr><th>症例名</th><th>術名</th><th>症例数</th></tr>'; | |
if( ! empty( $each_user_posts ) ) { | |
foreach( $each_user_posts as $post ) { | |
setup_postdata( $post ); | |
// その投稿の疾患名に対する手術名を取得し、疾患名(表示用)を取得 | |
$ope_disease = get_field( 'ope_disease', $post->ID ); | |
$ope_disease = esc_html( $ope_disease ); | |
// 手術名のカスタムフィールドkeyを作成 | |
$ope_name_key = 'ope_for_' . $ope_disease; | |
// 投稿に含まれる手術名を取得 ※ ここがカスタムタクソノミーになる!! | |
$ope_slug = get_post_meta( $post->ID, $ope_name_key, true ); | |
// 疾患名タクソノミーオブジェクト | |
$disease_term_obj = get_term_by( 'slug', $ope_disease, 'disease', OBJECT ); | |
$disease_name = esc_html( $disease_term_obj->name ); | |
$disease_count = (int)( $disease_term_obj->count ); | |
// 同名の疾患名をまとめる | |
if( ! in_array( $disease_name, $dis_names ) ) { | |
$dis_names[] = $disease_name; | |
} | |
// 手術名タクソノミーのオブジェクトをゲット | |
$ope_name_term_obj = get_term_by( 'slug', $ope_slug, 'ope_name', OBJECT ); | |
$ope_name = esc_html( $ope_name_term_obj->name ); | |
$ope_count = intval( $ope_name_term_obj->count ); | |
// 手術名タクソノミーをアップデート | |
$args = array( | |
'post_type' => 'ope_summary', | |
'posts_per_page' => -1, | |
'author' => $user->ID, | |
'tax_query' => array( | |
'relattion' => 'AND', | |
array( | |
'taxonomy' => 'disease', | |
'field' => 'slug', | |
'terms' => array( esc_html( $disease_term_obj->slug ) ), | |
), | |
array( | |
'taxonomy' => 'ope_name', | |
'field' => 'slug', | |
'terms' => array( esc_html( $ope_name_term_obj->slug ) ), | |
), | |
), | |
'meta_query' => array( | |
array( | |
'key' => 'ope_date', | |
'value' => "$year/01/01", | |
'type' => 'DATE', | |
'compare' => '>=', | |
), | |
array( | |
'key' => 'ope_date', | |
'value' => "$year/12/31", | |
'type' => 'DATE', | |
'compare' => '<=', | |
), | |
), | |
); | |
$query = get_posts( $args ); | |
$test_count = count( $query ); | |
foreach( $query as $result_post ) { | |
if( ! empty( $result_post ) ) { | |
$same_ope_name[ 'disease_name' ] = esc_html( $disease_name ); | |
$same_ope_name[ 'ope' ][ 'ope_name' ] = esc_html( $ope_name_term_obj->name ); | |
$same_ope_name[ 'ope' ][ 'test_count' ] = intval( $test_count ); | |
$ope_name_wrap[] = $same_ope_name; | |
} | |
} | |
} | |
wp_reset_postdata(); | |
$source = array_unique( $ope_name_wrap, SORT_REGULAR ); | |
$target = array(); | |
foreach ( $source as $item ) { | |
$id = $item[ 'disease_name' ]; | |
foreach ( $target as $skey=>$out ) { | |
if ( $out[ 'disease_name' ] === $id ) { | |
$outkey = $skey; | |
} | |
} | |
if ( ! isset( $outkey ) ) { | |
$each_ope[ 'ope_name' ] = $item[ 'ope' ][ 'ope_name' ]; | |
$each_ope[ 'test_count' ] = $item[ 'ope' ][ 'test_count' ]; | |
$item[ 'ope' ][] = $each_ope; | |
unset( $item[ 'ope' ][ 'ope_name' ] ); | |
unset( $item[ 'ope' ][ 'test_count' ] ); | |
$target[] = $item; | |
} else { | |
$target[ $outkey ][ 'ope' ][] = $item[ 'ope' ]; | |
unset( $outkey ); | |
} | |
} | |
// その他を最後に | |
foreach( $target as $pointer => $val ) { | |
foreach( $val[ 'ope' ] as $key => $data ) { | |
$check_string = mb_substr( $data[ 'ope_name' ], 0, 3 ); | |
if( $check_string === 'その他' ) { | |
unset( $target[ $pointer ][ 'ope' ][ $key ] ); | |
$target[ $pointer ][ 'ope' ][] = $data; | |
//array_values( $target[ $pointer ][ 'ope' ] ); | |
} | |
} | |
} | |
// テーブル作成 | |
foreach( $target as $dis_key => $ope ) { | |
// 疾患名tdのrowspan算出 | |
$rowspan_num = intval( count( $ope[ 'ope' ] ) ); | |
if( $rowspan_num >= 1 ) { | |
$html .= '<tr>'; | |
$html .= "<td class='dis_name' rowspan='" . $rowspan_num . "'>" . $ope[ 'disease_name' ] . "</td>"; | |
foreach( $ope[ 'ope' ] as $each_ope_name ) { | |
$html .= "<td class='ope_name'>" . $each_ope_name[ 'ope_name' ] . "</td>"; | |
$html .= "<td class='num'>" . $each_ope_name[ 'test_count' ] . "</td>"; | |
$html .= '</tr>'; | |
} | |
} else { | |
$html .= '<tr>'; | |
$html .= "<td class='dis_name'>" . $ope[ 'disease_name' ] . "</td>"; | |
$html .= "<td class='ope_name'>" . $ope[ 'ope' ][ 0 ][ 'ope_name' ] . "</td>"; | |
$html .= "<td class='num'>" . $ope[ 'ope' ][ 0 ][ 'test_count' ] . "</td>"; | |
$html .= '</tr>'; | |
} | |
} | |
$html .= '</tr></tbody></table></div>'; | |
echo $html; | |
} else { | |
echo "<div class='each_table'><p>まだデータはありません。</p></div>"; | |
} | |
?> | |
</div><!-- / .hospital --> | |
<?php } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment