You are here

function domain_prefix_form_submit in Domain Access 6.2

Same name and namespace in other branches
  1. 5 domain_prefix/domain_prefix.module \domain_prefix_form_submit()

FormsAPI for domain_prefix_form.

File

domain_prefix/domain_prefix.admin.inc, line 361
Admin page functions for selective table prefixing for use with Domain Access.

Code

function domain_prefix_form_submit($form, &$form_state) {

  // Flag messages for the administrative user only.
  $msg = TRUE;
  $create = TRUE;
  if (!empty($form_state['values']['domain_arguments']['user_submitted'])) {
    $msg = FALSE;

    // Should we create tables for user domains?
    $create = variable_get('domain_user_prefixing', 0);
  }
  if (!empty($create)) {

    // Throw away what we don't need.
    $prefix = domain_prefix_string($form_state['values']['domain_id']);
    $tables = $form_state['values']['table_data'];
    $unset = array(
      'prefix_theme',
      'domain_id',
      'op',
      'submit',
      'restore',
      'form_token',
      'form_id',
      'form_build_id',
      'execute',
      'table_data',
    );
    $data = $form_state['values'];
    foreach ($unset as $key) {
      unset($data[$key]);
    }

    // Delete existing records, but get the existing values first.
    $current = domain_prefix_lookup($form_state['values']['domain_id']);
    db_query("DELETE FROM {domain_prefix} WHERE domain_id = %d", $form_state['values']['domain_id']);
    foreach ($data as $key => $value) {

      // Do not process tables for the source elements.
      // But be sure to set the proper source table prefix for copying data.
      if (substr($key, 0, 8) != '_source_') {
        $source = isset($data['_source_' . $key]) ? $data['_source_' . $key] : 0;
        if ($source > 0) {
          $source_prefix = domain_prefix_string($source);
        }
        else {
          $source_prefix = '';
        }
        $update = FALSE;
        if (empty($value)) {
          $value = DOMAIN_PREFIX_IGNORE;
          $update = TRUE;
        }
        $newtable = db_escape_table($prefix . $key);
        $module = $tables[$key]['module'];
        $exists = domain_prefix_table_exists($prefix, $key);
        $oldtable = db_escape_table($key);
        $sourcetable = db_escape_table($source_prefix . $key);
        $table_schema = drupal_get_schema($key);
        if ($value == DOMAIN_PREFIX_CREATE) {
          if (!$exists) {

            // TODO: Make this a nice update function with a progress bar.
            $data_table = db_create_table_sql($newtable, $table_schema);
            db_query($data_table[0]);
            if ($msg) {
              drupal_set_message(t('!string table created.', array(
                '!string' => $newtable,
              )));
            }
            $update = TRUE;
          }
          else {
            if ($current[$oldtable]['status'] == DOMAIN_PREFIX_COPY) {
              drupal_set_message(t('!string table cannot be created, since it already exists.', array(
                '!string' => $newtable,
              )));
            }
          }
        }
        else {
          if ($value == DOMAIN_PREFIX_COPY) {
            if (!$exists) {

              // TODO: Make this a nice update function with a progress bar.
              $data_table = db_create_table_sql($newtable, $table_schema);
              db_query($data_table[0]);
              domain_prefix_insert_data($table_schema, $newtable, $sourcetable);
              if ($msg) {
                drupal_set_message(t('!string table copied.', array(
                  '!string' => $newtable,
                )));
              }
              $update = TRUE;
            }
            else {
              if ($current[$oldtable]['status'] == DOMAIN_PREFIX_CREATE) {
                drupal_set_message(t('!string table cannot be copied, since it already exists.', array(
                  '!string' => $newtable,
                )));
              }
            }
          }
          else {
            if ($value == DOMAIN_PREFIX_UPDATE) {
              if ($exists > 0) {
                db_query("TRUNCATE TABLE {%s}", $newtable);
                domain_prefix_insert_data($table_schema, $newtable, $sourcetable);
                if ($msg) {
                  drupal_set_message(t('!string table updated from source.', array(
                    '!string' => $newtable,
                  )));
                }
                $update = TRUE;

                // Set the stored value to "copy" for record keeping.
                $value = DOMAIN_PREFIX_COPY;
              }
            }
            else {
              if ($value == DOMAIN_PREFIX_DROP) {
                if ($exists > 0) {
                  db_query("DROP TABLE {%s}", $newtable);
                  $value = DOMAIN_PREFIX_IGNORE;
                  if ($msg) {
                    drupal_set_message(t('!string table dropped.', array(
                      '!string' => $newtable,
                    )));
                  }
                  $update = TRUE;
                }
                else {
                  drupal_set_message(t('!string table does not exist.', array(
                    '!string' => $newtable,
                  )));
                }
              }
            }
          }
        }

        // Update our records.
        if (!$update && $value != 1 && isset($current[$oldtable]['status'])) {
          $value = $current[$oldtable]['status'];
        }
        db_query("INSERT INTO {domain_prefix} (domain_id, status, tablename, module, source) VALUES (%d, %d, '%s', '%s', %d)", $form_state['values']['domain_id'], $value, $key, $module, $form_state['values']['_source_' . $key]);

        // Prevent errors after the form is passed.
        $form_state['values'][$key] = $value;
      }
    }
  }

  // Clear the cache.
  cache_clear_all();
}