You are here

function _move_field_data in Bibliography Module 6

1 call to _move_field_data()
biblio_update_6000 in ./biblio.install

File

./biblio.install, line 1380
Install file for biblio module

Code

function _move_field_data(&$result) {
  $schema = biblio_schema();

  // update default settings (tid=0) in biblio_field_type from old biblio_fields
  $result[] = update_sql("UPDATE {biblio_field_type_data} ftd, {biblio_field_type} ft\n    INNER JOIN {biblio_fields} f ON f.fid = ft.fid /* add biblio_fields.name */\n    INNER JOIN {biblio_fields_old} fo ON fo.name=f.name /* link to old biblio_fields by name */\n    SET ftd.title=fo.title, ftd.hint=fo.hint, ft.common=fo.common, ft.autocomplete=fo.autocomplete, ft.required=fo.required, ft.weight=fo.weight, ft.visible=fo.visible\n    WHERE ft.ftdid=ftd.ftdid AND ft.tid=0");

  // add new field types from old biblio_type_details
  $db_result = db_query("SELECT old.*,f.fid as fidnew FROM\n    /* biblio_type_details augmented by field name (biblio_*) */\n    (SELECT otd.*,fo.name FROM {biblio_type_details} otd\n     INNER JOIN {biblio_fields_old} fo ON otd.fid=fo.fid) old\n    /* left join: all entries from biblio_type_details are matched to existing entries in biblio_field_type_data */\n    LEFT JOIN {biblio_field_type_data} ftd ON old.title=ftd.title\n    /* add matching fids (fidnew) from new biblio_fields based on field name */\n    INNER JOIN {biblio_fields} f ON f.name=old.name\n    /* consider only those entries in biblio_type_details which have no match in biblio_field_type_data yet */\n    WHERE ftd.title IS NULL");
  while ($row = db_fetch_array($db_result)) {

    // check for presence of this field_type
    $fieldtype = db_fetch_array(db_query("SELECT * FROM {biblio_field_type_data} WHERE title='%s'", $row['title']));
    if (!is_array($fieldtype)) {
      $new_ftdid = variable_get('biblio_last_ftdid', 100);
      variable_set('biblio_last_ftdid', $new_ftdid + 1);
      $fieldtype = array(
        'ftdid' => $new_ftdid,
        'title' => $row['title'],
        'hint' => $row['hint'],
      );

      // write_record may not work if module is diabled.

      //drupal_write_record('biblio_field_type_data',$fieldtype);
      db_query("INSERT INTO {biblio_field_type_data} (ftdid, title, hint) VALUES(%d, '%s', '%s')", $fieldtype['ftdid'], $fieldtype['title'], $fieldtype['hint']);
    }

    // update ftdid in linking table to new field type
    $ftdid = $fieldtype['ftdid'];
    update_sql("UPDATE {biblio_field_type} SET ftdid={$ftdid}, cust_tdid={$ftdid} WHERE tid={$row['tid']} AND fid={$row['fidnew']}");
  }

  // update biblio_field_type (linking table) with overrides from old biblio_type_details
  $result[] = update_sql("UPDATE {biblio_field_type} ft\n    INNER JOIN {biblio_field_type_data} ftd ON ft.ftdid=ftd.ftdid\n    /* link to old biblio_type_details based on field title and publication type */\n    INNER JOIN {biblio_type_details} otd ON otd.title=ftd.title AND otd.tid=ft.tid\n    SET ft.required=otd.required, ft.weight=otd.weight");

  // update auth_type associated to (auth_category,biblio_type) from old biblio_type_details
  $result[] = update_sql("UPDATE {biblio_contributor_type} ct\n    INNER JOIN\n      /* select contributor fields from biblio_type_details (fid <= 4) and augment with new ctd.auth_type */\n      (SELECT tid,IF(fid=4,5,fid) AS auth_category,ctd.auth_type FROM {biblio_type_details} b\n        INNER JOIN {biblio_contributor_type_data} ctd ON b.title=ctd.title WHERE b.fid <= 4) otd\n      /* match on publication type and auth_category, fid=4 (corp. author) changed to catagory 5 */\n      ON otd.tid=ct.biblio_type AND otd.auth_category=ct.auth_category\n    SET ct.auth_type=otd.auth_type");
  return $result;
}