-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathding_item_viewer.module
892 lines (775 loc) · 22.9 KB
/
ding_item_viewer.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
<?php
/**
* @file
* Ding item viewer module.
*
* Shows ting items in form of a tabbed carousel.
*/
define('DING_ITEM_VIEWER_RESULT', 'ding_item_viewer_result');
/**
* Implements hook_menu().
*/
function ding_item_viewer_menu() {
$menu = array();
// Configuration page.
$menu['admin/config/ding/item_viewer'] = array(
'title' => 'Ding item viewer',
'description' => 'Configure ding item viewer',
'access arguments' => array('manage item viewer'),
'page callback' => 'ding_item_viewer_admin_page',
'page arguments' => array(),
'file' => 'ding_item_viewer.admin.inc',
);
// AJAX handler.
$menu['ding_item_viewer'] = array(
'title' => 'Ding item viewer AJAX handler',
'description' => 'Sends rendered items over AJAX',
'page callback' => 'ding_item_viewer_ajax',
'page arguments' => array(),
'access callback' => TRUE,
);
$menu['ding_item_viewer_get_data'] = array(
'title' => 'Ding item viewer data AJAX handler',
'page callback' => 'ding_item_viewer_get_data',
'page arguments' => array(),
'access callback' => TRUE,
);
return $menu;
}
/**
* Implements hook_permission().
*/
function ding_item_viewer_permission() {
$perm = array();
$perm['manage item viewer'] = array(
'title' => t('Manage ding item viewer'),
'description' => t(
'Configure ding item viewer settings such as search query and cache time.'
),
);
return $perm;
}
/**
* Implements hook_theme().
*/
function ding_item_viewer_theme($existing, $type, $theme, $path) {
$hooks = array();
$mod_path = drupal_get_path('module', 'ding_item_viewer');
$hooks['ding_item_viewer_admin_form'] = array(
'render element' => 'form',
);
$hooks['ding_item_viewer_container'] = array(
'variables' => array(
'url' => url('ding_item_viewer'),
'preload_image' => theme_image(
array(
'path' => $mod_path . '/img/loader.gif',
'alt' => t('Loading'),
'attributes' => array(),
)
),
),
'template' => 'templates/ding_item_viewer.container',
);
$hooks['item_viewer_tab_single'] = array(
'variables' => array(),
'template' => 'templates/ding_item_viewer.single-tab',
);
$hooks['item_viewer_tabs'] = array(
'variables' => array(),
'template' => 'templates/ding_item_viewer.tabs',
);
$hooks['item_viewer_content'] = array(
'variables' => array(),
'template' => 'templates/ding_item_viewer.content',
);
$hooks['ding_item_viewer_item'] = array(
'variables' => array(),
'template' => 'ding_item_viewer.item',
'path' => $path . '/templates',
);
$hooks['ding_item_viewer_item_active'] = array(
'variables' => array(),
'template' => 'ding_item_viewer.item_active',
'path' => $path . '/templates',
);
return $hooks;
}
/**
* Implements hook_block_info().
*/
function ding_item_viewer_block_info() {
$blocks = array();
$blocks['item_viewer'] = array(
'info' => t('Ding item viewer'),
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function ding_item_viewer_block_view($delta) {
$block = array();
$block['content'] = '';
switch ($delta) {
case 'item_viewer':
// Load required JS and CSS files.
$mod_path = drupal_get_path('module', 'ding_item_viewer');
drupal_add_css($mod_path . '/css/ding_item_viewer.styles.css');
drupal_add_js($mod_path . '/js/ding_item_viewer.js');
drupal_add_js(drupal_get_path('module', 'ding_availability') . '/js/ding_availability_lib.js');
drupal_add_js(drupal_get_path('module', 'ding_reservation') . '/js/ding_reservation_scripts.js');
drupal_add_js(
array(
'ding_item_viewer' => array(
'visible_items' => 6,
'big_item_positon' => 3,
),
),
'setting'
);
$block['content'] = theme('ding_item_viewer_container');
break;
}
return $block;
}
/**
* Get ting objects.
*
* @param string $query
* Search query string.
* @param int $count
* Number of objects to search.
* @param bool $no_cover
* FALSE if item must have cover.
* @param string $sort
* Sorting order.
*
* @return array
* Ting objects.
*/
function ding_item_viewer_get_objects($query = '', $count = 0, $no_cover = FALSE, $sort = '') {
$objects = array();
$request = '(' . $query . ')';
$page = 1;
if (!empty($query) && !empty($count)) {
module_load_include('client.inc', 'ting');
// Search with portions of 10 items.
while ($result = ding_item_viewer_search_ting($request, $page++, 10, $sort)) {
// This query is out of results OR we have needed amount of items.
if ($result->numTotalCollections == 0 || count($objects) == $count) {
break;
}
foreach ($result->collections as $collection) {
// Whether we reached our item limit per query.
if (count($objects) == $count) {
break;
}
foreach ($collection->reply->objects as $search_item) {
// No cover for this item? Skip it!
try {
$cover_exists = ding_item_viewer_check_cover($search_item->localId);
}
catch (Exception $e) {
break;
}
if (!$cover_exists && !$no_cover) {
break;
}
$objects[$search_item->localId] = ding_entity_load($search_item->id);
break;
}
}
}
}
return $objects;
}
/**
* Perform ting search.
*
* @param string $query
* Searched query string.
* @param int $page
* Requested results page.
* @param int $records
* Number of records to fetch.
* @param string $sort
* Sorting order.
*
* @return object|bool
* Ting search result object or FALSE.
*/
function ding_item_viewer_search_ting($query = '', $page = 1, $records = 10, $sort = '') {
if (!empty($query)) {
$query = '(' . $query . ')';
$options = array(
'allObjects' => FALSE,
'enrich' => TRUE,
'sort' => $sort,
);
$search_result = ting_do_search($query, $page, $records, $options);
return $search_result;
}
return FALSE;
}
/**
* Check for cover existence.
*
* @param string $local_id
* Ting item local id.
*
* @return bool
* TRUE if item has a cover, FALSE otherwise.
*/
function ding_item_viewer_check_cover($local_id) {
$cid = 'ding_item_viewer_cover-check-' . $local_id;
$cache = cache_get($cid, 'cache_ding_item');
$value = NULL;
if (!$cache) {
$service = new AdditionalInformationService(
variable_get('addi_wsdl_url'),
variable_get('addi_username'),
variable_get('addi_group'),
variable_get('addi_password')
);
$cover = $service->getByFaustNumber($local_id);
$value = isset($cover[$local_id]);
cache_set($cid, $value, 'cache_ding_item');
}
else {
$value = $cache->data;
}
return $value;
}
/**
* Create missed covers.
*
* @param array $items
* Set of ting objects.
*/
function ding_item_viewer_create_missed_covers(&$items) {
foreach ($items as $item_id => $item) {
$filepath = drupal_realpath($item->image_uri);
if (!file_exists($filepath)) {
$missing_images_ids[] = $item_id;
}
}
if (!empty($missing_images_ids)) {
ding_item_viewer_get_images_from_addi($items, $missing_images_ids);
}
}
/**
* Get images from ADDI web-service.
*
* @param array $items
* Set of ting objects.
* @param array $missing_images_local_ids
* Array of ting items id's with missing covers.
*
* @return array
* Set of ting objects, with image path attached.
*/
function ding_item_viewer_get_images_from_addi(&$items, $missing_images_local_ids) {
require_once drupal_get_path('module', 'ting_covers') . '/ting_covers.pages.inc';
$service = new AdditionalInformationService(variable_get('addi_wsdl_url'), variable_get('addi_username'), variable_get('addi_group'), variable_get('addi_password'));
try {
// Local ids = Faust numbers. Library object identifiers can be confusing...
$additional_informations = $service->getByFaustNumber($missing_images_local_ids);
}
catch (Exception $e) {
return;
}
foreach ($missing_images_local_ids as $local_id) {
// Try to extract the image url from the result.
$source_url = FALSE;
if (isset($additional_informations[$local_id]) && $ai = $additional_informations[$local_id]) {
if ($ai->detailUrl) {
$source_url = $ai->detailUrl;
}
elseif ($ai->thumbnailUrl) {
$source_url = $ai->thumbnailUrl;
}
}
// Try to download the image locally.
$file = _ting_covers_pages_fetch_image(ting_covers_object_path($local_id), $source_url);
if ($file) {
// Generate a path corresponding to the downloaded image, styled.
$items[$local_id]->image = file_create_url($file);
}
else {
// @todo
// Some default image perhaps.
$items[$local_id]->image = '';
}
}
}
/**
* Generate a cache id, based on a keyword.
*
* @param string $keyword
* A generic keyword.
*
* @return string
* Hash string, meaning a certain cache id.
*/
function ding_item_viewer_generate_cache_id($keyword) {
return 'ding_item_viewer-' . md5($keyword);
}
/**
* Implements hook_ding_item_cache().
*/
function ding_item_viewer_ding_item_cache() {
return array(
'ding_item_viewer' => t('Ding item viewer'),
);
}
/**
* Format ISBN number.
*
* Removes all spaces and dashes.
*
* @param string $isbn
* Unformated ISBN number.
*
* @return string
* Formated ISBN number.
*/
function _ding_item_viewer_format_isbn($isbn) {
foreach ($isbn as $k => $number) {
$isbn[$k] = str_replace(array(' ', '-'), '', $number);
}
rsort($isbn);
return $isbn;
}
/**
* Implements hook_cronapi().
*/
function ding_item_viewer_cronapi($op, $function = NULL) {
// Value in hours.
$ttl = variable_get('ding_item_viewer_cache_ttl', 1);
switch ($op) {
case 'list':
return array('ding_item_viewer_warm_cache' => 'Cache warmer');
break;
case 'rule':
switch ($function) {
case 'ding_item_viewer_warm_cache':
// Non positive values are incorrect.
$ttl = ($ttl > 0) ? $ttl : 1;
// Run every defined hour to warm the cache.
return "* {$ttl} * * *";
}
break;
case 'execute':
switch ($function) {
case 'ding_item_viewer_warm_cache':
// Clear cache.
cache_clear_all('ding_item_viewer', 'cache_ding_item', TRUE);
// Rebuild cache.
ding_item_viewer_build_data();
break;
}
break;
case 'settings':
switch ($function) {
case 'ding_item_viewer_warm_cache':
return array('enabled' => ($ttl > 0));
}
break;
case 'configure':
switch ($function) {
case 'ding_item_viewer_warm_cache':
return 'admin/config/ding/item_viewer';
}
break;
}
}
/**
* Implements hook_entity_info_alter().
*
* Set special view modes for item viewer.
*/
function ding_item_viewer_entity_info_alter(&$entity_info) {
if (!isset($entity_info['ting_object'])) {
return;
}
$entity_info['ting_object']['view modes'] += array(
'ding_item_viewer_small' => array(
'label' => t('Ding item viewer (small)'),
'custom settings' => TRUE,
),
'ding_item_viewer_big' => array(
'label' => t('Ding item viewer (big)'),
'custom settings' => TRUE,
),
);
}
/**
* Implements hook_field_formatter_info().
*/
function ding_item_viewer_field_formatter_info() {
$formatters = array();
$formatters['ting_title_ding_item_viewer'] = array(
'label' => t('Ding item viewer'),
'description' => t('Show title (truncates if it is too long)'),
'field types' => array(
'ting_title',
),
'settings' => array('link_type' => 'none'),
);
$formatters['ting_author_ding_item_viewer'] = array(
'label' => t('Ding item viewer'),
'description' => t('Show authors as plain text (without links)'),
'field types' => array(
'ting_author',
),
);
$formatters['ting_subjects_ding_item_viewer'] = array(
'label' => t('Ding item viewer'),
'description' => t('Show one subject'),
'field types' => array(
'ting_subjects',
),
);
$formatters['ting_abstract_ding_item_viewer'] = array(
'label' => t('Ding item viewer'),
'description' => t('Show trimmed abstract'),
'settings' => array('trim_length' => 600),
'field types' => array(
'ting_abstract',
),
);
$formatters['ting_object_more_info_default'] = array(
'label' => t('Default'),
'description' => t('Link to Ting object'),
'field types' => array(
'ting_object_more_info',
),
);
$formatters['ting_cover_ding_item_viewer'] = array(
'label' => t('Ding item viewer'),
'description' => t('Cover image without ajax'),
'field types' => array('ting_cover'),
'settings' => array('image_style' => 'thumbnail'),
);
return $formatters;
}
/**
* Implements hook_field_formatter_settings_form().
*/
function ding_item_viewer_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'ting_title_ding_item_viewer') {
$element['link_type'] = array(
'#type' => 'radios',
'#title' => t('Link title to'),
'#options' => array(
'none' => t("Don't link"),
'object' => t('Object'),
'collection' => t('Collection, if part of a collection'),
),
'#default_value' => $settings['link_type'],
);
}
if ($display['type'] == 'ting_abstract_ding_item_viewer') {
$element['trim_length'] = array(
'#title' => t('Trim length'),
'#type' => 'textfield',
'#size' => 10,
'#default_value' => $settings['trim_length'],
'#element_validate' => array('element_validate_integer_positive'),
'#required' => TRUE,
);
}
if ($display['type'] == 'ting_cover_ding_item_viewer') {
return ting_covers_field_formatter_settings_form(
$field,
$instance,
$view_mode,
$form,
$form_state
);
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function ding_item_viewer_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
if ($display['type'] == 'ting_title_ding_item_viewer') {
$summary = t('Link type: @type', array('@type' => $settings['link_type']));
}
if ($display['type'] == 'ting_abstract_ding_item_viewer') {
$summary = t('Trim length') . ': ' . $settings['trim_length'];
}
if ($display['type'] == 'ting_cover_ding_item_viewer') {
return ting_covers_field_formatter_settings_summary(
$field,
$instance,
$view_mode
);
}
return $summary;
}
/**
* Implements hook_field_formatter_view().
*/
function ding_item_viewer_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
foreach ($items as $delta => $item) {
switch ($display['type']) {
case 'ting_title_ding_item_viewer':
$type = 'ting_object';
$title = $entity->title;
// Truncate title.
if (strlen($title) > 50) {
$title = substr($title, 0, 50) . '...';
}
if (!empty($display['settings']['link_type']) && $display['settings']['link_type'] == 'collection') {
$type = 'ting_collection';
}
if (!empty($display['settings']['link_type']) && $display['settings']['link_type'] != 'none') {
$url = entity_uri($type, $entity);
$title = l($title, $url['path'], $url['options']);
}
else {
$title = check_plain($title);
}
$element[$delta] = array(
'#prefix' => '<h2>',
'#suffix' => '</h2>',
'#markup' => $title,
);
break;
// Use same logic as for default view, but without adding URLs.
case 'ting_author_ding_item_viewer':
$creator = NULL;
if (isset($entity->reply->record['dc:creator'])) {
if (isset($entity->reply->record['dc:creator']['oss:aut'][0])) {
$creator = $entity->reply->record['dc:creator']['oss:aut'][0];
}
elseif (isset($entity->reply->record['dc:creator']['oss:mus'][0])) {
$creator = $entity->reply->record['dc:creator']['oss:mus'][0];
}
elseif (isset($entity->reply->record['dc:creator']['oss:sort'][0])) {
$creator = $entity->reply->record['dc:creator']['oss:sort'][0];
}
}
if (!empty($creator) && !empty($entity->date)) {
$creator .= ' (' . $entity->date . ')';
}
$element[$delta] = array('#markup' => $creator);
break;
case 'ting_subjects_ding_item_viewer':
$subject = NULL;
if (isset($entity->reply->record['dc:subject']['dkdcplus:DK5-Text'][0])) {
$subject = $entity->reply->record['dc:subject']['dkdcplus:DK5-Text'][0];
}
elseif (isset($entity->reply->record['dc:subject']['oss:genre'][0])) {
$subject = $entity->reply->record['dc:subject']['oss:genre'][0];
}
elseif (isset($entity->reply->record['dc:subject'][''][0])) {
$subject = $entity->reply->record['dc:subject'][''][0];
}
if ($subject) {
$subject = l(
$subject,
'search/ting/dc.subject="' . $subject . '"',
array('attributes' => array('class' => array('subject')))
);
}
$element[$delta] = array(
'#markup' => $subject,
);
break;
case 'ting_abstract_ding_item_viewer':
$abstract = check_plain($entity->abstract);
$output = text_summary($abstract, NULL, $display['settings']['trim_length']);
if ($abstract != $output) {
$output .= ' ...';
}
$element[$delta] = array('#markup' => $output);
break;
case 'ting_object_more_info_default':
$link = l(t('More info'), 'ting/object/' . $entity->ding_entity_id);
$element[$delta] = array(
'#markup' => $link,
);
break;
case 'ting_cover_ding_item_viewer':
module_load_include('inc', 'ting_covers', 'ting_covers.pages');
list($owner_id, $local_id) = explode(':', $entity->ding_entity_id);
$cover_data = array(
array(
'image_style' => $display['settings']['image_style'],
'local_id' => $local_id,
'owner_id' => $owner_id,
),
);
$images = ting_covers_objects(FALSE, $cover_data);
$element[$delta] = array(
'#theme' => 'image',
'#path' => $images[0]['url'],
'#width' => '',
'#height' => '',
'#alt' => '',
'#title' => '',
);
break;
}
}
return $element;
}
/**
* Implements hook_field_info().
*/
function ding_item_viewer_field_info() {
return array(
'ting_object_more_info' => array(
'label' => t('More info link'),
'description' => t('Link to Ting object'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_object_more_info_default',
'no_ui' => TRUE,
),
);
}
/**
* Implements hook_field_load().
*/
function ding_item_viewer_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
foreach ($entities as $id => $entity) {
$items[$id][0]['safe_value'] = t('More info');
$items[$id][0]['value'] = t('More info');
}
}
/**
* Build widget data.
*/
function ding_item_viewer_get_data() {
$busy = variable_get(DING_ITEM_VIEWER_RESULT, FALSE);
if ($busy && $busy >= time()) {
return 'BUSY';
}
watchdog('div_ajax', 'Started ding_item_viewer_get_data');
// Set busy flag for 5min.
variable_set(DING_ITEM_VIEWER_RESULT, time() + 300);
$result = array(
'tabs' => ding_item_viewer_build_tabs(),
'content' => theme('item_viewer_content'),
'items' => ding_item_viewer_build_data(),
);
cache_set(DING_ITEM_VIEWER_RESULT, $result, 'cache_ding_item');
watchdog('div_ajax', 'Finished ding_item_viewer_get_data');
variable_delete(DING_ITEM_VIEWER_RESULT);
return $result;
}
/**
* Build tabs for item viewer.
*
* @return string
* Tabs layout markup.
*/
function ding_item_viewer_build_tabs() {
$queries = variable_get('ding_item_viewer_search_queries');
$tabs = '';
if (is_array($queries)) {
foreach ($queries as $k => $query) {
$tabs .= theme('item_viewer_tab_single', array('tab_index' => $k, 'tab_title' => $query['title']));
}
}
return theme('item_viewer_tabs', array('tabs' => $tabs));
}
/**
* Build ting related data.
*
* This entry fetches all needed objects and outputs the result as a json string
* into JS script for further use.
*/
function ding_item_viewer_build_data() {
$queries = variable_get('ding_item_viewer_search_queries');
$count = variable_get('ding_item_viewer_item_limit', 18);
// Clamp the value to 18.
if ($count > 18) {
$count = 18;
}
$sort = variable_get('ding_item_viewer_item_sorting', '');
// Cache time in hours.
$ttl = variable_get('ding_item_viewer_cache_ttl', 0) * 3600;
$result = array();
if (is_array($queries)) {
$tab = 0;
foreach ($queries as $query) {
$cid = ding_item_viewer_generate_cache_id($query['query']);
$cache = cache_get($cid, 'cache_ding_item');
if (!$cache) {
$objects = ding_item_viewer_get_objects($query['query'], $count, FALSE, $sort);
$items = array();
foreach ($objects as $item) {
$build = ting_object_view($item, 'ding_item_viewer_small');
$small = drupal_render($build);
$build = ting_object_view($item, 'ding_item_viewer_big');
$big = drupal_render($build);
$items[$item->localId] = array(
'small' => $small,
'big' => $big,
);
}
// Cache rendered items.
cache_set($cid, $items, 'cache_ding_item', REQUEST_TIME + $ttl);
}
else {
$items = $cache->data;
}
$result[$tab] = $items;
$tab++;
}
}
return $result;
}
/**
* Implements hook_flush_caches().
*/
function ding_item_viewer_flush_caches() {
// Remove local lock variable.
variable_delete(DING_ITEM_VIEWER_RESULT);
}
/**
* AJAX handler.
*/
function ding_item_viewer_ajax() {
// Continue running script if user refreshes the page.
ignore_user_abort(TRUE);
$response = array(
'status' => 'OK',
'data' => array(),
'error' => '',
);
$cache = cache_get(DING_ITEM_VIEWER_RESULT, 'cache_ding_item');
if (!$cache) {
$result = ding_item_viewer_get_data();
if ($result == 'BUSY') {
$response['status'] = $result;
}
else {
$response['data'] = $result;
}
}
else {
$response['data'] = $cache->data;
}
// Do not cache ajax reply.
drupal_add_http_header(
'Cache-Control',
'no-cache, no-store, must-revalidate'
);
drupal_add_http_header('Pragma', 'no-cache');
drupal_add_http_header('Expires', 0);
drupal_json_output($response);
}
include_once 'ding_item_viewer.features.inc';