123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- /**
- * @file
- * Provides synonyms integration with searching.
- */
- /**
- * Implements hook_ctools_plugin_directory().
- */
- function synonyms_search_ctools_plugin_directory($owner, $plugin_type) {
- switch ($owner) {
- case 'synonyms':
- switch ($plugin_type) {
- case 'behavior':
- return 'plugins/' . $plugin_type;
- }
- break;
- }
- }
- /**
- * Implements hook_taxonomy_term_update().
- */
- function synonyms_search_taxonomy_term_update($term) {
- // If we notice at least some change in synonyms of this term, we want to
- // trigger search re-indexing of nodes, where this term is referenced, since
- // change in term synonyms affects nodes ranking in the search.
- if (isset($term->original)) {
- $bundle = field_extract_bundle('taxonomy_term', $term);
- $behavior_implementations = synonyms_behavior_get('search', 'taxonomy_term', $bundle, TRUE);
- $current_search_synonyms = array();
- $previous_search_synonyms = array();
- foreach ($behavior_implementations as $behavior_implementation) {
- $current_search_synonyms = array_merge($current_search_synonyms, synonyms_extract_synonyms($term, $behavior_implementation));
- $previous_search_synonyms = array_merge($previous_search_synonyms, synonyms_extract_synonyms($term->original, $behavior_implementation));
- }
- $diff = array_diff($current_search_synonyms, $previous_search_synonyms);
- if (!empty($diff) || count($current_search_synonyms) != count($previous_search_synonyms)) {
- module_load_include('inc', 'synonyms_search', 'synonyms_search.pages');
- synonyms_search_reindex_nodes_by_terms(array($term->tid));
- }
- }
- }
- /**
- * Implements hook_node_update_index().
- */
- function synonyms_search_node_update_index($node) {
- $output = array();
- foreach (field_info_instances('node', $node->type) as $instance) {
- // We go a field by field looking for taxonomy term reference and if that
- // vocabulary has enabled search synonyms, we add them to the search index.
- $field_info = field_info_field($instance['field_name']);
- if ($field_info['type'] == 'taxonomy_term_reference') {
- // For each term referenced in this node we have to add synonyms.
- $terms = field_get_items('node', $node, $instance['field_name']);
- if (is_array($terms) && !empty($terms)) {
- foreach ($terms as $v) {
- $output[] = $v['tid'];
- }
- }
- }
- }
- if (!empty($output)) {
- $terms = taxonomy_term_load_multiple($output);
- $output = array();
- foreach ($terms as $term) {
- $bundle = field_extract_bundle('taxonomy_term', $term);
- $behavior_implementations = synonyms_behavior_get('search', 'taxonomy_term', $bundle, TRUE);
- foreach ($behavior_implementations as $implementation) {
- $output = array_merge($output, synonyms_extract_synonyms($term, $implementation));
- }
- }
- }
- return empty($output) ? '' : '<strong>' . implode($output, ', ') . '</strong>';
- }
- /**
- * Implements hook_term_update_index().
- */
- function synonyms_search_term_update_index($term) {
- $bundle = taxonomy_vocabulary_load($term->vid);
- $bundle = field_extract_bundle('taxonomy_term', $bundle);
- $behavior_implementations = synonyms_behavior_get('search', 'taxonomy_term', $bundle, TRUE);
- $synonyms = array();
- foreach ($behavior_implementations as $implementation) {
- $synonyms = array_merge($synonyms, synonyms_extract_synonyms($term, $implementation));
- }
- return implode(', ', $synonyms);
- }
- /**
- * Implements hook_synonyms_behavior_implementation_info().
- */
- function synonyms_search_synonyms_behavior_implementation_info($behavior) {
- switch ($behavior) {
- case 'search':
- return array(
- 'number_integer' => 'SearchTextSynonymsBehavior',
- 'number_decimal' => 'SearchTextSynonymsBehavior',
- 'number_float' => 'SearchTextSynonymsBehavior',
- 'text' => 'SearchTextSynonymsBehavior',
- 'taxonomy_term_reference' => 'SearchTaxonomySynonymsBehavior',
- 'entityreference' => 'SearchEntityReferenceSynonymsBehavior',
- );
- break;
- }
- return array();
- }
|