You are here

function data_ui_create_form_submit in Data 6

Same name and namespace in other branches
  1. 7 data_ui/data_ui.admin.inc \data_ui_create_form_submit()

Submit handler for create table form.

File

data_ui/data_ui.admin.inc, line 366
Admin UI functions.

Code

function data_ui_create_form_submit($form, &$form_state) {
  if (isset($form_state['values']['field_num'])) {
    $form_state['storage'] = $form_state['values'];
  }
  elseif (isset($form_state['values']['fields'])) {

    // Create a schema from user input.
    $schema = $index = $primary = $meta = array();
    foreach ($form_state['values']['fields'] as $field) {
      $field['name'] = db_escape_table($field['name']);
      $schema['fields'][$field['name']] = data_build_field_definition($field);
      $meta['fields'][$field['name']]['label'] = check_plain($field['label']);

      // Limit index if field type is text.
      if (!empty($field['index'])) {
        $index[$field['name']] = data_get_index_definition($field['name'], $field);
      }
      if (!empty($field['primary'])) {
        $primary[] = data_get_pk_definition($field['name'], $field);
      }
    }
    $schema['indexes'] = $index;
    $schema['primary key'] = $primary;

    // Create table.
    if ($table = data_create_table(trim($form_state['storage']['name']), $schema, trim($form_state['storage']['title']))) {
      $meta = $table
        ->update(array(
        'meta' => $meta,
      ));
      drupal_set_message(t('Created table @table', array(
        '@table' => $table
          ->get('name'),
      )));
    }
    else {
      drupal_set_message(t('Error creating table'), 'error');
    }

    // Unset storage to enable redirect.
    unset($form_state['storage']);
    $form_state['redirect'] = 'admin/build/data';
  }
}