You are here

function view_custom_table_views_data in Views Custom Table 7

Same name and namespace in other branches
  1. 8 view_custom_table.module \view_custom_table_views_data()
  2. 9.0.x view_custom_table.module \view_custom_table_views_data()

Implements hook_views_data().

File

./view_custom_table.module, line 125
view_custom_table.module

Code

function view_custom_table_views_data() {
  $query = db_select('custom_table_view_data', 'ctvd')
    ->fields('ctvd', array(
    'table_name',
    'column_relations',
  ));
  $all_custom_tables = $query
    ->execute()
    ->fetchAll();

  // My Sql Datatypes.
  $int_types = array(
    'tinyint',
    'smallint',
    'mediumint',
    'int',
    'bigint',
    'decimal',
    'float',
    'double',
    'bit',
  );
  $text_types = array(
    'varchar',
    'char',
    'tinytext',
    'text',
    'mediumtext',
    'longtext',
    'binary',
    'varbinary',
    'tinyblob',
    'mediumblob',
    'blob',
    'longblob',
    'enum',
    'set',
  );
  $time_type = array(
    'date',
    'datetime',
    'timestamp',
    'time',
    'year',
  );
  if (!empty($all_custom_tables)) {
    foreach ($all_custom_tables as $custom_table) {
      $table_relations = unserialize($custom_table->column_relations);
      $table_display_name = ucfirst($custom_table->table_name);
      $table_display_name = str_replace('_', ' ', $table_display_name);
      $data[$custom_table->table_name]['table']['group'] = t('Custom Table Views');
      $query = format_string('DESCRIBE { @table_name }', array(
        '@table_name' => $custom_table->table_name,
      ));
      $table = db_query($query);
      foreach ($table as $column) {
        $column_display_name = ucfirst($column->Field);
        $column_display_name = str_replace('_', ' ', $column_display_name);
        if (strpos($column->Type, '(')) {
          $column_type_values = explode('(', $column->Type);
          $column_type = $column_type_values[0];
        }
        else {
          $column_type = $column->Type;
        }
        if ($column->Key == 'PRI') {
          $data[$custom_table->table_name]['table']['base'] = array(
            'field' => $column->Field,
            'title' => t('@table data', array(
              '@table' => $table_display_name,
            )),
            'help' => t('Data of @table.', array(
              '@table' => $table_display_name,
            )),
            'weight' => 10,
          );
        }
        if (in_array($column_type, $int_types)) {
          $data[$custom_table->table_name][$column->Field] = array(
            'title' => t('@column_name', array(
              '@column_name' => $column_display_name,
            )),
            'help' => t('@column_name data form @table.', array(
              '@column_name' => $column_display_name,
              '@table' => $table_display_name,
            )),
            'field' => array(
              'handler' => 'views_handler_field_numeric',
              'click sortable' => TRUE,
            ),
            'sort' => array(
              'handler' => 'views_handler_sort',
            ),
            'filter' => array(
              'handler' => 'views_handler_filter_numeric',
            ),
            'argument' => array(
              'handler' => 'views_handler_argument_numeric',
            ),
          );
        }
        if (in_array($column_type, $text_types)) {
          $data[$custom_table->table_name][$column->Field] = array(
            'title' => t('@column_name', array(
              '@column_name' => $column_display_name,
            )),
            'help' => t('@column_name data form @table.', array(
              '@column_name' => $column_display_name,
              '@table' => $table_display_name,
            )),
            'field' => array(
              'handler' => 'views_handler_field',
              'click sortable' => TRUE,
            ),
            'sort' => array(
              'handler' => 'views_handler_sort',
            ),
            'filter' => array(
              'handler' => 'views_handler_filter_string',
            ),
            'argument' => array(
              'handler' => 'views_handler_argument_string',
            ),
          );
        }
        if (in_array($column_type, $time_type)) {
          $data[$custom_table->table_name][$column->Field] = array(
            'title' => t('@column_name', array(
              '@column_name' => $column_display_name,
            )),
            'help' => t('@column_name data form @table.', array(
              '@column_name' => $column_display_name,
              '@table' => $table_display_name,
            )),
            'field' => array(
              'handler' => 'view_custom_table_handler_field_date_time',
              'click sortable' => TRUE,
            ),
            'sort' => array(
              'handler' => 'views_handler_sort_date',
            ),
            'filter' => array(
              'handler' => 'view_custom_table_handler_filter_date',
            ),
            'argument' => array(
              'handler' => 'views_handler_argument_date',
            ),
          );
        }
        if (!empty($table_relations)) {
          if (array_key_exists($column->Field, $table_relations)) {
            $entity_info = entity_get_info($table_relations[$column->Field]);
            if (isset($entity_info['base table']) && isset($entity_info['entity keys']) && isset($entity_info['entity keys']['id'])) {
              $data[$custom_table->table_name][$column->Field]['relationship'] = array(
                'base' => $entity_info['base table'],
                // The name of the field on the joined table.
                'base field' => $entity_info['entity keys']['id'],
                'handler' => 'views_handler_relationship',
                'label' => t('@column_name', array(
                  '@column_name' => $column_display_name,
                )),
                'title' => t('@column_name', array(
                  '@column_name' => $column_display_name,
                )),
                'help' => t('@entity - @column_name relationship', array(
                  '@column_name' => $column_display_name,
                  '@entity' => $entity_info['label'],
                )),
              );
            }
          }
        }
      }
    }
    return $data;
  }
}