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.
 
 
 
 
 

182 lines
7.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\Tools;
  12. use Kitodo\Dlf\Common\Helper;
  13. use TYPO3\CMS\Core\Utility\GeneralUtility;
  14. use TYPO3\CMS\Core\Utility\MathUtility;
  15. /**
  16. * PDF Download tool for the plugin 'Toolbox' of the 'dlf' extension
  17. *
  18. * @author Sebastian Meyer <sebastian.meyer@slub-dresden.de>
  19. * @author Alexander Bigga <alexander.bigga@slub-dresden.de>
  20. * @package TYPO3
  21. * @subpackage dlf
  22. * @access public
  23. */
  24. class PdfDownloadTool extends \Kitodo\Dlf\Common\AbstractPlugin
  25. {
  26. public $scriptRelPath = 'Classes/Plugin/Tools/PdfDownloadTool.php';
  27. /**
  28. * The main method of the PlugIn
  29. *
  30. * @access public
  31. *
  32. * @param string $content: The PlugIn content
  33. * @param array $conf: The PlugIn configuration
  34. *
  35. * @return string The content that is displayed on the website
  36. */
  37. public function main($content, $conf)
  38. {
  39. $this->init($conf);
  40. // Merge configuration with conf array of toolbox.
  41. if (!empty($this->cObj->data['conf'])) {
  42. $this->conf = Helper::mergeRecursiveWithOverrule($this->cObj->data['conf'], $this->conf);
  43. }
  44. // Load current document.
  45. $this->loadDocument();
  46. if (
  47. $this->doc === null
  48. || $this->doc->numPages < 1
  49. || empty($this->conf['fileGrpDownload'])
  50. ) {
  51. // Quit without doing anything if required variables are not set.
  52. return $content;
  53. } else {
  54. if (!empty($this->piVars['logicalPage'])) {
  55. $this->piVars['page'] = $this->doc->getPhysicalPage($this->piVars['logicalPage']);
  56. // The logical page parameter should not appear again
  57. unset($this->piVars['logicalPage']);
  58. }
  59. // Set default values if not set.
  60. // $this->piVars['page'] may be integer or string (physical structure @ID)
  61. if (
  62. (int) $this->piVars['page'] > 0
  63. || empty($this->piVars['page'])
  64. ) {
  65. $this->piVars['page'] = MathUtility::forceIntegerInRange((int) $this->piVars['page'], 1, $this->doc->numPages, 1);
  66. } else {
  67. $this->piVars['page'] = array_search($this->piVars['page'], $this->doc->physicalStructure);
  68. }
  69. $this->piVars['double'] = MathUtility::forceIntegerInRange($this->piVars['double'], 0, 1, 0);
  70. }
  71. // Load template file.
  72. $this->getTemplate();
  73. // Get single page downloads.
  74. $markerArray['###PAGE###'] = $this->getPageLink();
  75. // Get work download.
  76. $markerArray['###WORK###'] = $this->getWorkLink();
  77. $content .= $this->templateService->substituteMarkerArray($this->template, $markerArray);
  78. return $this->pi_wrapInBaseClass($content);
  79. }
  80. /**
  81. * Get page's download link
  82. *
  83. * @access protected
  84. *
  85. * @return string Link to downloadable page
  86. */
  87. protected function getPageLink()
  88. {
  89. $page1Link = '';
  90. $page2Link = '';
  91. $pageNumber = $this->piVars['page'];
  92. $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->conf['fileGrpDownload']);
  93. // Get image link.
  94. while ($fileGrpDownload = array_shift($fileGrpsDownload)) {
  95. if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$pageNumber]]['files'][$fileGrpDownload])) {
  96. $page1Link = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$pageNumber]]['files'][$fileGrpDownload]);
  97. // Get second page, too, if double page view is activated.
  98. if (
  99. $this->piVars['double']
  100. && $pageNumber < $this->doc->numPages
  101. && !empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload])
  102. ) {
  103. $page2Link = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload]);
  104. }
  105. break;
  106. }
  107. }
  108. if (
  109. empty($page1Link)
  110. && empty($page2Link)
  111. ) {
  112. Helper::devLog('File not found in fileGrps "' . $this->conf['fileGrpDownload'] . '"', DEVLOG_SEVERITY_WARNING);
  113. }
  114. // Wrap URLs with HTML.
  115. $linkConf = [
  116. 'forceAbsoluteUrl' => !empty($this->conf['forceAbsoluteUrl']) ? 1 : 0,
  117. 'forceAbsoluteUrl.' => ['scheme' => !empty($this->conf['forceAbsoluteUrl']) && !empty($this->conf['forceAbsoluteUrlHttps']) ? 'https' : 'http']
  118. ];
  119. if (!empty($page1Link)) {
  120. $linkConf['parameter'] = $page1Link;
  121. if ($this->piVars['double']) {
  122. $linkConf['title'] = $this->pi_getLL('leftPage', '');
  123. $page1Link = $this->cObj->typoLink($this->pi_getLL('leftPage', ''), $linkConf);
  124. } else {
  125. $linkConf['title'] = $this->pi_getLL('singlePage', '');
  126. $page1Link = $this->cObj->typoLink($this->pi_getLL('singlePage', ''), $linkConf);
  127. }
  128. }
  129. if (!empty($page2Link)) {
  130. $linkConf['parameter'] = $page2Link;
  131. $linkConf['title'] = $this->pi_getLL('rightPage', '');
  132. $page2Link = $this->cObj->typoLink($this->pi_getLL('rightPage', ''), $linkConf);
  133. }
  134. return $page1Link . $page2Link;
  135. }
  136. /**
  137. * Get work's download link
  138. *
  139. * @access protected
  140. *
  141. * @return string Link to downloadable work
  142. */
  143. protected function getWorkLink()
  144. {
  145. $workLink = '';
  146. $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->conf['fileGrpDownload']);
  147. // Get work link.
  148. while ($fileGrpDownload = array_shift($fileGrpsDownload)) {
  149. if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[0]]['files'][$fileGrpDownload])) {
  150. $workLink = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[0]]['files'][$fileGrpDownload]);
  151. break;
  152. } else {
  153. $details = $this->doc->getLogicalStructure($this->doc->toplevelId);
  154. if (!empty($details['files'][$fileGrpDownload])) {
  155. $workLink = $this->doc->getFileLocation($details['files'][$fileGrpDownload]);
  156. break;
  157. }
  158. }
  159. }
  160. // Wrap URLs with HTML.
  161. if (!empty($workLink)) {
  162. $linkConf = [
  163. 'parameter' => $workLink,
  164. 'forceAbsoluteUrl' => !empty($this->conf['forceAbsoluteUrl']) ? 1 : 0,
  165. 'forceAbsoluteUrl.' => ['scheme' => !empty($this->conf['forceAbsoluteUrl']) && !empty($this->conf['forceAbsoluteUrlHttps']) ? 'https' : 'http'],
  166. 'title' => $this->pi_getLL('work', '')
  167. ];
  168. $workLink = $this->cObj->typoLink($this->pi_getLL('work', ''), $linkConf);
  169. } else {
  170. Helper::devLog('File not found in fileGrp "' . $this->conf['fileGrpDownload'] . '"', DEVLOG_SEVERITY_WARNING);
  171. }
  172. return $workLink;
  173. }
  174. }