From 436e169dde02eb41307a3d326baf544629f73a30 Mon Sep 17 00:00:00 2001 From: Peter Keung Date: Mon, 10 Nov 2014 16:19:34 -0800 Subject: [PATCH] Fix EZP-23617: Add subtree and class filters on eZ Find autocomplete --- classes/ezfindservercallfunctions.php | 37 +++++++++++++++++++ .../javascript/ezajax_autocomplete.js | 13 ++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/classes/ezfindservercallfunctions.php b/classes/ezfindservercallfunctions.php index 7861ca12..eddd3332 100644 --- a/classes/ezfindservercallfunctions.php +++ b/classes/ezfindservercallfunctions.php @@ -154,6 +154,43 @@ public static function autocomplete( $args ) $params['fq'] = 'meta_language_code_ms:(' . implode( ' OR ', $validLanguages ) . ')'; } + //build the query part for the subtree limitation + if ( isset( $args[2] ) && (int)$args[2] ) + { + if ( isset( $params['fq'] ) && $params['fq'] ) + { + $params['fq'] .= ' AND '; + } + $params['fq'] .= eZSolr::getMetaFieldName( 'path' ) . ':' . (int)$args[2]; + } + + //build the query part for the class limitation + if ( isset( $args[3] ) && $args[3] ) + { + if ( isset( $params['fq'] ) && $params['fq'] ) + { + $params['fq'] .= ' AND '; + } + $classes = explode( ',', $args[3] ); + $classQueryParts = array(); + foreach( $classes as $class ) + { + if ( is_string( $class ) ) + { + if ( $class = eZContentClass::fetchByIdentifier( $class ) ) + { + $classQueryParts[] = eZSolr::getMetaFieldName( 'contentclass_id' ) . ':' . $class->attribute( 'id' ); + } + } + else + { + $classQueryParts[] = eZSolr::getMetaFieldName( 'contentclass_id' ) . ':' . $class; + } + } + $classQueryParts = implode( ' OR ', $classQueryParts ); + $params['fq'] .= '(' . $classQueryParts . ')'; + } + $solrBase = new eZSolrBase( $fullSolrURI ); $result = $solrBase->rawSolrRequest( '/select', $params, 'json' ); diff --git a/design/standard/javascript/ezajax_autocomplete.js b/design/standard/javascript/ezajax_autocomplete.js index f4b2b267..b234fcae 100644 --- a/design/standard/javascript/ezajax_autocomplete.js +++ b/design/standard/javascript/ezajax_autocomplete.js @@ -19,6 +19,17 @@ YUI.add('ezfindautocomplete', function (Y) { source: conf.url }); + // Build Ajax request URL string, passed to AutoComplete as the requestTemplate + var urlString = "::{query}::" + conf.resultLimit + "::"; + if( typeof conf.subtree !== 'undefined' ) { + urlString += conf.subtree; + } + urlString += "::"; + if( typeof conf.classes !== 'undefined' ) { + urlString += conf.classes; + } + urlString += "?ContentType=json"; + Y.one(conf.inputSelector).plug(Y.Plugin.AutoComplete, { maxResults: conf.resultLimit, minQueryLength: conf.minQueryLength, @@ -29,7 +40,7 @@ YUI.add('ezfindautocomplete', function (Y) { }, source: ds, // This will be appended to the URL that was supplied to the DataSource's "source" config above. - requestTemplate: "::{query}::" + conf.resultLimit + "?ContentType=json", + requestTemplate: urlString, // Custom result list locator to parse the results out of the response. resultListLocator: function (response) { if (response && response[0] && response[0].responseText){