-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathding_serendipity_taxonomy_term.module
289 lines (261 loc) · 8.82 KB
/
ding_serendipity_taxonomy_term.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
* @file
* ding serendipity taxonomy term
*
* This module provides serendipity content based on taxonomy terms
*/
/**
* Implements hook_ctools_plugin_directory().
*/
function ding_serendipity_taxonomy_term_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'ctools' && $plugin_type == 'content_types') {
return 'plugins/content_types';
}
}
/**
* implements hook_serendipity_info()
*/
function ding_serendipity_taxonomy_term_serendipity_info() {
return array(
'recent' => array(
'title' => 'Recent content of current term',
'description' => 'Fetch objects from this year matching term.',
'keys' => array('taxonomy_tid', 'recent'),
),
'related_by_term' => array(
'title' => 'Content with similar term',
'description' => 'Fetch nodes or materials with similar term.',
'keys' => array('taxonomy_tid', 'related'),
),
'recommended_objects' => array(
'title' => 'Objects in taxonomy term recommended field',
'description' => 'On each taxonomy term a list of recommended objects may be suggested.',
'keys' => array('taxonomy_tid', 'recommended'),
),
'popular_objects' => array(
'title' => 'Objects in taxonomy term popular field',
'description' => 'On each taxonomy term a list of popular objects may be suggested.',
'keys' => array('taxonomy_tid', 'popular'),
),
);
}
/**
* Fetch object id's from term recommended field
*
* @context array
* Serendipity context array
*
* @return array
* Array of results, each result should contain a type and an id key.
* - type is the entity Type.
* - id the entity id.
* @todo comments
*/
function ding_serendipity_taxonomy_term_recommended_objects_serendipity_add($context) {
// No term no result @todo recommended among recent
if (isset($context['recent']) || !isset($context['taxonomy_tid']) || !is_numeric($context['taxonomy_tid']) || !isset($context['bundle']) || ($context['bundle'] != 'ting_object' )) {
return array();
}
$term = taxonomy_term_load($context['taxonomy_tid']);
$result = ding_serendipity_taxonomy_term_fetch_ting_objects('field_taxonomy_ting_objects', $term);
return $result;
}
/**
* Fetch ting_objects or nodes matching $context['taxonomy_term']
* @todo comments
*/
function ding_serendipity_taxonomy_term_recent_serendipity_add($context) {
$term_arr = ding_serendipity_taxonomy_get_term($context);
// Validate context
if ($term_arr === FALSE || !isset($context['recent'])) {
return array();
}
$term = $term_arr['term'];
// Select content from this year
$year = date('Y');
$limit = 16;
if(isset($context['max']) && is_numeric($context['max'])) {
$limit = $context['max'];
}
// Apply term query
$facets[] = $term_arr['query'];
$query = implode(' AND ', $facets);
// Order by date
$result = ding_base_do_search($query,
array(
'options' => array('sort' => 'acquisitionDate_descending'),
'limit' => $limit,
)
);
return $result;
}
/**
* Fetch ting_objects or nodes matching $context['taxonomy_term']
* @todo comments
*/
function ding_serendipity_taxonomy_term_related_by_term_serendipity_add($context) {
global $user;
// No term no result
$term_arr = ding_serendipity_taxonomy_get_term($context);
if ($term_arr === FALSE || isset($context['recent'])) {
return array();
}
$term = $term_arr['term'];
$query = $term_arr['query'];
$results = array();
if ($context['bundle'] == 'ting_object') {
$results = ding_base_do_search($query);
}
else {
if(isset($context['frontpage']) && $context['frontpage']) {
// Fetch promoted to frontpage nodes of the desired type
$nodeQuery = new EntityFieldQuery();
$nodeQuery->entityCondition('entity_type', 'node')
->propertyCondition('status', 1)
->propertyCondition('promote', 1) //change 2 to any vocabulary ID
->propertyOrderBy('created', 'DESC')
->range(0, 20);
if (!empty($context['bundle'])) {
$nodeQuery->propertyCondition('type', $context['bundle']);
}
$nodes = $nodeQuery->execute();
foreach($nodes['node'] as $node) {
$results[] = array('type' => 'node', 'id' => $node->nid);
}
} else {
// Fetch nodes matching $tid
$nodeQuery = new EntityFieldQuery();
$nodeQuery->entityCondition('entity_type', 'node')
->fieldCondition('ding_content_tags', 'tid', $term->tid)
->propertyOrderBy('created', 'DESC')
->range(0, 20);
if (!empty($context['bundle'])) {
$nodeQuery->propertyCondition('type', $context['bundle']);
}
$nodes = $nodeQuery->execute();
foreach($nodes['node'] as $node) {
$results[] = array('type' => 'node', 'id' => $node->nid);
}
}
}
return $results;
}
/**
* Fetch object id's from term recommended field
*
* @context array
* Serendipity context array
*
* @return array
* Array of results, each result should contain a type and an id key.
* - type is the entity Type.
* - id the entity id.
* @todo comments
*/
function ding_serendipity_taxonomy_term_popular_objects_serendipity_add($context) {
$term = taxonomy_term_load($context['taxonomy_tid']);
$result = ding_serendipity_taxonomy_term_fetch_ting_objects('field_popular_ting_objects', $term);
return $result;
}
/**
* Fetch object id's from any term field.
*
* @field
* Name of field to retrieve
* @term
* Term to retrive objects from
* @return array
* Array of results, each result should contain a type and an id key.
* - type is the entity Type.
* - id the entity id.
* @todo comments
*/
function ding_serendipity_taxonomy_term_fetch_ting_objects($field, $term) {
$result = array();
// Extract id's from field_taxonomy_ting_objects
$items = field_get_items('taxonomy_term', $term, $field);
if (!empty($items)) {
foreach ($items as $item) {
if (!empty($item['value'])) {
$result[] = array('type' => 'ting_object', 'id' => $item['value']);
}
}
}
return $result;
}
/**
* Helper function to extract term and often used stuff
*/
function ding_serendipity_taxonomy_get_term($context) {
if (!isset($context['taxonomy_tid']) ) {
return FALSE;
}
$term = taxonomy_term_load($context['taxonomy_tid']);
$term_arr = array();
if (!empty($term->field_search_string)) {
// Manual broend term query
$term_arr['query'] = $term->field_search_string[field_language('taxonomy_term', $term, 'field_search_string')][0]['value'];
$term_arr['has_query'] = TRUE;
}
else {
// Default broend term query
$term_arr['query'] = $term->name;
$term_arr['has_query'] = FALSE;
}
$term_arr['term'] = $term;
return $term_arr;
}
/**
* implements hook_serendipity_fallback
*/
function ding_serendipity_taxonomy_term_serendipity_fallback($ids, $context) {
$list = array();
$limit = $context['min'];
// Handle ting objects
// We assume that if no inspiration is found, at least recent materials exists
if (isset($context['bundle']) && $context['bundle'] == 'ting_object') {
$context['recent'] = TRUE;
$fallback = ding_serendipity_taxonomy_term_recent_serendipity_add($context);
foreach ($fallback as $item) {
$item['source'] = array(
'title' => 'Fallback ting content',
'description' => 'Fallback content are provided to meet minimum count of result.',
'keys' => array('fallback'),
);
$list[] = $item;
}
}
// @todo: handle other entities than node (nid)
if (isset($context['entity_type']) && $context['entity_type'] == 'node') {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $context['entity_type']);
if ($context['bundle']) {
$query->entityCondition('bundle', $context['bundle']);
}
if ($context['entity_type'] == 'node') {
$query->propertyCondition('status', 1);
$query->propertyOrderBy('created', 'DESC');
}
if (count($ids)) {
// @todo: handle other entities than node (nid)
$query->propertyCondition('nid', $ids, 'NOT IN');
}
$query->range(0, $limit)
->addMetaData('account', user_load(1)); // @todo: Run the query as user 1?
$result = $query->execute();
foreach ($result[$context['entity_type']] as $item) {
$list[] = array(
'type' => 'node',
'id' => $item->nid,
// Fake a source description
'source' => array(
'title' => 'Fallback content',
'description' => 'Fallback content are provided to meet minimum count of result.',
'keys' => array('fallback'),
),
);
}
}
return $list;
}