-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathting_covers_plus.pages.inc
139 lines (121 loc) · 4.92 KB
/
ting_covers_plus.pages.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
<?php
/**
* @file
* Retrieves covers for ting objects and returns them in the correct format
* based on configuration.
*/
// Load ting_covers.pages.inc for its helper functions
module_load_include('inc', 'ting_covers', 'ting_covers.pages');
/**
* Return the URL for the cover of an object.
*/
function ting_covers_plus_objects() {
$covers = array();
$image_styles = array();
$missing_images_local_ids = array();
foreach ($_POST['coverData'] as $cover_info) {
// Determine if the local id is a known negative.
if (cache_get('ting_covers:' . $cover_info['local_id'], FALSE)) {
break;
}
$path = ting_covers_object_path($cover_info['local_id']);
if (file_exists($path)) {
// If we we already have a valid cover image w/o style then just use it.
$covers[] = array(
'local_id' => $cover_info['local_id'],
'image_style' => $cover_info['image_style'],
'url' => image_style_url($cover_info['image_style'], $path),
);
}
else {
// Mark the image for retrieval.
$missing_images_local_ids[] = (object) array(
'localIdentifier' => $cover_info['local_id'],
'libraryCode' => $cover_info['owner_id'],
'materialType' => $cover_info['mtype'],
'sourceName' => $cover_info['source'],
);
// Image style to match later one.
$image_styles[$cover_info['local_id']] = $cover_info['image_style'];
}
}
// Try to download the missing images.
try {
ting_covers_plus_get_image_urls($missing_images_local_ids, $covers, $image_styles);
// The fallback call for remaining missing images is the ADDI service
$service = new AdditionalInformationService(variable_get('addi_wsdl_url'), variable_get('addi_username'), variable_get('addi_group'), variable_get('addi_password'));
// Local ids = Faust numbers. Library object identifiers can be confusing.
$additional_informations = $service->getByLocalIdentifier($missing_images_local_ids);
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->localIdentifier]) && $ai = $additional_informations[$local_id->localIdentifier]) {
if ($ai->detailUrl) {
$source_url = $ai->detailUrl;
}
elseif ($ai->thumbnailUrl) {
$source_url = $ai->thumbnailUrl;
}
}
// No cover image found? Pick from standard set of images
if (!$source_url) {
if ($source_url = _ting_covers_plus_get_default_image($local_id->materialType, $local_id->sourceName)) {
// Generate a path corresponding to the default image, styled.
$covers[] = array(
'local_id' => $local_id->localIdentifier,
'image_style' => $image_styles[$cover_info['local_id']],
'url' => image_style_url($image_styles[$cover_info['local_id']], $source_url),
);
continue;
}
}
// NB: The following should now NEVER run, as we always have a source_url defined..
// No cover image found? Cache this for future reference to avoid
// unnecessary requests.
if (!$source_url) {
cache_set('ting_covers:' . $local_id->localIdentifier, 1, 'cache', $_SERVER['REQUEST_TIME'] + TING_COVERS_DEFAULT_CACHE_LIFETIME);
continue;
}
// Try to download the image locally.
if ($file = _ting_covers_pages_fetch_image(ting_covers_object_path($local_id->localIdentifier), $source_url)) {
// Generate a path corresponding to the downloaded image, styled.
$covers[] = array(
'local_id' => $local_id->localIdentifier,
'image_style' => $image_styles[$cover_info['local_id']],
'url' => image_style_url($image_styles[$cover_info['local_id']], $file),
);
}
}
}
catch (Exception $e) {
watchdog('ting_covers_plus', 'Unable to retrieve covers from ADDI: %message', array('%message' => $e->getMessage()), WATCHDOG_ERROR);
}
// Return all image information.
drupal_json_output($covers);
}
/**
* Helper function returning default image URI for a given material type
*
* @param string $mtype
* The material type label to get standard image for.
* @return string
* An URI to an image file.
*/
function _ting_covers_plus_get_default_image($mtype, $source) {
$source_url = NULL;
$mtype_uris = variable_get('ting_default_covers_mtype_uris');
$source_uris = variable_get('ting_default_covers_source_uris');
// Check first if we have default cover defined for SOURCE NAME
if (isset($source_uris[$source])) {
$source_url = $source_uris[$source];
}
// Then check if we have default cover defined for MATERIAL TYPE
elseif (isset($mtype_uris[$mtype])) {
$source_url = $mtype_uris[$mtype];
}
// Last, use the default fallback cover if it has been defined
elseif (isset($mtype_uris['_default'])) {
$source_url = $mtype_uris['_default'];
}
return $source_url;
}