-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathting_object_tree.module
497 lines (435 loc) · 15.9 KB
/
ting_object_tree.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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
/**
* @file
* Ting smart carousel module main file.
*/
use OpenSearch\OpenSearchTingObjectCollection;
function ting_object_tree_menu() {
$items = array();
$items['bog/%/%/%'] = array(
'title' => 'Ting object tree',
'page callback' => 'ting_object_tree_show',
'page arguments' => array(0, 1, 2, 3),
'access arguments' => array('access content'),
);
$items['ebog/%/%/%'] = array(
'title' => 'Ting object tree',
'page callback' => 'ting_object_tree_show',
'page arguments' => array(0, 1, 2, 3),
'access arguments' => array('access content'),
);
$items['lydbog/%/%/%'] = array(
'title' => 'Ting object tree',
'page callback' => 'ting_object_tree_show',
'page arguments' => array(0, 1, 2, 3),
'access arguments' => array('access content'),
);
$items['musik/%/%/%'] = array(
'title' => 'Ting object tree',
'page callback' => 'ting_object_tree_show',
'page arguments' => array(0, 1, 2, 3),
'access arguments' => array('access content'),
);
$items['film/%/%/%'] = array(
'title' => 'Ting object tree',
'page callback' => 'ting_object_tree_show',
'page arguments' => array(0, 1, 2, 3),
'access arguments' => array('access content'),
);
$items['artikel/%/%/%'] = array(
'title' => 'Ting object tree',
'page callback' => 'ting_object_tree_show',
'page arguments' => array(0, 1, 2, 3),
'access arguments' => array('access content'),
);
$items['andet/%/%/%'] = array(
'title' => 'Ting object tree',
'page callback' => 'ting_object_tree_show',
'page arguments' => array(0, 1, 2, 3),
'access arguments' => array('access content'),
);
$items['tidsskrift/%/%/%'] = array(
'title' => 'Ting object tree',
'page callback' => 'ting_object_tree_show',
'page arguments' => array(0, 1, 2, 3),
'access arguments' => array('access content'),
);
return $items;
}
function ting_object_tree_theme($existing, $type, $theme, $path) {
return array(
'ting_object_tree_item' => array(
'variables' => array(
'title' => NULL,
'creators' => NULL,
'cover' => NULL,
),
'template' => 'ting-object-tree-item',
'path' => $path . '/templates',
),
'ting_object_tree_page' => array(
'variables' => array(
'items' => NULL,
'links' => NULL,
'breadcrumb' => NULL,
'menu' => NULL,
'title' => NULL,
'pager' => NULL,
),
'template' => 'ting-object-tree-page',
'path' => $path . '/templates',
),
'ting_object_tree_list' => array(
'variables' => array(
'items' => NULL,
'pager' => NULL,
'title' => NULL,
),
'template' => 'ting-object-tree-list',
'path' => $path . '/templates',
),
'ting_object_tree_mobile_menu' => array(
'variables' => array(
'menu' => NULL,
),
'template' => 'ting-object-tree-mobile-menu',
'path' => $path . '/templates',
),
);
}
/**
* Implements hook_ctools_plugin_directory().
*
* It simply tells panels where to find the .inc files that define various
* args, contexts, content_types.
*/
function ting_object_tree_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && !empty($plugin)) {
return "plugins/$plugin";
}
}
function ting_object_tree_show($type, $adult, $fiction, $subject) {
$query = ting_object_tree_get_query($type, $adult, $fiction, $subject);
$parameters = drupal_get_query_parameters();
$page = 1;
$size = 48;
if (isset($parameters['page'])) {
$page = $parameters['page'];
}
$result = ting_object_tree_find_entities_with_covers($query, $page, $size, array());
$breadcrumb = ting_object_tree_get_tree_breadcrumb($type, $adult, $fiction, $subject);
$menu_links = menu_tree_output(menu_tree_page_data('menu-object-navigation', NULL, TRUE));
$menu = drupal_render($menu_links);
$title = ucfirst($subject);
drupal_add_js(drupal_get_path('module', 'ting_object_tree') . '/js/object_tree_categories.js', 'file');
//file_put_contents("/var/www/drupal7vm/drupal/debug/newobj19.txt", print_r($menu_themed , TRUE), FILE_APPEND);
return theme('ting_object_tree_page', array('items' => $result['objects'], 'links' => $result['links'], 'breadcrumb' => $breadcrumb, 'menu' => $menu, 'title' => $title, 'pager' => $result['pager']));
}
function ting_object_tree_get_query($type, $adult, $fiction, $subject) {
$query = ting_object_tree_get_type($type);
if ($adult == 'børn') {
$query .= ' and facet.category="børnematerialer"';
} elseif ($adult != 'alle') {
$query .= ' and facet.category="voksenmaterialer"';
}
if ($fiction == 'fag') {
$query .= ' and facet.genreCategory="nonfiktion"';
} elseif ($fiction != 'alle') {
$query .= ' and facet.genreCategory="fiktion"';
}
if ($subject != 'alle') {
$query .= ' and facet.subject="' . $subject . '"';
}
//file_put_contents("/var/www/drupal7vm/drupal/debug/newobj.txt", print_r($query . "\n", TRUE), FILE_APPEND);
return $query;
}
function ting_object_tree_get_type($type){
$types = ting_object_tree_get_material_types();
if (array_key_exists($type, $types)) {
$query = 'facet.type=("' . implode('" or "', $types[$type]) . '")';
return $query;
}
return '*';
}
function ting_object_tree_get_material_types(){
$types = array (
'bog' => array ('bog', 'billedbog', 'tegneserie'),
'ebog' => array ('ebog', 'billedbog (net)'),
'lydbog' => array ('lydbog (net)', 'lydbog (cd-mp3)', 'lyd (podcast)', 'lydbog (cd)'),
'musik' => array ('cd (musik)', 'node', 'grammofonplade'),
'film' => array ('film (net)', 'dvd', 'blu-ray', 'film'),
'artikel' => array ('artikel', 'netdokument', ),
'tidsskrift' => array ('tidsskrift (net)', 'avis (net)', 'tidsskrift', 'periodikum'),
);
return $types;
}
/**
* Find ting entities with covers from a query.
*
* @param string $query
* Query to use.
* @param int $start
* Offset to start from.
* @param int $size
* Search chunk size to use.
* @param array $ignore
* Material ids to skip.
*
* @return array
* Array of found ting entities (an array), next start to use, and whether
* the result end was reached.
*/
function ting_object_tree_find_entities_with_covers($query, $page, $size, array $ignore) {
$entities_found_with_covers = array();
$finished = FALSE;
$entities = array();
$sal_query = ting_start_query()
->withRawQuery($query)
// ->withFacets(array('facet.subject'))
//->withTermsPrFacet(26)
->withSort('date_descending')
->withPage($page)
->withCount($size)
->withMaterialFilter($ignore, FALSE);
//$sal_query->reply_only = true;
$results = $sal_query->execute();
//file_put_contents("/var/www/drupal7vm/drupal/debug/newcar3.txt", print_r($results , TRUE), FILE_APPEND);
if (!$results->hasMoreResults()) {
$finished = TRUE;
}
$collections = $results->getTingEntityCollections();
$collections = $results->getTingEntityCollections();
foreach ($results->openSearchResult->collections as $collection) {
$object = $collection->getPrimary_object();
$entities[$object->getId()] = $object;
}
//file_put_contents("/var/www/drupal7vm/drupal/debug/newobj2.txt", print_r($entities, TRUE), FILE_APPEND);
// See what materials have covers.
$covers = ting_covers_get(array_keys($entities));
$objects = ting_object_tree_proces_results($entities);
$links = array();//ting_object_tree_get_subjects($results);
$pager = ting_object_tree_pager($results);
$result = array(
'objects' => $objects,
'links' => $links,
'pager' => $pager,
);
return $result;
}
function ting_object_tree_pager($results) {
$pager_prev = '';
$pager_next = '';
$parameters = drupal_get_query_parameters();
$path = request_path();
$page = 1;
if (isset($parameters['page'])) {
$page = $parameters['page'];
}
if ($page > 1) {
$prev_page = $page -1;
$options = array('query' => array('page' => $prev_page));
$link = l('Forrige',$path, $options);
$pager_prev = '<div class="ting-object-tree-objects-previous more-link">' . $link . '</div>';
}
if ($results->hasMoreResults()) {
$next_page = $page + 1;
$options = array('query' => array('page' => $next_page));
$link = l('Næste', $path, $options);
$pager_next = '<div class="ting-object-tree-objects-next more-link">' . $link . '</div>';
}
return $pager_prev . $pager_next;
}
function ting_object_tree_proces_results(array $objects) {
$items = array();
foreach ($objects as $object) {
$cover = ting_object_tree_get_cover($object);
$creators = ting_object_tree_get_creators($object);
$items[] = theme ('ting_object_tree_item', array('title' => $object->getTingObject()->getShortTitle(), 'creators' => $creators, 'cover' => $cover,));
}
return $items;
}
function ting_object_tree_get_cover($object) {
$image = theme('ting_object_cover', array('elements' => array('#object' => $object, '#image_style' => 'ding_list_medium')));
$options = array(
'html' => TRUE,
'attributes' => array('title' => $object->getTitle()),
);
return l($image, ting_object_tree_uri($object), $options);
}
function ting_object_tree_get_creators($object) {
if (count($object->getCreators())) {
if ($object->getDate()!= '') {
$markup_string = t('By !author_link (@year)', array(
'!author_link' => implode(', ', $object->getCreators()),
// So wrong, but appears to be the way the data is.
'@year' => $object->getDate(),
));
} else {
$markup_string = t('By !author_link', array(
'!author_link' => implode(', ', $object->getCreators()),
));
}
} elseif ($object->getDate() != '') {
$markup_string = t('(@year)', array('@year' => $object->getDate()));
}
return $markup_string;
}
function ting_object_tree_uri($object) {
return 'ting/collection/' . $object->id;
}
function ting_object_tree_get_subjects($results) {
$facets = $results->getFacets();
$path = request_path();
$path_elements = explode('/', $path);
$current = end($path_elements);
$links = array();
foreach ($facets['facet.subject']->getTerms() as $term ) {
if ($term->getName() != $current) {
$links[] = l($term->getName() . ' (' . $term->getCount() . ')', $path . '/' . $term->getName());
}
}
return $links;
}
function ting_object_tree_get_tree_breadcrumb($type, $adult, $fiction, $subject) {
$links = array();
$links[] = l(ucfirst($type), '/' . $type . '/alle/alle/alle');
if ($adult != 'alle') {
$links[] = l(ucfirst($adult), '/' . $type . '/' . $adult . '/alle/alle');
}
if ($fiction != 'alle') {
$links[] = l(ucfirst($fiction), '/' . $type . '/' . $adult . '/' . $fiction . '/alle');
}
if ($subject != 'alle') {
$links[] = l(ucfirst($subject), '/' . $type . '/' . $adult . '/' . $fiction . '/' . $subject);
}
$bread_crumb = implode('<span class="ting-object-tree-breadcrumb-icon"></span>', $links);
return $bread_crumb;
}
function ting_object_tree_get_breadcrumb($object) {
$ting_object = $object->getTingObject();
$type = ting_object_tree_get_breadcrumb_type($ting_object);
$audience = $ting_object->getAudience();
if (isset($audience) && $audience[0] == 'børnematerialer') {
$audience = 'børn';
} else {
$audience = 'voksen';
}
if ($ting_object->isFiction()) {
$fiction = 'skøn';
} else {
$fiction = 'fag';
}
$subject = ting_object_tree_get_subject($ting_object);
$bread_crumb = ting_object_tree_get_tree_breadcrumb($type, $audience, $fiction, $subject);//l($link_text, $path);
return $bread_crumb;
}
function ting_object_tree_get_breadcrumb_type ($object){
$type = $object->getType();
$types = ting_object_tree_get_material_types();
foreach ($types as $key => $material_type) {
if (in_array(strtolower($type), $material_type)) {
return $key;
}
}
return 'andet';
}
function ting_object_tree_get_subject($object) {
$subject = null;
$subjects = $object->getSubjects();
$genre = $object->getGenre();
if (isset($genre) && !empty($genre)) {
$subject = $genre[0];
//file_put_contents("/var/www/drupal7vm/drupal/debug/path21.txt", print_r($genre , TRUE), FILE_APPEND);
} elseif (isset($subjects) && !empty($subjects)) {
$subject = ting_object_tree_choose_subject($subjects) ;
} else {
$subject = 'ingetemne';
}
//file_put_contents("/var/www/drupal7vm/drupal/debug/path22.txt", print_r($subject , TRUE), FILE_APPEND);
return $subject;
}
function ting_object_tree_choose_subject($subjects) {
$subject_list = variable_get('ting_object_tree_subject_list');
if (!(isset($subject_list))) {
$module_path = drupal_get_path('module', 'ting_object_tree');
$data = file_get_contents($module_path . '/data/serialized_subjects.txt');
if (isset($data)) {
$subject_list = unserialize($data);
}
variable_set('ting_object_tree_subject_list', $subject_list);
}
$subject_on_list = array();
foreach ($subjects as $subject) {
if (array_key_exists($subject, $subject_list)) {
$subject_on_list[$subject] = $subject_list[$subject];
}
}
if (!(empty($subject_on_list))) {
arsort($subject_on_list);
$keys = array_keys($subject_on_list);
return $keys[0] ;
} else {
return $subjects[0];
}
//file_put_contents("/var/www/drupal7vm/drupal/debug/newobj8.txt", print_r($subject_list, TRUE), FILE_APPEND);
}
function ting_object_tree_get_current_path() {
$elements = array();
$path = request_path();
$path_elements = explode('/', $path);
if (count($path_elements) < 4) {
return NULL;
}
$elements['type'] = $path_elements[0];
$elements['audience'] = $path_elements[1];
$elements['fiction'] = $path_elements[2];
$elements['subject'] = $path_elements[3];
return $elements;
}
/**
* Get covers for an array of ids.
*
* @param array $requested_covers
* Ids of entities to return covers for.
*
* @return array
* Array of id => file path for found covers.
*/
function ting_object_tree_covers_get(array $requested_covers) {
$entities = array();
$covers = array();
// Create array of loaded entities for passing to hooks.
foreach ($requested_covers as $id) {
// Ensure that the id at least seems valid.
if (!mb_check_encoding($id, "UTF-8")) {
continue;
}
// Determine if the local id is a known negative.
if (cache_get('ting_covers:' . $id, FALSE)) {
continue;
}
// If we we already have a valid cover image, use it.
$path = ting_covers_object_path($id);
if (file_exists($path)) {
$covers[$id] = $path;
continue;
}
// Queue for fetching by hook.
$entities[$id] = ''; //ding_entity_load($id);
}
// Fetch covers by calling hook.
foreach (module_implements('ting_covers') as $module) {
foreach (module_invoke($module, 'ting_covers', $entities) as $id => $uri) {
if ($uri && $path = _ting_covers_get_file($id, $uri)) {
$covers[$id] = $path;
}
// Remove elements where a cover has been found, or suppressed.
unset($entities[$id]);
}
}
// Mark all remaining as not found in cache.
foreach ($entities as $id => $entity) {
cache_set('ting_covers:' . $id, 1, 'cache', $_SERVER['REQUEST_TIME'] + TING_COVERS_DEFAULT_CACHE_LIFETIME);
}
return $covers;
}