1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Enable the user of feeds to map taxonomy terms from the feed for the |
---|
6 | * current node to content_taxonomy CCK fields. |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * Implementation of hook_feeds_node_processor_targets_alter(). |
---|
11 | * |
---|
12 | * @see FeedsNodeProcessor::getMappingTargets(). |
---|
13 | */ |
---|
14 | function ente_planificador_import_extra_feeds_node_processor_targets_alter(&$targets, $content_type) { |
---|
15 | $info = content_types($content_type); |
---|
16 | $fields = array(); |
---|
17 | if (isset($info['fields']) && count($info['fields'])) { |
---|
18 | $text_add = t('Save new Term'); |
---|
19 | $text_add1 = t('Hierarchical Term'); |
---|
20 | foreach ($info['fields'] as $field_name => $field) { |
---|
21 | if (in_array($field['type'], array('content_taxonomy'))) { |
---|
22 | $name = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name; |
---|
23 | $targets[$field_name . ':newterm'] = array( |
---|
24 | 'name' => check_plain($name) . ' (' . $text_add . ')', |
---|
25 | 'callback' => 'ente_planificador_import_extra_feeds_set_target', |
---|
26 | 'description' => t('The CCK @name field of the node (@type).', array('@name' => $name, '@type' => $field['type'])), |
---|
27 | ); |
---|
28 | if ($field['widget']['type'] == 'content_taxonomy_hs') { |
---|
29 | $targets[$field_name . ':hs'] = array( |
---|
30 | 'name' => check_plain($name) . ' (' . $text_add1 . ')', |
---|
31 | 'callback' => 'ente_planificador_import_extra_feeds_set_target', |
---|
32 | 'description' => t('The CCK @name field of the node (@type).', array('@name' => $name, '@type' => $field['type'])), |
---|
33 | ); |
---|
34 | } |
---|
35 | |
---|
36 | } |
---|
37 | } |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * Callback for mapping. Here is where the actual mapping happens. |
---|
43 | * |
---|
44 | * @param $node |
---|
45 | * Reference to the node object we are working on. |
---|
46 | * |
---|
47 | * @param $vid |
---|
48 | * The selected content_taxonomy CCK field. |
---|
49 | * |
---|
50 | * @param $terms |
---|
51 | * Given terms as array. If a string is used, it is converted to an array |
---|
52 | * using <code>taxonomy_terms_parse_string($terms)->tids</code>. |
---|
53 | * |
---|
54 | * @see taxonomy_terms_parse_string(). |
---|
55 | * |
---|
56 | */ |
---|
57 | function ente_planificador_import_extra_feeds_set_target(&$node, $target, $terms) { |
---|
58 | static $fields = array(); |
---|
59 | list($field_name, $match_key) = explode(':', $target, 2); |
---|
60 | $field = content_fields($field_name, $node->type); |
---|
61 | // Parse string for multiple tags (comma separated) |
---|
62 | if (is_string($terms)) { |
---|
63 | $terms = explode(',', $terms); |
---|
64 | } |
---|
65 | |
---|
66 | // Return if there are no or empty terms. |
---|
67 | if (!is_array($terms) || empty($terms)) { |
---|
68 | return; |
---|
69 | } |
---|
70 | $multiple = $field['widget']['multiple']; |
---|
71 | if ($match_key == 'newterm') { |
---|
72 | foreach ($terms as $k => $term_name) { |
---|
73 | $term_name = trim($term_name); |
---|
74 | if ($terms_found = ente_planificador_import_extra_get_term_by_name_vid($term_name, $field['vid'])) { |
---|
75 | // If any terms are found add them to the field by found tid. |
---|
76 | foreach ($terms_found as $term_found) { |
---|
77 | if (!is_array($node->$field_name)) $node->$field_name = array(); |
---|
78 | array_push($node->$field_name, array('value' => $term_found['tid'])); |
---|
79 | if ($multiple != 0 && count($node->$field_name) >= $multiple) { |
---|
80 | // If the vocab is not for multiple tags break after the first hit. |
---|
81 | break; |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | else { |
---|
86 | $edit = array('vid' => $field['vid'], 'name' => $term_name); |
---|
87 | taxonomy_save_term($edit); |
---|
88 | if (!is_array($node->$field_name)) { |
---|
89 | $node->$field_name = array(); |
---|
90 | } |
---|
91 | array_push($node->$field_name, array('value' => $edit['tid'])); |
---|
92 | } |
---|
93 | if ($multiple != 0 && count($node->$field_name) >= $multiple) { |
---|
94 | // If the vocab is not for multiple tags break after a first term has been added. |
---|
95 | break; |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | if ($match_key == 'hs') { |
---|
100 | $father = 0; |
---|
101 | foreach ($terms as $k => $term_name) { |
---|
102 | $term_name = trim($term_name); |
---|
103 | if ($terms_found = ente_planificador_import_extra_hs_get_term_by_name_vid($term_name, $field['vid'], $father)) { |
---|
104 | // If any terms are found add them to the field by found tid. |
---|
105 | foreach ($terms_found as $term_found) { |
---|
106 | if (!is_array($node->$field_name)) $node->$field_name = array(); |
---|
107 | array_push($node->$field_name, array('value' => $term_found['tid'])); |
---|
108 | $father = $term_found['tid']; |
---|
109 | $father = $term_found['tid']; |
---|
110 | if ($multiple != 0 && count($node->$field_name) >= $multiple) { |
---|
111 | // If the vocab is not for multiple tags break after the first hit. |
---|
112 | break; |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|
116 | else { |
---|
117 | if ($terms_found = ente_planificador_import_extra_get_term_by_name_vid($term_name, $field['vid'])) { |
---|
118 | // If any terms are found add them to the field by found tid. |
---|
119 | foreach ($terms_found as $term_found) { |
---|
120 | if (!is_array($node->$field_name)) $node->$field_name = array(); |
---|
121 | array_push($node->$field_name, array('value' => $term_found['tid'])); |
---|
122 | if ($multiple != 0 && count($node->$field_name) >= $multiple) { |
---|
123 | // If the vocab is not for multiple tags break after the first hit. |
---|
124 | break; |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * Try to map a string to an existing term by name and vocabulary id. |
---|
135 | * |
---|
136 | * Provides a case-insensitive and trimmed mapping, to maximize the |
---|
137 | * likelihood of a successful match limited by a vocabulary id. |
---|
138 | * |
---|
139 | * @param $name |
---|
140 | * Name of the term to search for. |
---|
141 | * |
---|
142 | * @param $vid |
---|
143 | * The vocabulary's ID. |
---|
144 | * |
---|
145 | * @return |
---|
146 | * An array of matching term objects. |
---|
147 | */ |
---|
148 | function ente_planificador_import_extra_get_term_by_name_vid($name, $vid) { |
---|
149 | $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); |
---|
150 | $result = array(); |
---|
151 | while ($term = db_fetch_array($db_result)) { |
---|
152 | $result[] = $term; |
---|
153 | } |
---|
154 | return $result; |
---|
155 | } |
---|
156 | |
---|
157 | function ente_planificador_import_extra_hs_get_term_by_name_vid($name, $vid, $father) { |
---|
158 | $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); |
---|
159 | $result = array(); |
---|
160 | while ($term = db_fetch_array($db_result)) { |
---|
161 | $result[] = $term; |
---|
162 | } |
---|
163 | return $result; |
---|
164 | } |
---|
165 | |
---|
166 | |
---|