$field) { if (in_array($field['type'], array('content_taxonomy'))) { $name = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name; $targets[$field_name . ':newterm'] = array( 'name' => check_plain($name) . ' (' . $text_add . ')', 'callback' => 'ente_planificador_import_extra_feeds_set_target', 'description' => t('The CCK @name field of the node (@type).', array('@name' => $name, '@type' => $field['type'])), ); if ($field['widget']['type'] == 'content_taxonomy_hs') { $targets[$field_name . ':hs'] = array( 'name' => check_plain($name) . ' (' . $text_add1 . ')', 'callback' => 'ente_planificador_import_extra_feeds_set_target', 'description' => t('The CCK @name field of the node (@type).', array('@name' => $name, '@type' => $field['type'])), ); } } } } } /** * Callback for mapping. Here is where the actual mapping happens. * * @param $node * Reference to the node object we are working on. * * @param $vid * The selected content_taxonomy CCK field. * * @param $terms * Given terms as array. If a string is used, it is converted to an array * using taxonomy_terms_parse_string($terms)->tids. * * @see taxonomy_terms_parse_string(). * */ function ente_planificador_import_extra_feeds_set_target(&$node, $target, $terms) { static $fields = array(); list($field_name, $match_key) = explode(':', $target, 2); $field = content_fields($field_name, $node->type); // Parse string for multiple tags (comma separated) if (is_string($terms)) { $terms = explode(',', $terms); } // Return if there are no or empty terms. if (!is_array($terms) || empty($terms)) { return; } $multiple = $field['widget']['multiple']; if ($match_key == 'newterm') { foreach ($terms as $k => $term_name) { $term_name = trim($term_name); if ($terms_found = ente_planificador_import_extra_get_term_by_name_vid($term_name, $field['vid'])) { // If any terms are found add them to the field by found tid. foreach ($terms_found as $term_found) { if (!is_array($node->$field_name)) $node->$field_name = array(); array_push($node->$field_name, array('value' => $term_found['tid'])); if ($multiple != 0 && count($node->$field_name) >= $multiple) { // If the vocab is not for multiple tags break after the first hit. break; } } } else { $edit = array('vid' => $field['vid'], 'name' => $term_name); taxonomy_save_term($edit); if (!is_array($node->$field_name)) { $node->$field_name = array(); } array_push($node->$field_name, array('value' => $edit['tid'])); } if ($multiple != 0 && count($node->$field_name) >= $multiple) { // If the vocab is not for multiple tags break after a first term has been added. break; } } } if ($match_key == 'hs') { $father = 0; foreach ($terms as $k => $term_name) { $term_name = trim($term_name); if ($terms_found = ente_planificador_import_extra_hs_get_term_by_name_vid($term_name, $field['vid'], $father)) { // If any terms are found add them to the field by found tid. foreach ($terms_found as $term_found) { if (!is_array($node->$field_name)) $node->$field_name = array(); array_push($node->$field_name, array('value' => $term_found['tid'])); $father = $term_found['tid']; $father = $term_found['tid']; if ($multiple != 0 && count($node->$field_name) >= $multiple) { // If the vocab is not for multiple tags break after the first hit. break; } } } else { if ($terms_found = ente_planificador_import_extra_get_term_by_name_vid($term_name, $field['vid'])) { // If any terms are found add them to the field by found tid. foreach ($terms_found as $term_found) { if (!is_array($node->$field_name)) $node->$field_name = array(); array_push($node->$field_name, array('value' => $term_found['tid'])); if ($multiple != 0 && count($node->$field_name) >= $multiple) { // If the vocab is not for multiple tags break after the first hit. break; } } } } } } } /** * Try to map a string to an existing term by name and vocabulary id. * * Provides a case-insensitive and trimmed mapping, to maximize the * likelihood of a successful match limited by a vocabulary id. * * @param $name * Name of the term to search for. * * @param $vid * The vocabulary's ID. * * @return * An array of matching term objects. */ function ente_planificador_import_extra_get_term_by_name_vid($name, $vid) { $db_result = db_query(db_rewrite_sql("SELECT t.tid, t.name FROM {term_data} t WHERE LOWER(t.name) = LOWER('%s') AND vid = %d", 't', 'tid'), trim($name), $vid); $result = array(); while ($term = db_fetch_array($db_result)) { $result[] = $term; } return $result; } function ente_planificador_import_extra_hs_get_term_by_name_vid($name, $vid, $father) { $result = db_query(db_rewrite_sql("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid AND h.parent = %d WHERE LOWER(t.name) = LOWER('%s') AND t.vid = %d ORDER BY weight, name", 't', 'tid'), $father, trim($name), $vid); $result = array(); while ($term = db_fetch_array($db_result)) { $result[] = $term; } return $result; }