forked from ding2/ding_availability
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathding_availability.module
250 lines (223 loc) · 6.82 KB
/
ding_availability.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
<?php
/**
* @file
* Availability information for ding objects.
*/
// Load Field module hooks.
module_load_include('inc', 'ding_availability', 'ding_availability.field');
/**
* Implements hook_menu().
*/
function ding_availability_menu() {
$items['ding_availability/items'] = array(
'title' => 'Availability status',
'page callback' => 'ding_availability_js',
'access arguments' => array('access content'),
'delivery callback' => 'ajax_deliver',
'type' => MENU_CALLBACK,
);
$items['ding_availability/holdings'] = array(
'title' => 'Availability and holding status',
'page callback' => 'ding_availability_holdings_js',
'access arguments' => array('access content'),
'delivery callback' => 'ajax_deliver',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_ding_provider_user().
*/
function ding_availability_ding_provider_user() {
return array(
'availability' => array(
'required' => TRUE,
'install time setup' => TRUE,
),
);
}
/**
*
*/
function ding_availability_js($provider_ids) {
$data = $_POST['availability'];
drupal_json_output(ding_availability_items(explode(',', $provider_ids)));
}
/**
*
*/
function ding_availability_holdings_js($provider_ids = NULL) {
$commands = array();
$availability = $_POST['availability'];
$holdings = $_POST['holdings'];
if(is_array($availability) && is_array($holdings))
$ids = array_merge($availability, $holdings);
else
$ids = is_array($availability) ? $availability : $holdings;
$data = ding_availability_holdings($ids);
if(is_array($availability)) {
// Set availability
foreach($availability as $html_id => $avid) {
if(isset($data[$avid])) {
$return = theme('ding_availability_status', array('availability' => $data[$avid]));
$commands[] = ajax_command_replace('#' . $html_id, $return);
} else {
$default_data = array(
'local_id' => $avid,
'available' => 0,
'reservable' => 0,
'show_reservation_button' => 0,
'reserved_count' => 0,
'deferred_period' => FALSE,
'is_periodical' => 0,
'is_internet' => TRUE,
'status' => 'unavailable',
);
$return = theme('ding_availability_status', array('availability' => $default_data));
$commands[] = ajax_command_replace('#' . $html_id, $return);
}
}
}
if(is_array($holdings)) {
// Set holdings
foreach($holdings as $html_id => $avid) {
if(empty($data[$avid]) || !isset($data[$avid])) {
$data[$avid]['location']['holdings'][][] = t('Not provided');
}
$return = theme('ding_availability_holdings', array('holdings' => $data[$avid]));
$commands[] = ajax_command_html('#' . $html_id, $return);
}
}
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
/**
*
*/
function ding_availability_items($provider_ids) {
if (ding_provider_implements('availability', 'items')) {
$items = ding_provider_invoke('availability', 'items', $provider_ids);
if (!$items) {
return array();
}
foreach ($items as &$item) {
$item += array(
'reservable' => FALSE,
'available' => FALSE,
);
_ding_availability_text($item);
}
}
else {
$items = ding_availability_holdings($provider_ids);
}
return $items;
}
/**
*
*/
function ding_availability_holdings($provider_ids) {
$items = ding_provider_invoke('availability', 'holdings', $provider_ids);
if (!$items) {
return array();
}
foreach ($items as &$item) {
$item += array(
'reservable' => FALSE,
'available' => FALSE,
'holdings' => array(),
);
_ding_availability_text($item);
}
return $items;
}
/**
* Adds the human readable status text of an item.
*/
function _ding_availability_text(&$item) {
if (isset($item['status']) && $item['status'] == 'inAcquisition') {
$item['status'] = t('in acquisition');
}
elseif (($item['available'] && $item['reservable']) || $item['is_internet']) {
$item['status'] = t('available');
}
elseif (!$item['available'] && $item['reservable']) {
$item['status'] = t('on loan');
}
elseif ($item['available'] && !$item['reservable']) {
$item['status'] = t('not reservable');
}
elseif (!$item['available'] && !$item['reservable']) {
$item['status'] = t('unavailable');
}
}
/**
* Implements hook_block_info().
* Define availability legend block.
*/
function ding_availability_block_info() {
return array(
'legend' => array(
'info' => t('Ding availability legend'),
'cache' => DRUPAL_CACHE_PER_PAGE,
),
);
}
/**
* Implements hook_block_view().
* Define availability legend block.
*/
function ding_availability_block_view($delta = '') {
$block['subject'] = t('Ding availability legend');
$block['content'] = ding_availability_render_legend();
return $block;
}
/**
* Return rendered legend block for availability types.
*/
function ding_availability_render_legend() {
drupal_add_css(drupal_get_path('module', 'ding_availability') . '/css/ding_availability_legend.css');
// construct the image's path (.gif stored in a module subdir)
$image_path = drupal_get_path('module', 'ding_availability') . '/images/blank.gif';
// make some text, image's alt & title tags (SEO, accessibility)
$availability_legend['available'] = t('Available');
$availability_legend['on-loan'] = t('On loan');
$availability_legend['unavailable'] = t('Unavailable');
$availability_legend['unreservable'] = t('Not reservable');
// render image html using theme_image (returns NULL if file doesn't exist)
foreach ( $availability_legend as $key => $val ) {
$format_label = '<span class="availability-label">' . $val . '</span>';
$format_image = theme('image', array('path'=>$image_path, 'alt'=>$val, 'title'=>$val));
$format_items[] = '<div class="availability-legend-item ' . $key . '">' . $format_image . $format_label . '</div>';
};
$format_items[] = '<div class="clearfix"></div>';
return '<div class="availability-legend">' . implode($format_items) . '</div>';
}
/**
* ting_object_entities preprocessor.
*/
function ding_availability_preprocess_ting_object_entities(&$variables) {
if (!empty($variables['content']) && function_exists('ding_availability_render_legend')) {
$variables['content']['availability_legend'] = array(
'#markup' => ding_availability_render_legend(),
'#weight' => -10,
);
}
}
/**
* Implements hook_theme
*/
function ding_availability_theme($existing, $type, $theme, $path) {
return array(
'ding_availability_holdings' => array(
'variables' => array('holdings' => array()),
'file' => 'ding_availability.theme.inc',
),
'ding_availability_status' => array(
'variables' => array('availability' => NULL),
'file' => 'ding_availability.theme.inc',
),
);
}