forked from Zeticon/drupal-media-mediahaven
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media_mediahaven.module
513 lines (461 loc) · 14.9 KB
/
media_mediahaven.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<?php
/**
* @file
* A block module that displays recent blog and forum posts.
*/
/**
* load configuration settings
*/
include_once dirname(__FILE__) . '/includes/media_mediahaven.variables.inc';
include_once dirname(__FILE__) . '/includes/media_mediahaven.utils.inc';
// Hooks and callbacks for integrating with File Entity module for display.
require_once dirname(__FILE__) . '/includes/media_mediahaven.formatters.inc';
drupal_add_css(drupal_get_path('module', 'media_mediahaven') . '/media_mediahaven.css');
/**
* Implements hook_menu_local_tasks_alter().
*/
function media_mediahaven_menu_local_tasks_alter(&$data, $router_item, $root_path) {
// Add action link to 'file/add' on 'admin/content/file/thumbnails' page.
if ($root_path == 'admin/content/file') {
$item = menu_get_item('admin/content/media/media_mediahaven/upload');
if (!empty($item['access'])) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
'#weight' => $item['weight'],
);
}
}
}
/**
* Implements hook_stream_wrappers().
*/
function media_mediahaven_stream_wrappers() {
return array(
'mediahaven' => array(
'name' => t('MediaHaven media'),
'class' => 'MediaMediaHavenStreamWrapper',
'description' => t('Media provided by MediaHaven.'),
'type' => STREAM_WRAPPERS_READ_VISIBLE | STREAM_WRAPPERS_WRITE_VISIBLE,
),
);
}
/**
* Implements hook_help().
*
* Displays help and module information.
*/
function media_mediahaven_help($path, $arg) {
switch ($path) {
case "admin/advanced_help#media_mediahaven":
return '<p>' . t("Advanced Help: Displays links to nodes created on this date") . '</p>';
case "admin/help#media_mediahaven":
return '<p>' . t("Help: Displays links to nodes created on this date") . '</p>';
}
}
/**
* Implements hook_block_info().
*/
function media_mediahaven_block_info() {
$blocks['media_mediahaven'] = array(
// The name that will appear in the block list.
'info' => t('Mediahaven'),
// Default setting.
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
/**
* Custom content function.
*
* Set beginning and end dates, retrieve posts from database
* saved in that time period.
*
* @return array
* A result set of the targeted posts.
*/
function media_mediahaven_contents($display) {
// $display argument is new.
// Get today's date.
$today = getdate();
// Calculate midnight a week ago.
$start_time = mktime(0, 0, 0, $today['mon'], ($today['mday'] - 7), $today['year']);
// Get all posts from one week ago to the present.
$end_time = time();
$max_num = variable_get('media_mediahaven_max', 3);
// Use Database API to retrieve current posts.
// Published.
// Most recent first. Query paused here.
$query = db_select('node', 'n')->fields('n', array('nid', 'title', 'created'))->condition('status', 1)
->condition('created', array($start_time, $end_time), 'BETWEEN')->orderBy('created', 'DESC');
if ($display == 'block') {
$query->range(0, $max_num);
}
// If called by page, query proceeds directly to execute().
return $query->execute();
}
/**
* Implements hook_block_view().
*
* Prepares the contents of the block.
*/
function media_mediahaven_block_view($delta = '') {
switch ($delta) {
case 'media_mediahaven':
$block['subject'] = t('Mediahaven');
if (user_access('access content')) {
// Use our custom function to retrieve data.
$result = media_mediahaven_contents('block');
// Array to contain items for the block to render.
$items = array();
// Iterate over the resultset and format as links.
foreach ($result as $node) {
$items[] = array(
'data' => l($node->title, 'node/' . $node->nid),
);
}
// No content in the last week.
if (empty($items)) {
$block['content'] = t('No posts available.');
}
else {
// Pass data through theme function.
$block['content'] = theme('item_list', array(
'items' => $items,
));
}
}
return $block;
}
}
/**
* Implements hook menu().
*/
function media_mediahaven_menu() {
$items = array();
$items['admin/config/media/media_mediahaven'] = array(
'title' => 'Mediahaven Settings',
'description' => 'Configuration for media mediahaven module',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_mediahaven_form'),
'access arguments' => array('administer site configuration'),
'file' => 'includes/media_mediahaven.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['media_mediahaven'] = array(
'title' => 'Media',
'page callback' => '_media_mediahaven_page',
'access arguments' => array('access media_mediahaven content'),
// Will appear in Navigation menu.
'type' => MENU_NORMAL_ITEM,
);
$items['media_mediahaven/upload'] = array(
'title' => 'Add Media to MediaHaven',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_mediahaven_upload'),
'access arguments' => array('access media_mediahaven content'),
'file' => 'includes/media_mediahaven.upload.inc',
'weight' => -1,
);
$items['media/add/media_mediahaven'] = array(
'title' => 'Add media from Mediahaven',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_mediahaven_add'),
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_ACTION,
'file' => 'includes/media_mediahaven.admin.inc',
'weight' => -1,
);
$items['admin/content/media/add/media_mediahaven'] = $items['media/add/media_mediahaven'];
$items['admin/content/media/media_mediahaven/upload'] = $items['media_mediahaven/upload'];
return $items;
}
/**
* Implements hook_permission().
*/
function media_mediahaven_permission() {
return array(
'access media_mediahaven content' => array(
'title' => t('Access content for the Media Tuur module'),
),
);
}
/**
* Implements hook_media_browser_plugin_info().
*/
function media_mediahaven_media_browser_plugin_info() {
$info['mediahaven'] = array(
'title' => t('MediaHaven'),
'class' => 'MediaMediaHavenBrowser',
);
return $info;
}
/**
* Custom page callback function, declared in current_posts_menu().
*/
function _media_mediahaven_page() {
$result = media_mediahaven_contents('page');
// Array to contain items for the page to render.
$items = array();
// Iterate over the resultset and format as links.
foreach ($result as $node) {
$items[] = array(
'data' => l($node->title, 'node/' . $node->nid),
);
}
// No content in the last week.
if (empty($items)) {
$page_array['media_mediahaven_arguments'] = array(
// Title serves as page subtitle.
'#title' => t('All posts from the last week'),
'#markup' => t('No posts available.'),
);
return $page_array;
}
else {
$page_array['media_mediahaven_arguments'] = array(
'#title' => t('All posts from the last week'),
'#items' => $items,
// Theme hook with suggestion.
'#theme' => 'item_list__media_mediahaven',
);
return $page_array;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* This alter enhances the default admin/content/file page making modifications
* to the thumbnail view by replacing the existing
* checkboxes and table with thumbnails.
*/
function media_mediahaven_form_file_entity_edit_alter(&$form, $form_state) {
// Not implemented.
}
/**
* Implements hook form_file_entity_admin_file_alter().
*/
function media_mediahaven_form_file_entity_admin_file_alter(&$form, $form_state) {
if (!empty($form_state['values']['operation'])) {
// The form is being rebuilt because an operation requiring confirmation
// We don't want to be messing with it in this case.
return;
}
if (arg(3) == 'thumbnails') {
if (empty($form['admin']['files'])) {
// Display empty text if there are no files.
$form['admin']['files'] = array(
'#markup' => '<p>' . $form['files']['#empty'] . '</p>',
);
}
else {
$files = file_load_multiple(array_keys($form['admin']['files']));
$form['admin']['files'] = array(
'#tree' => TRUE,
'#prefix' => '<div class="media-display-thumbnails media-clear clearfix"><ul id="media-browser-library-list" class="media-list-thumbnails">',
'#suffix' => '</ul></div>',
);
foreach ($files as $file) {
if (strstr($file->uri, 'mediahaven://')) {
$preview = media_mediahaven_get_thumbnail_preview($file, TRUE);
}
else {
$preview = media_get_thumbnail_preview($file, TRUE);
}
$form['admin']['files'][$file->fid] = array(
'#type' => 'checkbox',
'#title' => '',
'#prefix' => '<li>' . drupal_render($preview),
'#suffix' => '</li>',
);
}
}
}
}
/**
* Retrieve the thumbnail for a file.
*/
function media_mediahaven_get_thumbnail_preview($file, $link = NULL) {
// If a file has an invalid type, allow file_view_file() to work.
if (!file_type_is_enabled($file->type)) {
$file->type = file_get_type($file);
}
$preview = file_view_file($file, 'preview');
$preview['#show_names'] = TRUE;
$preview['#add_link'] = $link;
$preview['#theme_wrappers'][] = 'media_mediahaven_thumbnail';
return $preview;
}
/**
* Implements hook_theme().
*/
function media_mediahaven_theme($existing, $type, $theme, $path) {
return array(
// The default media file list form element.
'media_file_list' => array(
'variables' => array('element' => NULL),
),
// A preview of the uploaded file.
'media_mediahaven_thumbnail' => array(
'render element' => 'element',
'file' => 'includes/media_mediahaven.theme.inc',
),
'media_mediahaven_video' => array(
'variables' => array('uri' => NULL, 'options' => array()),
'file' => 'media_mediahaven.theme.inc',
'path' => $path . '/includes',
'template' => 'media-mediahaven-video',
),
);
}
/**
* Provides a form for adding media items from MediaHaven search.
*/
function media_mediahaven_browser_add($form, &$form_state = array()) {
module_load_include('inc', 'media', 'includes/media.browser');
// Our search term can come from the form, or from the pager.
$term = isset($form_state['values']['search']) ? $form_state['values']['search'] : (isset($_GET['search']) ? $_GET['search'] : '');
$form['search'] = array(
'#type' => 'textfield',
'#title' => t('Search'),
'#description' => t('Input a phrase or tags to search.'),
'#default_value' => $term,
);
$form['apply'] = array(
'#type' => 'button',
'#value' => t('Apply'),
);
// This is our half-assed pager.
$page = isset($_GET['page-yt']) ? $_GET['page-yt'] : 0;
if (isset($form_state['values']['search'])) {
// Reset the pager when we press apply.
$page = 0;
}
if (!empty($term)) {
$search = json_decode(media_mediahaven_rest_client()->search($term, $page * 15, 15), TRUE);
}
$empty = FALSE;
$files = array();
if (!isset($search) || !isset($search['totalNrOfResults']) || $search['totalNrOfResults'] == 0) {
$empty = TRUE;
}
else {
foreach ($search['mediaDataList'] as $media) {
// Create a temporary file object for our retrieved video.
$file = media_mediahaven_mediadata_to_file($media);
if (!isset($file->fid)) {
$file->fid = 0;
}
$uri = $file->uri;
media_browser_build_media_item($file);
$file->preview = l($file->preview, 'media/browser', array(
'html' => TRUE,
'attributes' => array(
'data-uri' => $uri,
),
'query' => array('render' => 'media-popup', 'uri' => $uri),
));
$form['media'][$uri] = array(
'#markup' => $file->preview,
'#prefix' => '<li>',
'#suffix' => '</li>',
);
$files[$uri] = $file;
}
}
if (!count($files)) {
$empty = TRUE;
}
if ($empty) {
$form['empty'] = array(
'#markup' => '<div class="empty-message">' . t('No media match your search criteria. Please try again.') . '</div>',
);
}
$query = $_GET;
if ($term !== '') {
$query['search'] = $term;
}
$dest = $query['q'];
unset($query['q']);
$prev = $next = '';
if ($page) {
$query['page-yt'] = $page - 1;
$prev = l(t('previous'), $dest, array('query' => $query));
}
$query['page-yt'] = $page + 1;
if (!$empty && $search['totalNrOfResults'] > ($page + 1) * 15) {
$next = l(t('next'), $dest, array('query' => $query));
}
$form['submitted-media'] = array(
'#type' => 'hidden',
'#default_value' => FALSE,
);
// Add the files to JS so that they are accessible inside the browser.
drupal_add_js(array('media' => array('files' => $files)), 'setting');
// Add media browser javascript and CSS.
drupal_add_js(drupal_get_path('module', 'media_mediahaven') .
'/js/media-mediahaven.browser.js'
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['media']['#prefix'] = '<div id="container">';
if (isset($search) && $search['totalNrOfResults'] > 0) {
$form['media']['#prefix'] .= '<div>' . t('Number of results:') . ' ' . $search['totalNrOfResults'] . '<span style="float:right">' . $prev . ' ' . $next . '</span></div>';
}
$form['media']['#prefix'] .= '<div id="scrollbox"><ul id="media-browser-library-list" class="media-list-thumbnails">';
$form['media']['#suffix'] = '</ul><div id="status"></div></div></div>';
return $form;
}
/**
* Submit the selected media file.
*
* @param unknown $form
* the form
* @param unknown $form_state
* formstate
*/
function media_mediahaven_browser_add_submit($form, &$form_state) {
$uri = $form_state['values']['submitted-media'];
try {
// Save the remote file.
$file = file_uri_to_object($uri, TRUE);
if (!$file->fid) {
$fragment_id = media_mediahaven_uri_parse_fragmentid($uri);
$media_data = media_mediahaven_rest_client()->getMediaObject($fragment_id);
$file = media_mediahaven_mediadata_to_file(json_decode($media_data, TRUE));
file_save($file);
}
}
catch(Exception$e) {
form_set_error('url', $e->getMessage());
return;
}
if (!$file->fid) {
form_set_error('url', t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $uri)));
return;
}
else {
$form_state['file'] = $file;
}
}
/**
* Before saving the file.
*
* @param unknown $file
* the file
*/
function media_mediahaven_file_presave($file) {
if (stristr($file->uri, 'mediahaven://')) {
$file->metadata['height'] = media_mediahaven_variable_get('thumb_height');
$file->metadata['width'] = media_mediahaven_variable_get('thumb_width');
if (($file->type) == 'image') {
$file->filemime = 'image/mediahaven';
}
elseif (($file->type) == 'video') {
$file->filemime = 'video/mediahaven';
}
}
}