media_archive.admin.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page callbacks for Media: Archive.
  5. */
  6. /**
  7. * Callback for /media/add/media_archive and
  8. * /admin/content/media/add/media_archive.
  9. */
  10. function media_archive_add($form, &$form_state = array(), $redirect = NULL) {
  11. global $user;
  12. $form = array();
  13. $form['archive'] = array(
  14. '#type' => 'vertical_tabs',
  15. );
  16. $form['archive']['all'] = array(
  17. '#type' => 'fieldset',
  18. '#title' => t('All Archive videos'),
  19. );
  20. // Get all archive files for this user
  21. $results = db_query("SELECT fid FROM {file_managed} WHERE uid = :uid AND uri LIKE :uri", array(
  22. ':uid' => $user->uid,
  23. ':uri' => 'archive%%'
  24. ))->fetchAll();
  25. foreach ($results as $result) {
  26. $file = file_load($result->fid);
  27. $output = theme('image', array(
  28. 'path' => 'http://img.archive.org/vi/' . pathinfo($file->uri, PATHINFO_FILENAME) . '/0.jpg',
  29. 'title' => 'title',
  30. 'alt' => 'alt',
  31. 'attributes' => array('width' => 150),
  32. 'getsize' => FALSE,
  33. ));
  34. $form['archive']['all'][$file->fid] = array(
  35. '#markup' => $output,
  36. );
  37. }
  38. /* $form['archive']['all']['test'] = array(
  39. '#type' => 'checkbox',
  40. '#title' => 'test',
  41. );*/
  42. $form['archive']['add_from_url'] = array(
  43. '#type' => 'fieldset',
  44. '#title' => t('Add from URL'),
  45. );
  46. $form['archive']['add_from_url']['url'] = array(
  47. '#type' => 'textfield',
  48. '#title' => 'URL',
  49. '#description' => 'Input the URL of the desired Archive video page.',
  50. );
  51. $form['redirect'] = array(
  52. '#type' => 'value',
  53. '#value' => $redirect,
  54. );
  55. $form['submit'] = array(
  56. '#type' => 'submit',
  57. '#value' => 'Submit',
  58. );
  59. return $form;
  60. }
  61. /**
  62. * Validation for media_archive_add().
  63. */
  64. function media_archive_add_validate($form, &$form_state) {
  65. if (!preg_match('@blip\.tv/file/([^"\& ]+)@i', $form_state['values']['url'], $matches)) {
  66. form_set_error('url', t('Please submit a valid Archive video URL.'));
  67. }
  68. }
  69. /**
  70. * Submission for media_archive_add().
  71. *
  72. * This will create a file object for the Archive video.
  73. */
  74. function media_archive_add_submit($form, &$form_state) {
  75. $defaults = array (
  76. 'display' => TRUE,
  77. );
  78. $uri = media_archive_media_parse($form_state['values']['url']);
  79. // Check to see if this a duplicate of an existing file
  80. $files = file_load_multiple(NULL, array('uri' => $uri));
  81. if ($files) {
  82. // This is ugly.
  83. $file = array_shift($files);
  84. }
  85. else {
  86. // @TODO: This won't work for Archive and many other streams.
  87. // copy($url, $destination);
  88. $file = file_uri_to_object($uri);
  89. file_save($file);
  90. }
  91. // field_attach_insert('media', $file);
  92. if ($file) {
  93. $form_state['redirect'] = 'media/' . $file->fid . '/edit';
  94. field_attach_submit('media', $file, $form, $form_state);
  95. field_attach_insert('media', $file);
  96. }
  97. else {
  98. drupal_set_message(t('An error occurred and no file was saved.'), 'error');
  99. }
  100. $form_state['redirect'] = !empty($form_state['values']['redirect']) ? $form_state['values']['redirect'] : 'media/' . $file->fid . '/edit';
  101. }