92405c51032a39eb59f1872837c09d028e98eed8
[project/feeds.git] / mappers / taxonomy.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Mapper that exposes a node's taxonomy vocabularies as mapping targets.
7 */
8
9 /**
10 * Implements hook_feeds_parser_sources_alter().
11 */
12 function taxonomy_feeds_parser_sources_alter(&$sources, $content_type) {
13 if (!empty($content_type)) {
14 foreach (taxonomy_get_vocabularies($content_type) as $vocabulary) {
15 $sources['parent:taxonomy:'. taxonomy_vocabulary_id($vocabulary)] = array(
16 'name' => t('Feed node: Taxonomy: @vocabulary', array('@vocabulary' => $vocabulary->name)),
17 'description' => t('Taxonomy terms from feed node in given vocabulary.'),
18 'callback' => 'taxonomy_feeds_get_source',
19 );
20 }
21 }
22 }
23
24 /**
25 * Callback, returns taxonomy from feed node.
26 */
27 function taxonomy_feeds_get_source(FeedsImportBatch $batch, $key) {
28 if ($node = $batch->feedNode()) {
29 $terms = taxonomy_node_get_terms($node);
30 $vocabulary = taxonomy_get_vocabulary(str_replace('parent:taxonomy:', '', $key));
31 $result = array();
32 foreach ($terms as $tid => $term) {
33 if ($term->vid == $vocabulary->vid) {
34 $result[] = new FeedsTermElement($term);
35 }
36 }
37 return $result;
38 }
39 }
40
41 /**
42 * Implements hook_feeds_node_processor_targets_alter().
43 *
44 * @see FeedsNodeProcessor::getMappingTargets().
45 */
46 function taxonomy_feeds_node_processor_targets_alter(&$targets, $content_type) {
47 foreach (taxonomy_get_vocabularies($content_type) as $vocabulary) {
48 $description = t('The @name vocabulary of the node. If this is a "Tags" vocabulary, any new terms will be created on import. Otherwise only existing terms will be used. If this is not a "Tags" vocabulary and not a "Multiple select" vocabulary, only the first available term will be created. See !settings.', array('@name' => $vocabulary->name, '!settings' => l(t('vocabulary settings'), 'admin/content/taxonomy/edit/vocabulary/'. $vocabulary->vid, array('query' => 'destination='. $_GET['q']))));
49
50 $targets['taxonomy:'. taxonomy_vocabulary_id($vocabulary)] = array(
51 'name' => "Taxonomy: ". $vocabulary->name,
52 'callback' => 'taxonomy_feeds_set_target',
53 'description' => $description,
54 'real_target' => 'taxonomy',
55 );
56 }
57 }
58
59 /**
60 * Callback for mapping. Here is where the actual mapping happens.
61 *
62 * @param $node
63 * Reference to the node object we are working on.
64 *
65 * @param $key
66 * A key as added to the $targets array by
67 * taxonomy_feeds_node_processor_targets_alter().
68 *
69 * @param $terms
70 * Given terms as array.
71 *
72 * Add the given terms to the node object so the taxonomy module can add them
73 * on node_save().
74 */
75 function taxonomy_feeds_set_target(&$node, $key, $terms) {
76
77 // Return if there are no terms.
78 if (empty($terms)) {
79 return;
80 }
81
82 // Load target vocabulary to check, if it has the "tags" flag.
83 $vocabulary = taxonomy_get_vocabulary(str_replace('taxonomy:', '', $key));
84
85 // Cast a given single string to an array so we can use it.
86 if (!is_array($terms)) {
87 $terms = array($terms);
88 }
89
90 if ($vocabulary->tags) {
91 // Simply add a comma separated list to the node for a "tags" vocabulary.
92 $terms = array_merge($terms, drupal_explode_tags($node->taxonomy['tags'][$vocabulary->vid]));
93 $node->taxonomy['tags'][$vocabulary->vid] = implode(',', $terms);
94 }
95 else {
96 foreach ($terms as $term) {
97 if ($term instanceof FeedsTermElement) {
98 $node->taxonomy[$term->tid] = (object)$term;
99 }
100 // Check if a term already exists.
101 elseif ($terms_found = taxonomy_get_term_by_name_vid($term, $vocabulary->vid)) {
102 // If any terms are found add them to the node's taxonomy by found tid.
103 foreach ($terms_found AS $term_found) {
104 $node->taxonomy[$term_found->tid] = $term_found;
105 if (!$vocabulary->multiple) {
106 break;
107 }
108 }
109 }
110 // If the vocab is not for multiple tags break after the first hit.
111 if (!$vocabulary->multiple) {
112 break;
113 }
114 }
115 }
116 }
117
118 /**
119 * Try to map a string to an existing term by name and vocabulary id.
120 *
121 * Provides a case-insensitive and trimmed mapping, to maximize the likelihood
122 * of a successful match limited by a vocabulary id.
123 *
124 * @param $name
125 * Name of the term to search for.
126 *
127 * @param $vid
128 * The vocabulary's ID.
129 *
130 * @return
131 * An array of matching term objects.
132 */
133 function taxonomy_get_term_by_name_vid($name, $vid) {
134 $db_result = db_query(db_rewrite_sql("SELECT t.tid, t.name FROM {term_data} t WHERE LOWER(t.name) = LOWER('%s') AND t.vid = %d", 't', 'tid'), trim($name), $vid);
135 $result = array();
136 while ($term = db_fetch_object($db_result)) {
137 $result[] = $term;
138 }
139 return $result;
140 }
141
142 /**
143 * Look up a vocabulary by vid or module name.
144 *
145 * @param $id
146 * A module name or a numeric vocabulary id.
147 *
148 * @return
149 * An object of type stdClass that represents a vocabulary.
150 */
151 function taxonomy_get_vocabulary($id) {
152 static $vocabularies;
153 if (!isset($vocabularies[$id])) {
154 foreach (taxonomy_get_vocabularies() as $vocabulary) {
155 if ($vocabulary->vid == $id) {
156 $vocabularies[$id] = $vocabulary;
157 break;
158 }
159 elseif ($vocabulary->module == $id) {
160 $vocabularies[$id] = $vocabulary;
161 break;
162 }
163 }
164 }
165 return $vocabularies[$id];
166 }
167
168 /**
169 * Return the vocabulary identifier, the vocabulary's vid or module.
170 *
171 * @return
172 * Vocabulary's module name if it is a features vocabulary (= exportable),
173 * vocabulary's vid otherwise.
174 */
175 function taxonomy_vocabulary_id($vocabulary) {
176 if (strpos($vocabulary->module, 'features_') === 0) {
177 return $vocabulary->module;
178 }
179 return $vocabulary->vid;
180 }