BxPlyrPlayer.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Plyr Plyr player integration
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Plyr player integration.
  13. * @see BxDolPlayer
  14. */
  15. class BxPlyrPlayer extends BxDolPlayer
  16. {
  17. /**
  18. * Standard view initialization params
  19. */
  20. protected static $CONF_STANDARD = '
  21. <div class="bx_player_plyr" {attrs_wrapper}>
  22. <video {attrs}>
  23. {webm}
  24. {mp4}
  25. {captions}
  26. </video>
  27. </div>
  28. ';
  29. /**
  30. * Minimal view initialization params
  31. */
  32. protected static $CONF_MINI = "";
  33. /**
  34. * Embed view initialization params
  35. */
  36. protected static $CONF_EMBED = "";
  37. protected $_oTemplate;
  38. protected $_bJsCssAdded = false;
  39. public function __construct ($aObject, $oTemplate)
  40. {
  41. parent::__construct ($aObject);
  42. if ($oTemplate)
  43. $this->_oTemplate = $oTemplate;
  44. else
  45. $this->_oTemplate = BxDolTemplate::getInstance();
  46. $this->_aSkins = array(
  47. );
  48. }
  49. public function getCodeAudio ($iViewMode, $aParams, $bDynamicMode = false)
  50. {
  51. // TODO:
  52. }
  53. public function getCodeVideo ($iViewMode, $aParams, $bDynamicMode = false)
  54. {
  55. // set visual mode
  56. switch ($iViewMode) {
  57. case BX_PLAYER_STANDARD:
  58. case BX_PLAYER_MINI:
  59. case BX_PLAYER_EMBED:
  60. default:
  61. $sInit = self::$CONF_STANDARD;
  62. }
  63. // attrs
  64. $sId = 'BxPlyr' . mt_rand();
  65. $sAttrsWrapper = '';
  66. $aAttrsDefault = array(
  67. 'id' => $sId,
  68. 'controls' => '',
  69. 'controlsList' => 'nodownload',
  70. 'preload' => 'none',
  71. 'autobuffer' => '',
  72. );
  73. $aAttrs = isset($aParams['attrs']) && is_array($aParams['attrs']) ? $aParams['attrs'] : array();
  74. $aAttrs = array_merge($aAttrsDefault, $aAttrs);
  75. if (isset($aParams['poster']) && is_string($aParams['poster']))
  76. $aAttrs['poster'] = $aParams['poster'];
  77. if (isset($aParams['styles']) && is_string($aParams['styles']) ? $aParams['styles'] : false) {
  78. $sAttrsWrapper = bx_convert_array2attrs(array('styles' => $aParams['styles']));
  79. unset($aParams['styles']);
  80. }
  81. $sAttrs = bx_convert_array2attrs($aAttrs);
  82. // generate files list for HTML5 player
  83. $aTypes = array(
  84. 'webm' => '<source type="video/webm" src="{url}" size="{size}" />',
  85. 'mp4' => '<source type="video/mp4" src="{url}" size="{size}" />',
  86. );
  87. $mp4 = '';
  88. $webm = '';
  89. foreach ($aTypes as $s => $ss) {
  90. if (!isset($aParams[$s]))
  91. continue;
  92. if (is_array($aParams[$s])) {
  93. foreach ($aParams[$s] as $sType => $sUrl) {
  94. if (empty($sUrl) || !isset($this->_aSizes[$sType]))
  95. continue;
  96. $$s .= str_replace(
  97. array('{url}', '{size}'),
  98. array($sUrl, $this->_aSizes[$sType]),
  99. $ss
  100. );
  101. }
  102. }
  103. elseif (is_string($aParams[$s]) && !empty($aParams[$s]))
  104. $$s = str_replace('{url}', $aParams[$s], $ss);
  105. else
  106. $$s = '';
  107. }
  108. // player code
  109. $sCode = $this->_replaceMarkers($sInit, array(
  110. 'attrs' => $sAttrs,
  111. 'attrs_wrapper' => $sAttrsWrapper,
  112. 'webm' => $webm,
  113. 'mp4' => $mp4,
  114. 'captions' => isset($aParams['captions']) ? $aParams['captions'] : '',
  115. ));
  116. // plyr initialization
  117. $sFormat = getParam('sys_player_default_format');
  118. $aOptions = array_merge(array(
  119. 'debug' => true,
  120. 'quality' => array('default' => $this->_aSizes[$sFormat]),
  121. ), $this->_aConfCustom);
  122. $sInitEditor = "
  123. gl$sId = new Plyr('#$sId', " . json_encode($aOptions) . ");
  124. ";
  125. // load necessary JS and CSS
  126. if ($bDynamicMode) {
  127. list($aJs, $aCss) = $this->_getJsCss(true);
  128. $sCss = $this->_oTemplate->addCss($aCss, true);
  129. $sScript = $sCss . "<script>
  130. if ('undefined' == typeof(window.Plyr)) {
  131. bx_get_scripts(" . json_encode($aJs) . ", function () {
  132. $sInitEditor
  133. });
  134. } else {
  135. setTimeout(function () {
  136. $sInitEditor
  137. }, 10); // wait while html is rendered in case of dynamic adding html
  138. }
  139. </script>";
  140. } else {
  141. $sScript = "
  142. <script>
  143. $(document).ready(function () {
  144. $sInitEditor
  145. });
  146. </script>";
  147. }
  148. return $this->_addJsCss($bDynamicMode) . $sScript . $sCode;
  149. }
  150. /**
  151. * Add css/js files which are needed for editor display and functionality.
  152. */
  153. protected function _addJsCss($bDynamicMode = false, $sInitEditor = '')
  154. {
  155. if ($bDynamicMode)
  156. return '';
  157. if ($this->_bJsCssAdded)
  158. return '';
  159. list($aJs, $aCss) = $this->_getJsCss();
  160. $this->_oTemplate->addJs($aJs);
  161. $this->_oTemplate->addCss($aCss);
  162. $this->_bJsCssAdded = true;
  163. return '';
  164. }
  165. protected function _getJsCss($bUseUrlsForJs = false)
  166. {
  167. $sJsPrefix = $bUseUrlsForJs ? BX_DOL_URL_MODULES : BX_DIRECTORY_PATH_MODULES;
  168. $sJsSuffix = $bUseUrlsForJs ? '' : '|';
  169. $aJs = array(
  170. $sJsPrefix . 'boonex/plyr/plugins/plyr/' . $sJsSuffix . 'plyr.min.js',
  171. );
  172. $aCss = array(
  173. BX_DIRECTORY_PATH_MODULES . 'boonex/plyr/plugins/plyr/|plyr.css',
  174. BX_DIRECTORY_PATH_MODULES . 'boonex/plyr/template/css/|main.css',
  175. );
  176. return array($aJs, $aCss);
  177. }
  178. }
  179. /** @} */