A Drupal module that creates a block in which you can display a random Youtube video from a given pool.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

159 lines
4.6 KiB

  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Creates block selecting a random YouTube video from a given list
  6. */
  7. /**
  8. * Valid permissions for this module
  9. * @return array An array of valid permissions for the youtuberotate module
  10. */
  11. function youtuberotate_perm() {
  12. return array( 'access youtube rotator content', 'administer youtube rotator' );
  13. }
  14. /**
  15. *Create the admin form
  16. */
  17. function youtuberotate_admin() {
  18. $form = array();
  19. $form['settings']['youtuberotate_account'] = array(
  20. '#type' => 'textfield',
  21. '#title' => t( 'YouTube UserName' ),
  22. '#default_value' => variable_get( 'youtuberotate_account', '' ),
  23. '#size' => 20,
  24. '#description' => t( 'Enter the YouTube username for a link back to the YouTube page. This is used only for the url and will not be displayed to the user.' ),
  25. '#required' => FALSE,
  26. );
  27. $form['settings']['youtuberotate_displayname'] = array(
  28. '#type' => 'textfield',
  29. '#title' => t( 'YouTube Display Name' ),
  30. '#default_value' => variable_get( 'youtuberotate_displayname', '' ),
  31. '#size' => 20,
  32. '#description' => t( 'Enter the name of the YouTube account you would like it displayed to the user.' ),
  33. '#required' => FALSE,
  34. );
  35. $form['settings']['youtuberotate_height'] = array(
  36. '#type' => 'textfield',
  37. '#title' => t( 'Video Height' ),
  38. '#default_value' => variable_get( 'youtuberotate_height', '238' ),
  39. '#size' => 4,
  40. '#description' => t( 'Enter the height of the video in pixels.' ),
  41. '#required' => TRUE,
  42. );
  43. $form['settings']['youtuberotate_width'] = array(
  44. '#type' => 'textfield',
  45. '#title' => t( 'Video Width' ),
  46. '#default_value' => variable_get( 'youtuberotate_width', '296' ),
  47. '#size' => 4,
  48. '#description' => t( 'Enter the width of the video in pixels.' ),
  49. '#required' => TRUE,
  50. );
  51. $form['youtuberotate_movieids'] = array(
  52. '#type' => 'textarea',
  53. '#title' => t( 'List of YouTube Movie URLs' ),
  54. '#default_value' => variable_get( 'youtuberotate_movieids', '' ),
  55. '#cols' => 60,
  56. '#rows' => 10,
  57. '#resizable' => TRUE,
  58. '#description' => t( 'Enter the list of YouTube movie URLs (1 per line).' ),
  59. '#required' => TRUE,
  60. );
  61. return system_settings_form( $form );
  62. }
  63. /**
  64. *Create the menu item for the menu page
  65. */
  66. function youtuberotate_menu() {
  67. $items = array();
  68. $items['admin/settings/youtuberotate'] = array(
  69. 'title' => 'YouTube Rotator Settings',
  70. 'description' => 'Administer YouTube Rotator Settings',
  71. 'page callback' => 'drupal_get_form',
  72. 'page arguments' => array( 'youtuberotate_admin' ),
  73. 'access arguments' => array( 'administer youtube rotator' ),
  74. 'type' => MENU_NORMAL_ITEM,
  75. );
  76. return $items;
  77. }
  78. /**
  79. *Validate user input
  80. */
  81. function youtuberotate_admin_validate( $form, &$form_state ) {
  82. $movieStr = explode( "\n", $form_state['values']['youtuberotate_movieids'] );
  83. $ct = 1;
  84. foreach ( $movieStr as &$value ) {
  85. $value = substr( $value, 0, strlen( $value ) - 1 );
  86. if ( ! valid_url( $value, TRUE ) || ! strstr( $value, 'youtube.com' ) ) {
  87. form_set_error( 'youtuberotate_movieids', t( 'The Movie <em>' . $value . '</em> on line ' . $ct . ' does not appear to be a valid YouTube movie.' ) );
  88. }
  89. $ct++;
  90. }
  91. }
  92. /**
  93. *Create a block for displaying the videos
  94. */
  95. function youtuberotate_block( $op='list', $delta=0, $edit=array() ) {
  96. switch ($op) {
  97. case 'list':
  98. $blocks[0]['info'] = t( 'YouTube Rotator' );
  99. return $blocks;
  100. break;
  101. case 'view':
  102. $blocks['subject'] = t( 'YouTube Video Rotator' );
  103. $blocks['content'] = youtuberotate_content();
  104. return $blocks;
  105. break;
  106. }
  107. }
  108. /**
  109. *Display the video in the block
  110. */
  111. function youtuberotate_content() {
  112. $movieStr = explode( "\n", variable_get( 'youtuberotate_movieids', 0 ) );
  113. $chosen = array_rand( $movieStr, 1 );
  114. $movieUrl = $movieStr[$chosen];
  115. if ( strstr( $movieUrl, 'watch' ) ) {
  116. $eSign = strpos( $movieUrl, '=' ) + 1;
  117. $movieUrl = substr( $movieUrl, $eSign, strlen( $movieUrl ) );
  118. } else {
  119. $eSign = strrpos( $movieUrl, '/' ) + 1;
  120. $movieUrl = substr( $movieUrl, $eSign, strlen( $movieUrl ) );
  121. }
  122. if ( strlen( variable_get( 'youtuberotate_account', 0 ) ) > 1 ) {
  123. $ytMoreLink = '<a href="http://www.youtube.com/thesiucaviation" target="_blank">View all ' . variable_get( 'youtuberotate_displayname', 'our' ) . ' videos</a>';
  124. }
  125. return '<iframe width="' . variable_get( 'youtuberotate_width', 0 ) . '" height="' . variable_get( 'youtuberotate_height', 0 ) . '" src="http://www.youtube.com/embed/' . $movieUrl . '" frameborder="0" allowfullscreen></iframe><br />' . $ytMoreLink;
  126. }