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.
 
 
 
 
 

162 lines
6.4 KiB

  1. <?php
  2. /**
  3. * (c) Kitodo. Key to digital objects e.V. <contact@kitodo.org>
  4. *
  5. * This file is part of the Kitodo and TYPO3 projects.
  6. *
  7. * @license GNU General Public License version 3 or later.
  8. * For the full copyright and license information, please read the
  9. * LICENSE.txt file that was distributed with this source code.
  10. */
  11. namespace Kitodo\Dlf\Plugin;
  12. use TYPO3\CMS\Core\Page\PageRenderer;
  13. use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
  14. use TYPO3\CMS\Core\Utility\GeneralUtility;
  15. use TYPO3\CMS\Core\Utility\MathUtility;
  16. use TYPO3\CMS\Core\Utility\PathUtility;
  17. /**
  18. * Plugin AudioPlayer for the 'dlf' extension
  19. *
  20. * @author Sebastian Meyer <sebastian.meyer@slub-dresden.de>
  21. * @package TYPO3
  22. * @subpackage dlf
  23. * @access public
  24. */
  25. class AudioPlayer extends \Kitodo\Dlf\Common\AbstractPlugin
  26. {
  27. public $scriptRelPath = 'Classes/Plugin/AudioPlayer.php';
  28. /**
  29. * Holds the current audio file's URL, MIME type and optional label
  30. *
  31. * @var array
  32. * @access protected
  33. */
  34. protected $audio = [];
  35. /**
  36. * Adds Player javascript
  37. *
  38. * @access protected
  39. *
  40. * @return string The output string for the ###JAVASCRIPT### template marker
  41. */
  42. protected function addPlayerJS()
  43. {
  44. $markerArray = '';
  45. // CSS files.
  46. $cssFiles = [
  47. 'Resources/Public/Javascript/jPlayer/blue.monday/css/jplayer.blue.monday.min.css'
  48. ];
  49. // Inline CSS.
  50. $inlineCSS = '#tx-dlf-audio { width: 100px; height: 100px; }';
  51. //Javascript files.
  52. $jsFiles = [
  53. // jPlayer
  54. 'Resources/Public/Javascript/jPlayer/jquery.jplayer.min.js',
  55. // AudioPlayer
  56. 'Resources/Public/Javascript/AudioPlayer/AudioPlayer.js'
  57. ];
  58. // AudioPlayer configuration.
  59. $audioplayerConfiguration = '
  60. $(document).ready(function() {
  61. AudioPlayer = new dlfAudioPlayer({
  62. audio: {
  63. mimeType: "' . $this->audio['mimetype'] . '",
  64. title: "' . $this->audio['label'] . '",
  65. url: "' . $this->audio['url'] . '"
  66. },
  67. parentElId: "tx-dlf-audio",
  68. swfPath: "' . PathUtility::stripPathSitePrefix(ExtensionManagementUtility::extPath($this->extKey)) . 'Resources/Public/Javascript/jPlayer/jquery.jplayer.swf"
  69. });
  70. });
  71. ';
  72. // Add Javascript to page footer if not configured otherwise.
  73. if (empty($this->conf['addJStoBody'])) {
  74. $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
  75. foreach ($cssFiles as $cssFile) {
  76. $pageRenderer->addCssFile(PathUtility::stripPathSitePrefix(ExtensionManagementUtility::extPath($this->extKey)) . $cssFile);
  77. }
  78. $pageRenderer->addCssInlineBlock('kitodo-audioplayer-configuration', $inlineCSS);
  79. foreach ($jsFiles as $jsFile) {
  80. $pageRenderer->addJsFooterFile(PathUtility::stripPathSitePrefix(ExtensionManagementUtility::extPath($this->extKey)) . $jsFile);
  81. }
  82. $pageRenderer->addJsFooterInlineCode('kitodo-audioplayer-configuration', $audioplayerConfiguration);
  83. } else {
  84. foreach ($jsFiles as $jsFile) {
  85. $markerArray .= '<script type="text/javascript" src="' . PathUtility::stripPathSitePrefix(ExtensionManagementUtility::extPath($this->extKey)) . $jsFile . '"></script>' . "\n";
  86. }
  87. $markerArray .= '
  88. <script type="text/javascript">
  89. /*<![CDATA[*/
  90. /*kitodo-audioplayer-configuration*/
  91. ' . $audioplayerConfiguration . '
  92. /*]]>*/
  93. </script>';
  94. }
  95. return $markerArray;
  96. }
  97. /**
  98. * The main method of the PlugIn
  99. *
  100. * @access public
  101. *
  102. * @param string $content: The PlugIn content
  103. * @param array $conf: The PlugIn configuration
  104. *
  105. * @return string The content that is displayed on the website
  106. */
  107. public function main($content, $conf)
  108. {
  109. $this->init($conf);
  110. // Load current document.
  111. $this->loadDocument();
  112. if (
  113. $this->doc === null
  114. || $this->doc->numPages < 1
  115. ) {
  116. // Quit without doing anything if required variables are not set.
  117. return $content;
  118. } else {
  119. // Set default values if not set.
  120. // $this->piVars['page'] may be integer or string (physical structure @ID)
  121. if (
  122. (int) $this->piVars['page'] > 0
  123. || empty($this->piVars['page'])
  124. ) {
  125. $this->piVars['page'] = MathUtility::forceIntegerInRange((int) $this->piVars['page'], 1, $this->doc->numPages, 1);
  126. } else {
  127. $this->piVars['page'] = array_search($this->piVars['page'], $this->doc->physicalStructure);
  128. }
  129. $this->piVars['double'] = MathUtility::forceIntegerInRange($this->piVars['double'], 0, 1, 0);
  130. }
  131. // Check if there are any audio files available.
  132. $fileGrpsAudio = GeneralUtility::trimExplode(',', $this->conf['fileGrpAudio']);
  133. while ($fileGrpAudio = array_shift($fileGrpsAudio)) {
  134. if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$this->piVars['page']]]['files'][$fileGrpAudio])) {
  135. // Get audio data.
  136. $this->audio['url'] = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$this->piVars['page']]]['files'][$fileGrpAudio]);
  137. $this->audio['label'] = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$this->piVars['page']]]['label'];
  138. $this->audio['mimetype'] = $this->doc->getFileMimeType($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$this->piVars['page']]]['files'][$fileGrpAudio]);
  139. break;
  140. }
  141. }
  142. if (!empty($this->audio)) {
  143. // Add jPlayer javascript.
  144. $markerArray['###JAVASCRIPT###'] = $this->addPlayerJS();
  145. } else {
  146. // Quit without doing anything if required variables are not set.
  147. return $content;
  148. }
  149. // Load template file.
  150. $this->getTemplate();
  151. $content .= $this->templateService->substituteMarkerArray($this->template, $markerArray);
  152. return $this->pi_wrapInBaseClass($content);
  153. }
  154. }