-
Notifications
You must be signed in to change notification settings - Fork 1
/
enacast.module
executable file
·411 lines (366 loc) · 10.5 KB
/
enacast.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
<?php
/**
* @file
* Implements fields and functionalities for the Enacast podcast service.
*/
/**
* Implements hook_init().
*/
function enacast_init() {
backdrop_add_js(backdrop_get_path('module', 'enacast') . '/js/enacast.js');
}
/**
* Implements hook_config_info().
*/
function enacast_config_info() {
return array(
'enacast.settings' => array(
'label' => t('Enacast settings'),
'group' => t('Configuration'),
),
);
}
/**
* Implements hook_menu().
*/
function enacast_menu() {
$items = array();
$items['admin/config/services/enacast'] = array(
'title' => 'Enacast',
'description' => 'Settings for Enacast services.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('enacast_settings_form'),
'access arguments' => array('administer enacast'),
'file' => 'enacast.admin.inc',
'weight' => -15,
);
$items['admin/config/services/enacast/settings'] = array(
'title' => 'Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/config/services/enacast/update'] = array(
'title' => 'Update',
'description' => 'Update content from Enacast services.',
'page callback' => 'enacast_update_content_page',
'access arguments' => array('administer enacast'),
'file' => 'enacast.admin.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_permission().
*/
function enacast_permission() {
return array(
'administer enacast' => array(
'title' => t('Administer Enacast'),
),
);
}
/**
* Implements hook_field_info().
*/
function enacast_field_info() {
return array(
'enacast_program' => array(
'label' => t('Enacast program'),
'description' => t('Machine name of the Enacast program.'),
'instance_settings' => array(
'text_processing' => 0,
'allowed_formats' => array(),
),
'default_widget' => 'text_textfield',
'default_formatter' => 'enacast_text',
),
'enacast_radio' => array(
'label' => t('Enacast radio station'),
'description' => t('Machine name of the Enacast radio station.'),
'instance_settings' => array(
'text_processing' => 0,
'allowed_formats' => array(),
),
'default_widget' => 'text_textfield',
'default_formatter' => 'enacast_text',
),
'enacast_podcast' => array(
'label' => t('Enacast podcast PK ID'),
'description' => t('PK ID of the Enacast podcast.'),
'instance_settings' => array(
'min' => '-2147483647',
'max' => '2147483647',
'prefix' => '',
'suffix' => '',
),
'default_widget' => 'number',
'default_formatter' => 'number_integer',
),
);
}
/**
* Implements hook_field_validate().
*/
function enacast_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
if (!empty($item['enacast_program'])) {
if (preg_match('/\s/', $item['enacast_program'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'enacast_invalid',
'message' => t('Program name must not contain white spaces.'),
);
}
}
}
}
/**
* Implements hook_field_is_empty().
*/
function enacast_field_is_empty($item, $field) {
return empty($item['value']);
}
/**
* Implements hook_field_formatter_info_alter().
*/
function enacast_field_formatter_info_alter(&$info) {
$info['text_plain']['field types'][] = 'enacast_program';
$info['text_plain']['field types'][] = 'enacast_radio';
$info['number_integer']['field types'][] = 'enacast_podcast';
}
/**
* Implements hook_field_formatter_info().
*/
function enacast_field_formatter_info() {
return array(
'enacast_last_podcast_player' => array(
'label' => t('Last podcast player'),
'field types' => array('enacast_program'),
),
'enacast_podcast_player' => array(
'label' => t('Podcast player'),
'field types' => array('enacast_podcast'),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function enacast_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$config = config('enacast.settings');
$element = array();
if ($config->get('radio') == '') {
backdrop_set_message(
t('You need to configure the Enacast module to show any player: !link.', array(
'!link' => l('Enacast settings', 'admin/config/services/enacast'),
)),
'error'
);
return $element;
}
foreach ($items as $delta => $item) {
switch ($display['type']) {
case 'enacast_text':
$element[$delta] = array('#markup' => strip_tags($item['value']));
break;
case 'enacast_last_podcast_player':
$program = $item['value'];
$radio = $config->get('radio');
$url = "https://enacast.com/api/v3/podcast/?format=json&page=1&page_size=1&program={$program}&radio={$radio}";
$data = enacast_get_json($url);
if (!empty($data['results'][0])) {
$podcast = $data['results'][0];
$variables = array(
'mp3_url' => $podcast['audio'],
'title' => $podcast['name'],
'summary' => $podcast['summary'],
);
$element[$delta] = array('#markup' => theme('enacast_last_podcast', $variables));
}
break;
case 'enacast_podcast_player':
$podcast_id = $item['value'];
$radio = $config->get('radio');
$mp3_url = "https://enacast.com/{$radio}/play_podcast/{$podcast_id}.mp3";
$variables = array('mp3_url' => $mp3_url);
$element[$delta] = array('#markup' => theme('enacast_podcast', $variables));
break;
}
}
return $element;
}
/**
* Implements hook_field_widget_info_alter().
*/
function enacast_field_widget_info_alter(&$info) {
$info['text_textfield']['field types'][] = 'enacast_program';
$info['text_textfield']['field types'][] = 'enacast_radio';
$info['number']['field types'][] = 'enacast_podcast';
}
/**
* Implements hook_theme().
*/
function enacast_theme() {
return array(
'enacast_last_podcast' => array(
'variables' => array(
'mp3_url' => NULL,
'title' => NULL,
'summary' => NULL,
),
'template' => 'templates/enacast-last-podcast',
),
'enacast_podcast' => array(
'variables' => array(
'mp3_url' => NULL,
),
'template' => 'templates/enacast-podcast',
),
'enacast_now_playing_block' => array(
'variables' => array(
'radio' => NULL,
),
'template' => 'templates/enacast-now-playing-block',
),
'enacast_markers' => array(
'variables' => array(
'markers' => NULL,
'nid' => NULL,
),
'template' => 'templates/enacast-markers',
),
);
}
/**
* Implements hook_block_info().
*/
function enacast_block_info() {
return array(
'enacast_now_playing' => array(
'info' => t('Enacast Now Playing Block'),
'description' => t('Displays the currently playing content from Enacast.'),
),
);
}
/**
* Implements hook_block_configure().
*/
function enacast_block_configure($delta = '', $settings = array()) {
$form = array();
if ($delta == 'enacast_now_playing') {
$settings += array('radio' => '');
$form['radio'] = array(
'#type' => 'textfield',
'#title' => t('Radio'),
'#size' => 60,
'#description' => t('Radio station to show the now playing block from.'),
'#default_value' => $settings['radio'],
);
}
return $form;
}
/**
* Implements hook_block_view().
*/
function enacast_block_view($delta = '', $settings = array(), $contexts = array()) {
$block = array();
if ($delta == 'enacast_now_playing') {
$block['subject'] = t('Now Playing');
$block['content'] = array(
'#theme' => 'enacast_now_playing_block',
'#radio' => $settings['radio'],
);
}
return $block;
}
/**
* Preprocess function for theme_enacast_now_playing_block().
*
* @param array $variables
* An array of variables to pass to the theme function.
*/
function enacast_preprocess_enacast_now_playing_block(&$variables) {
$variables['source'] = 'https://relay.stream.enacast-cloud.com:30230/' . $variables['radio'] . 'HD.mp3';
}
/**
* Fetches JSON data from a URL.
*
* @param string $url
* The URL to fetch data from.
*
* @return mixed
* The decoded JSON data or FALSE on failure.
*/
function enacast_get_json($url) {
$options = array(
'method' => 'GET',
'timeout' => 30,
'headers' => array(
'Accept' => 'application/json',
),
);
$response = backdrop_http_request($url, $options);
if ($response->code == 200) {
return json_decode($response->data, TRUE);
}
return FALSE;
}
/**
* Implements hook_node_load().
*/
function enacast_node_load($nodes, $types) {
$config = config('enacast.settings');
$podcast_type = $config->get('podcast_type');
if (in_array($podcast_type, $types)) {
$podcast_field = $config->get('podcast_field');
foreach ($nodes as $node) {
if (!empty($node->{$podcast_field}['und'][0]['value'])) {
$pkid = $node->{$podcast_field}['und'][0]['value'];
$node->enacast_markers = enacast_get_markers($pkid);
}
}
}
}
/**
* Retrieves markers for a given PK ID.
*
* @param int $pkid
* The PK ID of the podcast.
*
* @return array
* An array of marker objects.
*/
function enacast_get_markers($pkid) {
$query = db_select('enacast_markers', 'm')
->fields('m', array('descr', 'startsecond'))
->condition('m.pkid', $pkid)
->orderBy('m.startsecond', 'ASC');
return $query->execute()->fetchAll();
}
/**
* Implements hook_node_view().
*/
function enacast_node_view($node, $view_mode, $langcode) {
$config = config('enacast.settings');
if ($node->type == $config->get('podcast_type')) {
$node->content['enacast_markers'] = array(
'#theme' => 'enacast_markers',
'#markers' => $node->enacast_markers,
'#nid' => $node->nid,
'#weight' => 10,
);
}
}
/**
* Preprocess function for theme_enacast_markers().
*
* @param array $variables
* An array of variables to pass to the theme function.
*/
function enacast_preprocess_enacast_markers(&$variables) {
$query_params = backdrop_get_query_parameters();
if (isset($query_params['s'])) {
backdrop_add_js(array('audioPlayer' => array('startTime' => $query_params['s'])), 'setting');
}
$variables['share_icon'] = backdrop_get_path('module', 'enacast') . '/images/share-2.svg';
$variables['path'] = url('node/' . $variables['nid']);
}