forked from ding2/ding_availability
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathding_availability.module
182 lines (158 loc) · 4.63 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
<?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'),
'type' => MENU_CALLBACK,
);
$items['ding_availability/holdings'] = array(
'title' => 'Availability and holding status',
'page callback' => 'ding_availability_holdings_js',
'access arguments' => array('access content'),
'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) {
drupal_json_output(ding_availability_items(explode(',', $provider_ids)));
}
/**
*
*/
function ding_availability_holdings_js($provider_ids) {
drupal_json_output(ding_availability_holdings(explode(',', $provider_ids)));
}
/**
*
*/
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 ($item['available'] && $item['reservable']) {
$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,
);
}
}