You are here

function domain_prefix_form_submit in Domain Access 5

Same name and namespace in other branches
  1. 6.2 domain_prefix/domain_prefix.admin.inc \domain_prefix_form_submit()

FormsAPI for domain_prefix_form.

File

domain_prefix/domain_prefix.module, line 473
Interface for selective table prefixing for use with Domain Access. For this module to work correctly, you will need to follow the INSTALL.txt instructions for editing your settings.php file.

Code

function domain_prefix_form_submit($form_id, $form_values) {

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

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

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

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

    // Get the database type.
    $db_type = $GLOBALS['db_type'];
    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 = $data['_source_' . $key];
        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 = domain_prefix_module_lookup($key);
        $exists = domain_prefix_table_exists($prefix, $key);
        $oldtable = db_escape_table($key);
        $sourcetable = db_escape_table($source_prefix . $key);
        if ($value == DOMAIN_PREFIX_CREATE) {
          if (!$exists) {
            if ($db_type == 'pgsql') {
              db_query("CREATE TABLE {%s} AS SELECT * FROM {%s}", $newtable, $sourcetable);
              db_query("TRUNCATE TABLE {%s}", $newtable);
            }
            else {
              db_query("CREATE TABLE {%s} LIKE {%s}", $newtable, $sourcetable);
            }
            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) {
              if ($db_type == 'pgsql') {
                db_query("CREATE TABLE {%s} AS SELECT * FROM {%s}", $newtable, $sourcetable);
              }
              else {
                db_query("CREATE TABLE {%s} LIKE {%s}", $newtable, $sourcetable);
                db_query("INSERT INTO {%s} SELECT * FROM {%s}", $newtable, $sourcetable);
              }

              // Update {sequences} table.
              domain_prefix_update_sequences('update', $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);
                db_query("INSERT INTO {%s} SELECT * FROM {%s}", $newtable, $sourcetable);

                // Update {sequences} table.
                domain_prefix_update_sequences('update', $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;

                  // Update {sequences} table.
                  domain_prefix_update_sequences('drop', $newtable, $sourcetable);
                  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) {
          $value = $current[$oldtable]['status'];
        }
        db_query("INSERT INTO {domain_prefix} (domain_id, status, tablename, module, source) VALUES (%d, %d, '%s', '%s', %d)", $form_values['domain_id'], $value, $key, $module, $form_values['_source_' . $key]);
      }
    }
  }

  // Clear the cache.
  cache_clear_all();
}