function _add_custom_field_data in Bibliography Module 6
Same name and namespace in other branches
- 7 biblio.install \_add_custom_field_data()
- 7.2 biblio.install \_add_custom_field_data()
3 calls to _add_custom_field_data()
- biblio_install in ./biblio.install
- @file
Install file for biblio module
- biblio_reset_types in ./biblio.install
- biblio_update_6000 in ./biblio.install
File
- ./biblio.install, line 1171
- Install file for biblio module
Code
function _add_custom_field_data() {
$next_ctdid = 10;
$schema = biblio_schema();
$fieldnames = array_keys($schema['biblio_field_type_data']['fields']);
$query = "SELECT fid, name FROM {biblio_fields} ";
$res = db_query($query);
while ($row = db_fetch_object($res)) {
$fieldmap[$row->name] = $row->fid;
}
$csv_file = drupal_get_path('module', 'biblio') . '/biblio.field.type.data.csv';
if ($handle = fopen($csv_file, "r")) {
$header = fgetcsv($handle, 10000, ",");
$generic = fgetcsv($handle, 10000, ",");
_id_by_name(NULL, NULL, NULL, array(
'tablename' => 'biblio_field_type_data',
'name_column' => 'title',
'id_column' => 'ftdid',
));
_id_by_name(NULL, NULL, NULL, array(
'tablename' => 'biblio_contributor_type_data',
'name_column' => 'title',
'id_column' => 'auth_type',
));
$res = db_query("SELECT fid,name FROM {biblio_fields} WHERE type='contrib_widget'");
$contributor_categories = array();
while ($row = db_fetch_object($res)) {
$contributor_categories[$row->name] = $row->fid;
}
while (($row = fgetcsv($handle, 10000, ",")) !== FALSE) {
$column = 0;
if (empty($row[1])) {
continue;
}
foreach ($header as $key => $field_name) {
if (!empty($field_name) && $field_name != 'tid') {
if (!empty($row[$column]) && $row[$column] != "~" && isset($fieldmap[$field_name])) {
$ftd[0] = ($existing_id = _id_by_name('biblio_field_type_data', $row[$column])) ? $existing_id : variable_get('biblio_last_ftdid', 100);
$ftd[1] = trim($row[$column]);
$ftd[2] = "";
db_query("UPDATE {biblio_field_type}\n SET ftdid = %d, cust_tdid = %d, visible = %d\n WHERE tid = %d AND fid = %d ", $ftd[0], $ftd[0], 1, $row[1], $fieldmap[$field_name]);
if (!$existing_id) {
db_query("INSERT INTO {biblio_field_type_data} (" . implode(", ", $fieldnames) . ")\n VALUES (%d, '%s', '%s')", $ftd);
_id_by_name('biblio_field_type_data', $row[$column], $ftd[0]);
variable_set('biblio_last_ftdid', $ftd[0] + 1);
}
if (substr($field_name, -7, 7) == 'authors' && $row[$column] != '~') {
$type = $contributor_categories[$field_name];
$title = trim($row[$column]);
$biblio_type = $row[1];
$ctdid = ($eid = _id_by_name('biblio_contributor_type_data', $title)) ? $eid : $next_ctdid;
db_query("UPDATE {biblio_contributor_type} SET auth_type=%d where auth_category=%d and biblio_type=%d", $ctdid, $type, $biblio_type);
if (!$eid) {
db_query("INSERT INTO {biblio_contributor_type_data} (auth_type, title) VALUES (%d, '%s')", $ctdid, $title);
_id_by_name('biblio_contributor_type_data', $title, $ctdid);
$next_ctdid++;
}
}
}
elseif ($row[$column] == "~" && isset($fieldmap[$field_name])) {
db_query("UPDATE {biblio_field_type}\n SET visible = 0\n WHERE tid = %d AND fid = %d ", $row[1], $fieldmap[$field_name]);
}
elseif (empty($row[$column]) && isset($fieldmap[$field_name])) {
db_query("UPDATE {biblio_field_type}\n SET visible = 1\n WHERE tid = %d AND fid = %d ", $row[1], $fieldmap[$field_name]);
}
}
$column++;
}
}
fclose($handle);
$result = array(
'success' => TRUE,
'query' => 'Added type specific field titles',
);
}
else {
$result = array(
'success' => FALSE,
'query' => 'Could not open ' . $csv_file,
);
}
return $result;
}