Skip to content

Commit

Permalink
Issue #145 - add totals= parameter to [bw_table] shortcode
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed Feb 9, 2020
1 parent dc62b7a commit af063e3
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 21 deletions.
34 changes: 20 additions & 14 deletions libs/bw_fields.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php // (C) Copyright Bobbing Wide 2013-2018
<?php // (C) Copyright Bobbing Wide 2013-2018, 2020
if ( !defined( "BW_FIELDS_INCLUDED" ) ) {
define( "BW_FIELDS_INCLUDED", "3.2.6" );
define( "BW_FIELDS_INCLUDED", "4.1.0" );

/**
* Library: bw_fields
Expand Down Expand Up @@ -65,7 +65,8 @@ function bw_custom_column_admin( $column, $post_id ) {
function bw_custom_column_taxonomy( $column, $post_id ) {
$terms = get_the_term_list( $post_id, $column, "", ",", "" );
$terms = apply_filters( "bw_custom_column_taxonomy", $terms, $column, $post_id );
e( $terms );
e( $terms );
return $terms;
}


Expand All @@ -78,9 +79,10 @@ function bw_custom_column_post_meta( $column, $post_id ) {
/* At least one value to format */
bw_format_custom_column( $column, $data );
} else {
$data = null;
bw_format_custom_column( $column );
}
$data = bw_format_custom_column( $column );

}
return $data;
}

/**
Expand All @@ -95,10 +97,11 @@ function bw_custom_column_post_meta( $column, $post_id ) {
function bw_custom_column( $column, $post_id ) {
$type = bw_query_field_type( $column );
if ( $type === "taxonomy" ) {
bw_custom_column_taxonomy( $column, $post_id );
$value = bw_custom_column_taxonomy( $column, $post_id );
} else {
bw_custom_column_post_meta( $column, $post_id );
}
$value = bw_custom_column_post_meta( $column, $post_id );
}
return $value;
}

/**
Expand Down Expand Up @@ -137,12 +140,13 @@ function bw_custom_column( $column, $post_id ) {
*/
function bw_format_custom_column( $column=null, $data=null ) {
// @TODO - this code can be replaced by bw_query_field_type(), perhaps

$value = null;
global $bw_fields;
$field = bw_array_get( $bw_fields, $column, null );
if ( $field ) {
bw_theme_field( $column, $data, $field );
}
$value = bw_theme_field( $column, $data, $field );
}
return $value;
}


Expand Down Expand Up @@ -172,6 +176,7 @@ function bw_pre_theme_field() {
* @param array $field - the field structure if defined using `bw_register_field()`
*/
function bw_theme_field( $key, $value, $field=null ) {
$field_value = null;
$type = bw_array_get( $field, "#field_type", null );
//bw_trace2( $type, "Type", true, BW_TRACE_DEBUG );
bw_pre_theme_field();
Expand All @@ -186,11 +191,12 @@ function bw_theme_field( $key, $value, $field=null ) {

if ( is_callable( $funcname ) ) {
//bw_trace2( $funcname, "funcname chosen", false );
call_user_func( $funcname, $key, $value, $field );
$field_value = call_user_func( $funcname, $key, $value, $field );
} else {
bw_trace2( $funcname, "funcname chosen not callable, using default _bw_theme_field_default", false );
_bw_theme_field_default( $key, $value, $field );
$field_value = _bw_theme_field_default( $key, $value, $field );
}
return $field_value;
}

/**
Expand Down
158 changes: 158 additions & 0 deletions libs/class-oik-csv-totals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php
if ( !defined( 'CLASS_OIK_CSV_TOTALS_INCLUDED' ) ) {
define( 'CLASS_OIK_CSV_TOTALS_INCLUDED', '0.0.0' );

/**
* @copyright Bobbing Wide 2020
* @package oik-bob-bing-wide
*
* Produces a totals rows for the CSV.
*
* Usage:
* ```
* csv_totals = new oik_csv_totals( 'c,t,a' );
* foreach row {
* csv_totals->row( array of column cells );
* }
* csv_totals->totals_row( 'Count:,Total:,Average:');
* ```
*/
class Oik_csv_totals {

private $rows=0; // Row count
private $totals=[]; // array of totalling actions - one for each column
private $columns=[]; // Array of column totals - for 't' or 'a'

/**
* Constructor for the oik_csv_totals class.
*
* @param string|array $totals Totalling actions for each column to be handled.
*/
function __construct( $totals ) {
$this->set_totals( $totals );
$this->columns=array_fill( 0, count( $this->totals ), 0 );
$this->rows( null );
}

/**
* Sets the totalling method for each column.
*
* @param string|mixed $totals Totalling method for each column to be totalled.
*/
function set_totals( $totals ) {
$this->totals=explode( ',', $totals );
foreach ( $this->totals as $key=>$total ) {
$total=trim( $total );
$total=strtolower( $total );
$total.=' ';
$total=$total[0];
switch ( $total ) {
case 's':
$total='t';
break;
case 't':
case 'a':
case '-':
case 'c':
break;

default:
$total='-';
}
$this->totals[ $key ]=$total;
}
}

/**
* Incremements, resets or returns the rows.
*
* @param bool|null $inc Option to increment, reset or return the row.
*/
function rows( $inc=true ) {
if ( $inc ) {
$this->rows ++;
} elseif ( null === $inc ) {
$this->rows=0;
}

return $this->rows;
}

/**
* Accumulates another table row.
*
* @param array $tablerow Array of cells for the row.
*/
function row( $tablerow ) {
$this->rows();
$this->columns( $tablerow );
}

/**
* Accumulates column total when required.
*
* @param array $tablerow array of cells for the row.
*/
function columns( $tablerow ) {
foreach ( $this->totals as $key=>$total ) {
$cell=$tablerow[ $key ];
switch ( $total ) {
case 'a':
case 't':
if ( is_numeric( $cell ) ) {
$this->columns[ $key ]+=$cell;
}
}
}
}

function column( $key, $cell ) {
if ( isset( $this->totals[ $key ] )) {
$total=$this->totals[ $key ];
switch ( $total ) {
case 'a':
case 't':
if ( is_array( $cell )) {
$cell = $cell[0];
}
if ( is_numeric( $cell ) ) {
$this->columns[ $key ]+=$cell;
}
}
}
}


/**
* Display the Totals row
*
* @param string| mixed $prefixes Optional prefixes for each cell in the totals row.
*/
function totals_row( $prefixes=null ) {
$prefixes=bw_as_array( $prefixes );
$totals =[];
foreach ( $this->totals as $key=>$total ) {
$cell='';
switch ( $total ) {
case 't':
$cell=$this->columns[ $key ];
break;

case 'a':
$cell=$this->columns[ $key ] / $this->rows( false );
break;

case 'c':
$cell=$this->rows( false );
break;
}
$prefix=isset( $prefixes[ $key ] ) ? $prefixes[ $key ] . ' ' : '';

$totals[]=$prefix . $cell;
}
bw_tablerow( $totals );
}

}

}
35 changes: 28 additions & 7 deletions shortcodes/oik-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ function bw_query_table_columns( $atts=null, $post_type ) {
* @param array $atts - shortcode parameters
*
*/
function bw_format_table_row( $post, $atts ) {
function bw_format_table_row( $post, $atts, $csv_totals ) {
global $field_arr;
$atts['post'] = $post;
//bw_trace2( $field_arr, "field_arr", false );
$index = 0;
bw_trace2( $field_arr, "field_arr", false );
stag( "tr" );
foreach ( $field_arr as $key => $value ) {

//bw_trace2( $key, "key", false );
//bw_trace2( $value, "value", false );
stag( "td", $value, $key );
Expand All @@ -125,10 +127,14 @@ function bw_format_table_row( $post, $atts ) {
$field_value = $post->$field_name;
bw_theme_field( $field_name, $field_value, $atts );
} else {
bw_trace2( $value );
bw_custom_column( $value, $post->ID );
}
bw_trace2( $value, 'value', false, BW_TRACE_VERBOSE );
$field_value = bw_custom_column( $value, $post->ID );
}
if ( $csv_totals) {
$csv_totals->column( $index, $field_value );
}
etag( "td" );
$index++;
}
etag( "tr");
}
Expand Down Expand Up @@ -159,13 +165,28 @@ function bw_format_table( $posts, $atts ) {
return;
}
}

$totals = bw_array_get( $atts, 'totals', null );
$csv_totals = null;
if ( $totals ) {
oik_require_lib( 'class-oik-csv-totals' );
$csv_totals = new Oik_csv_totals( $totals );
}
$cp = bw_current_post_id();
foreach ( $posts as $post ) {
bw_current_post_id( $post->ID );
if ( $excerpts )
$post->excerpt = bw_excerpt( $post );
bw_format_table_row( $post, $atts );
$post->excerpt = bw_excerpt( $post );
if ( $totals ) {
$csv_totals->rows();
}
bw_format_table_row( $post, $atts, $csv_totals );
}

if ( $csv_totals ) {
$prefixes = bw_array_get( $atts, 'prefixes', null );
$csv_totals->totals_row( $prefixes );
}
bw_current_post_id( $cp );
etag( "tbody" );
etag( "table" );
Expand Down

0 comments on commit af063e3

Please sign in to comment.