Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore resources with classKey "modWebLink" #49

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/components/simplesearch/docs/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Changelog for SimpleSearch.

- Added a new snippet property onlyFacet [#28]
- Add basic debug option [#27]
- Update Elastica [#33]
- Update exclude parameter to always be active and to use idType [#33]

SimpleSearch 1.9.2
========================================================================
- Generate extract for whole search string, not just for last part
Expand Down
138 changes: 103 additions & 35 deletions core/components/simplesearch/elements/snippets/simplesearch.snippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,30 @@
$searchIndex = $modx->getOption('searchIndex',$scriptProperties,'search');
$toPlaceholder = $modx->getOption('toPlaceholder',$scriptProperties,false);
$noResultsTpl = $modx->getOption('noResultsTpl',$scriptProperties,'SearchNoResults');
$debug = (bool)$modx->getOption('debug', $scriptProperties, false);

$debug_output = '';
/* get search string */
if (empty($_REQUEST[$searchIndex])) {
$output = $search->getChunk($noResultsTpl,array(
'query' => '',
));
return $search->output($output,$toPlaceholder);

if ( $debug ) {
$debug_output .= '<br>No search in the URL request for searchIndex: '.$searchIndex;
}
return $debug_output.$search->output($output,$toPlaceholder);
}
$searchString = $search->parseSearchString($_REQUEST[$searchIndex]);
if (!$searchString) {
$output = $search->getChunk($noResultsTpl,array(
'query' => $searchString,
));
return $search->output($output,$toPlaceholder);

if ( $debug ) {
$debug_output .= '<br>Search string was empty after parsing &amp; sanitizing for searchIndex: '.$searchIndex;
}
return $debug_output.$search->output($output,$toPlaceholder);
}

/* setup default properties */
Expand All @@ -75,48 +85,66 @@
$postHooks = $modx->getOption('postHooks',$scriptProperties,'');
$activeFacet = $modx->getOption('facet',$_REQUEST,$modx->getOption('activeFacet',$scriptProperties,'default'));
$activeFacet = $modx->sanitizeString($activeFacet);
$onlyFacet = $modx->getOption('onlyFacet', $scriptProperties, null);
$facetLimit = $modx->getOption('facetLimit',$scriptProperties,5);
$outputSeparator = $modx->getOption('outputSeparator',$scriptProperties,"\n");
$addSearchToLink = intval($modx->getOption('addSearchToLink',$scriptProperties,"0"));
$searchInLinkName = $modx->getOption('searchInLinkName',$scriptProperties,"search");

/* get results */
$response = $search->getSearchResults($searchString,$scriptProperties);
$placeholders = array('query' => $searchString);
$resultsTpl = array('default' => array('results' => array(),'total' => $response['total']));
if (!empty($response['results'])) {
/* iterate through search results */
foreach ($response['results'] as $resourceArray) {
$resourceArray['idx'] = $idx;
if (empty($resourceArray['link'])) {
$ctx = !empty($resourceArray['context_key']) ? $resourceArray['context_key'] : $modx->context->get('key');
$args = '';
if ($addSearchToLink) {
$args = array($searchInLinkName => $searchString);
}
$resourceArray['link'] = $modx->makeUrl($resourceArray['id'],$ctx,$args);
if ( !is_null($onlyFacet) && $onlyFacet != 'default' ) {
$activeFacet = $onlyFacet;
} else {
$response = $search->getSearchResults($searchString, $scriptProperties);
$placeholders = array('query' => $searchString);
$resultsTpl = array('default' => array('results' => array(), 'total' => $response['total']));
if (!empty($response['results'])) {
if ($debug) {
$debug_output .= '<br>Begin iterate through search results';
}
if ($showExtract) {
$extract = $searchString;
if (array_key_exists($extractSource, $resourceArray)) {
$text = $resourceArray[$extractSource];
} else {
$text = $modx->runSnippet($extractSource, $resourceArray);
/* iterate through search results */
foreach ($response['results'] as $resourceArray) {
$resourceArray['idx'] = $idx;
if ($debug) {
$debug_output .= '<br>Search found resource ID: ' . $resourceArray['id'];
}
$extract = $search->createExtract($text,$extractLength,$extract,$extractEllipsis);
/* cleanup extract */
$extract = strip_tags(preg_replace("#\<!--(.*?)--\>#si",'',$extract));
$extract = preg_replace("#\[\[(.*?)\]\]#si",'',$extract);
$extract = str_replace(array('[[',']]'),'',$extract);
$resourceArray['extract'] = !empty($highlightResults) ? $search->addHighlighting($extract,$highlightClass,$highlightTag) : $extract;
if (empty($resourceArray['link'])) {
$ctx = !empty($resourceArray['context_key']) ? $resourceArray['context_key'] : $modx->context->get('key');
$args = '';
if ($addSearchToLink) {
$args = array($searchInLinkName => $searchString);
}
$resourceArray['link'] = $modx->makeUrl($resourceArray['id'], $ctx, $args);
}
if ($showExtract) {
$extract = $searchString;
if (array_key_exists($extractSource, $resourceArray)) {
$text = $resourceArray[$extractSource];
} else {
$text = $modx->runSnippet($extractSource, $resourceArray);
}
$extract = $search->createExtract($text, $extractLength, $extract, $extractEllipsis);
/* cleanup extract */
$extract = strip_tags(preg_replace("#\<!--(.*?)--\>#si", '', $extract));
$extract = preg_replace("#\[\[(.*?)\]\]#si", '', $extract);
$extract = str_replace(array('[[', ']]'), '', $extract);
$resourceArray['extract'] = !empty($highlightResults) ? $search->addHighlighting($extract, $highlightClass, $highlightTag) : $extract;
}
$resultsTpl['default']['results'][] = $search->getChunk($tpl, $resourceArray);
$idx++;
}
} else {
if ($debug) {
$debug_output .= '<br>No search results for search term';
}
$resultsTpl['default']['results'][] = $search->getChunk($tpl,$resourceArray);
$idx++;
}
}

/* load postHooks to get faceted results */
$isFacetResults = false;
if (!empty($postHooks)) {
if ($debug) {
$debug_output .= '<br>Post hooks found';
}
$limit = !empty($facetLimit) ? $facetLimit : $perPage;
$search->loadHooks('post');
$search->postHooks->loadMultiple($postHooks,$response['results'],array(
Expand All @@ -128,23 +156,40 @@
));
if (!empty($search->postHooks->facets)) {
foreach ($search->postHooks->facets as $facetKey => $facetResults) {

if ($debug) {
$debug_output .= '<br>Facet key: '.$facetKey;
}
if (empty($resultsTpl[$facetKey])) {
$resultsTpl[$facetKey] = array();
$resultsTpl[$facetKey]['total'] = $facetResults['total'];
$resultsTpl[$facetKey]['results'] = array();
if ($debug) {
$debug_output .= ' - results have not yet been added';
}
} else {
$resultsTpl[$facetKey]['total'] = $resultsTpl[$facetKey]['total'] = $facetResults['total'];
$resultsTpl[$facetKey]['total'] = $facetResults['total'];
if ($debug) {
$debug_output .= ' - results have already been added: '.$resultsTpl[$facetKey]['total'];
}
}

$idx = !empty($resultsTpl[$facetKey]) ? count($resultsTpl[$facetKey]['results'])+1 : 1;
foreach ($facetResults['results'] as $r) {
if ($debug) {
$debug_output .= '<br>'.$facetKey.' results # '.$idx;
}
$r['idx'] = $idx;
$fTpl = !empty($scriptProperties['tpl'.$facetKey]) ? $scriptProperties['tpl'.$facetKey] : $tpl;
$resultsTpl[$facetKey]['results'][] = $search->getChunk($fTpl,$r);
$idx++;
}
}
}
} else {
if ($debug) {
$debug_output .= '<br>No post hooks found';
}
}

/* set faceted results to placeholders for easy result positioning */
Expand All @@ -154,13 +199,25 @@
$placeholders[$facetKey.'.results'] = $resultSet;
$placeholders[$facetKey.'.total'] = !empty($facetResults['total']) ? $facetResults['total'] : 0;
$placeholders[$facetKey.'.key'] = $facetKey;
if ( $placeholders[$facetKey.'.total'] > 0 ) {
$isFacetResults = true;
}
}
if ($debug) {
$debug_output .= '<br>Active facet: '.$activeFacet;
}
$placeholders['results'] = $placeholders[$activeFacet.'.results']; /* set active facet results */
$placeholders['total'] = !empty($resultsTpl[$activeFacet]['total']) ? $resultsTpl[$activeFacet]['total'] : 0;
$placeholders['page'] = isset($_REQUEST[$offsetIndex]) ? ceil(intval($_REQUEST[$offsetIndex]) / $perPage) + 1 : 1;
$placeholders['pageCount'] = !empty($resultsTpl[$activeFacet]['total']) ? ceil($resultsTpl[$activeFacet]['total'] / $perPage) : 1;
if ($debug) {
$debug_output .= '<br>Active facet total: '.$placeholders['total'].' Page: '.$placeholders['page'].' Page count: '.$placeholders['page'];
}

if (!empty($response['results'])) {
if (!empty($response['results']) || $isFacetResults ) {
if ($debug) {
$debug_output .= '<br>Results found for simple search, add highlighting and pagination';
}
/* add results found message */
$placeholders['resultInfo'] = $modx->lexicon('sisea.results_found',array(
'count' => $placeholders['total'],
Expand All @@ -170,6 +227,10 @@
if ($perPage > 0) {
$placeholders['paging'] = $search->getPagination($searchString,$perPage,$pagingSeparator,$placeholders['total']);
}
} else {
if ($debug) {
$debug_output .= '<br> No Results found for simple search';
}
}
$placeholders['query'] = $searchString;
$placeholders['facet'] = $activeFacet;
Expand All @@ -178,11 +239,18 @@
$modx->setPlaceholder($placeholderPrefix.'query',$searchString);
$modx->setPlaceholder($placeholderPrefix.'count',$response['total']);
$modx->setPlaceholders($placeholders,$placeholderPrefix);
if (empty($response['results'])) {

if (empty($response['results']) && !$isFacetResults ) {
$output = $search->getChunk($noResultsTpl,array(
'query' => $searchString,
));
if ($debug) {
$debug_output .= '<br>No results send to: '.$noResultsTpl.' Chunk';
}
} else {
if ($debug) {
$debug_output .= '<br>Results found send to: '.$containerTpl.' Chunk';
}
$output = $search->getChunk($containerTpl,$placeholders);
}
return $search->output($output,$toPlaceholder);
return $debug_output.$search->output($output,$toPlaceholder);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Elastica;

use Elastica\Script\AbstractScript as BaseAbstractScript;

/**
* Kept for BC reasons.
*
* @deprecated Use \Elastica\Script\AbstractScript instead.
*/
abstract class AbstractScript extends BaseAbstractScript
{
}
Loading