You are here

function data_ui_edit_form_submit in Data 6

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

Submit form.

File

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

Code

function data_ui_edit_form_submit($form, &$form_state) {
  $table = $form_state['values']['table'];
  $schema = $table
    ->get('table_schema');
  if (!db_table_exists($table
    ->get('name'))) {
    drupal_set_message(t('Table does not exist in database.') . ' ' . t('Go to !compare to resolve conflicts.', array(
      '!compare' => l(t('Compare schemas'), 'admin/build/data/compare'),
    )), 'error');
    return;
  }
  try {
    if ($form_state['clicked_button']['#value'] == t('Save')) {
      $fields = $schema['fields'];
      $new_fields = $form_state['values']['fields'];
      $new_index = array();
      $new_primary_key = array();
      if (empty($form_state['values']['bulk_operation']) && isset($fields)) {

        // Convert schema.
        foreach ($fields as $field_name => $field) {
          $safe_field_name = db_escape_table($field_name);
          if ($new_spec = _data_ui_changed($new_fields[$safe_field_name], $field)) {
            $table
              ->changeField($safe_field_name, $new_spec);
            drupal_set_message(t('Changed field !field_name', array(
              '!field_name' => $safe_field_name,
            )));
          }
          if ($new_fields[$safe_field_name]['index']) {
            $new_index[] = $safe_field_name;
          }
          if ($new_fields[$safe_field_name]['primary']) {
            $new_primary_key[] = $safe_field_name;
          }
        }
        $table
          ->changeIndex($new_index);
        $table
          ->changePrimaryKey($new_primary_key);

        // Update meta data.
        $meta = $table
          ->get('meta');
        foreach ($new_fields as $safe_field_name => $field) {
          $meta['fields'][$safe_field_name]['label'] = check_plain($field['label']);
        }
        $table
          ->update(array(
          'meta' => $meta,
        ));
        drupal_set_message(t('Saved changes'));
      }
      else {

        // Bulk updates.
        switch ($form_state['values']['bulk_operation']) {
          case 'delete':
            foreach ($new_fields as $field_name => $field) {
              if (!empty($field['selected'])) {

                // One field must stay.
                $schema = $table
                  ->get('table_schema');
                if (count($schema['fields']) > 1) {
                  $table
                    ->dropField(db_escape_table($field_name));
                  drupal_set_message(t('Deleted field !field_name', array(
                    '!field_name' => check_plain($field_name),
                  )));
                }
                else {
                  drupal_set_message('You cannot delete all fields from a table, drop the table instead.', 'error');
                }
              }
            }
            break;
        }
      }
    }
    elseif ($form_state['clicked_button']['#value'] == t('Add new')) {
      $new = $form_state['values']['new'];
      $spec = data_build_field_definition($new);
      $table
        ->addField(db_escape_table($new['name']), $spec);
      drupal_set_message(t('Added field !field', array(
        '!field' => check_plain($new['name']),
      )));
      if (!empty($new['index'])) {
        $table
          ->addIndex(db_escape_table($new['name']));
        drupal_set_message(t('Added index for field !field', array(
          '!field' => check_plain($new['name']),
        )));
      }
      $meta = $table
        ->get('meta');
      $meta['fields'][db_escape_table($new['name'])]['label'] = check_plain($new['label']);
      $table
        ->update(array(
        'meta' => $meta,
      ));
    }
  } catch (DataException $e) {
    drupal_set_message($e
      ->getMessage(), 'error');
  }
}