You are here

function views_aggregator_tally in Views Aggregator Plus 8

Same name and namespace in other branches
  1. 7 views_aggregator_functions.inc \views_aggregator_tally()

Aggregates a field group as the tally of its members.

Parameters

array $groups: An array of groups of rows, each group indexed by group value.

object $field_handler: The handler for the view column to find members of the group.

string $separator_group: The separator to use between tallies in a group, defaults to '<br/>'.

string $separator_column: The separator to use between tallies in the totals field, defaults to '<br/>'.

Return value

array An array of values, one for each group.

1 string reference to 'views_aggregator_tally'
ViewsAggregatorResultsTest::testGroupResultFunctions in tests/src/Functional/Plugin/ViewsAggregatorResultsTest.php
Test the group functions.

File

./views_aggregator_functions.inc, line 670
views_aggregator_functions.inc

Code

function views_aggregator_tally(array $groups, $field_handler, $separator_group, $separator_column) {
  $separator_group = empty($separator_group) ? '<br/>' : $separator_group;
  $separator_column = empty($separator_column) ? '<br/>' : $separator_column;
  $values = [
    'column' => [],
  ];
  $is_new_php = version_compare(phpversion(), '5.4.0', '>=');
  foreach ($groups as $group => $rows) {
    $tally = [];
    foreach ($rows as $num => $row) {
      $cell = trim(views_aggregator_get_cell($field_handler, $num, TRUE));
      if (empty($cell)) {

        // Not tallying empty values.
        $values[$group] = NULL;
        break;
      }
      if (isset($tally[$cell])) {
        $tally[$cell]++;
      }
      else {
        $tally[$cell] = 1;
      }
    }
    if (count($tally) > 1) {

      // With more than one field in a group the fields no longer belong to one
      // particular entity. Cannot support hyper-linking, so switch it off.
      // Unfortunately this applies to the entire column.
      unset($field_handler->options['link_to_node']);
    }
    if ($is_new_php) {
      ksort($tally, SORT_NATURAL | SORT_FLAG_CASE);
    }
    else {
      ksort($tally);
    }
    $rendered_tally = [];
    foreach ($tally as $cell => $count) {
      $rendered_tally[] = "{$cell} ({$count})";
    }
    $values[$group] = implode($separator_group, $rendered_tally);
  }
  return $values;
}