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.
 
 
 
 
 

213 lines
9.5 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 Kitodo\Dlf\Common\Helper;
  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 'Page Grid' for the 'dlf' extension
  19. *
  20. * @author Henrik Lochmann <dev@mentalmotive.com>
  21. * @author Sebastian Meyer <sebastian.meyer@slub-dresden.de>
  22. * @package TYPO3
  23. * @subpackage dlf
  24. * @access public
  25. */
  26. class PageGrid extends \Kitodo\Dlf\Common\AbstractPlugin
  27. {
  28. public $scriptRelPath = 'Classes/Plugin/PageGrid.php';
  29. /**
  30. * Renders entry for one page of the current document.
  31. *
  32. * @access protected
  33. *
  34. * @param int $number: The page to render
  35. * @param string $template: Parsed template subpart
  36. *
  37. * @return string The rendered entry ready for output
  38. */
  39. protected function getEntry($number, $template)
  40. {
  41. // Set current page if applicable.
  42. if (!empty($this->piVars['page']) && $this->piVars['page'] == $number) {
  43. $markerArray['###STATE###'] = 'cur';
  44. } else {
  45. $markerArray['###STATE###'] = 'no';
  46. }
  47. // Set page number.
  48. $markerArray['###NUMBER###'] = $number;
  49. // Set pagination.
  50. $markerArray['###PAGINATION###'] = htmlspecialchars($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$number]]['orderlabel']);
  51. // Get thumbnail or placeholder.
  52. $fileGrpsThumb = GeneralUtility::trimExplode(',', $this->conf['fileGrpThumbs']);
  53. if (array_intersect($fileGrpsThumb, array_keys($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$number]]['files'])) !== [] ) {
  54. while ($fileGrpThumb = array_shift($fileGrpsThumb)) {
  55. if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$number]]['files'][$fileGrpThumb])) {
  56. $thumbnailFile = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$number]]['files'][$fileGrpThumb]);
  57. break;
  58. }
  59. }
  60. } elseif (!empty($this->conf['placeholder'])) {
  61. $thumbnailFile = $GLOBALS['TSFE']->tmpl->getFileName($this->conf['placeholder']);
  62. } else {
  63. $thumbnailFile = PathUtility::stripPathSitePrefix(ExtensionManagementUtility::extPath($this->extKey)) . 'Resources/Public/Images/PageGridPlaceholder.jpg';
  64. }
  65. $thumbnail = '<img alt="' . $markerArray['###PAGINATION###'] . '" src="' . $thumbnailFile . '" />';
  66. // Get new plugin variables for typolink.
  67. $piVars = $this->piVars;
  68. // Unset no longer needed plugin variables.
  69. // unset($piVars['pagegrid']) is for DFG Viewer compatibility!
  70. unset($piVars['pointer'], $piVars['DATA'], $piVars['pagegrid']);
  71. $piVars['page'] = $number;
  72. $linkConf = [
  73. 'useCacheHash' => 1,
  74. 'parameter' => $this->conf['targetPid'],
  75. 'forceAbsoluteUrl' => !empty($this->conf['forceAbsoluteUrl']) ? 1 : 0,
  76. 'forceAbsoluteUrl.' => ['scheme' => !empty($this->conf['forceAbsoluteUrl']) && !empty($this->conf['forceAbsoluteUrlHttps']) ? 'https' : 'http'],
  77. 'additionalParams' => GeneralUtility::implodeArrayForUrl($this->prefixId, $piVars, '', true, false),
  78. 'title' => $markerArray['###PAGINATION###']
  79. ];
  80. $markerArray['###THUMBNAIL###'] = $this->cObj->typoLink($thumbnail, $linkConf);
  81. return $this->templateService->substituteMarkerArray($template, $markerArray);
  82. }
  83. /**
  84. * Renders the page browser
  85. *
  86. * @access protected
  87. *
  88. * @return string The rendered page browser ready for output
  89. */
  90. protected function getPageBrowser()
  91. {
  92. // Get overall number of pages.
  93. $maxPages = intval(ceil($this->doc->numPages / $this->conf['limit']));
  94. // Return empty pagebrowser if there is just one page.
  95. if ($maxPages < 2) {
  96. return '';
  97. }
  98. // Get separator.
  99. $separator = htmlspecialchars($this->pi_getLL('separator', ' - '));
  100. // Add link to previous page.
  101. if ($this->piVars['pointer'] > 0) {
  102. $output = $this->pi_linkTP_keepPIvars(htmlspecialchars($this->pi_getLL('prevPage', '<')), ['pointer' => $this->piVars['pointer'] - 1, 'page' => (($this->piVars['pointer'] - 1) * $this->conf['limit']) + 1], true) . $separator;
  103. } else {
  104. $output = '<span class="prev-page not-active">' . htmlspecialchars($this->pi_getLL('prevPage', '<')) . '</span>' . $separator;
  105. }
  106. $i = 0;
  107. // Add links to pages.
  108. while ($i < $maxPages) {
  109. if ($i < 3 || ($i > $this->piVars['pointer'] - 3 && $i < $this->piVars['pointer'] + 3) || $i > $maxPages - 4) {
  110. if ($this->piVars['pointer'] != $i) {
  111. $output .= $this->pi_linkTP_keepPIvars(htmlspecialchars(sprintf($this->pi_getLL('page', '%d'), $i + 1)), ['pointer' => $i, 'page' => ($i * $this->conf['limit']) + 1], true) . $separator;
  112. } else {
  113. $output .= '<span class="active">' . htmlspecialchars(sprintf($this->pi_getLL('page', '%d'), $i + 1)) . '</span>' . $separator;
  114. }
  115. $skip = true;
  116. } elseif ($skip == true) {
  117. $output .= '<span class="skipped">' . htmlspecialchars($this->pi_getLL('skip', '...')) . '</span>' . $separator;
  118. $skip = false;
  119. }
  120. $i++;
  121. }
  122. // Add link to next page.
  123. if ($this->piVars['pointer'] < $maxPages - 1) {
  124. $output .= $this->pi_linkTP_keepPIvars(htmlspecialchars($this->pi_getLL('nextPage', '>')), ['pointer' => $this->piVars['pointer'] + 1, 'page' => ($this->piVars['pointer'] + 1) * $this->conf['limit'] + 1], true);
  125. } else {
  126. $output .= '<span class="next-page not-active">' . htmlspecialchars($this->pi_getLL('nextPage', '>')) . '</span>';
  127. }
  128. return $output;
  129. }
  130. /**
  131. * The main method of the PlugIn
  132. *
  133. * @access public
  134. *
  135. * @param string $content: The PlugIn content
  136. * @param array $conf: The PlugIn configuration
  137. *
  138. * @return string The content that is displayed on the website
  139. */
  140. public function main($content, $conf)
  141. {
  142. $this->init($conf);
  143. $this->loadDocument();
  144. if (
  145. $this->doc === null
  146. || $this->doc->numPages < 1
  147. || empty($this->conf['fileGrpThumbs'])
  148. ) {
  149. // Quit without doing anything if required variables are not set.
  150. return $content;
  151. } else {
  152. // Set default values for page if not set.
  153. $this->piVars['pointer'] = MathUtility::forceIntegerInRange($this->piVars['pointer'], 0, $this->doc->numPages, 0);
  154. }
  155. // Load template file.
  156. $this->getTemplate();
  157. $entryTemplate = $this->templateService->getSubpart($this->template, '###ENTRY###');
  158. if (empty($entryTemplate)) {
  159. Helper::devLog('No template subpart for list entry found', DEVLOG_SEVERITY_WARNING);
  160. // Quit without doing anything if required variables are not set.
  161. return $content;
  162. }
  163. if (!empty($this->piVars['logicalPage'])) {
  164. $this->piVars['page'] = $this->doc->getPhysicalPage($this->piVars['logicalPage']);
  165. // The logical page parameter should not appear
  166. unset($this->piVars['logicalPage']);
  167. }
  168. // Set some variable defaults.
  169. // $this->piVars['page'] may be integer or string (physical structure @ID)
  170. if (
  171. (int) $this->piVars['page'] > 0
  172. || empty($this->piVars['page'])
  173. ) {
  174. $this->piVars['page'] = MathUtility::forceIntegerInRange((int) $this->piVars['page'], 1, $this->doc->numPages, 1);
  175. } else {
  176. $this->piVars['page'] = array_search($this->piVars['page'], $this->doc->physicalStructure);
  177. }
  178. if (!empty($this->piVars['page'])) {
  179. $this->piVars['pointer'] = intval(floor(($this->piVars['page'] - 1) / $this->conf['limit']));
  180. }
  181. if (
  182. !empty($this->piVars['pointer'])
  183. && (($this->piVars['pointer'] * $this->conf['limit']) + 1) <= $this->doc->numPages
  184. ) {
  185. $this->piVars['pointer'] = max(intval($this->piVars['pointer']), 0);
  186. } else {
  187. $this->piVars['pointer'] = 0;
  188. }
  189. // Iterate through visible page set and display thumbnails.
  190. for ($i = $this->piVars['pointer'] * $this->conf['limit'], $j = ($this->piVars['pointer'] + 1) * $this->conf['limit']; $i < $j; $i++) {
  191. // +1 because page counting starts at 1.
  192. $number = $i + 1;
  193. if ($number > $this->doc->numPages) {
  194. break;
  195. } else {
  196. $content .= $this->getEntry($number, $entryTemplate);
  197. }
  198. }
  199. // Render page browser.
  200. $markerArray['###PAGEBROWSER###'] = $this->getPageBrowser();
  201. // Merge everything with template.
  202. $content = $this->templateService->substituteMarkerArray($this->templateService->substituteSubpart($this->template, '###ENTRY###', $content, true), $markerArray);
  203. return $this->pi_wrapInBaseClass($content);
  204. }
  205. }