You are here

function view_custom_table_custom_table_update_relations_form in Views Custom Table 7

Function to update custom table relations.

1 string reference to 'view_custom_table_custom_table_update_relations_form'
view_custom_table_menu in ./view_custom_table.module
Implements hook_menu().

File

./view_custom_table.admin.inc, line 265
File for administrative functions.

Code

function view_custom_table_custom_table_update_relations_form($form, &$form_state, $table_id = NULL) {
  $table_info = view_custom_table_load_table($table_id);
  $table_name = $table_info->table_name;
  $form_state['table_name'] = $table_name;
  $table_relations = unserialize($table_info->column_relations);
  $views_info = view_custom_table_load_custom_table_views($table_info->table_name);
  if (!empty($views_info)) {
    drupal_set_message(t('views are using @table_name as base table, so presaved values can not be changed.', array(
      '@table_name' => $table_info->table_name,
    )), 'warning');
  }
  $all_entities = entity_get_info();
  $entities[''] = t('--None--');
  foreach ($all_entities as $entity_name => $all_entities_item) {
    $entities[$entity_name] = $all_entities_item['label'];
  }
  $form['columns'] = array(
    '#type' => 'fieldset',
    '#title' => t('"@table" Int Type Columns', array(
      '@table' => $table_name,
    )),
    '#tree' => TRUE,
  );
  $int_types = array(
    'tinyint',
    'smallint',
    'mediumint',
    'int',
    'bigint',
  );
  $text_query = format_string('DESCRIBE { @table_name }', array(
    '@table_name' => $table_name,
  ));
  $query = db_query($text_query);
  foreach ($query as $row) {
    $row_type = explode('(', $row->Type);
    if (in_array($row_type[0], $int_types)) {
      $form['columns']['column_' . $row->Field] = array(
        '#type' => 'fieldset',
        '#title' => t('Relation of "@field_name" with', array(
          '@field_name' => ucfirst($row->Field),
        )),
        '#tree' => TRUE,
        '#attributes' => array(
          'class' => array(
            'container-inline',
          ),
        ),
      );
      $form['columns']['column_' . $row->Field]['entity'] = array(
        '#type' => 'select',
        '#title' => t('Entity'),
        '#options' => $entities,
        '#default_value' => $table_relations[$row->Field] != NULL ? $table_relations[$row->Field] : '',
      );
      if (!empty($views_info) && isset($table_relations[$row->Field]) && $table_relations[$row->Field] != NULL) {
        $form['columns']['column_' . $row->Field]['entity']['#disabled'] = TRUE;
      }
      $form['columns']['column_' . $row->Field]['field'] = array(
        '#type' => 'hidden',
        '#value' => $row->Field,
      );
    }
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  $form['actions']['cancel'] = array(
    '#type' => 'markup',
    '#markup' => l(t('Cancel'), 'admin/structure/views/custom_table'),
  );
  return $form;
}