| 1 |
<?php
|
| 2 |
/* $Id: aggregator2_autotaxonomy.module,v 1.8 2006/04/03 23:27:38 budda Exp $ */
|
| 3 |
|
| 4 |
/*
|
| 5 |
* Aggregator2 Autotaxonomy
|
| 6 |
*
|
| 7 |
* @file
|
| 8 |
* Allows Aggregator2 RSS items with <category> tags to auto create Drupal taxonomy terms.
|
| 9 |
* @author Mike Carter <mike@ixis.co.uk>
|
| 10 |
*/
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implementation of hook_help().
|
| 14 |
*/
|
| 15 |
function aggregator2_autotaxonomy_help($section) {
|
| 16 |
switch ($section) {
|
| 17 |
case 'admin/modules#description':
|
| 18 |
return t('Creates & assign taxonomy terms to RSS feed items from <category> tags.');
|
| 19 |
|
| 20 |
case 'admin/settings/aggregator2_autotaxonomy':
|
| 21 |
return '<p>' . t('If the %url include %category tag information
|
| 22 |
to organise the items, then this module will automatically create the same category in the
|
| 23 |
%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', '<category>')) ) . '</p>';
|
| 24 |
}
|
| 25 |
}
|
| 26 |
|
| 27 |
|
| 28 |
function aggregator2_autotaxonomy_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
|
| 29 |
static $cat_names = array();
|
| 30 |
|
| 31 |
if($node->type == 'aggregator2_item' && isset($node->rss_item_data)) {
|
| 32 |
$vocab = variable_get('aggregator2_autotaxonomy_vocabulary', FALSE);
|
| 33 |
if(!$vocab) {
|
| 34 |
watchdog('aggregator2', t('No vocabulary has been set for autotaxonomy to use.'), WATCHDOG_WARNING, l('set', 'admin/settings/aggregator2_autotaxonomy'));
|
| 35 |
return;
|
| 36 |
}
|
| 37 |
//[RSS:CATEGORY][][VALUE] && [RSS:CATEGORY][][DOMAIN] attrib
|
| 38 |
//[ATOM:CATEGORY][][TERM][][VALUE]
|
| 39 |
//[DC:SUBJECT][][VALUE]
|
| 40 |
if ($node->rss_item_data['CATEGORY'][0]['VALUE']) $categories = $node->rss_item_data['CATEGORY']; // RSS 0.92, 2.0
|
| 41 |
else if ($node->rss_item_data['CATEGORY'][0]['TERM'][0]['VALUE']) $categories = $node->rss_item_data['CATEGORY']; // ATOM 1.0
|
| 42 |
else if ($node->rss_item_data['ATOM:CATEGORY'][0]['TERM'][0]['VALUE']) $categories = $node->rss_item_data['ATOM:CATEGORY']; // ATOM 1.0
|
| 43 |
else if ($node->rss_item_data['SUBJECT']) $categories = $node->rss_item_data['SUBJECT']; // DublinCore
|
| 44 |
else if ($node->rss_item_data['DC:SUBJECT']) $categories = $node->rss_item_data['DC:SUBJECT']; // DublinCore
|
| 45 |
|
| 46 |
switch($op) {
|
| 47 |
case 'insert':
|
| 48 |
case 'update':
|
| 49 |
// If the RSS item has any category tags, add new ones to taxonomy if needed
|
| 50 |
if (count($categories) > 0) {
|
| 51 |
if (!isset($node->taxonomy) || !is_array($node->taxonomy)) {
|
| 52 |
$node->taxonomy = array();
|
| 53 |
}
|
| 54 |
|
| 55 |
foreach ($categories as $category_name) {
|
| 56 |
if ($category_name['TERM']) { // ATOM 1.0
|
| 57 |
$category_name = strtolower($category_name['TERM'][0]['VALUE']);
|
| 58 |
}
|
| 59 |
else { // RSS 0.92, RSS 2.0, DublinCore
|
| 60 |
$category_name = strtolower($category_name['VALUE']);
|
| 61 |
}
|
| 62 |
|
| 63 |
if (trim($category_name) != '') {
|
| 64 |
if (!isset($cat_names[$category_name])) {
|
| 65 |
$cat_names[$category_name] = module_invoke('taxonomy', 'get_term_by_name', $category_name);
|
| 66 |
}
|
| 67 |
|
| 68 |
// Create a new category term
|
| 69 |
if (count($cat_names[$category_name]) == 0) {
|
| 70 |
$term = array();
|
| 71 |
$term['name'] = $category_name;
|
| 72 |
$term['description'] = t('Auto generated by aggregator2 autotaxonomy');
|
| 73 |
$term['vid'] = $vocab;
|
| 74 |
$term['weight'] = 0;
|
| 75 |
$term = taxonomy_save_term($term);
|
| 76 |
$node->taxonomy[] = $term['tid'];
|
| 77 |
$cat_names[$category_name][0]->tid = $term['tid'];
|
| 78 |
}
|
| 79 |
else {
|
| 80 |
// Use the existing category term in the database
|
| 81 |
if (!in_array($cat_names[$category_name][0]->tid, $node->taxonomy)) {
|
| 82 |
$node->taxonomy[] = $cat_names[$category_name][0]->tid;
|
| 83 |
}
|
| 84 |
}
|
| 85 |
}
|
| 86 |
}
|
| 87 |
}
|
| 88 |
break;
|
| 89 |
}
|
| 90 |
}
|
| 91 |
}
|
| 92 |
|
| 93 |
|
| 94 |
function aggregator2_autotaxonomy_settings() {
|
| 95 |
$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');
|
| 96 |
|
| 97 |
// Build list of available vocabularies
|
| 98 |
$options = array();
|
| 99 |
while ($vocabulary = db_fetch_object($vocabs)) {
|
| 100 |
$options[$vocabulary->vid] = $vocabulary->name;
|
| 101 |
}
|
| 102 |
if($options) {
|
| 103 |
$form['aggregator2_autotaxonomy_vocabulary'] = array(
|
| 104 |
'#type' => 'select',
|
| 105 |
'#title' => t('Vocabulary'),
|
| 106 |
'#default_value' => variable_get('aggregator2_autotaxonomy_vocabulary', ''),
|
| 107 |
'#options' => $options,
|
| 108 |
'#description' => t('Which vocabulary should the auto created category names be associated with?')
|
| 109 |
);
|
| 110 |
}
|
| 111 |
else {
|
| 112 |
$form['helptext'] = array('#value' => '<p>' . t("You need to associate a %url with the node type %node to use this module.
|
| 113 |
If you don't have any vocabularies set-up you can %addurl now.",
|
| 114 |
array('%url' => l('vocabulary', 'admin/taxonomy'),
|
| 115 |
'%node' => theme('placeholder', 'feed item'),
|
| 116 |
'%addurl' => l('add one', 'admin/taxonomy/add/vocabulary')
|
| 117 |
)) . '</p>');
|
| 118 |
}
|
| 119 |
|
| 120 |
return $form;
|
| 121 |
}
|