| 1 |
<?php
|
| 2 |
// $Id: $
|
| 3 |
/**
|
| 4 |
* On behalf implementation of feed element mapper for taxonomy.module.
|
| 5 |
*/
|
| 6 |
|
| 7 |
/**
|
| 8 |
* Implementation of hook_feedapi_mapper().
|
| 9 |
* Maps feed elements to taxonomy values.
|
| 10 |
* @param $op
|
| 11 |
* 'list' or 'map'
|
| 12 |
* @param $node
|
| 13 |
* Feed item node to map on.
|
| 14 |
* @param $field_name
|
| 15 |
* Name of field to map to.
|
| 16 |
* @param $feed_element
|
| 17 |
* Feed item element to map from. Parameter only present on $op = 'map'
|
| 18 |
* @param @sub_field
|
| 19 |
* Subfield on field to map to. Parameter only present on $op = 'map'.
|
| 20 |
* This parameter will depend on if the hook implementation returns a subfield on
|
| 21 |
* $op = 'list'.
|
| 22 |
*
|
| 23 |
* Return key => name array of sub fields on 'list'.
|
| 24 |
* If only one field and no subfields are available for this content type return TRUE.
|
| 25 |
* If no fields available for this content type return FALSE.
|
| 26 |
* Options are necessary because a field like
|
| 27 |
* e. g. "taxonomy" might have more than one slot for information - e. g. vocabularies.
|
| 28 |
*
|
| 29 |
* Todo: $node could be passed by reference - implementers wouldn't need to return node
|
| 30 |
* (PHP5 passes in by reference by default).
|
| 31 |
*/
|
| 32 |
function taxonomy_feedapi_mapper($op, $node, $field_name, $feed_element = array(), $sub_field = '') {
|
| 33 |
if ($field_name == 'taxonomy') {
|
| 34 |
if ($op == 'describe') {
|
| 35 |
return t('Maps a string or an array of strings to taxonomy terms. Chose a vocabulary from sub options.');
|
| 36 |
}
|
| 37 |
else if ($op == 'list') {
|
| 38 |
if ($vocabularies = taxonomy_get_vocabularies($node->type)) {
|
| 39 |
foreach ($vocabularies as $v) {
|
| 40 |
$sub_fields[$v->vid] = $v->name;
|
| 41 |
}
|
| 42 |
return $sub_fields;
|
| 43 |
}
|
| 44 |
return FALSE;
|
| 45 |
}
|
| 46 |
else if ($op == 'map') {
|
| 47 |
// Todo: some plausibility check of $feed_element
|
| 48 |
// Todo: security check of $feed_element
|
| 49 |
if (is_string($feed_element)) {
|
| 50 |
$feed_element = array($feed_element);
|
| 51 |
}
|
| 52 |
if (is_array($feed_element)) {
|
| 53 |
if (!is_array($node->taxonomy)) {
|
| 54 |
$node->taxonomy = array();
|
| 55 |
}
|
| 56 |
$node->taxonomy = array_merge($node->taxonomy, _feedapi_mapper_create_terms($feed_element, $sub_field));
|
| 57 |
}
|
| 58 |
return $node;
|
| 59 |
}
|
| 60 |
}
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Creates terms from keywords, borrowed from yahoo_terms module.
|
| 65 |
* Todo: simplify.
|
| 66 |
*/
|
| 67 |
function _feedapi_mapper_create_terms($keywords, $vid, $static = FALSE) {
|
| 68 |
if ($vid == FALSE) {
|
| 69 |
return FALSE;
|
| 70 |
}
|
| 71 |
$tids = array();
|
| 72 |
if (is_array($keywords)) {
|
| 73 |
foreach ($keywords as $term) {
|
| 74 |
$curr_terms = taxonomy_get_term_by_name($term);
|
| 75 |
if (count($curr_terms) != 0) {
|
| 76 |
foreach ($curr_terms as $curr_term) {
|
| 77 |
if ($curr_term->vid == $vid) {
|
| 78 |
$tids[$curr_term->tid] = $curr_term;
|
| 79 |
}
|
| 80 |
}
|
| 81 |
if (count($tids) == 0 and $static == FALSE) {
|
| 82 |
$new_term['name'] = $term;
|
| 83 |
$new_term['vid'] = $vid;
|
| 84 |
taxonomy_save_term($new_term);
|
| 85 |
$tids[$new_term['tid']] = taxonomy_get_term($new_term['tid']);
|
| 86 |
unset($new_term);
|
| 87 |
}
|
| 88 |
}
|
| 89 |
else {
|
| 90 |
if ($static == FALSE) {
|
| 91 |
$new_term['name'] = $term;
|
| 92 |
$new_term['vid'] = $vid;
|
| 93 |
taxonomy_save_term($new_term);
|
| 94 |
$tids[$new_term['tid']] = taxonomy_get_term($new_term['tid']);
|
| 95 |
unset($new_term);
|
| 96 |
}
|
| 97 |
}
|
| 98 |
}
|
| 99 |
}
|
| 100 |
return $tids;
|
| 101 |
}
|