Index: supported/cck/content_taxonomy.inc =================================================================== RCS file: supported/cck/content_taxonomy.inc diff -N supported/cck/content_taxonomy.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ supported/cck/content_taxonomy.inc 23 Aug 2010 00:53:33 -0000 @@ -0,0 +1,159 @@ + $fieldinfo ) { + foreach ( $fieldinfo['columns'] as $colname => $colinfo ) { + $cck_fieldname = node_import_cck_name( $fieldname, $colname ); + $fields[$cck_fieldname] = node_import_cck_field_defaults( $fieldinfo ); + + switch ( $colname ) { + case 'value': + $fields[$cck_fieldname]['title'] = $fieldinfo['widget']['label']; + $fields[$cck_fieldname]['map_required'] = $fieldinfo['required']; + break; + default: + $fields[$cck_fieldname]['title'] = t('Unsupported: ') . $fieldinfo['widget']['label'] .' - '. $colname; + } + } + } + + return $fields; +} + + + +/** + * Implementation of hook_node_import_values_alter() + */ +function content_taxonomy_node_import_values_alter( &$values, $type, $defaults, $options, $fields, $preview ) { + // Place the following line in the validation function on the content_taxonomy module in order to determine + // what the widget is expecting: + // + // drupal_set_message( '
Element: ' . print_r( $element, TRUE ) . '
' ); + // + // For example, I placed the above line in the content_taxonomy_tree_validate function in order to discover + // the data structure that the validator expects. I assume the other widgets expect different structures. + + foreach ( node_import_cck_fields( $type, 'content_taxonomy' ) as $fieldname => $fieldinfo ) { + if ( $fieldinfo['widget']['type'] == 'content_taxonomy_tree' ) { + $vid = $fieldinfo['vid']; + $terms = array(); + $results = array(); + foreach ( $values[$fieldname] as $i => $value ) { + $term = false; + if ( is_numeric( $value['value'] ) ) { + $term = taxonomy_get_term( intval($value['value']) ); + } else { + $term = taxonomy_get_term_by_name( $value['value'] ); + } + $tid = $term[0]->tid; + $terms[$tid] = (array)$term[0]; + $children = taxonomy_get_children( $tid, $vid ); + $terms[$tid]['children'] = $children; + $parents = taxonomy_get_parents( $tid ); + $terms[$tid]['parents'] = $parents; + } + + // content_taxonomy_node_import_tree_adjust( $terms, $converted ); + foreach ( $terms as $term_id => $data ) { + if ( empty( $data['parents'] ) ) { + // We only want top-level terms here + $results['value'][$term_id] = array( 'checkbox' => $term_id ); + + if ( !empty( $data['children'] ) ) { + // Handle the children terms + content_taxonomy_node_import_tree_adjust( $terms, $data, $results['value'][$term_id]['children'] ); + } else { + unset( $terms[$term_id] ); + } + } + } + + $values[$fieldname] = $results; + + + } elseif ( $fieldinfo['widget']['type'] == 'content_taxonomy_options' ) { + // Content taxonomy checkboxes + $vid = $fieldinfo['vid']; + $results = array(); + foreach ( $values[$fieldname] as $i => $value ) { + if ( is_numeric( $value['value'] ) ) { // Using TID... + $sql = "SELECT tid FROM {term_data} WHERE vid = %d AND tid = %d"; + if ( $tid = db_result( db_query( $sql, $vid, intval( $value['value'] ) ) ) ) { + $results[$tid] = $tid; + } + } else { // Using term name... + $sql = "SELECT tid FROM {term_data} WHERE vid = %d AND LOWER(name) = '%s'"; + if ( $tid = db_result( db_query( $sql, $vid, drupal_strtolower( $value['value'] ) ) ) ) { + $results[$tid] = $tid; + } + } + } + + $values[$fieldname]['value'] = $results; + + + } else { + $vid = $fieldinfo['vid']; + foreach ( $values[$fieldname] as $i => $value ) { + if ( is_numeric($value['value']) ) { // Using TID... + $sql = "SELECT tid FROM {term_data} WHERE vid = %d AND tid = %d"; + if ( $tid = db_result( db_query( $sql, $vid, intval( $value['value'] ) ) ) ) { + $values[$fieldname][$i]['value'] = $tid; + } else { + if ( trim( $value['value'] ) != '' ) { + $edit = array( 'vid' => $vid, 'name' => $value['value'] ); + taxonomy_save_term( $edit ); + $sql = "SELECT tid FROM {term_data} WHERE vid = %d AND tid = %d"; + if ( $tid = db_result( db_query( $sql, $vid, intval( $value['value'] ) ) ) ) { + $values[$fieldname][$i]['value'] = $tid; + } + } + } + } else { // Using term name... + $sql = "SELECT tid FROM {term_data} WHERE vid = %d AND LOWER(name) = '%s'"; + if ( $tid = db_result( db_query( $sql, $vid, drupal_strtolower( $value['value'] ) ) ) ) { + $values[$fieldname][$i]['value'] = $tid; + } else { + if ( trim( $value['value'] ) != '' ) { + $edit = array( 'vid' => $vid, 'name' => $value['value'] ); + taxonomy_save_term( $edit ); + $sql = "SELECT tid FROM {term_data} WHERE vid = %d AND LOWER(name) = '%s'"; + if ( $tid = db_result( db_query( $sql, $vid, drupal_strtolower( $value['value'] ) ) ) ) { + $values[$fieldname][$i]['value'] = $tid; + } + } + } + } + } + } + } +} + + + +function content_taxonomy_node_import_tree_adjust( &$terms, &$data, &$results ) { + foreach ( $data['children'] as $id => $contents ) { + if ( key_exists( $id, $terms ) ) { + $results[$id] = array( 'checkbox' => $id ); + if ( !empty( $terms[$id]['children'] ) ) { + content_taxonomy_node_import_tree_adjust( $terms, $terms[$id], $results[$id]['children'] ); + } else { + // Unset the item from the terms list; this should be a leaf term + unset( $terms[$id] ); + } + } + } +}