You are here

function fc_matrix_field_cardinality in Field Complete 7

2 string references to 'fc_matrix_field_cardinality'
matrix_custom.inc in plugins/fields/matrix_custom.inc
matrix_text.inc in plugins/fields/matrix_text.inc

File

plugins/fields/matrix.inc, line 22
Field Complete - Provides field-based completeness for any entity - matrix field plugin.

Code

function fc_matrix_field_cardinality($function, $items, $instance, $field) {

  // Create the matrix, if not fixed size we have to calculate
  // the size first. And it's simpler just to do it anyway.
  $cols = $rows = 0;
  foreach ($items as $item) {
    $cols = $item['col'] > $cols ? $item['col'] : $cols;
    $rows = $item['row'] > $rows ? $item['row'] : $rows;
  }
  if ($field['settings']['cols_count'] == -1) {
    $field['settings']['cols_count'] = $cols;
  }
  if ($field['settings']['rows_count'] == -1) {
    $field['settings']['rows_count'] = $rows;
  }
  $row = array_fill(0, $field['settings']['cols_count'], FALSE);
  $matrix = array_fill(0, $field['settings']['rows_count'], $row);

  // Fill in the values
  $function($items, $instance, $field, $matrix);
  $completed = FALSE;
  switch ($instance['settings']['fc']['fc_check_cells']) {

    // As long as something is set...
    case MATRIX_COMPLETE_ANY:
      foreach ($matrix as $col => $row) {
        if (in_array(TRUE, $row)) {
          $completed = TRUE;
          break;
        }
      }
      break;

    // Everything must be set...
    case MATRIX_COMPLETE_ALL:
      $completed = TRUE;
      foreach ($matrix as $col => $row) {
        if (in_array(FALSE, $row)) {
          $completed = FALSE;
          break;
        }
      }
      break;

    // At least one row must be set...
    case MATRIX_COMPLETE_ROW:
      foreach ($matrix as $col => $row) {
        if (!in_array(FALSE, $row)) {
          $completed = TRUE;
          break;
        }
      }
      break;

    // At least one col must be set...
    case MATRIX_COMPLETE_COL:

      // Transpose the matrix
      $xitram = array();
      foreach ($matrix as $i => $row) {
        foreach ($row as $j => $v) {
          $xitram[$j][$i] = $v;
        }
      }
      $matrix = $xitram;

      // Now just check the rows
      foreach ($matrix as $col => $row) {
        if (!in_array(FALSE, $row)) {
          $completed = TRUE;
          break;
        }
      }
      break;
  }
  return $completed;
}