synonyms_search.module 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @file
  4. * Provides synonyms integration with searching.
  5. */
  6. /**
  7. * Implements hook_ctools_plugin_directory().
  8. */
  9. function synonyms_search_ctools_plugin_directory($owner, $plugin_type) {
  10. switch ($owner) {
  11. case 'synonyms':
  12. switch ($plugin_type) {
  13. case 'behavior':
  14. return 'plugins/' . $plugin_type;
  15. }
  16. break;
  17. }
  18. }
  19. /**
  20. * Implements hook_taxonomy_term_update().
  21. */
  22. function synonyms_search_taxonomy_term_update($term) {
  23. // If we notice at least some change in synonyms of this term, we want to
  24. // trigger search re-indexing of nodes, where this term is referenced, since
  25. // change in term synonyms affects nodes ranking in the search.
  26. if (isset($term->original)) {
  27. $bundle = field_extract_bundle('taxonomy_term', $term);
  28. $behavior_implementations = synonyms_behavior_get('search', 'taxonomy_term', $bundle, TRUE);
  29. $current_search_synonyms = array();
  30. $previous_search_synonyms = array();
  31. foreach ($behavior_implementations as $behavior_implementation) {
  32. $current_search_synonyms = array_merge($current_search_synonyms, $behavior_implementation['object']->extractSynonyms($term));
  33. $previous_search_synonyms = array_merge($previous_search_synonyms, $behavior_implementation['object']->extractSynonyms($term->original));
  34. }
  35. $diff = array_diff($current_search_synonyms, $previous_search_synonyms);
  36. if (!empty($diff) || count($current_search_synonyms) != count($previous_search_synonyms)) {
  37. module_load_include('inc', 'synonyms_search', 'synonyms_search.pages');
  38. synonyms_search_reindex_nodes_by_terms(array($term->tid));
  39. }
  40. }
  41. }
  42. /**
  43. * Implements hook_node_update_index().
  44. */
  45. function synonyms_search_node_update_index($node) {
  46. $output = array();
  47. foreach (field_info_instances('node', $node->type) as $instance) {
  48. // We go a field by field looking for taxonomy term reference or entity
  49. // reference of taxonomy term type and if that vocabulary has enabled search
  50. // synonyms, we add them to the search index.
  51. // TODO: implement this through foreign keys information. See
  52. // term_merge_fields_with_foreign_key() function.
  53. $field_info = field_info_field($instance['field_name']);
  54. if ($field_info['type'] == 'taxonomy_term_reference') {
  55. $terms = field_get_items('node', $node, $instance['field_name']);
  56. if (is_array($terms) && !empty($terms)) {
  57. foreach ($terms as $v) {
  58. $output[] = $v['tid'];
  59. }
  60. }
  61. }
  62. if ($field_info['type'] == 'entityreference' && $field_info['settings']['target_type'] == 'taxonomy_term') {
  63. $terms = field_get_items('node', $node, $instance['field_name']);
  64. if (is_array($terms) && !empty($terms)) {
  65. foreach ($terms as $v) {
  66. $output[] = $v['target_id'];
  67. }
  68. }
  69. }
  70. }
  71. if (!empty($output)) {
  72. $terms = taxonomy_term_load_multiple($output);
  73. $output = array();
  74. foreach ($terms as $term) {
  75. $bundle = field_extract_bundle('taxonomy_term', $term);
  76. $behavior_implementations = synonyms_behavior_get('search', 'taxonomy_term', $bundle, TRUE);
  77. foreach ($behavior_implementations as $implementation) {
  78. $output = array_merge($output, $implementation['object']->extractSynonyms($term));
  79. }
  80. }
  81. }
  82. return empty($output) ? '' : '<strong>' . implode($output, ', ') . '</strong>';
  83. }
  84. /**
  85. * Implements hook_term_update_index().
  86. */
  87. function synonyms_search_term_update_index($term) {
  88. $bundle = taxonomy_vocabulary_load($term->vid);
  89. $bundle = field_extract_bundle('taxonomy_term', $bundle);
  90. $behavior_implementations = synonyms_behavior_get('search', 'taxonomy_term', $bundle, TRUE);
  91. $synonyms = array();
  92. foreach ($behavior_implementations as $implementation) {
  93. $synonyms = array_merge($synonyms, $implementation['object']->extractSynonyms($term));
  94. }
  95. return implode(', ', $synonyms);
  96. }
  97. /**
  98. * Implements hook_synonyms_provider_field_behavior_implementation_info().
  99. */
  100. function synonyms_search_synonyms_provider_field_behavior_implementation_info($behavior) {
  101. switch ($behavior) {
  102. case 'search':
  103. return array(
  104. 'number_integer' => 'SearchTextSynonymsBehavior',
  105. 'number_decimal' => 'SearchTextSynonymsBehavior',
  106. 'number_float' => 'SearchTextSynonymsBehavior',
  107. 'text' => 'SearchTextSynonymsBehavior',
  108. 'taxonomy_term_reference' => 'SearchTaxonomySynonymsBehavior',
  109. 'entityreference' => 'SearchEntityReferenceSynonymsBehavior',
  110. 'commerce_product_reference' => 'SearchCommerceProductReferenceSynonymsBehavior',
  111. );
  112. break;
  113. }
  114. return array();
  115. }