-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathubox.node_view.inc
388 lines (326 loc) · 12.6 KB
/
ubox.node_view.inc
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
/**
* Viewing a node in a ubox context.
* Alter imagestyle preset
*/
function ubox_node_view_uboxitem($node, $view_mode, $langcode) {
// Create image style name
$imagestyle = array();
$imagestyle[] = 'ubox';
$imagestyle[] = $node->ubox_template;
$imagestyle[] = $node->ubox_template_size;
if ($node->ubox_display_region) {
$imagestyle[] = $node->ubox_display_region;
}
foreach($node->content as $field_name => $field){
if(isset($field['#field_type']) && $field['#field_type'] == 'image'){
$i = 0;
while(isset($node->content[$field_name][$i])) {
$node->content[$field_name][$i]['#image_style'] = implode('--', $imagestyle);
$i++;
}
}
}
// nodetitle permet d'avoir accès au titre sans passer par la variable $title et sans être affiché par défaut
$node->nodetitle = $node->title;
} // ubox_node_view_uboxitem
function ubox_node_view_prepare($node, $view_mode, $langcode) {
$node->view_mode = $view_mode;
$node->ubox_template = $node->field_ubox_template[LANGUAGE_NONE][0]['value'];
$node->items = array();
// Clean ubox fields from content
foreach ($node->content as $field_name => $field) {
if (strpos($field_name, 'field_ubox_') === 0) {
unset($node->content[$field_name]);
}
}
// Unset default node links
$node->content['links'] = array();
}
function ubox_node_view_custom($node, $view_mode, $langcode) {
$custom_content = check_markup(
$node->field_ubox_content_custom[LANGUAGE_NONE][0]['value'],
$node->field_ubox_content_custom[LANGUAGE_NONE][0]['format'],
$langcode
);
$node->items[] = array(
'#markup' => $custom_content
);
} // ubox_node_view_custom
function ubox_node_view_existing($node, $view_mode, $langcode) {
global $language;
$existing_nids = array();
if(isset($node->field_ubox_content_existing) && !empty($node->field_ubox_content_existing[LANGUAGE_NONE])) {
foreach($node->field_ubox_content_existing[LANGUAGE_NONE] as $v) {
$existing_nids[] = $v['target_id'];
}
$existing_nodes = node_load_multiple($existing_nids);
}
if(!empty($existing_nodes)){
foreach($existing_nodes as &$existing_node) {
// Alter existing node properties
$existing_node->ubox_template = $node->field_ubox_template[LANGUAGE_NONE][0]['value'];
$existing_node->ubox_template_size = $node->field_ubox_template_size[LANGUAGE_NONE][0]['value'];
$existing_node->ubox_template_count = $node->field_ubox_template_count[LANGUAGE_NONE][0]['value'];
$existing_node->ubox_display_region = (!empty($node->field_ubox_display_region)) ? $node->field_ubox_display_region[LANGUAGE_NONE][0]['value'] : FALSE;
}
$node->items = node_view_multiple($existing_nodes, 'ubox_'.$node->field_ubox_template[LANGUAGE_NONE][0]['value'], $language->language);
}
} // ubox_node_view_existing
function ubox_node_view_search($node, $view_mode, $langcode) {
global $language, $_domain;
$limit = 0;
$display_pager = FALSE;
$element = FALSE;
$node->exposed_form = FALSE;
$only_expose = TRUE;
// Limit
if (!empty($node->field_ubox_search_limit)) {
$limit = (int) $node->field_ubox_search_limit[LANGUAGE_NONE][0]['value'];
}
// Check if we have to add pager
if($limit > 0 && $node->field_ubox_search_pager[LANGUAGE_NONE][0]['value']) {
$display_pager = TRUE;
// Determine pager number on this page
global $pager_total;
if(is_null($pager_total)) {
$element = 0;
}
else {
$element = end($pager_total)+1;
}
}
// dpm(array(
// 'limit'=>$limit,
// 'display_pager'=>$display_pager,
// 'element'=>$element
// ));
/**
* Select nodes to display in ubox
*/
$nodes = array();
$joins = array();
$all_types = array();
// Create query
$query = db_select('node', 'n');
if($limit > 0) {
if ($display_pager) {
// http://stackoverflow.com/questions/7637279/how-do-i-get-pagerdefault-queries-to-work-correctly-with-drupal-7
$query = $query->extend('PagerDefault')->limit($limit)->element($element);
}
else {
$query->range(0, $limit);
}
}
$query
->fields('n', array('nid'))
->distinct()
->condition('n.status', 1)
->condition('n.language', array($language->language, LANGUAGE_NONE), 'IN')
->condition('n.type', 'ubox', '!=') // No Inception
->orderBy('n.sticky', 'DESC');
// Load field_collections to use their datas
$field_ubox_search_filters = field_get_items('node', $node, 'field_ubox_search_filters');
if (!empty($field_ubox_search_filters)) {
// Load field_collection items
$field_collection_items = array();
foreach($field_ubox_search_filters as $item) {
$field_collection_items[] = $item['value'];
}
$filters = entity_load('field_collection_item', $field_collection_items);
// Build query conditions
$conditions = db_or();
$conditions_not = db_and();
foreach($filters as $filter) {
// Expose filter
if (!empty($filter->field_ubox_filter_expose) && $filter->field_ubox_filter_expose[LANGUAGE_NONE][0]['value'] !== '_none') {
$node->exposed_form = TRUE;
}
else {
$only_expose = FALSE;
// Create condition item
$condition_item = db_and();
// Exclude filter ?
if ($filter->field_ubox_filter_exclude[LANGUAGE_NONE][0]['value'] == 1) {
$exclude = TRUE;
$operator = 'NOT IN';
}
else {
$exclude = FALSE;
$operator = 'IN';
}
// Content types
if (!empty($filter->field_ubox_filter_type)) {
$types = array();
foreach($filter->field_ubox_filter_type[LANGUAGE_NONE] as $type) {
$types[] = $type['value'];
$all_types[$type['value']] = $type['value'];
}
$condition_item->condition('n.type', $types, $operator);
}
// Taxonomy terms
if (!empty($filter->field_ubox_filter_terms)) {
// Get operator value for the filter (IN or AND)
$operator_filter = 'IN';
if (isset($filter->field_ubox_filter_operator[LANGUAGE_NONE])) {
$operator_filter = $filter->field_ubox_filter_operator[LANGUAGE_NONE][0]['value'];
}
// Get selected terms
$terms = array();
foreach($filter->field_ubox_filter_terms[LANGUAGE_NONE] as $term) {
$terms[] = (int) $term['value'];
}
$exclusive_length = 1;
if($operator_filter == 'and') {
// How many exclusive value have we to make AND condition ?
$exclusive_length = sizeof($terms);
}
// If we are in AND operator, we have to make as join as selected terms
// $joins['taxonomy_index'] iteration is used to make a minimum of join
for($exclusive_iteration = 0; $exclusive_iteration < $exclusive_length; $exclusive_iteration++) {
// Does JOIN condition has already be done ?
if(!isset($joins['taxonomy_index']) || $exclusive_length > $joins['taxonomy_index']) {
if(!isset($joins['taxonomy_index']) || $joins['taxonomy_index'] < $exclusive_iteration) {
// First join have to be INNER to limit display
$query->innerJoin('taxonomy_index', 'ti' . $exclusive_iteration, 'n.nid = ti' . $exclusive_iteration . '.nid');
$joins['taxonomy_index'] = 1;
}
else if($joins['taxonomy_index'] <= $exclusive_iteration) {
// Limit on next join only if needed
$query->leftJoin('taxonomy_index', 'ti' . $exclusive_iteration, 'n.nid = ti' . $exclusive_iteration . '.nid');
$joins['taxonomy_index']++;
}
}
// For and operator, we will execute as time as selected terms, so condition is : = term_id.
if($operator_filter == 'and') {
$operator = "=";
$terms_condition = $terms[$exclusive_iteration];
}
else {
// Else condition is IN ( array(terms) )
$terms_condition = $terms;
}
$condition_item->condition('ti' . $exclusive_iteration . '.tid', $terms_condition, $operator);
}
}
// If we have some condition
if (count($condition_item->conditions()) > 1) {
if ($exclude == TRUE) {
$conditions_not->condition($condition_item); // Get all condition_item separed by OR
}
else {
$conditions->condition($condition_item); // Get all exclude condition_item separed by AND
}
}
} // end if expose
} // Loop through filters
// Add filters conditions to query
if (count($conditions->conditions()) > 1) {
$query->condition($conditions);
}
if (count($conditions_not->conditions()) > 1) {
$query->condition($conditions_not);
}
} // field_ubox_search_filters
// Special case were there is no un-exposed filter
if ($only_expose) {
$query->condition('n.type', array_keys(_ubox_get_contenttype()), 'IN');
}
// Exposed filter alteration
if ($node->exposed_form) {
if (isset($_GET['filters'])) {
foreach ($filters as $filter) {
if (isset($_GET['filters'][$filter->item_id])) {
foreach ($_GET['filters'][$filter->item_id] as $field_name => $value) {
// $value needs to be an array, no matter what
if (!is_array($value)) {
$value = array($value);
}
// Special case: 0 = all
if (isset($value[0]) && $value[0] == 0) {
$value = array();
foreach($filter->{$field_name}[LANGUAGE_NONE] as $filter_value) {
$value[] = $filter_value['value'];
}
}
$condition = db_and();
if ($field_name == 'field_ubox_filter_type') {
$condition->condition('n.type', $value, "IN");
}
if ($field_name == 'field_ubox_filter_terms') {
// Does JOIN condition has already be done ?
if(!isset($joins['taxonomy_index'])) {
$query->innerjoin('taxonomy_index', 'ti', 'n.nid = ti.nid');
$joins['taxonomy_index'] = TRUE;
}
$condition->condition('ti.tid', $value, "IN");
}
if (count($condition->conditions())) {
$query->condition($condition);
}
} // foreach
}
} // foreach
}
}
// Query alter
if (($query_alter = field_get_items('node', $node, 'field_ubox_query_alter')) != FALSE) {
// Something bad happen right now.
eval($query_alter[0]['value']);
}
// Allow module to alter the query
$context = array(
'ubox' => clone $node,
'view_mode' => $view_mode,
'langcode' => $langcode,
'joins' => $joins,
'types' => $all_types,
);
drupal_alter('ubox_search_query', $query, $context);
$query->orderBy('n.title', 'ASC');
$entities = array();
// Do not execute query if 'exposeonly' field is checked.
if (($node->field_ubox_search_exposeonly[LANGUAGE_NONE][0]['value'] == 0) || isset($_GET['filters'])) {
try {
$entities = $query->execute()->fetchAll();
}
catch (Exception $e) {
drupal_set_message('Exception caught: ' . $e->getMessage(), 'error');
}
}
if(!empty($entities)) {
foreach($entities as $entity) {
$nids[] = $entity->nid;
}
$nodes = node_load_multiple($nids);
// Vérifie que les nodes ont les droits pour être affichés
foreach($nodes as $nid=>$n) {
if(!node_access('view', $n)){
unset($nodes[$nid]);
}
else {
/* Choix du view_mode à utiliser. Par défaut : teaser pris en compte */
$nodes[$nid]->ubox_template = $node->field_ubox_template[LANGUAGE_NONE][0]['value'];
$nodes[$nid]->view_mode = 'ubox_'.$nodes[$nid]->ubox_template;
$nodes[$nid]->ubox_template_size = $node->field_ubox_template_size[LANGUAGE_NONE][0]['value'];
$nodes[$nid]->ubox_template_count = $node->field_ubox_template_count[LANGUAGE_NONE][0]['value'];
$nodes[$nid]->ubox_display_region = (!empty($node->field_ubox_display_region)) ? $node->field_ubox_display_region[LANGUAGE_NONE][0]['value'] : FALSE;
}
}
if (!empty($nodes)) {
$node->items = node_view_multiple($nodes, 'ubox_'.$node->field_ubox_template[LANGUAGE_NONE][0]['value'], $language->language);
}
if($display_pager) {
$node->items['pager'] = array(
'#theme' => 'pager',
'#tags' => array(),
'#element' => $element
);
}
}
else {
if (isset($_GET['filters'])) {
$node->items['empty']['#markup'] = t("No results");
}
}
} // ubox_node_view_search