<?php
/**
 * @file
 * Defines how the fields table in the Bulk Loader Template Edit form should be
 *   rendered.
 *
 * @param $element
 *   The FAPI definition of the records table.
 */

// generate table\
$record_header = ['class' => ['record'], 'data' => 'Record'];
$header = [
  $record_header,
  'Field Operations',
  'Field Name',
  'Chado Table',
  'Chado Field',
  'Field Type',
  'Field Settings',
]; //'Data Column', 'Constant Value', 'Referred Record');
$rows = [];

// This is an array to keep track of the record => field index as well as the number of fields.
// It is used to make the record column span all the field rows providing more room for 
// information and a visual separation between records.
// Expect: record_id => array(index1, index2, index3)
$field_record = [];

// Create a row for each sub-element that is not a form-api key (ie: #title).
foreach (element_children($element) as $key) {

  $row_element = &$element[$key];
  $row = [];

  // Describe the record.
  $row[] = [
    'class' => ['record'],
    'data' => drupal_render($row_element['record_id']),
  ];
  if (isset($field_record[$row_element['record_id']['#markup']])) {
    $field_record[$row_element['record_id']['#markup']][] = $key;
  }
  else {
    $field_record[$row_element['record_id']['#markup']] = [$key];
  }

  // Provide action links to interact with fields.
  $row[] = [
    'class' => ['tbl-action-field-links', 'active'],
    'data' => drupal_render($row_element['edit_submit']) . ' | '
      . drupal_render($row_element['delete_submit']) . '<br />'
      . drupal_render($row_element['view-record-link']),
  ];

  // Describe the field.
  $row[] = drupal_render($row_element['field_name']);
  $row[] = drupal_render($row_element['chado_table_name']);
  $row[] = drupal_render($row_element['chado_field_name']);

  // Determine the Type
  // Default to data field.
  $type = 'Data Field (Spreadsheet Value)';
  $icon = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAFs0lEQVR4Xu2dPW8UMRCGWQo+Cj4UCYmWIoL67kqKdAho+QEgOhokGpIqSgU0SGnoUPgBaSGko6C8vRpEQRslUkSg4KMgvA5B4nadeGSNdLvWs9Jx4Twe26/f9Y7H43V14vAajUY39/f3H+q/A30u/fud76IQ2FFrJlVVrY7H443Qsir8MxwOn+rrcVFNpTEpBJ7Vdb1YHd75b1LSpJeHgEaCW5Xu/rdq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4AfAD4AfoFozDCBMA5kGMg1kGmgYKiwirAayGsj7ASK2B8vBmcvBHBhhGXcjMjq4oYgDIzKbT7auI2D1BHa9HdQvEwEIkAlcKdkgQCk9mdkOCJAJXCnZIEApPZnZDgiQCVwp2SBAKT2Z2Q4IkAlcKdmsBNhRg587NnpBum4Y9K1LpjbIWUWWJZh6PV14XduKVaFBbiiZOwa5Tcm8M8hZRR5J8FJK2EoAIoJSSB6RTkRQBBg2hkTZQkBIExbCwjOHHWUjHiA+8rAc3MAFG6BNFMLCIzcPRmDmaIwRiBFIWHiTA8wCmAWwPbzNAaaBTAPZFzDFAfwAmZYnfoA4cGwPb+OCHwA/AKeGNTiAIwhHUH0x/+k7nRNHEI4gHEE4gng/wBQH8ATiCcQT2DNPYDi+9bOXYSQ9c/ok49Uks6XPnmO589KVOk41nI7yybHMC9J12aAvxF3uGuSsIlckGE4rOfay+gFSekjvKQIQoKcd51VtCOCFZE/1QICedpxXtQMBto0GmVeZ6OkOAjuBAG9VH8sune5Um5p4IbBZjUajm1p3f+OlET39QUAvsLpVhepqFHiqr8f9qTo1dUDgWV3XiwcECNfhSPBQfw6wCRzg7aaK4Gya6M5fHY/HG6GKfwCXsRp7BtTkxgAAAABJRU5ErkJggg==';
  $type_class = 'data';
  $value = drupal_render($row_element['column_num']);
  if (!empty($row_element['constant_value']['#markup'])) {
    $type = 'Constant';
    $icon = 'data:image/png;base64,[encoded data, 4360 characters]';
    $type_class = 'constant';
    $value = drupal_render($row_element['constant_value']);
  }
  if (!empty($row_element['foreign_record_id']['#markup'])) {
    $type = 'Record Referral';
    $icon = 'data:image/png;base64,[encoded data, 6040 characters]';
    $type_class = 'record-referral';
    $value = drupal_render($row_element['foreign_record_id']);
  }
  $row[] = [
    'data' => '<img src="' . $icon . '" width=16 height=16 title="' . $type . '"/>',
    'class' => ['field-type'],
  ];

  // Finally specify the field value.

  $row[] = $value;

  // Add this field to the table.
  $rows[] = [
    'class' => [$type_class],
    'data' => $row,
  ];

}

// Finally edit the table to make the record span the correct rows.
foreach ($field_record as $record_id => $keys) {
  foreach ($keys as $row_order => $row_key) {

    // We want to adjust the first row of this record to have the rowspan.
    if ($row_order == 0) {
      $rows[$row_key]['data'][0]['rowspan'] = sizeof($keys);
      $rows[$row_key]['class'][] = 'record-first-row';
    }
    // Then we want to delete the record cell in the other rows.
    else {
      unset($rows[$row_key]['data'][0]);
    }
  }
}

// Finally print the table.
print theme(
  'table',
  [
    'header' => $header,
    'rows' => $rows,
    //'attributes' => array('style'=>'table-layout: fixed; width: 100%')
  ]
);
?>
