simplesearch.snippet.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * SimpleSearch snippet
  4. *
  5. * @var modX $modx
  6. * @var array $scriptProperties
  7. * @package simplesearch
  8. */
  9. require_once $modx->getOption(
  10. 'simplesearch.core_path',
  11. null,
  12. $modx->getOption('core_path') . 'components/simplesearch/'
  13. ) . 'model/simplesearch/simplesearch.class.php';
  14. $search = new SimpleSearch($modx, $scriptProperties);
  15. /* Find search index and toplaceholder setting */
  16. $searchIndex = $modx->getOption('searchIndex', $scriptProperties, 'search');
  17. $toPlaceholder = $modx->getOption('toPlaceholder', $scriptProperties, false);
  18. $noResultsTpl = $modx->getOption('noResultsTpl', $scriptProperties, 'SearchNoResults');
  19. /* Get search string */
  20. if (empty($_REQUEST[$searchIndex])) {
  21. $output = $search->getChunk($noResultsTpl, array(
  22. 'query' => '',
  23. ));
  24. return $search->output($output, $toPlaceholder);
  25. }
  26. $searchString = $search->parseSearchString($_REQUEST[$searchIndex]);
  27. if (!$searchString) {
  28. $output = $search->getChunk($noResultsTpl, array(
  29. 'query' => $searchString,
  30. ));
  31. return $search->output($output, $toPlaceholder);
  32. }
  33. /* Setup default properties. */
  34. $tpl = $modx->getOption('tpl', $scriptProperties, 'SearchResult');
  35. $containerTpl = $modx->getOption('containerTpl', $scriptProperties, 'SearchResults');
  36. $showExtract = $modx->getOption('showExtract', $scriptProperties, true);
  37. $extractSource = $modx->getOption('extractSource', $scriptProperties, 'content');
  38. $extractLength = $modx->getOption('extractLength', $scriptProperties, 200);
  39. $extractEllipsis = $modx->getOption('extractEllipsis', $scriptProperties, '...');
  40. $highlightResults = $modx->getOption('highlightResults', $scriptProperties, true);
  41. $highlightClass = $modx->getOption('highlightClass', $scriptProperties, 'simplesearch-highlight');
  42. $highlightTag = $modx->getOption('highlightTag', $scriptProperties, 'span');
  43. $perPage = $modx->getOption('perPage', $scriptProperties, 10);
  44. $pagingSeparator = $modx->getOption('pagingSeparator', $scriptProperties, ' | ');
  45. $placeholderPrefix = $modx->getOption('placeholderPrefix', $scriptProperties, 'simplesearch.');
  46. $includeTVs = $modx->getOption('includeTVs', $scriptProperties, '');
  47. $processTVs = $modx->getOption('processTVs', $scriptProperties, '');
  48. $tvPrefix = $modx->getOption('tvPrefix', $scriptProperties, '');
  49. $offsetIndex = $modx->getOption('offsetIndex', $scriptProperties, 'simplesearch_offset');
  50. $idx = isset($_REQUEST[$offsetIndex]) ? (int) $_REQUEST[$offsetIndex] + 1 : 1;
  51. $postHooks = $modx->getOption('postHooks', $scriptProperties, '');
  52. $activeFacet = $modx->getOption('facet', $_REQUEST, $modx->getOption('activeFacet', $scriptProperties, 'default'));
  53. $activeFacet = $modx->sanitizeString($activeFacet);
  54. $facetLimit = $modx->getOption('facetLimit', $scriptProperties, 5);
  55. $outputSeparator = $modx->getOption('outputSeparator', $scriptProperties, "\n");
  56. $addSearchToLink = (int) $modx->getOption('addSearchToLink', $scriptProperties, 0);
  57. $searchInLinkName = $modx->getOption('searchInLinkName', $scriptProperties, 'search');
  58. $noResults = true;
  59. /* Get results */
  60. $response = $search->getSearchResults($searchString, $scriptProperties);
  61. $placeholders = array('query' => $searchString);
  62. $resultsTpl = array('default' => array('results' => array(), 'total' => $response['total']));
  63. if (!empty($response['results'])) {
  64. /* iterate through search results */
  65. foreach ($response['results'] as $resourceArray) {
  66. $resourceArray['idx'] = $idx;
  67. if (empty($resourceArray['link'])) {
  68. $ctx = !empty($resourceArray['context_key']) ? $resourceArray['context_key'] : $modx->context->get('key');
  69. $args = '';
  70. if ($addSearchToLink) {
  71. $args = array($searchInLinkName => $searchString);
  72. }
  73. $resourceArray['link'] = $modx->makeUrl($resourceArray['id'], $ctx, $args);
  74. }
  75. if ($showExtract) {
  76. $extract = $searchString;
  77. if (array_key_exists($extractSource, $resourceArray)) {
  78. $text = $resourceArray[$extractSource];
  79. } else {
  80. $text = $modx->runSnippet($extractSource, $resourceArray);
  81. }
  82. $extract = $search->createExtract($text, $extractLength, $extract,$extractEllipsis);
  83. /* Cleanup extract */
  84. $extract = strip_tags(preg_replace("#\<!--(.*?)--\>#si", '', $extract));
  85. $extract = preg_replace("#\[\[(.*?)\]\]#si", '', $extract);
  86. $extract = str_replace(array('[[',']]'), '', $extract);
  87. $resourceArray['extract'] = !empty($highlightResults) ? $search->addHighlighting($extract, $highlightClass, $highlightTag) : $extract;
  88. }
  89. $resultsTpl['default']['results'][] = $search->getChunk($tpl, $resourceArray);
  90. $idx++;
  91. }
  92. }
  93. /* Load postHooks to get faceted results. */
  94. if (!empty($postHooks)) {
  95. $limit = !empty($facetLimit) ? $facetLimit : $perPage;
  96. $search->loadHooks('post');
  97. $search->postHooks->loadMultiple($postHooks, $response['results'],
  98. array(
  99. 'hooks' => $postHooks,
  100. 'search' => $searchString,
  101. 'offset' => !empty($_GET[$offsetIndex]) ? (int) $_GET[$offsetIndex] : 0,
  102. 'limit' => $limit,
  103. 'perPage' => $limit,
  104. )
  105. );
  106. if (!empty($search->postHooks->facets)) {
  107. foreach ($search->postHooks->facets as $facetKey => $facetResults) {
  108. if (empty($resultsTpl[$facetKey])) {
  109. $resultsTpl[$facetKey] = array();
  110. $resultsTpl[$facetKey]['total'] = $facetResults['total'];
  111. $resultsTpl[$facetKey]['results'] = array();
  112. } else {
  113. $resultsTpl[$facetKey]['total'] = $resultsTpl[$facetKey]['total'] = $facetResults['total'];
  114. }
  115. $idx = !empty($resultsTpl[$facetKey]) ? count($resultsTpl[$facetKey]['results']) + 1 : 1;
  116. foreach ($facetResults['results'] as $r) {
  117. $r['idx'] = $idx;
  118. $fTpl = !empty($scriptProperties['tpl' . $facetKey]) ? $scriptProperties['tpl' . $facetKey] : $tpl;
  119. $resultsTpl[$facetKey]['results'][] = $search->getChunk($fTpl, $r);
  120. $idx++;
  121. }
  122. }
  123. }
  124. }
  125. /* Set faceted results to placeholders for easy result positioning. */
  126. $output = array();
  127. foreach ($resultsTpl as $facetKey => $facetResults) {
  128. $resultSet = implode($outputSeparator, $facetResults['results']);
  129. $placeholders[$facetKey.'.results'] = $resultSet;
  130. $placeholders[$facetKey.'.total'] = !empty($facetResults['total']) ? $facetResults['total'] : 0;
  131. $placeholders[$facetKey.'.key'] = $facetKey;
  132. if ($placeholders[$facetKey.'.total'] !== 0) {
  133. $noResults = false;
  134. }
  135. }
  136. $placeholders['results'] = $placeholders[$activeFacet . '.results']; /* Set active facet results. */
  137. $placeholders['total'] = !empty($resultsTpl[$activeFacet]['total']) ? $resultsTpl[$activeFacet]['total'] : 0;
  138. $placeholders['page'] = isset($_REQUEST[$offsetIndex]) ? ceil((int) $_REQUEST[$offsetIndex] / $perPage) + 1 : 1;
  139. $placeholders['pageCount'] = !empty($resultsTpl[$activeFacet]['total']) ? ceil($resultsTpl[$activeFacet]['total'] / $perPage) : 1;
  140. if (!empty($placeholders['results'])) {
  141. /* add results found message */
  142. $placeholders['resultInfo'] = $modx->lexicon('simplesearch.results_found', array(
  143. 'count' => $placeholders['total'],
  144. 'text' => !empty($highlightResults) ? $search->addHighlighting($searchString, $highlightClass, $highlightTag) : $searchString,
  145. ));
  146. /* If perPage set to >0, add paging */
  147. if ($perPage > 0) {
  148. $placeholders['paging'] = $search->getPagination($searchString, $perPage, $pagingSeparator, $placeholders['total']);
  149. }
  150. }
  151. $placeholders['query'] = $searchString;
  152. $placeholders['facet'] = $activeFacet;
  153. /* output */
  154. $modx->setPlaceholder($placeholderPrefix . 'query', $searchString);
  155. $modx->setPlaceholder($placeholderPrefix . 'count', $response['total']);
  156. $modx->setPlaceholders($placeholders, $placeholderPrefix);
  157. if ($noResults) {
  158. $output = $search->getChunk($noResultsTpl, $placeholders);
  159. } else {
  160. $output = $search->getChunk($containerTpl, $placeholders);
  161. }
  162. return $search->output($output, $toPlaceholder);