You are here

protected function YamlFormToWebformMigrateManager::renameColumns in YAML Form 8

Rename a table's columns.

Parameters

string $table_name: The table name.

Return value

array An associative array containing status messages.

1 call to YamlFormToWebformMigrateManager::renameColumns()
YamlFormToWebformMigrateManager::migrate in modules/yamlform_to_webform/src/YamlFormToWebformMigrateManager.php
Migrate the YAML Form module's configuration and data to the Webform module.

File

modules/yamlform_to_webform/src/YamlFormToWebformMigrateManager.php, line 314

Class

YamlFormToWebformMigrateManager
Defines the YAML Form to Webform migrate manager.

Namespace

Drupal\yamlform_to_webform

Code

protected function renameColumns($table_name) {
  $messages = [];
  $columns = $this
    ->getColumns($table_name);
  foreach ($columns as $column_name => $column) {
    $new_column_name = str_replace([
      'yamlform',
      'yaml_form',
    ], [
      'webform',
      'webform',
    ], $column_name);
    $t_args = [
      '@source' => "{$table_name}.{$column_name}",
      '@destination' => "{$table_name}.{$new_column_name}",
    ];

    // Replace values.
    $replace_type = FALSE;
    if (isset($this->replaceColumns[$column_name])) {
      $replace_type = $this->replaceColumns[$column_name];
    }
    elseif (isset($this->replaceColumns["{$table_name}.{$column_name}"])) {
      $replace_type = $this->replaceColumns["{$table_name}.{$column_name}"];
    }
    if ($replace_type) {
      switch ($replace_type) {
        case 'serial':
          $result = $this->connection
            ->query("SELECT {$column_name} FROM {$table_name} WHERE {$column_name} LIKE '%yaml%' OR {$column_name} LIKE '%Yaml%' OR {$column_name} LIKE '%YAML%'");
          while ($record = $result
            ->fetchAssoc()) {
            $value = $record[$column_name];
            $new_value = $value;
            if (preg_match_all('/s:\\d+:"[^"]*?(yamlform|yaml form|yaml_form)[^"]*?";/i', $value, $matches)) {
              foreach ($matches[0] as $match) {
                $string = unserialize($match);
                $new_string = $this
                  ->replace($string);
                $new_value = str_replace($match, serialize($new_string), $new_value);
              }
              $params = [
                ':value' => $value,
                ':new_value' => $new_value,
              ];
              $this->connection
                ->query("UPDATE {$table_name} SET {$column_name}=:new_value WHERE {$column_name}=:value", $params);
            }
          }
          break;
        default:
          $this->connection
            ->query("UPDATE {$table_name} SET {$column_name} = REPLACE({$column_name}, 'yamlform', 'webform')");
          $this->connection
            ->query("UPDATE {$table_name} SET {$column_name} = REPLACE({$column_name}, 'yaml_form', 'webform')");
          break;
      }
      $messages["{$table_name}.{$column_name}.value"] = $this
        ->t("Changed 'yamlform' to 'webform in '@destination' (@type)", [
        '@type' => $replace_type,
      ] + $t_args);
    }

    // Rename column.
    if ($column_name !== $new_column_name) {
      $column_type = $column['type'];

      // Execute MySQL specific ALTER TABLE commands.
      $this->connection
        ->query("ALTER TABLE {$table_name} CHANGE {$column_name} {$new_column_name} {$column_type}");
      $messages["{$table_name}.{$column_name}"] = $this
        ->t("Renamed '@source' to '@destination'", $t_args);
    }
  }
  return $messages;
}