Mercurial > hg > drupalISMI
comparison relation_processor/nodes.inc @ 5:1c73c660c2f2
new relation_processor
author | Dirk Wintergruen <dwinter@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 17 Jun 2015 18:55:14 +0200 |
parents | 124ef8f3b22d |
children | bdf91a4a40ff |
comparison
equal
deleted
inserted
replaced
0:124ef8f3b22d | 5:1c73c660c2f2 |
---|---|
23 | 23 |
24 /** | 24 /** |
25 * Search by CITEKEY. | 25 * Search by CITEKEY. |
26 */ | 26 */ |
27 define('FEEDS_ENTITY_SEARCH_TERM_CITEKEY', 3); | 27 define('FEEDS_ENTITY_SEARCH_TERM_CITEKEY', 3); |
28 | 28 dpm("RELATION_include"); |
29 /** | 29 /** |
30 * Implements hook_feeds_parser_sources_alter(). | 30 * Implements hook_feeds_parser_sources_alter(). |
31 */ | 31 */ |
32 function entity_feeds_parser_sources_alter(&$sources, $content_type) { | 32 function relation_feeds_parser_sources_alter(&$sources, $content_type) { |
33 | 33 |
34 if (!empty($content_type)) { | 34 if (!empty($content_type)) { |
35 $sources['sources'] = array( | 35 $sources['sources'] = array( |
36 'name' => t('Feed node: Entity'), | 36 'name' => t('Feed node: Entity'), |
37 'description' => t('Entites from feed node.'), | 37 'description' => t('Entites from feed node.'), |
42 | 42 |
43 | 43 |
44 /** | 44 /** |
45 * Callback, returns taxonomy from feed node. | 45 * Callback, returns taxonomy from feed node. |
46 */ | 46 */ |
47 function entity_feeds_get_source(FeedsSource $source, FeedsParserResult $result, $key) { | 47 function relation_feeds_get_source(FeedsSource $source, FeedsParserResult $result, $key) { |
48 /*if ($node = node_load($source->feed_nid)) { | 48 /*if ($node = node_load($source->feed_nid)) { |
49 $terms = taxonomy_feeds_node_get_terms($node); | 49 $terms = taxonomy_feeds_node_get_terms($node); |
50 $vocabularies = taxonomy_vocabulary_load_multiple(array(), array('machine_name' => str_replace('parent:taxonomy:', '', $key))); | 50 $vocabularies = taxonomy_vocabulary_load_multiple(array(), array('machine_name' => str_replace('parent:taxonomy:', '', $key))); |
51 $vocabulary = array_shift($vocabularies); | 51 $vocabulary = array_shift($vocabularies); |
52 $result = array(); | 52 $result = array(); |
61 } | 61 } |
62 | 62 |
63 /** | 63 /** |
64 * Implements hook_feeds_processor_targets_alter(). | 64 * Implements hook_feeds_processor_targets_alter(). |
65 */ | 65 */ |
66 function entity_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) { | 66 function relation_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) { |
67 /*foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) { | 67 /*foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) { |
68 $info = field_info_field($name); | 68 $info = field_info_field($name); |
69 if ($info['type'] == 'taxonomy_term_reference') { | 69 if ($info['type'] == 'taxonomy_term_reference') { |
70 $targets[$name] = array( | 70 $targets[$name] = array( |
71 'name' => check_plain($instance['label']), | 71 'name' => check_plain($instance['label']), |
80 | 80 |
81 if ($entity_type == "relation"){ | 81 if ($entity_type == "relation"){ |
82 | 82 |
83 $targets["source_id"]= array( | 83 $targets["source_id"]= array( |
84 'name' => 'Source Field', | 84 'name' => 'Source Field', |
85 'callback' => 'entity_feeds_set_target', | 85 'callback' => 'relation_feeds_set_target', |
86 'description' => t('The guid of the entity.'), | 86 'description' => t('The guid of the entity.'), |
87 'summary_callback' => 'entity_feeds_summary_callback', | 87 'summary_callback' => 'relation_feeds_summary_callback', |
88 'form_callback' => 'entity_feeds_form_callback', | 88 'form_callback' => 'relation_feeds_form_callback', |
89 ); | 89 ); |
90 | 90 |
91 $targets["target_id"]= array( | 91 $targets["target_id"]= array( |
92 'name' => 'Target Field', | 92 'name' => 'Target Field', |
93 'callback' => 'entity_feeds_set_target', | 93 'callback' => 'relation_feeds_set_target', |
94 'description' => t('The guid of the entity.'), | 94 'description' => t('The guid of the entity.'), |
95 'summary_callback' => 'entity_feeds_summary_callback', | 95 'summary_callback' => 'relation_feeds_summary_callback', |
96 'form_callback' => 'entity_feeds_form_callback', | 96 'form_callback' => 'relation_feeds_form_callback', |
97 ); | 97 ); |
98 | 98 |
99 $targets["relation_name"]= array( | 99 $targets["relation_name"]= array( |
100 'name' => 'Relation Type', | 100 'name' => 'Relation Type', |
101 'callback' => 'entity_feeds_set_relation_type', | 101 'callback' => 'relation_feeds_set_relation_type', |
102 | 102 |
103 ); | 103 ); |
104 | 104 |
105 } | 105 } |
106 | 106 |
110 | 110 |
111 /*callback for relation_types | 111 /*callback for relation_types |
112 * | 112 * |
113 * | 113 * |
114 */ | 114 */ |
115 function entity_feeds_set_relation_type($source, $entity, $target, $terms, $mapping = array()) { | 115 function relation_feeds_set_relation_type($source, $entity, $target, $terms, $mapping = array()) { |
116 | |
117 if (empty($terms)) { | 116 if (empty($terms)) { |
118 return; | 117 return; |
119 } | 118 } |
120 | 119 |
121 // don't Handle non-multiple values. | 120 // don't Handle non-multiple values. |
122 if (is_array($terms)) { | 121 if (is_array($terms)) { |
123 return; | 122 return; |
124 } | 123 } |
125 | 124 |
126 | 125 |
126 dpm("CHECK: TYPE"); | |
127 dpm($terms); | |
128 | |
127 $types=relation_get_types(array($terms)); | 129 $types=relation_get_types(array($terms)); |
128 | 130 |
131 | |
129 if(sizeof($types)==0){ //typ gibt es nicht dann erzeuge mit defualt werten #TODO konfigurierbar? | 132 if(sizeof($types)==0){ //typ gibt es nicht dann erzeuge mit defualt werten #TODO konfigurierbar? |
130 dpm("CREATE RELATION TYPE:" . $terms); | 133 |
131 $info = array(); | 134 $info = array(); |
132 $info['relation_type']=$terms; | 135 $info['relation_type']=$terms; |
133 $info['directional']=TRUE; | 136 $info['directional']=TRUE; |
134 $info['source_bundles'][]="node:*"; | 137 $info['source_bundles'][]="node:*"; |
135 $info['target_bundles'][]="node:*"; | 138 $info['target_bundles'][]="node:*"; |
136 $info['reverse_label']="invOf_" . $terms; | 139 $info['reverse_label']="invOf_" . $terms; |
137 $rel_type = relation_type_create($info); | 140 $rel_type = relation_type_create($info); |
138 relation_type_save($rel_type); | 141 relation_type_save($rel_type); |
139 } | 142 } |
140 | 143 |
144 | |
141 $entity->relation_type=$terms; | 145 $entity->relation_type=$terms; |
146 | |
147 if ( _relation_feeds_checkRelationComplete($entity)){ | |
148 | |
149 $entity->is_new=FALSE; | |
150 } | |
142 relation_update($entity); | 151 relation_update($entity); |
152 | |
153 } | |
154 | |
155 | |
156 function _relation_feeds_checkRelationComplete($relation){ | |
157 //einer der Endpunkte existiert nicht | |
158 | |
159 if (count($relation->endpoints['und'][0]) == 0) | |
160 return false; | |
161 if (!isset($relation->endpoints['und'][1])) | |
162 return false; | |
163 if (count($relation->endpoints['und'][1]) == 0) | |
164 return false; | |
165 | |
166 if ($relation->relation_type == "generic") | |
167 return false; | |
168 return true; | |
169 | |
170 } | |
171 | |
172 function _relation_feeds_checkEndpointsComplete($relation){ | |
173 //einer der Endpunkte existiert nicht | |
174 | |
175 | |
176 if (count($relation->endpoints['und'][0]) == 0) | |
177 return false; | |
178 if (!isset($relation->endpoints['und'][1])) | |
179 return false; | |
180 if (count($relation->endpoints['und'][1]) == 0) | |
181 return false; | |
182 | |
183 return true; | |
184 | |
143 } | 185 } |
144 | 186 |
145 /** | 187 /** |
146 * Callback for mapping. Here is where the actual mapping happens. | 188 * Callback for mapping. Here is where the actual mapping happens. |
147 * | 189 * |
148 * @todo Do not create new terms for non-autotag fields. | 190 * @todo Do not create new terms for non-autotag fields. |
149 */ | 191 */ |
150 function entity_feeds_set_target($source, $entity, $target, $terms, $mapping = array()) { | 192 function relation_feeds_set_target($source, $entity, $target, $terms, $mapping = array()) { |
151 | 193 |
152 // Allow mapping the string '0' to a term name. | 194 // Allow mapping the string '0' to a term name. |
153 if (empty($terms) && $terms != 0) { | 195 if (empty($terms) && $terms != 0) { |
154 return; | 196 return; |
155 } | 197 } |
204 break; | 246 break; |
205 | 247 |
206 // Lookup by GUID. | 248 // Lookup by GUID. |
207 case FEEDS_ENTITY_SEARCH_TERM_GUID: | 249 case FEEDS_ENTITY_SEARCH_TERM_GUID: |
208 | 250 |
209 $tid = entity_feeds_entity_lookup_entity_by_guid($term); | 251 |
210 | 252 $tid = relation_feeds_entity_lookup_entity_by_guid($term); |
253 | |
211 break; | 254 break; |
212 | 255 |
213 case FEEDS_ENTITY_SEARCH_TERM_CITEKEY: | 256 case FEEDS_ENTITY_SEARCH_TERM_CITEKEY: |
214 $tid = _searchEntityByBiblioValue("biblio","citekey",$term); | 257 $tid = _searchEntityByBiblioValue("biblio","citekey",$term); |
215 } | 258 } |
216 } | 259 } |
260 | |
217 | 261 |
218 | 262 |
219 $eps = $entity->endpoints[LANGUAGE_NONE]; | 263 $eps = $entity->endpoints[LANGUAGE_NONE]; |
220 if (!isset($entity->endpoints[LANGUAGE_NONE])){ // noch nicht existent | 264 if (!isset($entity->endpoints[LANGUAGE_NONE])){ // noch nicht existent |
221 $entity->endpoints[LANGUAGE_NONE][]=array(); | 265 $entity->endpoints[LANGUAGE_NONE][]=array(); |
223 $entity->endpoints[LANGUAGE_NONE][]=array(); | 267 $entity->endpoints[LANGUAGE_NONE][]=array(); |
224 } | 268 } |
225 | 269 |
226 switch ($target){ | 270 switch ($target){ |
227 case "source_id": | 271 case "source_id": |
272 | |
228 // source is immer der erste eintrag | 273 // source is immer der erste eintrag |
229 $entity->endpoints[LANGUAGE_NONE][0]=array('entity_type' => 'node', 'entity_id' => $tid); | 274 $entity->endpoints[LANGUAGE_NONE][0]=array('entity_type' => 'node', 'entity_id' => $tid); |
230 break; | 275 break; |
231 | 276 |
232 case "target_id": | 277 case "target_id": |
233 //target können mehrere existieren, hinten anfügen. | 278 //target können mehrere existieren, hinten anfügen. |
234 $entity->endpoints[LANGUAGE_NONE][]=array('entity_type' => 'node', 'entity_id' => $tid); | 279 $entity->endpoints[LANGUAGE_NONE][]=array('entity_type' => 'node', 'entity_id' => $tid); |
235 } | 280 } |
236 | 281 |
237 //dpm($entity); | 282 |
238 | 283 |
239 | 284 if ( _relation_feeds_checkRelationComplete($entity)){ |
240 //if ($target=="target_id"){ | 285 |
241 relation_update($entity); | 286 $entity->is_new=FALSE; |
242 | 287 relation_update($entity); |
243 | 288 } |
244 if (sizeof($entity->endpoints[LANGUAGE_NONE])>1){ // WENN ZWEI ENDPUNKTE VORHANDEN DANN abspeichern | 289 |
245 $entity->is_new=FALSE; | 290 |
246 | 291 if ( _relation_feeds_checkEndpointsComplete($entity)){ |
247 } | 292 |
293 relation_update($entity); | |
294 } | |
295 | |
296 # if ($target=="source_id"){ | |
297 # relation_update($entity); | |
298 # } | |
299 | |
300 #if (sizeof($entity->endpoints[LANGUAGE_NONE])>1){ // WENN ZWEI ENDPUNKTE VORHANDEN DANN abspeichern | |
301 | |
302 //$entity->is_new=FALSE; | |
303 | |
304 #} | |
248 // } | 305 // } |
249 } | 306 } |
250 | 307 |
251 /** | 308 /** |
252 * Finds all terms associated with the given node, within one vocabulary. | 309 * Finds all terms associated with the given node, within one vocabulary. |
291 * Term id extracted from $item. | 348 * Term id extracted from $item. |
292 * | 349 * |
293 * @see taxonomy_feeds_node_get_terms() | 350 * @see taxonomy_feeds_node_get_terms() |
294 * @see field_get_items() | 351 * @see field_get_items() |
295 */ | 352 */ |
296 function _entity_feeds_extract_tid($item) { | 353 function _relation_feeds_extract_tid($item) { |
297 return $item['tid']; | 354 return $item['tid']; |
298 } | 355 } |
299 | 356 |
300 /** | 357 /** |
301 * Looks up a term by GUID, assumes SQL storage backend. | 358 * Looks up a term by GUID, assumes SQL storage backend. |
304 * The Feeds GUID to compare against. | 361 * The Feeds GUID to compare against. |
305 * | 362 * |
306 * @return int|FALSE | 363 * @return int|FALSE |
307 * The term id, or FALSE if one was not found. | 364 * The term id, or FALSE if one was not found. |
308 */ | 365 */ |
309 function entity_feeds_entity_lookup_entity_by_guid($guid) { | 366 function relation_feeds_entity_lookup_entity_by_guid($guid) { |
310 | 367 |
311 | 368 |
312 $res = db_select('feeds_item') | 369 $res = db_select('feeds_item') |
313 ->fields('feeds_item', array('entity_id')) | 370 ->fields('feeds_item', array('entity_id')) |
314 //->condition('feed_nid', $source->feed_nid) | 371 //->condition('feed_nid', $source->feed_nid) |
345 * Returns, as a string that may contain HTML, the summary to display while | 402 * Returns, as a string that may contain HTML, the summary to display while |
346 * the full form isn't visible. | 403 * the full form isn't visible. |
347 * If the return value is empty, no summary and no option to view the form | 404 * If the return value is empty, no summary and no option to view the form |
348 * will be displayed. | 405 * will be displayed. |
349 */ | 406 */ |
350 function entity_feeds_summary_callback($mapping, $target, $form, $form_state) { | 407 function relation_feeds_summary_callback($mapping, $target, $form, $form_state) { |
351 $options = _entity_feeds_form_callback_options(); | 408 $options = _relation_feeds_form_callback_options(); |
352 if (empty($mapping['term_search'])) { | 409 if (empty($mapping['term_search'])) { |
353 return t('Search Entity terms by: <strong>@search</strong>', array('@search' => $options[FEEDS_ENTITY_SEARCH_TERM_NAME])); | 410 return t('Search Entity terms by: <strong>@search</strong>', array('@search' => $options[FEEDS_ENTITY_SEARCH_TERM_NAME])); |
354 } | 411 } |
355 return t('Search Entity terms by: <strong>@search</strong>', array('@search' => $options[$mapping['term_search']])); | 412 return t('Search Entity terms by: <strong>@search</strong>', array('@search' => $options[$mapping['term_search']])); |
356 } | 413 } |
360 * | 417 * |
361 * @return array | 418 * @return array |
362 * The per mapping configuration form. Once the form is saved, $mapping will | 419 * The per mapping configuration form. Once the form is saved, $mapping will |
363 * be populated with the form values. | 420 * be populated with the form values. |
364 */ | 421 */ |
365 function entity_feeds_form_callback($mapping, $target, $form, $form_state) { | 422 function relation_feeds_form_callback($mapping, $target, $form, $form_state) { |
366 return array( | 423 return array( |
367 'term_search' => array( | 424 'term_search' => array( |
368 '#type' => 'select', | 425 '#type' => 'select', |
369 '#title' => t('Search Entity by'), | 426 '#title' => t('Search Entity by'), |
370 '#options' => _entity_feeds_form_callback_options(), | 427 '#options' => _relation_feeds_form_callback_options(), |
371 '#default_value' => !empty($mapping['term_search']) ? $mapping['term_search'] : FEEDS_ENTITY_SEARCH_TERM_NAME, | 428 '#default_value' => !empty($mapping['term_search']) ? $mapping['term_search'] : FEEDS_ENTITY_SEARCH_TERM_NAME, |
372 ), | 429 ), |
373 ); | 430 ); |
374 } | 431 } |
375 | 432 |
377 * Returns the list of available term search methods. | 434 * Returns the list of available term search methods. |
378 * | 435 * |
379 * @return array | 436 * @return array |
380 * An array of taxonomy search option titles. | 437 * An array of taxonomy search option titles. |
381 */ | 438 */ |
382 function _entity_feeds_form_callback_options() { | 439 function _relation_feeds_form_callback_options() { |
383 return array( | 440 return array( |
384 FEEDS_ENTITY_SEARCH_TERM_NAME => 'Entity name', | 441 FEEDS_ENTITY_SEARCH_TERM_NAME => 'Entity name', |
385 FEEDS_ENTITY_SEARCH_TERM_ID => 'Entity ID', | 442 FEEDS_ENTITY_SEARCH_TERM_ID => 'Entity ID', |
386 FEEDS_ENTITY_SEARCH_TERM_GUID => 'GUID', | 443 FEEDS_ENTITY_SEARCH_TERM_GUID => 'GUID', |
387 FEEDS_ENTITY_SEARCH_TERM_CITEKEY => 'CITEKEY' | 444 FEEDS_ENTITY_SEARCH_TERM_CITEKEY => 'CITEKEY' |