function content_taxonomy_update_6000 in Content Taxonomy 6
Same name and namespace in other branches
- 6.2 content_taxonomy.install \content_taxonomy_update_6000()
Implemenation of hook_update_N().
updates existing fields from 5.x to 6.x
File
- ./
content_taxonomy.install, line 47
Code
function content_taxonomy_update_6000() {
if ($abort = content_check_update('content_taxonomy')) {
return $abort;
}
drupal_load('module', 'content');
$ret = array();
// Get the latest cache values and schema.
content_clear_type_cache(TRUE);
include_once drupal_get_path('module', 'content') . '/content.install';
include_once drupal_get_path('module', 'content') . '/includes/content.admin.inc';
$types = content_types_install();
foreach ($types as $type_name => $fields) {
foreach ($fields as $field) {
switch ($field['type']) {
case 'content_taxonomy':
$db_info = content_database_info($field);
$table = $db_info['table'];
//fix old settings first and map them to the new ones.
$result = db_query("SELECT * FROM {" . content_field_tablename() . "} WHERE type = 'content_taxonomy' AND field_name = '%s'", $field['field_name']);
while ($field = db_fetch_array($result)) {
$field_settings = unserialize($field['global_settings']);
$field_settings['save_term_node'] = isset($field_settings['save_term_node']) ? $field_settings['save_term_node'] : 0;
if ($field_settings['save'] == 'tag') {
$field_settings['save_term_node'] = 1;
$field['type_name'] = $type_name;
$field['columns'] = $db_info['columns'];
content_alter_schema(array(), $field);
$sql_nodes = db_query("SELECT * FROM {node} WHERE type = '%s'", $type_name);
while ($node = db_fetch_object($sql_nodes)) {
$tids = content_taxonomy_install_terms_by_field($node, $field_settings);
foreach ($tids as $tid) {
$record = array();
foreach ($db_info['columns'] as $column => $attributes) {
$record[$attributes['column']] = $tid;
}
$record['nid'] = $node->nid;
$record['vid'] = $node->vid;
if ($field['multiple']) {
$record['delta'] = $tid;
}
if (db_result(db_query("SELECT COUNT(*) FROM {" . $table . "} WHERE vid = %d", $node->vid))) {
content_write_record($table, $record, array(
'vid',
));
}
else {
content_write_record($table, $record);
}
}
}
}
if ($field_settings['save'] == 'both') {
$field_settings['save_term_node'] = 1;
}
unset($field_settings['save']);
$field_settings['hide_taxonomy_fields'] = isset($field_settings['hide_taxonomy_fields']) ? $field_settings['hide_taxonomy_fields'] : TRUE;
$field_settings['parent'] = $field_settings['tid'];
$field['global_settings'] = array();
$setting_names = module_invoke($field['module'], 'field_settings', 'save', $field);
if (is_array($setting_names)) {
foreach ($setting_names as $setting) {
$field['global_settings'][$setting] = isset($field_settings[$setting]) ? $field_settings[$setting] : '';
}
}
// 'columns' is a reserved word in MySQL4, so our column is named 'db_columns'.
$field['db_columns'] = $field['columns'];
drupal_write_record(content_field_tablename(), $field, 'field_name');
}
foreach ($db_info['columns'] as $column => $attributes) {
$attributes['not null'] = FALSE;
$column = $attributes['column'];
db_change_field($ret, $table, $column, $column, $attributes);
db_field_set_no_default($ret, $table, $column);
$ret[] = update_sql("UPDATE {" . $table . "} SET " . $column . " = NULL WHERE " . $column . " = 0");
}
break;
}
}
}
content_associate_fields('content_taxonomy');
content_associate_fields('content_taxonomy_autocomplete');
content_associate_fields('content_taxonomy_options');
content_clear_type_cache(TRUE);
return $ret;
}