Mercurial > hg > drupalISMI
comparison relation_processor/nodes.inc @ 0:124ef8f3b22d
initial
| author | Dirk Wintergruen <dwinter@mpiwg-berlin.mpg.de> |
|---|---|
| date | Fri, 27 Mar 2015 19:21:42 +0100 |
| parents | |
| children | 1c73c660c2f2 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:124ef8f3b22d |
|---|---|
| 1 <?php | |
| 2 | |
| 3 /** | |
| 4 * @file | |
| 5 * On behalf implementation of Feeds mapping API for taxonomy.module. | |
| 6 */ | |
| 7 | |
| 8 /** | |
| 9 * Search by term name. | |
| 10 */ | |
| 11 define('FEEDS_ENTITY_SEARCH_TERM_NAME', 0); | |
| 12 | |
| 13 /** | |
| 14 * Search by term id. | |
| 15 */ | |
| 16 define('FEEDS_ENTITY_SEARCH_TERM_ID', 1); | |
| 17 | |
| 18 /** | |
| 19 * Search by GUID. | |
| 20 */ | |
| 21 define('FEEDS_ENTITY_SEARCH_TERM_GUID', 2); | |
| 22 | |
| 23 | |
| 24 /** | |
| 25 * Search by CITEKEY. | |
| 26 */ | |
| 27 define('FEEDS_ENTITY_SEARCH_TERM_CITEKEY', 3); | |
| 28 | |
| 29 /** | |
| 30 * Implements hook_feeds_parser_sources_alter(). | |
| 31 */ | |
| 32 function entity_feeds_parser_sources_alter(&$sources, $content_type) { | |
| 33 | |
| 34 if (!empty($content_type)) { | |
| 35 $sources['sources'] = array( | |
| 36 'name' => t('Feed node: Entity'), | |
| 37 'description' => t('Entites from feed node.'), | |
| 38 'callback' => 'entites_feeds_get_source', | |
| 39 ); | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 | |
| 44 /** | |
| 45 * Callback, returns taxonomy from feed node. | |
| 46 */ | |
| 47 function entity_feeds_get_source(FeedsSource $source, FeedsParserResult $result, $key) { | |
| 48 /*if ($node = node_load($source->feed_nid)) { | |
| 49 $terms = taxonomy_feeds_node_get_terms($node); | |
| 50 $vocabularies = taxonomy_vocabulary_load_multiple(array(), array('machine_name' => str_replace('parent:taxonomy:', '', $key))); | |
| 51 $vocabulary = array_shift($vocabularies); | |
| 52 $result = array(); | |
| 53 foreach ($terms as $tid => $term) { | |
| 54 if ($term->vid == $vocabulary->vid) { | |
| 55 $result[] = new FeedsTermElement($term); | |
| 56 } | |
| 57 }*/ | |
| 58 | |
| 59 return $result; | |
| 60 //} | |
| 61 } | |
| 62 | |
| 63 /** | |
| 64 * Implements hook_feeds_processor_targets_alter(). | |
| 65 */ | |
| 66 function entity_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) { | |
| 67 /*foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) { | |
| 68 $info = field_info_field($name); | |
| 69 if ($info['type'] == 'taxonomy_term_reference') { | |
| 70 $targets[$name] = array( | |
| 71 'name' => check_plain($instance['label']), | |
| 72 'callback' => 'taxonomy_feeds_set_target', | |
| 73 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])), | |
| 74 'summary_callback' => 'taxonomy_feeds_summary_callback', | |
| 75 'form_callback' => 'taxonomy_feeds_form_callback', | |
| 76 ); | |
| 77 } | |
| 78 }*/ | |
| 79 | |
| 80 | |
| 81 if ($entity_type == "relation"){ | |
| 82 | |
| 83 $targets["source_id"]= array( | |
| 84 'name' => 'Source Field', | |
| 85 'callback' => 'entity_feeds_set_target', | |
| 86 'description' => t('The guid of the entity.'), | |
| 87 'summary_callback' => 'entity_feeds_summary_callback', | |
| 88 'form_callback' => 'entity_feeds_form_callback', | |
| 89 ); | |
| 90 | |
| 91 $targets["target_id"]= array( | |
| 92 'name' => 'Target Field', | |
| 93 'callback' => 'entity_feeds_set_target', | |
| 94 'description' => t('The guid of the entity.'), | |
| 95 'summary_callback' => 'entity_feeds_summary_callback', | |
| 96 'form_callback' => 'entity_feeds_form_callback', | |
| 97 ); | |
| 98 | |
| 99 $targets["relation_name"]= array( | |
| 100 'name' => 'Relation Type', | |
| 101 'callback' => 'entity_feeds_set_relation_type', | |
| 102 | |
| 103 ); | |
| 104 | |
| 105 } | |
| 106 | |
| 107 } | |
| 108 | |
| 109 | |
| 110 | |
| 111 /*callback for relation_types | |
| 112 * | |
| 113 * | |
| 114 */ | |
| 115 function entity_feeds_set_relation_type($source, $entity, $target, $terms, $mapping = array()) { | |
| 116 | |
| 117 if (empty($terms)) { | |
| 118 return; | |
| 119 } | |
| 120 | |
| 121 // don't Handle non-multiple values. | |
| 122 if (is_array($terms)) { | |
| 123 return; | |
| 124 } | |
| 125 | |
| 126 | |
| 127 $types=relation_get_types(array($terms)); | |
| 128 | |
| 129 if(sizeof($types)==0){ //typ gibt es nicht dann erzeuge mit defualt werten #TODO konfigurierbar? | |
| 130 dpm("CREATE RELATION TYPE:" . $terms); | |
| 131 $info = array(); | |
| 132 $info['relation_type']=$terms; | |
| 133 $info['directional']=TRUE; | |
| 134 $info['source_bundles'][]="node:*"; | |
| 135 $info['target_bundles'][]="node:*"; | |
| 136 $info['reverse_label']="invOf_" . $terms; | |
| 137 $rel_type = relation_type_create($info); | |
| 138 relation_type_save($rel_type); | |
| 139 } | |
| 140 | |
| 141 $entity->relation_type=$terms; | |
| 142 relation_update($entity); | |
| 143 } | |
| 144 | |
| 145 /** | |
| 146 * Callback for mapping. Here is where the actual mapping happens. | |
| 147 * | |
| 148 * @todo Do not create new terms for non-autotag fields. | |
| 149 */ | |
| 150 function entity_feeds_set_target($source, $entity, $target, $terms, $mapping = array()) { | |
| 151 | |
| 152 // Allow mapping the string '0' to a term name. | |
| 153 if (empty($terms) && $terms != 0) { | |
| 154 return; | |
| 155 } | |
| 156 | |
| 157 // Handle non-multiple values. | |
| 158 if (!is_array($terms)) { | |
| 159 $terms = array($terms); | |
| 160 } | |
| 161 | |
| 162 // Add in default values. | |
| 163 $mapping += array( | |
| 164 'term_search' => FEEDS_ENTITY_SEARCH_TERM_NAME, | |
| 165 'autocreate' => FALSE, | |
| 166 ); | |
| 167 | |
| 168 | |
| 169 $cache = &drupal_static(__FUNCTION__); | |
| 170 | |
| 171 | |
| 172 //$query = new EntityFieldQuery(); | |
| 173 //$query->entityCondition('entity_type', 'node') | |
| 174 // ->range(0, 1); | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 // Iterate over all values. | |
| 182 | |
| 183 | |
| 184 #$mapping['term_search']=FEEDS_ENTITY_SEARCH_TERM_GUID; | |
| 185 foreach ($terms as $term) { | |
| 186 //dpm($term); | |
| 187 | |
| 188 | |
| 189 $tid = FALSE; | |
| 190 | |
| 191 switch ($mapping['term_search']){ | |
| 192 // Lookup by name. | |
| 193 case FEEDS_ENTITY_SEARCH_TERM_NAME: | |
| 194 $name_query = clone $query; | |
| 195 if ($tids = $name_query->propertyCondition('name', $term)->execute()) { | |
| 196 $tid = key($tids['taxonomy_term']); | |
| 197 } | |
| 198 | |
| 199 // Lookup by tid. | |
| 200 case FEEDS_ENTITY_SEARCH_TERM_ID: | |
| 201 if (is_numeric($term)) { | |
| 202 $tid = $term; | |
| 203 } | |
| 204 break; | |
| 205 | |
| 206 // Lookup by GUID. | |
| 207 case FEEDS_ENTITY_SEARCH_TERM_GUID: | |
| 208 | |
| 209 $tid = entity_feeds_entity_lookup_entity_by_guid($term); | |
| 210 | |
| 211 break; | |
| 212 | |
| 213 case FEEDS_ENTITY_SEARCH_TERM_CITEKEY: | |
| 214 $tid = _searchEntityByBiblioValue("biblio","citekey",$term); | |
| 215 } | |
| 216 } | |
| 217 | |
| 218 | |
| 219 $eps = $entity->endpoints[LANGUAGE_NONE]; | |
| 220 if (!isset($entity->endpoints[LANGUAGE_NONE])){ // noch nicht existent | |
| 221 $entity->endpoints[LANGUAGE_NONE][]=array(); | |
| 222 } else if (sizeof($entity->endpoints[LANGUAGE_NONE])<1) { // nicht mindestens ein eintrag | |
| 223 $entity->endpoints[LANGUAGE_NONE][]=array(); | |
| 224 } | |
| 225 | |
| 226 switch ($target){ | |
| 227 case "source_id": | |
| 228 // source is immer der erste eintrag | |
| 229 $entity->endpoints[LANGUAGE_NONE][0]=array('entity_type' => 'node', 'entity_id' => $tid); | |
| 230 break; | |
| 231 | |
| 232 case "target_id": | |
| 233 //target können mehrere existieren, hinten anfügen. | |
| 234 $entity->endpoints[LANGUAGE_NONE][]=array('entity_type' => 'node', 'entity_id' => $tid); | |
| 235 } | |
| 236 | |
| 237 //dpm($entity); | |
| 238 | |
| 239 | |
| 240 //if ($target=="target_id"){ | |
| 241 relation_update($entity); | |
| 242 | |
| 243 | |
| 244 if (sizeof($entity->endpoints[LANGUAGE_NONE])>1){ // WENN ZWEI ENDPUNKTE VORHANDEN DANN abspeichern | |
| 245 $entity->is_new=FALSE; | |
| 246 | |
| 247 } | |
| 248 // } | |
| 249 } | |
| 250 | |
| 251 /** | |
| 252 * Finds all terms associated with the given node, within one vocabulary. | |
| 253 */ | |
| 254 | |
| 255 /* | |
| 256 function taxonomy_feeds_node_get_terms($node, $key = 'tid') { | |
| 257 $terms = &drupal_static(__FUNCTION__); | |
| 258 | |
| 259 if (!isset($terms[$node->nid][$key])) { | |
| 260 // Get tids from all taxonomy_term_reference fields. | |
| 261 $tids = array(); | |
| 262 $fields = field_info_fields(); | |
| 263 foreach ($fields as $field_name => $field) { | |
| 264 if ($field['type'] == 'taxonomy_term_reference' && field_info_instance('node', $field_name, $node->type)) { | |
| 265 if (($items = field_get_items('node', $node, $field_name)) && is_array($items)) { | |
| 266 $tids = array_merge($tids, array_map('_taxonomy_feeds_extract_tid', $items)); | |
| 267 } | |
| 268 } | |
| 269 } | |
| 270 | |
| 271 // Load terms and cache them in static var. | |
| 272 $curr_terms = taxonomy_term_load_multiple($tids); | |
| 273 $terms[$node->nid][$key] = array(); | |
| 274 foreach ($curr_terms as $term) { | |
| 275 $terms[$node->nid][$key][$term->$key] = $term; | |
| 276 } | |
| 277 } | |
| 278 return $terms[$node->nid][$key]; | |
| 279 } | |
| 280 | |
| 281 */ | |
| 282 | |
| 283 /** | |
| 284 * Extracts tid from array item returned by field_get_items(). | |
| 285 * | |
| 286 * @param array $item | |
| 287 * Tid information in the form of a single element array | |
| 288 * (key == 'tid', value == tid we're looking for) | |
| 289 * | |
| 290 * @return int | |
| 291 * Term id extracted from $item. | |
| 292 * | |
| 293 * @see taxonomy_feeds_node_get_terms() | |
| 294 * @see field_get_items() | |
| 295 */ | |
| 296 function _entity_feeds_extract_tid($item) { | |
| 297 return $item['tid']; | |
| 298 } | |
| 299 | |
| 300 /** | |
| 301 * Looks up a term by GUID, assumes SQL storage backend. | |
| 302 * | |
| 303 * @param string $guid | |
| 304 * The Feeds GUID to compare against. | |
| 305 * | |
| 306 * @return int|FALSE | |
| 307 * The term id, or FALSE if one was not found. | |
| 308 */ | |
| 309 function entity_feeds_entity_lookup_entity_by_guid($guid) { | |
| 310 | |
| 311 | |
| 312 $res = db_select('feeds_item') | |
| 313 ->fields('feeds_item', array('entity_id')) | |
| 314 //->condition('feed_nid', $source->feed_nid) | |
| 315 //->condition('entity_type', $this->entityType()) | |
| 316 //->condition('id', $source->id); | |
| 317 ->condition('guid', $guid) | |
| 318 ->execute() | |
| 319 ->fetchField(); | |
| 320 | |
| 321 | |
| 322 return $res; | |
| 323 /* return db_select('feeds_item') | |
| 324 ->fields('feeds_item', array('entity_id')) | |
| 325 ->condition('entity_type', 'taxonomy_term') | |
| 326 ->condition('guid', $guid) | |
| 327 ->execute() | |
| 328 ->fetchField();*/ | |
| 329 } | |
| 330 | |
| 331 /** | |
| 332 * Mapping configuration summary for taxonomy.module. | |
| 333 * | |
| 334 * @param array $mapping | |
| 335 * Associative array of the mapping settings. | |
| 336 * @param array $target | |
| 337 * Array of target settings, as defined by the processor or | |
| 338 * hook_feeds_processor_targets_alter(). | |
| 339 * @param array $form | |
| 340 * The whole mapping form. | |
| 341 * @param array $form_state | |
| 342 * The form state of the mapping form. | |
| 343 * | |
| 344 * @return string | |
| 345 * Returns, as a string that may contain HTML, the summary to display while | |
| 346 * the full form isn't visible. | |
| 347 * If the return value is empty, no summary and no option to view the form | |
| 348 * will be displayed. | |
| 349 */ | |
| 350 function entity_feeds_summary_callback($mapping, $target, $form, $form_state) { | |
| 351 $options = _entity_feeds_form_callback_options(); | |
| 352 if (empty($mapping['term_search'])) { | |
| 353 return t('Search Entity terms by: <strong>@search</strong>', array('@search' => $options[FEEDS_ENTITY_SEARCH_TERM_NAME])); | |
| 354 } | |
| 355 return t('Search Entity terms by: <strong>@search</strong>', array('@search' => $options[$mapping['term_search']])); | |
| 356 } | |
| 357 | |
| 358 /** | |
| 359 * Settings form callback. | |
| 360 * | |
| 361 * @return array | |
| 362 * The per mapping configuration form. Once the form is saved, $mapping will | |
| 363 * be populated with the form values. | |
| 364 */ | |
| 365 function entity_feeds_form_callback($mapping, $target, $form, $form_state) { | |
| 366 return array( | |
| 367 'term_search' => array( | |
| 368 '#type' => 'select', | |
| 369 '#title' => t('Search Entity by'), | |
| 370 '#options' => _entity_feeds_form_callback_options(), | |
| 371 '#default_value' => !empty($mapping['term_search']) ? $mapping['term_search'] : FEEDS_ENTITY_SEARCH_TERM_NAME, | |
| 372 ), | |
| 373 ); | |
| 374 } | |
| 375 | |
| 376 /** | |
| 377 * Returns the list of available term search methods. | |
| 378 * | |
| 379 * @return array | |
| 380 * An array of taxonomy search option titles. | |
| 381 */ | |
| 382 function _entity_feeds_form_callback_options() { | |
| 383 return array( | |
| 384 FEEDS_ENTITY_SEARCH_TERM_NAME => 'Entity name', | |
| 385 FEEDS_ENTITY_SEARCH_TERM_ID => 'Entity ID', | |
| 386 FEEDS_ENTITY_SEARCH_TERM_GUID => 'GUID', | |
| 387 FEEDS_ENTITY_SEARCH_TERM_CITEKEY => 'CITEKEY' | |
| 388 ); | |
| 389 } | |
| 390 | |
| 391 | |
| 392 | |
| 393 /* sucht nach entity vom typ mit Wert von Feld (fieldname) = term */ | |
| 394 function _searchEntityByBiblioValue($bundle,$fieldName,$term){ | |
| 395 | |
| 396 $fc = module_load_include('inc', 'biblio', 'includes/biblio.pages'); | |
| 397 if ($fc==FALSE){ | |
| 398 dpm("FEHLER: cann't find biblio module!"); | |
| 399 return; | |
| 400 } | |
| 401 $arglist = array ( | |
| 402 "f" => array( $fieldName => $term) | |
| 403 ); | |
| 404 $res = biblio_build_query($arglist); | |
| 405 $nid = $res[0][0]; | |
| 406 | |
| 407 | |
| 408 return $nid; | |
| 409 } | |
| 410 | |
| 411 function _searchEntityByFieldValue($bundle,$fieldName,$term){ | |
| 412 | |
| 413 $query = new EntityFieldQuery(); | |
| 414 $query->entityCondition('entity_type', 'node') | |
| 415 ->entityCondition('bundle', $bundle) | |
| 416 ->fieldCondition($fieldName, 'value',$term, '='); | |
| 417 | |
| 418 $result = $query->execute(); | |
| 419 if (isset($result['node'])) { | |
| 420 $news_items_nids = array_keys($result['node']); | |
| 421 $news_items = entity_load('node', $news_items_nids); | |
| 422 return $news_items[0]; | |
| 423 } | |
| 424 } |
