kevinhowbrook
5/10/2017 - 11:17 PM

DG table rendering

DG table rendering

<?php

/**
 * @file
 * Callback functions defining custom DS fields.
 */


// Get parent term function
function dg_fields_taxonomy_get_parents($tid) {
  $taxonomy_hierarchy = db_query('SELECT tid, parent FROM {taxonomy_term_hierarchy} WHERE parent != 0')->fetchAllKeyed();
  $parent_tids = array();
  while (!empty($taxonomy_hierarchy[$tid])) {
    $tid = $taxonomy_hierarchy[$tid];
    $parent_tids[] = $tid;
  }
  return $parent_tids;
} 

function dg_advisories_helper_full_table($field){
   $entity = $field['entity'];
   // Building a table

   // Building the table headers
   $th = array();
   $first_col_header = (isset($entity->field_first_column_header['und'][0]['value']) ? $entity->field_first_column_header['und'][0]['value'] : '');
   // Prep the field collection table header
   foreach ($entity->field_ca_table_header as $key => $value) {
     foreach ($value as $key => $id) {
        $fci_wrapper = entity_metadata_wrapper('field_collection_item', $id['value']);
        $fci_group_col_group_header = $fci_wrapper->field_fc_table_group_header->value();
        $fci_group_col_header =  $fci_wrapper->field_fc_table_column_header->value();
        $item_array['group_title'] = ($fci_group_col_group_header != NULL ? $fci_group_col_group_header : 'title');
        $item_array['items'] = $fci_group_col_header;
        array_push($th, $item_array);
     }
   }
   // Wrap in markup the table header
   $th_render = '';
   foreach ($th as $key => $value) {
       $an_item = '<ul><span class="th-group-header">' . $value['group_title'] . '</span>';
       foreach ($value['items'] as $key => $item) {
         $an_item .= "<li> $item </li>";
       }
       $an_item .= '</ul>';
       $th_render .= $an_item;
   }
   $th_render = '<div class="adv_table th_wrapper"><div class="th_wrapper left_title">'. $first_col_header . '</div><div class="th_wrapper th_headers"> '  . $th_render . '</div></div>';

   // Get the rows
   krumo($entity);
   $rows = $entity->field_ca_advisory_rows[LANGUAGE_NONE];
   $parent_terms_container = array();
   $rows_container = '';
   $first_filter = '';
   foreach ($rows as $key => $row) {
    // Set the wrapper for the row up
    $row_metadata_wrapper = entity_metadata_wrapper('advisories', $row['target_id']);
     //Push the rows parent term to the array for the filters
     // Will need making Unique somehow

    // Problem here if there is no row term set 
     $parent_term = dg_fields_taxonomy_get_parents($row_metadata_wrapper->field_advisory_row_country->tid->value());
     //krumo($row_metadata_wrapper->field_advisory_row_country->tid->value());
     $parent_term_wrapper = entity_metadata_wrapper('taxonomy_term', $parent_term[0]);
     array_push($parent_terms_container, '<a class="'. drupal_clean_css_identifier($parent_term_wrapper->name->value()) . '">' .  $parent_term_wrapper->name->value() . '</a>');
      // Set a variable to contain the cell values
      $cells_in_row = '<ul>';

      /*
      *  Formatting the cells themselves with the fields
      * The field_column refs are individually numbered and aren't in a great order
      * So some natsorting is needed
      */
      $holder_for_cell_refs = array();
      foreach ($row_metadata_wrapper->getPropertyInfo() as $key => $value) {
        $pos = strpos($key, 'field_advisory_row_column');
         if($pos !== false){
           array_push($holder_for_cell_refs, $key);
        }  
      }
      natsort($holder_for_cell_refs);
      $holder_for_cell_refs = array_values($holder_for_cell_refs);
      // We only want as many cells as specified on the row entity:
      $cell_limit = $row_metadata_wrapper->field_column_number->value() - 1;
      foreach ($holder_for_cell_refs as $key => $value) {
        if($key > $cell_limit){
          unset($holder_for_cell_refs[$key]);
        }
      }

      //krumo($holder_for_cell_refs);
      foreach ($holder_for_cell_refs as $key => $value) {
        $col_id = $row_metadata_wrapper->$value->label();
        $cell_entity_wrapper = entity_metadata_wrapper('advisories', $col_id); 
        $cell_id = $cell_entity_wrapper->label();
        $cell_status = $cell_entity_wrapper->field_advisory_col_status->value();
        $cell_icon = $cell_entity_wrapper->field_advisory_col_icon->value();
        $cell_col_content = $cell_entity_wrapper->field_column_content->value()['value'];
        $cell_color = $cell_entity_wrapper->field_value_colour_->value();
        $cell_dropdown = $cell_entity_wrapper->field_cell_dropdown->value()['value'];
        $cell_note_link = $cell_entity_wrapper->field_section_link->value();
        $cell = array();
        array_push($cell, $cell_id .  $cell_status . $cell_icon . $cell_col_content . $cell_color . $cell_dropdown . $cell_note_link);
        $cell =  implode(' | ', $cell);
        $cells_in_row .= '<li>' . $cell .  '</li>';
      }


      $cells_in_row .= '</ul>';

      $rows_container .= '<li data-search-term="' . strtolower($row_metadata_wrapper->field_advisory_row_country->name->value()) . '" class="' . drupal_clean_css_identifier($parent_term_wrapper->name->value()) . '"><span class="t_row_country"><input type="checkbox" class="advisory-row-checkbox" name="' . drupal_clean_css_identifier($parent_term_wrapper->name->value()). '">' .  $row_metadata_wrapper->field_advisory_row_country->name->value() . '</span>' . $cells_in_row . '</li>';

   }


   // Close the row elem
    $rows_container .= '</ul>';

   // Finish making the rows parent items for grouping
    $parent_terms_container = array_values(array_unique($parent_terms_container));
    foreach ($parent_terms_container as $key => $value) {
      if($value === reset($parent_terms_container)){
        $parent_terms_container[$key]  = '<li class="active">' . $value .'</li>';  
      } else{
        $parent_terms_container[$key]  = '<li>' . $value .'</li>';
      }
    }
    $parent_terms_container = '<ul class="nav nav-pills adv_table adv_parent_filters">' . implode('',$parent_terms_container) . '</ul>';

    

    $output = array(
      '#attached' => array(
            'js' => array(
               array('data' => array('advisory_tools' => 'dg_advisories_helper_tools'), 'type' => 'setting'),
               array('data' => array('advisory_sizing' => 'dg_advisories_helper_sizing'), 'type' => 'setting'),
                 drupal_get_path('module', 'dg_advisories_helper') . '/js/dg_advisories_helper.js'
            ),
        ),
      'parent_filters' => array(
        '#type' => 'markup',
        '#markup' => 'Parent Filters' . $parent_terms_container,
      ),
      'table_headers' => array(
        '#type' => 'markup',
        '#markup' => $th_render,
      ),
      'row_entities' => array(
        '#type' => 'markup',
        '#markup' => '<ul class="adv_table t_body">' . $rows_container . '</ul>',
      ),    
  );
  return render($output);
}