tags to auto create Drupal taxonomy terms. * @author Mike Carter */ /** * Implementation of hook_help(). */ function aggregator2_autotaxonomy_help($section) { switch ($section) { case 'admin/modules#description': return t('Creates & assign taxonomy terms to RSS feed items from <category> tags.'); case 'admin/settings/aggregator2_autotaxonomy': return '

' . t('If the %url include %category tag information to organise the items, then this module will automatically create the same category in the %taxurl so that you can organise your aggregated posts.', array('%taxurl' => l('taxonomy system', 'admin/taxonomy'), '%url' => l('RSS feeds you are aggregating', 'admin/aggregator2'), '%category' => theme('placeholder', '')) ) . '

'; } } function aggregator2_autotaxonomy_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { $cat_names = array(); if($node->type == 'aggregator2_item' && isset($node->rss_item_data)) { $vocab = variable_get('aggregator2_autotaxonomy_vocabulary', FALSE); if(!$vocab) { watchdog('aggregator2', t('No vocabulary has been set for autotaxonomy to use.'), WATCHDOG_WARNING, l('set', 'admin/settings/aggregator2_autotaxonomy')); return; } //[RSS:CATEGORY][][VALUE] && [RSS:CATEGORY][][DOMAIN] attrib //[ATOM:CATEGORY][][TERM][][VALUE] //[DC:SUBJECT][][VALUE] if ($node->rss_item_data['CATEGORY'][0]['VALUE']) $categories = $node->rss_item_data['CATEGORY']; // RSS 0.92, 2.0 else if (is_string($node->rss_item_data['CATEGORY'][0]['TERM'])) $categories = $node->rss_item_data['CATEGORY']; // ATOM 1.0 else if ($node->rss_item_data['CATEGORY'][0]['TERM'][0]['VALUE']) $categories = $node->rss_item_data['CATEGORY']; // ATOM 1.0 else if ($node->rss_item_data['ATOM:CATEGORY'][0]['TERM'][0]['VALUE']) $categories = $node->rss_item_data['ATOM:CATEGORY']; // ATOM 1.0 else if ($node->rss_item_data['SUBJECT']) $categories = $node->rss_item_data['SUBJECT']; // DublinCore else if ($node->rss_item_data['DC:SUBJECT']) $categories = $node->rss_item_data['DC:SUBJECT']; // DublinCore switch($op) { //case 'insert': //case 'update': // The aggregator2 module uses hook_prepare, so we need to account for it. The other two hooks don't matter case 'prepare': //prevent dupes $parsed_cats = array(); // If the RSS item has any category tags, add new ones to taxonomy if needed if (count($categories) > 0) { if (!isset($node->taxonomy) || !is_array($node->taxonomy)) { $node->taxonomy = array(); } foreach ($categories as $category_name) { if ($category_name['TERM']) { // ATOM 1.0 if (is_string($category_name['TERM'])) { $category_name = strtolower($category_name['TERM']); } else { $category_name = strtolower($category_name['TERM'][0]['VALUE']); } } else { // RSS 0.92, RSS 2.0, DublinCore $category_name = strtolower($category_name['VALUE']); } $category_name = trim($category_name); // explode comma-separated category lists $category_names = explode(",", $category_name); foreach ($category_names as $category_name) { $category_name = trim($category_name); if ($category_name != '') { if (!isset($cat_names[$category_name])) { $cat_names[$category_name] = taxonomy_get_term_by_name($category_name); } /* print '
';
	              print 'CatName: ';
	              print_r($cat_names[$category_name]);
	              print '
'; print '
';
	              print 'Parsed: ';
	              print_r($parsed_cats);
	              print '
'; */ // Create a new category term if (empty($cat_names[$category_name]) && !in_array($category_name, $parsed_cats)) { $term = array(); $term['name'] = $category_name; $term['description'] = "More stories about $category_name"; $term['vid'] = $vocab; $term['weight'] = 0; // broken //$term = taxonomy_save_term($term); taxonomy_save_term($term); $parsed_cats[] = $category_name; // prevent dupes $terms = taxonomy_get_term_by_name($category_name); /* print '
';
	              print 'Name: ' . $term['name'];
	              print '
'; */ foreach ($terms as $term) { $node->taxonomy[$term->tid] = $term; } // $node->taxonomy[] = array($term['name']); // $cat_names[$category_name][0]->tid = $term['tid']; } else { // Use the existing category term in the database // if (!in_array($cat_names[$category_name][0]->tid, $node->taxonomy)) { $terms = taxonomy_get_term_by_name($category_name); foreach ($terms as $term) { if (!in_array($term->name, $parsed_cats) && !in_array($term->tid, $node->taxonomy)) { $node->taxonomy[$term->tid] = $term; $parsed_cats[] = $category_name; // prevent dupes /* print '
';
	                print 'CName: ' . $category_name;
	               print '
'; */ } } // $node->taxonomy[$cat_names[$category_name][0]->tid] =taxonomy_get_term_by_name($cat_names[$category_name][0]->name); // } } } // end new foreach } } } break; } } } function aggregator2_autotaxonomy_settings() { $vocabs = db_query("SELECT v.vid, v.name FROM {vocabulary} v INNER JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", 'aggregator2_item'); // Build list of available vocabularies $options = array(); while ($vocabulary = db_fetch_object($vocabs)) { $options[$vocabulary->vid] = $vocabulary->name; } if($options) { $form['aggregator2_autotaxonomy_vocabulary'] = array( '#type' => 'select', '#title' => t('Vocabulary'), '#default_value' => variable_get('aggregator2_autotaxonomy_vocabulary', ''), '#options' => $options, '#description' => t('Which vocabulary should the auto created category names be associated with?') ); } else { $form['helptext'] = array('#value' => '

' . t("You need to associate a %url with the node type %node to use this module. If you don't have any vocabularies set-up you can %addurl now.", array('%url' => l('vocabulary', 'admin/taxonomy'), '%node' => theme('placeholder', 'feed item'), '%addurl' => l('add one', 'admin/taxonomy/add/vocabulary') )) . '

'); } return $form; }