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.
 
 
 
 
 

159 lines
5.9 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\Page\PageRenderer;
  14. use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
  15. use TYPO3\CMS\Core\Utility\GeneralUtility;
  16. use TYPO3\CMS\Core\Utility\MathUtility;
  17. use TYPO3\CMS\Core\Utility\PathUtility;
  18. /**
  19. * SearchInDocument tool for the plugin 'Toolbox' of the 'dlf' extension
  20. *
  21. * @author Sebastian Meyer <sebastian.meyer@slub-dresden.de>
  22. * @author Alexander Bigga <alexander.bigga@slub-dresden.de>
  23. * @package TYPO3
  24. * @subpackage dlf
  25. * @access public
  26. */
  27. class SearchInDocumentTool extends \Kitodo\Dlf\Common\AbstractPlugin
  28. {
  29. public $scriptRelPath = 'Classes/Plugin/Tools/SearchInDocumentTool.php';
  30. /**
  31. * The main method of the PlugIn
  32. *
  33. * @access public
  34. *
  35. * @param string $content: The PlugIn content
  36. * @param array $conf: The PlugIn configuration
  37. *
  38. * @return string The content that is displayed on the website
  39. */
  40. public function main($content, $conf)
  41. {
  42. $this->init($conf);
  43. // Merge configuration with conf array of toolbox.
  44. if (!empty($this->cObj->data['conf'])) {
  45. $this->conf = Helper::mergeRecursiveWithOverrule($this->cObj->data['conf'], $this->conf);
  46. }
  47. $this->addSearchInDocumentJS();
  48. // Load current document.
  49. $this->loadDocument();
  50. if (
  51. $this->doc === null
  52. || $this->doc->numPages < 1
  53. || empty($this->conf['fileGrpFulltext'])
  54. || empty($this->conf['solrcore'])
  55. ) {
  56. // Quit without doing anything if required variables are not set.
  57. return $content;
  58. } else {
  59. if (!empty($this->piVars['logicalPage'])) {
  60. $this->piVars['page'] = $this->doc->getPhysicalPage($this->piVars['logicalPage']);
  61. // The logical page parameter should not appear again
  62. unset($this->piVars['logicalPage']);
  63. }
  64. // Set default values if not set.
  65. // $this->piVars['page'] may be integer or string (physical structure @ID)
  66. if (
  67. (int) $this->piVars['page'] > 0
  68. || empty($this->piVars['page'])
  69. ) {
  70. $this->piVars['page'] = MathUtility::forceIntegerInRange((int) $this->piVars['page'], 1, $this->doc->numPages, 1);
  71. } else {
  72. $this->piVars['page'] = array_search($this->piVars['page'], $this->doc->physicalStructure);
  73. }
  74. }
  75. // Quit if no fulltext file is present
  76. $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->conf['fileGrpFulltext']);
  77. while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
  78. if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$this->piVars['page']]]['files'][$fileGrpFulltext])) {
  79. $fullTextFile = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$this->piVars['page']]]['files'][$fileGrpFulltext];
  80. break;
  81. }
  82. }
  83. if (empty($fullTextFile)) {
  84. return $content;
  85. }
  86. // Load template file.
  87. $this->getTemplate();
  88. // Configure @action URL for form.
  89. $linkConf = [
  90. 'parameter' => $GLOBALS['TSFE']->id,
  91. 'forceAbsoluteUrl' => 1,
  92. 'forceAbsoluteUrl.' => ['scheme' => !empty($this->conf['forceAbsoluteUrlHttps']) ? 'https' : 'http']
  93. ];
  94. $encryptedSolr = $this->getEncryptedCoreName();
  95. // Fill markers.
  96. $markerArray = [
  97. '###ACTION_URL###' => $this->cObj->typoLink_URL($linkConf),
  98. '###LABEL_QUERY###' => htmlspecialchars($this->pi_getLL('label.query')),
  99. '###LABEL_DELETE_SEARCH###' => htmlspecialchars($this->pi_getLL('label.delete_search')),
  100. '###LABEL_LOADING###' => htmlspecialchars($this->pi_getLL('label.loading')),
  101. '###LABEL_SUBMIT###' => htmlspecialchars($this->pi_getLL('label.submit')),
  102. '###LABEL_SEARCH_IN_DOCUMENT###' => htmlspecialchars($this->pi_getLL('label.searchInDocument')),
  103. '###LABEL_NEXT###' => htmlspecialchars($this->pi_getLL('label.next')),
  104. '###LABEL_PREVIOUS###' => htmlspecialchars($this->pi_getLL('label.previous')),
  105. '###LABEL_PAGE###' => htmlspecialchars($this->pi_getLL('label.logicalPage')),
  106. '###LABEL_NORESULT###' => htmlspecialchars($this->pi_getLL('label.noresult')),
  107. '###CURRENT_DOCUMENT###' => $this->doc->uid,
  108. '###SOLR_ENCRYPTED###' => $encryptedSolr ?: '',
  109. ];
  110. $content .= $this->templateService->substituteMarkerArray($this->template, $markerArray);
  111. return $this->pi_wrapInBaseClass($content);
  112. }
  113. /**
  114. * Adds the JS files necessary for search in document
  115. *
  116. * @access protected
  117. *
  118. * @return void
  119. */
  120. protected function addSearchInDocumentJS()
  121. {
  122. $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
  123. $pageRenderer->addJsFooterFile(PathUtility::stripPathSitePrefix(ExtensionManagementUtility::extPath($this->extKey)) . 'Resources/Public/Javascript/Search/SearchInDocument.js');
  124. }
  125. /**
  126. * Get the encrypted Solr core name
  127. *
  128. * @access protected
  129. *
  130. * @return string with encrypted core name
  131. */
  132. protected function getEncryptedCoreName()
  133. {
  134. // Get core name.
  135. $name = Helper::getIndexNameFromUid($this->conf['solrcore'], 'tx_dlf_solrcores');
  136. // Encrypt core name.
  137. if (!empty($name)) {
  138. $name = Helper::encrypt($name);
  139. }
  140. return $name;
  141. }
  142. }