swftools_jw5.module

Tracking 6.x-3.x branch
  1. drupal
    1. 6 contributions/swftools/jw5/swftools_jw5.module

Enables SWF Tools support for LongTail Player 5.

Functions & methods

NameDescription
swftools_jw5_add_jsAdds Wijering4 JavaScript to the page.
swftools_jw5_helpImplementation of hook_help().
swftools_jw5_initImplementation of hook_init().
swftools_jw5_menuImplementation of hook_menu().
swftools_jw5_swftools_methodsImplementation of hook_swftools_methods().
swftools_jw5_swftools_playlist_jwplayer5Implementation of hook_swftools_playlist_[player]().
swftools_jw5_swftools_preprocess_jwplayer5Implementation of hook_swftools_preprocess_[player]().
swftools_jw5_swftools_variable_mappingImplementation of hook_swftools_variable_mapping().
swftools_jw5_themeImplementation of hook_theme().
theme_swftools_jw5_accessibleReturns markup to enable accessible controls for the Wijering 4 player.
_swftools_jw5_flashvarsReturn the default flashvar configuration for JW Player 4.
_swftools_jw5_settingsReturns an array of default settings for the JW Player

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Enables SWF Tools support for LongTail Player 5.
  5. */
  6. /**
  7. * Implementation of hook_swftools_methods().
  8. */
  9. function swftools_jw5_swftools_methods() {
  10. // Wijering4 now comes in two versions - viral and non-viral - since we now cache the results of this function we can
  11. // check which is present, testing for the viral one first
  12. // Start with the viral player
  13. $player = '/player-viral.swf';
  14. // Get the library path
  15. $library = swftools_get_library('mediaplayer-5');
  16. // Discover if the viral player is present
  17. if (!file_exists($library . $player)) {
  18. // If not, try the non-viral version
  19. $player = '/player.swf';
  20. if (!file_exists($library . $player)) {
  21. // If non-viral isn't present either revert to viral for purposes of the download message (since viral is the default)
  22. $player = '/player-viral.swf';
  23. }
  24. }
  25. // Define the media player
  26. $jwplayer = array(
  27. 'module' => 'swftools_jw5',
  28. 'title' => t('JW Player 5'),
  29. 'download' => 'http://www.longtailvideo.com/players',
  30. 'width' => 400,
  31. 'height' => 300,
  32. 'library' => $library . $player,
  33. 'profile' => array(
  34. 'path' => 'jw5',
  35. 'settings' => array('swftools_jwplayer5'),
  36. 'file' => 'swftools_jw5.admin.inc',
  37. 'page argument' => 'swftools_jw5_profile_form',
  38. ),
  39. );
  40. // Wijering support various actions with the same player and info.
  41. $methods['video']['jwplayer5'] = $jwplayer;
  42. $methods['video_list']['jwplayer5'] = $jwplayer;
  43. $methods['audio']['jwplayer5'] = $jwplayer;
  44. $methods['audio_list']['jwplayer5'] = $jwplayer;
  45. $methods['swftools_media_display']['jwplayer5'] = $jwplayer;
  46. $methods['media_list']['jwplayer5'] = $jwplayer;
  47. $methods['image_list']['jwplayer5'] = $jwplayer;
  48. // Return methods
  49. return $methods;
  50. }
  51. /**
  52. * Implementation of hook_menu().
  53. */
  54. function swftools_jw5_menu() {
  55. $items['admin/settings/swftools/jw5'] = array(
  56. 'title' => 'JW Player 5',
  57. 'description' => 'Settings for '. l('JW Player 5', 'http://www.longtailvideo.com/players') . '.',
  58. 'access arguments' => array('administer flash'),
  59. 'page callback' => 'drupal_get_form',
  60. 'page arguments' => array('swftools_jw5_admin_form'),
  61. 'file' => 'swftools_jw5.admin.inc',
  62. );
  63. $items['admin/settings/swftools/jw5/autocomplete'] = array(
  64. 'title' => 'Autocomplete skin name',
  65. 'access arguments' => array('administer flash'),
  66. 'page callback' => 'swftools_jw5_autocomplete_skin',
  67. 'type' => MENU_CALLBACK,
  68. 'file' => 'swftools_jw5.admin.inc',
  69. );
  70. return $items;
  71. }
  72. /**
  73. * Returns an array of default settings for the JW Player
  74. *
  75. * Return either the full set, or the minimum set, of player settings. The full set is
  76. * normally request by the administration page in order to populate the form, while the
  77. * minimum set returns just those settings needed to successfully render a player.
  78. *
  79. * @param string $profile
  80. * (optional) Name of profile to use.
  81. * @param int $mode
  82. * (optional) Use one of the following constants:
  83. * - SWFTOOLS_MINIMUM_SETTINGS: Return the minimum settings to render a player.
  84. * - SWFTOOLS_FULL_SETTINGS: Return the full complement of settings.
  85. *
  86. * @return array
  87. * An array of settings.
  88. */
  89. function _swftools_jw5_settings($profile = '', $mode = SWFTOOLS_MINIMUM_SETTINGS) {
  90. // Define the defaults for the minimum and full conditions
  91. $defaults = array(
  92. SWFTOOLS_MINIMUM_SETTINGS => array(
  93. 'basic' => array(
  94. 'width' => '400',
  95. 'height' => '320',
  96. ),
  97. 'appearance' => array(
  98. 'fillemptyimages' => 0,
  99. ),
  100. 'accessibility' => array(
  101. 'accessible' => 0,
  102. ),
  103. 'imagecache' => array(
  104. 'imagecache_player' => SWFTOOLS_UNDEFINED,
  105. 'imagecache_playlist' => SWFTOOLS_UNDEFINED,
  106. ),
  107. ),
  108. SWFTOOLS_FULL_SETTINGS => array(
  109. 'basic' => array(
  110. 'playlistsize' => '',
  111. 'width' => '400',
  112. 'height' => '320',
  113. ),
  114. 'color' => array(
  115. 'backcolor' => '',
  116. 'frontcolor' => '',
  117. 'lightcolor' => '',
  118. 'screencolor' => '',
  119. ),
  120. 'appearance' => array(
  121. 'skin' => '',
  122. 'logo' => '',
  123. 'overstretch' => 'default',
  124. 'controlbar' => 'default',
  125. 'playlist' => 'default',
  126. 'plugins' => '',
  127. 'fillemptyimages' => 0,
  128. ),
  129. 'playback' => array(
  130. 'autostart' => 'default',
  131. 'bufferlength' => '',
  132. 'displayclick' => 'default',
  133. 'repeat' => 'default',
  134. 'shuffle' => 'default',
  135. 'volume' => '',
  136. ),
  137. 'interaction' => array(
  138. 'captions' => '',
  139. 'link' => '',
  140. 'linktarget' => 'default',
  141. 'streamscript' => '',
  142. 'type' => 'default',
  143. 'fullscreen' => 'default',
  144. ),
  145. 'accessibility' => array(
  146. 'accessible' => SWFTOOLS_ACCESSIBLE_DISABLED,
  147. ),
  148. 'imagecache' => array(
  149. 'imagecache_player' => SWFTOOLS_UNDEFINED,
  150. 'imagecache_playlist' => SWFTOOLS_UNDEFINED,
  151. ),
  152. ),
  153. );
  154. // TODO: Does this actually work? Once a form has been stored will swftools_variable_get() not
  155. // always retrieve a full set of variables? The minimum set will only be returned when there
  156. // are no settings stored in the database?
  157. // Retrieve current default settings
  158. $settings = swftools_variable_get('swftools_jwplayer4', $defaults[SWFTOOLS_MINIMUM_SETTINGS], $profile);
  159. // If a full set of settings are requested for the admin page then merge with full defaults
  160. if ($mode == SWFTOOLS_FULL_SETTINGS) {
  161. $settings = swftools_array_merge($defaults[SWFTOOLS_FULL_SETTINGS], $settings);
  162. }
  163. // Return resulting defaults
  164. return $settings;
  165. }
  166. /**
  167. * Return the default flashvar configuration for JW Player 4.
  168. *
  169. * This function is called from swftools_jw5_swftools_flashvars() which is called from swf()
  170. * It will return the default flashvar configuration, just prior to any overrides
  171. * passed into swf(). We start with the settings defined on admin/swf/wijering
  172. * which are returned by _swftools_jw5_settings(). Then we prepare these values for output
  173. * to html (eg. '1' become 'true') and we unset undefined flashvars to prevent their output.
  174. *
  175. * TODO: Could we just do this when we save the settings, and then save the result, as this doesn't
  176. * change until the settings are updated.
  177. *
  178. * @param string $profile
  179. * (optional) Name of profile to use.
  180. *
  181. * @return array
  182. * An array of flashvars as key => value pairs.
  183. */
  184. function _swftools_jw5_flashvars($profile = '') {
  185. // Initialise cache to handle repeated calls
  186. static $cache = array();
  187. // Store name of profile
  188. $_profile = $profile ? $profile : SWFTOOLS_UNDEFINED;
  189. // If no settings stored for this profile then fetch them
  190. if (!isset($cache[$_profile])) {
  191. // Get saved settings for this method.
  192. $settings = _swftools_jw5_settings($profile);
  193. // Flatten defaults
  194. swftools_array_flatten($settings);
  195. // Iterate over boolean settings
  196. foreach (array('autostart', 'shuffle', 'fullscreen') as $key) {
  197. // Settings are already encoded properly on the settings page, but if we called from PHP it might be 1/0 or TRUE/FALSE
  198. if (isset($settings[$key])) {
  199. $settings[$key] = _swftools_tf($settings[$key]);
  200. }
  201. }
  202. // Store the result
  203. $cache[$_profile] = $settings;
  204. }
  205. // Return the result
  206. return $cache[$_profile];
  207. }
  208. /**
  209. * Implementation of hook_swftools_playlist_[player]().
  210. */
  211. function swftools_jw5_swftools_playlist_jwplayer5($data) {
  212. // Get current defaults for the player
  213. $saved_settings = _swftools_jw5_flashvars($data['othervars']['profile']);
  214. // Initialise a string to contain the elements
  215. $xml = '';
  216. // Iterate over the playlist to build elements xml
  217. foreach ($data['othervars']['playlist_data']['playlist'] AS $track => $details) {
  218. if (!$details['image'] && $saved_settings['fillemptyimages']) {
  219. $details['image'] = theme('swftools_empty_image', $data);
  220. }
  221. // Create an individual xml element
  222. $xml .= theme('swftools_jw5_playlist_element', $details, $saved_settings['imagecache_playlist'] == SWFTOOLS_UNDEFINED ? '' : $saved_settings['imagecache_playlist']);
  223. }
  224. // Add xml wrapper around the elements
  225. $xml = theme('swftools_jw5_playlist_wrapper', $data['othervars']['playlist_data']['header'], $xml);
  226. // Return the resulting xml
  227. return $xml;
  228. }
  229. /**
  230. * Implementation of hook_swftools_variable_mapping().
  231. *
  232. */
  233. function swftools_jw5_swftools_variable_mapping() {
  234. return array(
  235. 'jwplayer5' => array(
  236. 'height' => 'flashvars',
  237. 'width' => 'flashvars',
  238. 'shuffle' => 'flashvars',
  239. 'fullscreen' => 'flashvars',
  240. 'autostart' => 'flashvars',
  241. 'quality' => 'flashvars',
  242. 'backcolor' => 'flashvars',
  243. 'frontcolor' => 'flashvars',
  244. 'lightcolor' => 'flashvars',
  245. 'screencolor' => 'flashvars',
  246. 'logo' => 'flashvars',
  247. 'captions' => 'flashvars',
  248. 'link' => 'flashvars',
  249. 'streamscript' => 'flashvars',
  250. 'skin' => 'flashvars',
  251. 'playlistsize' => 'flashvars',
  252. 'bufferlength' => 'flashvars',
  253. 'volume' => 'flashvars',
  254. 'type' => 'flashvars',
  255. 'repeat' => 'flashvars',
  256. 'linktarget' => 'flashvars',
  257. 'stretching' => 'flashvars',
  258. 'playlist' => 'flashvars',
  259. 'controlbar' => 'flashvars',
  260. 'displayclick' => 'flashvars',
  261. ),
  262. );
  263. }
  264. /**
  265. * Implementation of hook_help().
  266. */
  267. function swftools_jw5_help($path, $arg) {
  268. switch ($path) {
  269. case 'admin/settings/swftools/jw5':
  270. return '<p>' . t('These are the settings for Longtail Video\'s JW Player 5. For details of what each parameter does refer to the <a href="@wijering">JW Player for Flash wiki page</a>. It is possible that you do not need to change any of these settings and blank values will defer to friendly defaults. Note that the label in (<em>brackets</em>) is the actual flashvar name and corresponds to the wiki page. If content is embedded using the SWF Tools filter then each parameter can be over-ridden by specifying a new value in the filter string.', array('@wijering' => 'http://www.longtailvideo.com/support/jw-player/jw-player-for-flash-v5/12536/configuration-options')) . '</p>';
  271. }
  272. }
  273. /**
  274. * Implementation of hook_theme().
  275. */
  276. function swftools_jw5_theme() {
  277. return array(
  278. 'swftools_jw5_playlist_element' => array(
  279. 'template' => 'swftools-jw5-playlist-element',
  280. 'arguments' => array('element' => NULL, 'imagecache' => NULL),
  281. ),
  282. 'swftools_jw5_playlist_wrapper' => array(
  283. 'template' => 'swftools-jw5-playlist-wrapper',
  284. 'arguments' => array('header' => NULL, 'xml' => NULL),
  285. ),
  286. 'swftools_jw5_accessible' => array(
  287. 'arguments' => array('id' => NULL, 'visible' => NULL),
  288. ),
  289. );
  290. }
  291. /**
  292. * Returns markup to enable accessible controls for the Wijering 4 player.
  293. *
  294. * @param string $id
  295. * id of the player to be made accessible.
  296. * @param int $visible
  297. * Whether the controls should be visible. Options are one the following constants:
  298. * - SWFTOOLS_ACCESSIBLE_DISABLED
  299. * - SWFTOOLS_ACCESSIBLE_HIDDEN
  300. * - SWFTOOLS_ACCESSIBLE_VISIBLE
  301. *
  302. * @return string
  303. * Mark up to place accessible controls on the page.
  304. *
  305. * @ingroup themeable
  306. * @ingroup swftools
  307. */
  308. function theme_swftools_jw5_accessible($id, $visible) {
  309. $actions = array(
  310. 'play' => t('Play'),
  311. 'pause' => t('Pause'),
  312. 'mute' => t('Mute'),
  313. 'unmute' => t('Unmute'),
  314. 'stop' => t('Rewind and stop'),
  315. );
  316. return theme('swftools_accessible_controls', 'jw5', $id, $actions, $visible);
  317. }
  318. /**
  319. * Implementation of hook_init().
  320. */
  321. function swftools_jw5_init() {
  322. // Add JavaScript to enable auto-close behavior and accessibility
  323. if (variable_get('swftools_always_add_js', SWFTOOLS_ALWAYS_ADD_JS)) {
  324. swftools_jw5_add_js();
  325. }
  326. }
  327. /**
  328. * Adds Wijering4 JavaScript to the page.
  329. */
  330. function swftools_jw5_add_js() {
  331. // Add JavaScript to enable auto-close behavior and accessibility
  332. drupal_add_js(drupal_get_path('module', 'swftools_jw5') . '/swftools_jw5.js');
  333. }
  334. /**
  335. * Implementation of hook_swftools_preprocess_[player]().
  336. */
  337. function swftools_jw5_swftools_preprocess_jwplayer5(&$data) {
  338. // Get current defaults for the player
  339. $saved_settings = _swftools_jw5_flashvars($data['othervars']['profile']);
  340. // Prepare an array of flashvars by merging defaults and user values
  341. $data['flashvars'] = array_merge($saved_settings, $data['flashvars']);
  342. // If a skin has been set it may need to be expanded to a path if not already a full url
  343. if (isset($data['flashvars']['skin'])) {
  344. if (!valid_url($data['flashvars']['skin'], TRUE)) {
  345. $data['flashvars']['skin'] = base_path() . swftools_get_library('mediaplayer-5') . '/skins/' . $data['flashvars']['skin'];
  346. }
  347. }
  348. // If an image has been set then use it
  349. if ($data['othervars']['image']) {
  350. // Get source path to the image file
  351. $source = swftools_get_url_and_path($data['othervars']['image']);
  352. // If $source succeeded add image to the playlist
  353. if ($source) {
  354. // See if we need to apply an imagecache preset
  355. if ($saved_settings['imagecache_player'] != SWFTOOLS_UNDEFINED) {
  356. $source['fileurl'] = swftools_imagecache_create_path($saved_settings['imagecache_player'], $source['fileurl']);
  357. };
  358. // Store result in flashvars
  359. $data['flashvars']['image'] = $source['fileurl'];
  360. }
  361. }
  362. // Don't output imagecache_variables
  363. unset($data['flashvars']['imagecache_player'], $data['flashvars']['imagecache_playlist']);
  364. // Is this a single streamed file, or an entirely stream playlist?
  365. if ($data['othervars']['stream'] && $data['othervars']['stream'] !== TRUE) {
  366. // Set the streamer flashvar
  367. $data['flashvars']['streamer'] = $data['othervars']['stream'];
  368. }
  369. // Add the accessible controls?
  370. if ($data['flashvars']['accessible']) {
  371. // Add accessible controls
  372. $data['othervars']['#suffix'] = theme('swftools_jw5_accessible', $data['othervars']['id'], $data['flashvars']['accessible']);
  373. }
  374. // Unset the accessible parameters from flashvars so they are not output
  375. unset($data['flashvars']['accessible']);
  376. // Attach file url of the content to display to flashvars
  377. $data['flashvars']['file'] = $data['othervars']['file_url'];
  378. // Add JavaScript to enable auto-close behavior and accessibility
  379. swftools_jw5_add_js();
  380. }