You are here

function media_migrate_file_types_upgrade_file_types_confirm_submit in D7 Media 7.4

Same name and namespace in other branches
  1. 7.2 modules/media_migrate_file_types/includes/media_migrate_file_types.pages.inc \media_migrate_file_types_upgrade_file_types_confirm_submit()
  2. 7.3 modules/media_migrate_file_types/includes/media_migrate_file_types.pages.inc \media_migrate_file_types_upgrade_file_types_confirm_submit()

File types migration confirmation page submit. Executes actual migration.

File

modules/media_migrate_file_types/includes/media_migrate_file_types.pages.inc, line 133
Common pages for the Media Migrate File Types module.

Code

function media_migrate_file_types_upgrade_file_types_confirm_submit($form, &$form_state) {
  $migratable_types = _media_migrate_file_types_get_migratable_file_types();
  foreach ($migratable_types as $type) {
    if ($_GET[$type] && ($bundle_new = file_type_load($_GET[$type]))) {

      // Old bundle might be deleted so let's fake some values.
      $bundle_old = file_type_load($type);
      if (empty($bundle_old)) {
        $bundle_old = new stdClass();
        $bundle_old->type = $type;
        $bundle_old->mimetypes = array();
        $bundle_old->export_type = 2;
      }

      // Migrate fields to new bundle.
      if ($_GET['migrate_fields']) {
        $old_fields = db_select('field_config_instance', 'fc')
          ->fields('fc', array(
          'field_name',
        ))
          ->condition('entity_type', 'file')
          ->condition('bundle', $bundle_old->type)
          ->execute()
          ->fetchCol();
        $new_fields = db_select('field_config_instance', 'fc')
          ->fields('fc', array(
          'field_name',
        ))
          ->condition('entity_type', 'file')
          ->condition('bundle', $bundle_new->type)
          ->execute()
          ->fetchCol();
        $fields_to_move = array_diff($old_fields, $new_fields);
        $fields_to_drop = array_diff($old_fields, $fields_to_move);
        if (!empty($fields_to_move)) {
          db_update('field_config_instance')
            ->fields(array(
            'bundle' => $bundle_new->type,
          ))
            ->condition('entity_type', 'file')
            ->condition('bundle', $bundle_old->type)
            ->condition('field_name', $fields_to_move, 'IN')
            ->execute();
        }
        if (!empty($fields_to_drop)) {
          db_delete('field_config_instance')
            ->condition('entity_type', 'file')
            ->condition('bundle', $bundle_old->type)
            ->condition('field_name', $fields_to_drop, 'IN')
            ->execute();
        }
        field_cache_clear();
        module_invoke_all('field_attach_rename_bundle', 'file', $bundle_old->type, $bundle_new->type);
      }

      // Migrate mimetypes to new bundle.
      if ($_GET['migrate_mimes']) {
        $changed = FALSE;
        foreach ($bundle_old->mimetypes as $mime) {
          if (!file_entity_match_mimetypes($bundle_new->mimetypes, $mime)) {
            $bundle_new->mimetypes[] = $mime;
            $changed = TRUE;
          }
        }
        if ($changed) {
          file_type_save($bundle_new);
        }
      }

      // Delete old bundle.
      if ($_GET['delete_old_type'] && $bundle_old->export_type == 1) {
        file_type_delete($bundle_old);
      }

      // Migrate files.
      db_update('file_managed')
        ->fields(array(
        'type' => $bundle_new->type,
      ))
        ->condition('type', $bundle_old->type)
        ->execute();
    }
  }
  $form_state['redirect'] = 'admin/structure/file-types';
}