-
Notifications
You must be signed in to change notification settings - Fork 0
/
media_theplatform_mpx.browser.inc
151 lines (136 loc) · 5.3 KB
/
media_theplatform_mpx.browser.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
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* @file
* Functions for Media Browser forms.
*/
/**
* Form constructor for mpx Media Browser.
*
* @param string $mpx_type
* Valid values are 'videos' or 'players'.
*
* @see media_theplatform_mpx_media_browser_plugin_view()
* @ingroup forms
*/
function media_theplatform_mpx_form_media_browser($form, &$form_state = array(), $mpx_type = NULL) {
// Need this for media_browser_build_media_item.
module_load_include('inc', 'media', 'includes/media.browser');
$form['list']['#prefix'] = '<div id="container"><div id="scrollbox"><ul id="media-browser-library-list" class="media-list-thumbnails ' . $mpx_type . '-browser">';
$form['list']['#suffix'] = '</ul><div id="status"></div></div></div>';
$files = array();
if ($mpx_type == 'videos') {
$list = media_theplatform_mpx_get_all_mpx_videos();
$label = 'mpxMedia';
if (count($list) > 0) {
$form['mpxmedia_search'] = array(
'#title' => t('Search:'),
'#type' => 'textfield',
'#weight' => -20,
);
// Fetch all distinct accounts from mpx_player.
$accounts = media_theplatform_mpx_get_mpx_player_accounts();
foreach ($accounts as $account) {
$account_label = str_replace('%', '-', $account->account);
$form['player_fid_' . $account_label] = array(
'#type' => 'select',
'#title' => t('Render with mpxPlayer:'),
'#options' => media_theplatform_mpx_get_players_select($account->account),
'#default_value' => media_theplatform_mpx_variable_get('default_player_fid'),
'#prefix' => '<div class="mpxplayer_select ' . $account_label . '">',
'#suffix' => '</div>',
);
}
}
}
elseif ($mpx_type == 'players') {
$list = media_theplatform_mpx_get_all_mpx_players();
$label = 'mpxPlayers';
}
else {
return FALSE;
}
if (count($list) == 0) {
$form['no-items'] = array(
'#markup' => t('No :type have been imported.', array(':type' => $label)),
);
return $form;
}
foreach ($list as $item) {
$file = file_load($item->fid);
$account_label = str_replace('%', '-', $item->account);
media_browser_build_media_item($file);
$file->preview = l('<span class="' . $account_label . '">' . $file->preview . '</span>', 'media/browser', array(
'html' => TRUE,
'attributes' => array(
'data-uri' => $file->uri,
),
'query' => array('render' => 'media-popup', 'uri' => $file->uri),
));
$form['list'][$file->uri] = array(
'#markup' => $file->preview,
'#prefix' => '<li class="mpx-browser">' . '<div class="item-data">' . $item->title . ' ' . $item->description . '</div>',
'#suffix' => '</li>',
);
$files[$file->uri] = $file;
}
$form['selected_file'] = array(
'#type' => 'hidden',
'#default_value' => FALSE,
);
$form['mpx_type'] = array(
'#type' => 'hidden',
'#default_value' => $mpx_type,
);
// Add the files to JS so that they are accessible inside the browser.
drupal_add_js(array('media' => array('files' => $files)), 'setting');
// Add media_theplatform_mpx browser javascript and CSS.
drupal_add_js(drupal_get_path('module', 'media_theplatform_mpx') . '/js/media_theplatform_mpx.browser.js');
// Standard Media module files.
drupal_add_css(drupal_get_path('module', 'media') . '/js/plugins/media.library.css');
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Select'),
'#attributes' => array('class' => array('mpx-submit')),
);
return $form;
}
/**
* Form submission handler for media_theplatform_mpx_form_media_browser().
*/
function media_theplatform_mpx_form_media_browser_submit($form, &$form_state) {
$mpx_type = $form_state['values']['mpx_type'];
$embed_code = $form_state['values']['selected_file'];
// If its a mpxPlayer, simply return the fid from file_managed
if ($mpx_type == 'players') {
$fid = db_query("SELECT fid FROM {file_managed} WHERE uri=:uri", array(':uri' => $embed_code))->fetchField();
}
// Elseif its mpxMedia:
elseif ($mpx_type == 'videos') {
// The embed_code given is the file saved on the mpx_video record, so we need to split it out to see check if mpxMedia/mpxPlayer combo exists.
$url = parse_url($embed_code);
$parts = explode('/', $url['path']);
$video = media_theplatform_mpx_get_mpx_video_by_field('guid', $parts[1]);
$account_label = str_replace('%', '-', $video['account']);
// Check if this mpxMedia / mpxPlayer combo exists.
$key = 'player_fid_' . $account_label;
$player_fid = $form_state['input'][$key];
$uri = 'mpx://m/' . $parts[1] . '/p/' . $player_fid;
$fid = db_query("SELECT fid FROM {file_managed} WHERE uri=:uri", array(':uri' => $uri))->fetchField();
// If it doesn't exist, create the file.
if (!$fid) {
$provider = media_internet_get_provider($uri);
$file = $provider->save();
$fid = $file->fid;
// Update the new file's name.
$player = media_theplatform_mpx_get_mpx_player_by_fid($player_fid);
media_theplatform_mpx_update_video_filename($fid, $video['title'], $player['title']);
}
}
// This stops page reload inside the media browser iframe.
$form_state['redirect'] = array('media/browser', array(
'query' => array(
'render' => 'media-popup',
'fid' => $fid,
)));
}