Mercurial > hg > drupalISMI
view relation_processor/nodes.inc @ 11:61dd2a96c4e2
add redirect /entity/%openmind_id as menu hook.
author | casties |
---|---|
date | Mon, 06 Jul 2015 18:53:41 +0200 |
parents | 1c73c660c2f2 |
children | bdf91a4a40ff |
line wrap: on
line source
<?php /** * @file * On behalf implementation of Feeds mapping API for taxonomy.module. */ /** * Search by term name. */ define('FEEDS_ENTITY_SEARCH_TERM_NAME', 0); /** * Search by term id. */ define('FEEDS_ENTITY_SEARCH_TERM_ID', 1); /** * Search by GUID. */ define('FEEDS_ENTITY_SEARCH_TERM_GUID', 2); /** * Search by CITEKEY. */ define('FEEDS_ENTITY_SEARCH_TERM_CITEKEY', 3); dpm("RELATION_include"); /** * Implements hook_feeds_parser_sources_alter(). */ function relation_feeds_parser_sources_alter(&$sources, $content_type) { if (!empty($content_type)) { $sources['sources'] = array( 'name' => t('Feed node: Entity'), 'description' => t('Entites from feed node.'), 'callback' => 'entites_feeds_get_source', ); } } /** * Callback, returns taxonomy from feed node. */ function relation_feeds_get_source(FeedsSource $source, FeedsParserResult $result, $key) { /*if ($node = node_load($source->feed_nid)) { $terms = taxonomy_feeds_node_get_terms($node); $vocabularies = taxonomy_vocabulary_load_multiple(array(), array('machine_name' => str_replace('parent:taxonomy:', '', $key))); $vocabulary = array_shift($vocabularies); $result = array(); foreach ($terms as $tid => $term) { if ($term->vid == $vocabulary->vid) { $result[] = new FeedsTermElement($term); } }*/ return $result; //} } /** * Implements hook_feeds_processor_targets_alter(). */ function relation_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) { /*foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) { $info = field_info_field($name); if ($info['type'] == 'taxonomy_term_reference') { $targets[$name] = array( 'name' => check_plain($instance['label']), 'callback' => 'taxonomy_feeds_set_target', 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])), 'summary_callback' => 'taxonomy_feeds_summary_callback', 'form_callback' => 'taxonomy_feeds_form_callback', ); } }*/ if ($entity_type == "relation"){ $targets["source_id"]= array( 'name' => 'Source Field', 'callback' => 'relation_feeds_set_target', 'description' => t('The guid of the entity.'), 'summary_callback' => 'relation_feeds_summary_callback', 'form_callback' => 'relation_feeds_form_callback', ); $targets["target_id"]= array( 'name' => 'Target Field', 'callback' => 'relation_feeds_set_target', 'description' => t('The guid of the entity.'), 'summary_callback' => 'relation_feeds_summary_callback', 'form_callback' => 'relation_feeds_form_callback', ); $targets["relation_name"]= array( 'name' => 'Relation Type', 'callback' => 'relation_feeds_set_relation_type', ); } } /*callback for relation_types * * */ function relation_feeds_set_relation_type($source, $entity, $target, $terms, $mapping = array()) { if (empty($terms)) { return; } // don't Handle non-multiple values. if (is_array($terms)) { return; } dpm("CHECK: TYPE"); dpm($terms); $types=relation_get_types(array($terms)); if(sizeof($types)==0){ //typ gibt es nicht dann erzeuge mit defualt werten #TODO konfigurierbar? $info = array(); $info['relation_type']=$terms; $info['directional']=TRUE; $info['source_bundles'][]="node:*"; $info['target_bundles'][]="node:*"; $info['reverse_label']="invOf_" . $terms; $rel_type = relation_type_create($info); relation_type_save($rel_type); } $entity->relation_type=$terms; if ( _relation_feeds_checkRelationComplete($entity)){ $entity->is_new=FALSE; } relation_update($entity); } function _relation_feeds_checkRelationComplete($relation){ //einer der Endpunkte existiert nicht if (count($relation->endpoints['und'][0]) == 0) return false; if (!isset($relation->endpoints['und'][1])) return false; if (count($relation->endpoints['und'][1]) == 0) return false; if ($relation->relation_type == "generic") return false; return true; } function _relation_feeds_checkEndpointsComplete($relation){ //einer der Endpunkte existiert nicht if (count($relation->endpoints['und'][0]) == 0) return false; if (!isset($relation->endpoints['und'][1])) return false; if (count($relation->endpoints['und'][1]) == 0) return false; return true; } /** * Callback for mapping. Here is where the actual mapping happens. * * @todo Do not create new terms for non-autotag fields. */ function relation_feeds_set_target($source, $entity, $target, $terms, $mapping = array()) { // Allow mapping the string '0' to a term name. if (empty($terms) && $terms != 0) { return; } // Handle non-multiple values. if (!is_array($terms)) { $terms = array($terms); } // Add in default values. $mapping += array( 'term_search' => FEEDS_ENTITY_SEARCH_TERM_NAME, 'autocreate' => FALSE, ); $cache = &drupal_static(__FUNCTION__); //$query = new EntityFieldQuery(); //$query->entityCondition('entity_type', 'node') // ->range(0, 1); // Iterate over all values. #$mapping['term_search']=FEEDS_ENTITY_SEARCH_TERM_GUID; foreach ($terms as $term) { //dpm($term); $tid = FALSE; switch ($mapping['term_search']){ // Lookup by name. case FEEDS_ENTITY_SEARCH_TERM_NAME: $name_query = clone $query; if ($tids = $name_query->propertyCondition('name', $term)->execute()) { $tid = key($tids['taxonomy_term']); } // Lookup by tid. case FEEDS_ENTITY_SEARCH_TERM_ID: if (is_numeric($term)) { $tid = $term; } break; // Lookup by GUID. case FEEDS_ENTITY_SEARCH_TERM_GUID: $tid = relation_feeds_entity_lookup_entity_by_guid($term); break; case FEEDS_ENTITY_SEARCH_TERM_CITEKEY: $tid = _searchEntityByBiblioValue("biblio","citekey",$term); } } $eps = $entity->endpoints[LANGUAGE_NONE]; if (!isset($entity->endpoints[LANGUAGE_NONE])){ // noch nicht existent $entity->endpoints[LANGUAGE_NONE][]=array(); } else if (sizeof($entity->endpoints[LANGUAGE_NONE])<1) { // nicht mindestens ein eintrag $entity->endpoints[LANGUAGE_NONE][]=array(); } switch ($target){ case "source_id": // source is immer der erste eintrag $entity->endpoints[LANGUAGE_NONE][0]=array('entity_type' => 'node', 'entity_id' => $tid); break; case "target_id": //target können mehrere existieren, hinten anfügen. $entity->endpoints[LANGUAGE_NONE][]=array('entity_type' => 'node', 'entity_id' => $tid); } if ( _relation_feeds_checkRelationComplete($entity)){ $entity->is_new=FALSE; relation_update($entity); } if ( _relation_feeds_checkEndpointsComplete($entity)){ relation_update($entity); } # if ($target=="source_id"){ # relation_update($entity); # } #if (sizeof($entity->endpoints[LANGUAGE_NONE])>1){ // WENN ZWEI ENDPUNKTE VORHANDEN DANN abspeichern //$entity->is_new=FALSE; #} // } } /** * Finds all terms associated with the given node, within one vocabulary. */ /* function taxonomy_feeds_node_get_terms($node, $key = 'tid') { $terms = &drupal_static(__FUNCTION__); if (!isset($terms[$node->nid][$key])) { // Get tids from all taxonomy_term_reference fields. $tids = array(); $fields = field_info_fields(); foreach ($fields as $field_name => $field) { if ($field['type'] == 'taxonomy_term_reference' && field_info_instance('node', $field_name, $node->type)) { if (($items = field_get_items('node', $node, $field_name)) && is_array($items)) { $tids = array_merge($tids, array_map('_taxonomy_feeds_extract_tid', $items)); } } } // Load terms and cache them in static var. $curr_terms = taxonomy_term_load_multiple($tids); $terms[$node->nid][$key] = array(); foreach ($curr_terms as $term) { $terms[$node->nid][$key][$term->$key] = $term; } } return $terms[$node->nid][$key]; } */ /** * Extracts tid from array item returned by field_get_items(). * * @param array $item * Tid information in the form of a single element array * (key == 'tid', value == tid we're looking for) * * @return int * Term id extracted from $item. * * @see taxonomy_feeds_node_get_terms() * @see field_get_items() */ function _relation_feeds_extract_tid($item) { return $item['tid']; } /** * Looks up a term by GUID, assumes SQL storage backend. * * @param string $guid * The Feeds GUID to compare against. * * @return int|FALSE * The term id, or FALSE if one was not found. */ function relation_feeds_entity_lookup_entity_by_guid($guid) { $res = db_select('feeds_item') ->fields('feeds_item', array('entity_id')) //->condition('feed_nid', $source->feed_nid) //->condition('entity_type', $this->entityType()) //->condition('id', $source->id); ->condition('guid', $guid) ->execute() ->fetchField(); return $res; /* return db_select('feeds_item') ->fields('feeds_item', array('entity_id')) ->condition('entity_type', 'taxonomy_term') ->condition('guid', $guid) ->execute() ->fetchField();*/ } /** * Mapping configuration summary for taxonomy.module. * * @param array $mapping * Associative array of the mapping settings. * @param array $target * Array of target settings, as defined by the processor or * hook_feeds_processor_targets_alter(). * @param array $form * The whole mapping form. * @param array $form_state * The form state of the mapping form. * * @return string * Returns, as a string that may contain HTML, the summary to display while * the full form isn't visible. * If the return value is empty, no summary and no option to view the form * will be displayed. */ function relation_feeds_summary_callback($mapping, $target, $form, $form_state) { $options = _relation_feeds_form_callback_options(); if (empty($mapping['term_search'])) { return t('Search Entity terms by: <strong>@search</strong>', array('@search' => $options[FEEDS_ENTITY_SEARCH_TERM_NAME])); } return t('Search Entity terms by: <strong>@search</strong>', array('@search' => $options[$mapping['term_search']])); } /** * Settings form callback. * * @return array * The per mapping configuration form. Once the form is saved, $mapping will * be populated with the form values. */ function relation_feeds_form_callback($mapping, $target, $form, $form_state) { return array( 'term_search' => array( '#type' => 'select', '#title' => t('Search Entity by'), '#options' => _relation_feeds_form_callback_options(), '#default_value' => !empty($mapping['term_search']) ? $mapping['term_search'] : FEEDS_ENTITY_SEARCH_TERM_NAME, ), ); } /** * Returns the list of available term search methods. * * @return array * An array of taxonomy search option titles. */ function _relation_feeds_form_callback_options() { return array( FEEDS_ENTITY_SEARCH_TERM_NAME => 'Entity name', FEEDS_ENTITY_SEARCH_TERM_ID => 'Entity ID', FEEDS_ENTITY_SEARCH_TERM_GUID => 'GUID', FEEDS_ENTITY_SEARCH_TERM_CITEKEY => 'CITEKEY' ); } /* sucht nach entity vom typ mit Wert von Feld (fieldname) = term */ function _searchEntityByBiblioValue($bundle,$fieldName,$term){ $fc = module_load_include('inc', 'biblio', 'includes/biblio.pages'); if ($fc==FALSE){ dpm("FEHLER: cann't find biblio module!"); return; } $arglist = array ( "f" => array( $fieldName => $term) ); $res = biblio_build_query($arglist); $nid = $res[0][0]; return $nid; } function _searchEntityByFieldValue($bundle,$fieldName,$term){ $query = new EntityFieldQuery(); $query->entityCondition('entity_type', 'node') ->entityCondition('bundle', $bundle) ->fieldCondition($fieldName, 'value',$term, '='); $result = $query->execute(); if (isset($result['node'])) { $news_items_nids = array_keys($result['node']); $news_items = entity_load('node', $news_items_nids); return $news_items[0]; } }