function tripal_chado_migrate_organism_images

3.x tripal_chado.migrate.inc tripal_chado_migrate_organism_images($bundle_name)

Migrate images for Tripal v2 organism content

Migrate images for all chado_organism

1 call to tripal_chado_migrate_organism_images()
tripal_chado_migrate_selected_types in tripal_chado/includes/tripal_chado.migrate.inc
Migrate only selected Tripal v2 content types

File

tripal_chado/includes/tripal_chado.migrate.inc, line 852

Code

function tripal_chado_migrate_organism_images($bundle_name) {
  // Get all organism entities
  $bio_data_table = array_pop(tripal_chado_migrate_get_biodata_tables('organism'));
  $results =
    db_select($bio_data_table, 'ce')
    ->fields('ce', array('entity_id', 'record_id', 'nid'))
    ->execute();

  // Iterate through each organism entity
  while ($organism = $results->fetchObject()) {
    $nid = $organism->nid;
    $entity_id = $organism->entity_id;

    // check if there is a file record for the organism node
    $fid = db_select('file_usage', 'fu')
      ->fields('fu', array('fid'))
      ->condition('module', 'tripal_organism')
      ->condition('type', 'organism_image')
      ->condition('id', $nid)
      ->execute()
      ->fetchField();
    // check if the image was added using the old interface.
    if (!$fid) {
      $sql =
        "SELECT genus,species,nid
          FROM {organism} O
          INNER JOIN chado_organism CO ON O.organism_id = CO.organism_id
          WHERE O.organism_id = :organism_id";
      $chado_org = chado_query($sql, array(':organism_id' => $organism->record_id))->fetchObject();

      if ($chado_org) {
        $base_path = realpath('.');
        $image_dir = tripal_get_files_dir('tripal_organism') . "/images";
        $image_name = $chado_org->genus . "_" . $chado_org->species . ".jpg";
        $image_path = "$base_path/$image_dir/$image_name";
        // image files are stored as 'genus_species.jpg'
        $file = NULL;
        if (file_exists($image_path)) {
          $handle = fopen($image_path, 'r');
          $file = file_save_data($handle, "public://$image_name");
          fclose($handle);
        }
        // last possible case: image files are stored as 'organism_id.jpg'
        else {
          $image_name = $chado_org->nid . ".jpg";
          $image_path = "$base_path/$image_dir/$image_name";
          if (file_exists($image_path)) {
            $handle = fopen($image_path, 'r');
            $file = file_save_data($handle, "public://$image_name");
            fclose($handle);
          }
        }
        if ($file) {
          tripal_chado_migrate_organism_image_add_file($file->fid, $entity_id, $bundle_name);
        }
      }
    }
    else {
      // If there is an image, add it to the organism entity
      tripal_chado_migrate_organism_image_add_file($fid, $entity_id, $bundle_name);
    }

  }
}