Skip to content

Instantly share code, notes, and snippets.

@ka215
Last active June 25, 2016 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ka215/84be407ccb052aec5e6624ad6b36c0d1 to your computer and use it in GitHub Desktop.
Save ka215/84be407ccb052aec5e6624ad6b36c0d1 to your computer and use it in GitHub Desktop.
ユーザーごとにenum型の登録値を合計して、ショートコードに一覧化するフィルターフックおよび集計関数の例。 デモはこちら -> http://demo.ka2.org/narrow-by-sum/
<?php
/**
* Function to aggregate the value of the enum type
*
* @param string $target_table [requied]
* @param string $userid_col [requied]
* @param string $enum_col [required]
* @param string $period [optional] It must be a target date string, ex."2016-06-23"
*/
function enum_aggregate( $target_table, $userid_col, $enum_col, $period=null ) {
global $cdbt;
if ( is_object( $cdbt ) && $cdbt->check_table_exists( $target_table ) ) {
$_schema = $cdbt->get_table_schema( $target_table );
$_enum_values = isset( $_schema[$enum_col] ) && preg_match('/^enum\(\'(.*)\'\)$/i', $_schema[$enum_col]['type_format'], $matchies ) ? explode( "','", $matchies[1] ) : [];
if ( empty( $_enum_values ) ) return;
$_data = $cdbt->run_query( sprintf( 'SELECT `%s` FROM `%s` GROUP BY `%s`', $userid_col, $target_table, $userid_col ), 'PDO' );
$_where_date = ! empty( $period ) ? sprintf( "AND `created` BETWEEN '%s 00:00:00' AND '%s 23:59:59'", $period, $period ) : '';
foreach ( $_data as $_i => $_row ) {
$_sum_col = 0;
foreach ( $_enum_values as $_val ) {
$_res = $cdbt->run_query( sprintf( "SELECT COUNT(*) FROM `%s` WHERE `%s`='%s' AND `%s`='%s' %s", $target_table, $userid_col, $_row[$userid_col], $enum_col, $_val, $_where_date ), 'PDO' );
$_data[$_i][$_val] = intval( $_res['COUNT(*)'] );
$_sum_col += intval( $_res['COUNT(*)'] );
}
$_data[$_i]['total'] = $_sum_col;
}
$_sum_row = $_data[0];
$_sum_row[$userid_col] = 'total';
$_sum_col = 0;
foreach ( $_enum_values as $_i => $_val ) {
$_sum_row[$_val] = 0;
foreach ( $_data as $_row ) {
$_sum_row[$_val] += $_row[$_val];
$_sum_col += $_row[$_val];
}
}
$_sum_row['total'] = $_sum_col;
$_data[] = $_sum_row;
} else {
$_data = false;
}
return $_data;
}
function render_enum_aggregate( $component_options, $shortcode_name, $table ) {
if ( ! is_admin() && 'your_target_table' === $table ) {
$component_options['data'] = enum_aggregate( 'your_target_table', 'userid_column', 'enum_column', '2016-06-23' );
}
return $component_options;
}
add_filter( 'cdbt_shortcode_custom_component_options', 'render_enum_aggregate', 10, 3 );
function custom_enum_aggregate_columns( $columns, $shortcode_name, $table ) {
if ( ! is_admin() && 'your_target_table' === $table ) {
// Exclude columns
foreach ( $columns as $_i => $_column ) {
$_custom_column = '';
switch( $_column['property'] ) {
case 'ID':
case 'enum_column':
case 'created':
case 'updated':
unset( $columns[$_i] );
break;
}
}
// Add new columns
global $cdbt;
$_schema = $cdbt->get_table_schema( $table );
$enum_col = 'enum_column';
$_new_cols = [];
$_enum_values = isset( $_schema[$enum_col] ) && preg_match('/^enum\(\'(.*)\'\)$/i', $_schema[$enum_col]['type_format'], $matchies ) ? explode( "','", $matchies[1] ) : [];
$_new_cols = array_merge( $_new_cols, $_enum_values, [ 'total' ] );
foreach ( $_new_cols as $_val ){
$columns = array_merge( $columns, [ [
'label' => $_val,
'property' => $_val,
'sortable' => true,
'sortDirection' => 'asc',
'dataNumric' => true,
'truncateStrings' => '0',
'className' => '',
] ] );
}
}
return $columns;
}
add_filter( 'cdbt_shortcode_custom_columns', 'custom_enum_aggregate_columns', 10, 3 );
/**
* I tested table structure below, then worked correctly.
*
CREATE TABLE `sum_enum` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
`user_id` varchar(10) NOT NULL DEFAULT '0',
`category` enum('aaa','bbb','ccc','ddd','eee') NOT NULL DEFAULT 'aaa',
`created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Created Datetime',
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Updated Datetime',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
*/
@ka215
Copy link
Author

ka215 commented Jun 25, 2016

Also, it's placing of search field to frontend as post content.

<form method="get" class="form-inline" style="margin-bottom: .5em;">
  <div class="input-group">
    <input type="search" name="period" class="form-control" placeholder="Narrow by date">
    <span class="input-group-btn"><button type="submit" class="btn btn-default">Narrow Down</button></span>
  </div>
</form>
[cdbt-view table="your_table_name" display_search="false" display_title="false" display_index_row="head-only" footer_interface="pager" sort_order="grouping_column_name:asc"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment