forked from mooffie/flag_solr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_solr.module
148 lines (128 loc) · 3.75 KB
/
flag_solr.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* @file
*
* Please see the README.txt file for general introduction to this module.
*/
include_once dirname(__FILE__) . '/flag_solr.facets.inc';
/**
* Implements hook_apachesolr_update_index().
*
* Here we add Solr fields to the document, based on the flags set on them.
*/
function flag_solr_apachesolr_update_index($document, $node, $namespace) {
if (($flaggings_by_user = flag_get_content_flags('node', $node->nid))) {
foreach ($flaggings_by_user as $uid => $flaggings_by_fid) {
foreach ($flaggings_by_fid as $flagging) {
if (($flag = flag_get_flag(NULL, $flagging->fid))) {
$document->setField('bs_flag_' . $flag->name . '_flagged', TRUE);
// For non-global flags, we also record each flagging user in a Solr multi-valued field.
if (!$flag->global) {
$document->setMultiValue('im_flag_' . $flag->name . '_uid', $flagging->uid);
}
}
}
}
}
//_flag_solr_dsm($document);
}
/**
* Implements hook_flag().
*
* Whenever a node is flagged (or unflagged) we tell ApacheSolr to queue it for
* re-indexing.
*/
function flag_solr_flag($action, $flag, $content_id) {
if ($flag->content_type == 'node') {
apachesolr_mark_node($content_id);
}
}
/**
* Implements hook_apachesolr_facets().
*/
function flag_solr_apachesolr_facets() {
$facets = array();
foreach (flag_get_flags() as $flag) {
$facet = new FlagFacetByAnybody($flag->name);
$facets[ $facet->get_delta() ] = $facet->get_definition();
if (!$flag->global) {
$facet = new FlagFacetBySelf($flag->name);
$facets[ $facet->get_delta() ] = $facet->get_definition();
}
}
return $facets;
}
/**
* Implements hook_block().
*/
function flag_solr_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks = array();
foreach (FlagFacet::get_enabled_facets() as $facet) {
$blocks[ $facet->get_delta() ] = $facet->get_block_definition();
}
return $blocks;
case 'configure':
if (($facet = FlagFacet::by_id($delta))) {
return $facet->block_form();
}
break;
case 'save':
if (($facet = FlagFacet::by_id($delta))) {
return $facet->block_form_save($edit);
}
break;
case 'view':
if (apachesolr_has_searched()) {
// Get the query and response. Without these no blocks make sense.
$response = apachesolr_static_response_cache();
if (empty($response)) {
return;
}
$query = apachesolr_current_query();
if (($facet = FlagFacet::by_id($delta)) &&
$facet->is_enabled() &&
$facet->is_visible($query)) {
return $facet->block($response, $query);
}
}
break;
}
}
/**
* Implements hook_apachesolr_modify_query().
*
* Here we allow unconventional facets to alter the Solr query directly.
*/
function flag_solr_apachesolr_modify_query($query, &$params, $caller) {
foreach (FlagFacet::get_enabled_facets() as $facet) {
$facet->modify_query($query, $params, $caller);
}
_flag_solr_dsm($params);
}
/**
* Implements hook_theme().
*/
function flag_solr_theme() {
return array(
'apachesolr_breadcrumb_flag' => array(
'arguments' => array('field' => NULL, 'exclude' => FALSE),
),
);
}
/**
* Implements hook_apachesolr_theme_breadcrumb_alter().
*
* He we direct ApacheSolr to the real theming function.
*/
function flag_solr_apachesolr_theme_breadcrumb_alter(&$breadcrumb_name) {
if (FlagFacet::by_id($breadcrumb_name)) {
$breadcrumb_name = 'apachesolr_breadcrumb_flag';
}
}
function theme_apachesolr_breadcrumb_flag($filter) {
if (($facet = FlagFacet::by_id($filter['#name']))) {
return $facet->get_breadcrumb_value($filter['#exclude']);
}
}