You are here

function _add_db_field_data_XML in Bibliography Module 7

Same name and namespace in other branches
  1. 7.2 biblio.install \_add_db_field_data_XML()

File

./biblio.install, line 1530

Code

function _add_db_field_data_XML() {

  // First contributor_type_data id.
  $next_ctdid = 10;
  $schema = biblio_schema();
  $fieldnames = array_keys($schema['biblio_fields']['fields']);
  $field_type_fieldnames = array_keys($schema['biblio_field_type']['fields']);
  $field_type_data_fieldnames = array_keys($schema['biblio_field_type_data']['fields']);
  db_query("/*!40000 ALTER TABLE {biblio_field_type_data} DISABLE KEYS */;");
  db_query("/*!40000 ALTER TABLE {biblio_fields} DISABLE KEYS */;");
  for ($type = 1; $type <= 5; $type++) {
    for ($biblio_type = 100; $biblio_type <= 130; $biblio_type++) {
      db_query("INSERT INTO {biblio_contributor_type} (auth_category, biblio_type, auth_type) VALUES (%d, %d, %d)", $type, $biblio_type, $type);
    }
  }
  _id_by_name(NULL, NULL, NULL, array(
    'tablename' => 'biblio_field_type_data',
    'name_column' => 'title',
    'id_column' => 'ftdid',
  ));
  $xml_file = drupal_get_path('module', 'biblio') . '/field_data.xml';
  $xml = simplexml_load_file($xml_file);
  foreach ($xml->field as $field) {
    $link_data = array(
      0,
      $field['fid'],
      $field['fid'],
      $field['fid'],
      $field->common,
      $field->autocomplete,
      $field->required,
      $field->weight,
      $field->visible,
    );
    db_query("INSERT INTO {biblio_field_type} (" . implode(", ", $field_type_fieldnames) . ")\n                  VALUES (%d, %d, %d, %d, %d, %d, %d, %d, %d)", $link_data);
    for ($t = 100; $t <= 130; $t++) {
      $values = array(
        $t,
        $field['fid'],
        $field['fid'],
        $field['fid'],
        $field->common,
        $field->autocomplete,
        $field->required,
        $field->weight,
        $field->visible,
      );
      db_query("INSERT INTO {biblio_field_type} (" . implode(", ", $field_type_fieldnames) . ")\n                      VALUES('" . implode("', '", $values) . "')");
    }
    $ftd = array(
      $field['fid'],
      $field->default_name,
      $field->hint,
    );
    db_query("INSERT INTO {biblio_field_type_data} (" . implode(", ", $field_type_data_fieldnames) . ")\n                    VALUES('" . implode("', '", $ftd) . "')");
    $field_data = array(
      $field['fid'],
      $field->field_name,
      $field->type,
      $field->width,
      $field->maxlength,
    );
    db_query("INSERT INTO {biblio_fields} (" . implode(", ", $fieldnames) . ")\n                    VALUES('" . implode("', '", $field_data) . "')");
    foreach ($field->name as $name) {
      if ($name != "~") {

        // Ftdid.
        $ftd[0] = ($existing_id = _id_by_name('biblio_field_type_data', $name)) ? $existing_id : variable_get('biblio_last_ftdid', 100);

        // title.
        $ftd[1] = trim($name);

        // hint.
        $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, $name['tid'], $field['fid']);
        if (!$existing_id) {

          // If this title doesn't alreay exist, then insert it into the table.
          db_query("INSERT INTO {biblio_field_type_data} (" . implode(", ", $field_type_data_fieldnames) . ")\n                        VALUES (%d, '%s', '%s')", $ftd);

          // Cache the new id value for future use.
          _id_by_name('biblio_field_type_data', $name, $ftd[0]);

          // Increment the field type data id by one.
          variable_set('biblio_last_ftdid', $ftd[0] + 1);
        }
      }
      elseif ($name == "~") {

        // Turn the visibility off for this (~) type.
        db_query("UPDATE {biblio_field_type}\n                      SET visible = 0\n                      WHERE tid = %d AND fid = %d ", $name['tid'], $field['fid']);
      }
      if ($field->type == 'contrib_widget' && $name != "~") {
        db_query("UPDATE {biblio_contributor_type} SET auth_type=%d where auth_category=%d and biblio_type=%d", $ftd[0], $field->contrib_type, $name['tid']);
      }
    }
  }
  db_query("/*!40000 ALTER TABLE {biblio_field_type_data} ENABLE KEYS */;");
  db_query("/*!40000 ALTER TABLE {biblio_fields} ENABLE KEYS */;");
  return $result;
}