-
Notifications
You must be signed in to change notification settings - Fork 0
/
luggage_podcasts.module
418 lines (323 loc) · 14.4 KB
/
luggage_podcasts.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
<?php
/**
* @file
* Code for the luggage_podcasts feature.
*/
include_once 'luggage_podcasts.features.inc';
/**
* Implements hook_theme()
*/
function luggage_podcasts_theme($existing, $type, $theme, $path) {
return array(
'node__podcast__full' => array(
'render element' => 'elements',
'template' => 'node--podcast--full',
'path' => drupal_get_path('module','luggage_podcasts') . '/templates'
)
);
}
/**
* Implements template_preprocess_node()
*/
function luggage_podcasts_preprocess_node(&$variables) {
$node = $variables['node'];
if (!($node->type == 'podcast' && $variables['view_mode'] == 'full')) {
return;
}
$variables['theme_hook_suggestions'][] = 'node__' . $node->type . '__' . $variables['view_mode'];
// Build player options
$options = array(
'alwaysShowHours' => TRUE,
'hidesharebutton' => TRUE,
'width' => 'auto',
'activeTab' => 'info',
);
$show = taxonomy_term_load($node->field_podcast_show[LANGUAGE_NONE][0]['tid']);
$options['show']['title'] = $show->name;
// if (!empty($show->field_show_subtitle[LANGUAGE_NONE][0]['value'])) {
// $options['show']['subtitle'] = $show->field_show_subtitle[LANGUAGE_NONE][0]['value'];
// }
if (!empty($show->description)) {
$options['show']['summary'] = $show->description;
}
$options['show']['poster'] = file_create_url($show->field_show_logo[LANGUAGE_NONE][0]['uri']);
$options['show']['url'] = url('taxonomy/term/' . $show->tid, array('absolute' => TRUE));
$file_url = file_create_url($node->field_podcast_file[LANGUAGE_NONE][0]['uri']);
$title = '';
if (!empty($node->field_podcast_season[LANGUAGE_NONE][0]['value'])) {
$title = '(S' . $node->field_podcast_season[LANGUAGE_NONE][0]['value'] . ':' . 'E' . $node->field_podcast_episode[LANGUAGE_NONE][0]['value'] . '): ' . $node->title;
}
else {
$title = '(E' . $node->field_podcast_episode[LANGUAGE_NONE][0]['value'] . '): ' . $node->title;
}
$options['title'] = $title;
$options['subtitle'] = format_date($node->created, 'custom', 'm/d/Y');
$options['summary'] = $node->field_podcast_description[LANGUAGE_NONE][0]['value'];
// $options['poster'] = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'luggage_podcasts') . '/images/default-avatar.jpg';
$options['permalink'] = url('node/' . $node->nid, array('absolute' => TRUE));
$file_id3_info = views_rss_itunes_get_file_info($node->field_podcast_file[LANGUAGE_NONE][0]['uri']);
if (!empty($file_id3_info) && isset($file_id3_info['playtime_seconds'])) {
$options['duration'] = format_date((int) $file_id3_info['playtime_seconds'], 'custom', 'H:i:s', 'GMT');
}
$options['publicationDate'] = format_date($node->created, 'custom', 'c');
if (!empty($node->field_podcast_chapters)) {
foreach ($node->field_podcast_chapters[LANGUAGE_NONE] as $chapter_delta => $chapter_item) {
// field_podcast_chapters_time format is hh:mm:ss
// start format is hh:mm:ss.xxx where xxx is the number of milliseconds
$chapter = array(
'start' => $chapter_item['field_podcast_chapters_time'][LANGUAGE_NONE][0]['value'] . '.000',
'title' => $chapter_item['field_podcast_chapters_title'][LANGUAGE_NONE][0]['value'],
);
if (!empty($chapter_item['field_podcast_chapters_url'][LANGUAGE_NONE][0]['url'])) {
$chapter['url'] = $chapter_item['field_podcast_chapters_url'][LANGUAGE_NONE][0]['url'];
}
$options['chapters'][] = $chapter;
}
}
$options['sources'][] = array(
'src' => $file_url,
'type' => 'audio/mp3'
);
$options['downloads'][] = array(
'name' => 'MP3',
'size' => $node->field_podcast_file[LANGUAGE_NONE][0]['filesize'],
'url' => $file_url,
'dlurl' => $file_url,
);
// Add player js
$library_dir = $GLOBALS['base_url'] . '/' . libraries_get_path('luggage_podcasts_player');
drupal_add_js(array('luggage_podcasts' => array('library_dir' => $library_dir)), 'setting');
drupal_add_js(libraries_get_path('luggage_podcasts_player') . '/js/vendor/iframeResizer.min.js');
drupal_add_js(libraries_get_path('luggage_podcasts_player') . '/js/podlove-web-moderator.min.js');
drupal_add_js(drupal_get_path('module', 'luggage_podcasts') . '/js/load_players.js', array('scope' => 'footer'));
// Set template variables
$variables['player_id'] = drupal_html_id($node->nid);
$variables['library_dir'] = $library_dir;
$variables['file_url'] = $file_url;
$variables['options_json'] = drupal_json_encode($options);
}
/**
* Implements hook_views_rss_channel_elements_alter().
*
* Channel element settings forms in views_rss_core and views_rss_itunes are text
* fields or select boxes with hard-coded options
*
* This modifies all configurable elements defined by these modules to be select
* boxes for term fields in the Show vocabulary defined by this module. The term
* the field values are eventually fetched from is the contextual filter on the
* view. A preprocess function that takes care of this is prepended to each
* element so that the expected values are available to the preprocess functions
* already defined for these elements
*/
function luggage_podcasts_views_rss_channel_elements_alter(&$elements) {
// The default title is unconfigurable and uses the site title
unset($elements['views_rss_core']['title']['configurable']);
unset($elements['views_rss_core']['title']['preprocess functions']);
// The default category preprocess function expects a comma delimited list of
// terms that it turns into individual elements
// Our generic element preprocessor already handles this
unset($elements['views_rss_core']['category']['preprocess functions']);
$elements['views_rss_core']['link']['preprocess functions'] = array('luggage_podcasts_preprocess_views_rss_core_channel_link');
$elements['views_rss_core']['image']['preprocess functions'] = array('luggage_podcasts_preprocess_views_rss_core_channel_image');
$show_field_names = array(
'' => '--',
'name' => 'Name',
'description' => 'Description'
);
foreach (field_info_instances('taxonomy_term', 'podcast_show') as $field_name => $field_info) {
$show_field_names[$field_name] = $field_info['label'];
}
$element_providers = array('views_rss_core', 'views_rss_itunes');
foreach ($element_providers as $provider) {
foreach (array_keys($elements[$provider]) as $element_id) {
$element_info = &$elements[$provider][$element_id];
if (!isset($element_info['configurable']) || $element_info['configurable']) {
$element_info['settings form'] = array(
'#type' => 'select',
'#options' => $show_field_names
);
if (isset($element_info['settings form options callback'])) {
unset($element_info['settings form options callback']);
}
if (empty($element_info['preprocess functions'])) {
$element_info['preprocess functions'] = array();
}
array_unshift($element_info['preprocess functions'], 'luggage_podcasts_preprocess_views_rss_channel_element_value');
}
}
}
}
/**
* Implements hook_views_rss_options_form_validate().
*
* views_rss_core tries to validate the values of the channel image and channel
* docs elements on the settings form but the term field values aren't available
* until rendering the view because they come from the contextual filter.
*
* These validators are also unnecessary. The image validator checks that the URL
* is valid, which ours is because it comes from a file field. It also checks that
* the width and height of the image match those specified by the RSS standard. We
* enforce this check in the filefield settings.
*
* To work around the views_rss_core form validator, we stash the real values of
* channel image and channel docs elements and set them value to empty. Then in a s
* submit handler we re-set them to the real values.
*/
function luggage_podcasts_views_rss_options_form_validate($form, &$form_state) {
$form_state['luggage_podcasts'] = array();
if (!empty($form_state['values']['style_options']['channel']['core']['views_rss_core']['image'])) {
$form_state['luggage_podcasts']['image'] = $form_state['values']['style_options']['channel']['core']['views_rss_core']['image'];
$form_state['values']['style_options']['channel']['core']['views_rss_core']['image'] = '';
}
if (!empty($form_state['values']['style_options']['channel']['core']['views_rss_core']['docs'])) {
$form_state['luggage_podcasts']['docs'] = $form_state['values']['style_options']['channel']['core']['views_rss_core']['docs'];
$form_state['values']['style_options']['channel']['core']['views_rss_core']['docs'] = '';
}
}
/**
* Implements hook_views_rss_options_form_submit().
*
* This restores the real values for the channel image and channel docs elements
* that were stashed during validation.
*/
function luggage_podcasts_views_rss_options_form_submit($form, &$form_state) {
if (isset($form_state['values']['style_options']['channel']['core']['views_rss_core']['image']) && !empty($form_state['luggage_podcasts']['image'])) {
$form_state['values']['style_options']['channel']['core']['views_rss_core']['image'] = $form_state['luggage_podcasts']['image'];
}
if (isset($form_state['values']['style_options']['channel']['core']['views_rss_core']['docs']) && !empty($form_state['luggage_podcasts']['docs'])) {
$form_state['values']['style_options']['channel']['core']['views_rss_core']['docs'] = $form_state['luggage_podcasts']['docs'];
}
unset($form_state['luggage_podcasts']);
}
/**
* Preprocess function for views_rss_core and views_rss_itunes channel elements
*
* This replaces the term field name configured in the views settings with the value
* of that field from the term received as the contextual filter
*/
function luggage_podcasts_preprocess_views_rss_channel_element_value(&$variables) {
// No value = no preprocessing.
if (empty($variables['elements'][0]['value'])) {
return;
}
$term = taxonomy_term_load($variables['view']->argument['field_podcast_show_tid']->get_value());
if (!$term) {
return;
}
$element_key = $variables['elements'][0]['key'];
$term_field_name = $variables['elements'][0]['value'];
$elements = array();
if ($term_field_name == 'name') {
$elements[] = array(
'key' => $element_key,
'value' => $term->name
);
} elseif ($term_field_name == 'description') {
$elements[] = array(
'key' => $element_key,
'value' => $term->description
);
} else {
$term_field = $term->{$term_field_name}[LANGUAGE_NONE];
$field_map = field_info_field_map();
$term_field_type = $field_map[$term_field_name]['type'];
// the itunes:category element was originally a select box allowing up to 3 options
// and the preprocess function for that element expects $variables['elements'][0]['value']
// to be an array with all 3 values that it converts into a individual elements itself
if ($element_key == 'itunes:category') {
$elements[] = array(
'key' => $element_key,
'value' => array()
);
}
foreach($term_field as $delta => $field_value) {
switch ($term_field_type) {
case 'image':
$elements[] = array(
'key' => $element_key,
'value' => substr(file_create_url($field_value['uri']), strlen($GLOBALS['base_url'] . '/'))
);
break;
case 'email':
$elements[] = array(
'key' => $element_key,
'value' => $field_value['email']
);
break;
case 'taxonomy_term_reference':
if ($element_key == 'itunes:category') {
$elements[0]['value'][] = $field_value['tid'];
} else {
$value_term = taxonomy_term_load($field_value['tid']);
$elements[] = array(
'key' => $element_key,
'value' => $value_term->name
);
}
break;
default:
$elements[] = array(
'key' => $element_key,
'value' => $field_value['value']
);
break;
}
}
}
$variables['elements'] = $elements;
}
/**
* Preprocess function for channel <link> element
*
* Replaces views_rss_core_preprocess_channel_link. The original used the site
* home page, while this links to the podcast's page
*/
function luggage_podcasts_preprocess_views_rss_core_channel_link(&$variables) {
$variables['elements'][0]['value'] = url($variables['view']->get_url(NULL, $variables['view']->display['show_page']->handler->get_path()), array('absolute' => TRUE));
}
/**
* Preprocess function for channel <image> element
*
* Replaces views_rss_core_preprocess_channel_image. The original has hardcoded
* values for the title and description, while this replaces them with field values
* from the contextual filter term
*/
function luggage_podcasts_preprocess_views_rss_core_channel_image(&$variables) {
// No value = no preprocessing.
if (empty($variables['elements'][0]['value'])) {
return;
}
$path = $variables['elements'][0]['value'];
// Get value of channel <title> element from its preprocess function.
$variables['elements'][0]['value'] = $variables['item']['core']['views_rss_core']['title'];
luggage_podcasts_preprocess_views_rss_channel_element_value($variables);
$title = $variables['elements'][0]['value'];
// Get value of channel <description> element from its preprocess function.
$variables['elements'][0]['value'] = $variables['item']['core']['views_rss_core']['description'];
luggage_podcasts_preprocess_views_rss_channel_element_value($variables);
$description = $variables['elements'][0]['value'];
// Get value of channel <title> element from its preprocess function.
luggage_podcasts_preprocess_views_rss_core_channel_link($variables);
$link = $variables['elements'][0]['value'];
// Create subelements array.
$variables['elements'][0]['value'] = array(
'url' => file_create_url($path),
'title' => $title,
'link' => $link,
);
$variables['elements'][0]['value']['description'] = $description;
// Get image width and height.
$image = image_load($path);
if (!empty($image)) {
$variables['elements'][0]['value']['width'] = $image->info['width'];
$variables['elements'][0]['value']['height'] = $image->info['height'];
}
}
/*
* Implements hook_views_pre_build()
*/
function luggage_podcasts_views_pre_build(&$view) {
if($view->name == 'luggage_podcasts' && $view->current_display == 'show_page') {
drupal_add_css(drupal_get_path('module', 'luggage_podcasts') . '/css/view-luggage-podcasts--show-page.css');
}
}