diff options
| -rw-r--r-- | ajax_search.php | 43 | ||||
| -rw-r--r-- | modules/mod_ajax_search.php | 7 | ||||
| -rwxr-xr-x | modules/mod_ajax_search.tpl | 20 |
3 files changed, 58 insertions, 12 deletions
diff --git a/ajax_search.php b/ajax_search.php new file mode 100644 index 0000000..c144b0d --- /dev/null +++ b/ajax_search.php @@ -0,0 +1,43 @@ +<?php +/** + * JSON endpoint for live search module (mod_ajax_search). + * Returns [{title, href, type}, ...] for the given ?highlight= term. + */ + +require_once '../kernel/includes/setup_inc.php'; + +use Bitweaver\Search\SearchLib; +use Bitweaver\Liberty\LibertyContent; +use Bitweaver\KernelTools; + +$gBitSystem->verifyPackage( 'search' ); + +header( 'Content-Type: application/json; charset=utf-8' ); + +$words = isset( $_REQUEST['highlight'] ) ? strip_tags( trim( $_REQUEST['highlight'] ) ) : ''; +if( strlen( $words ) < 3 ) { + echo '[]'; + exit; +} + +$_REQUEST['words'] = $words; +$_REQUEST['max_records'] = 10; +LibertyContent::prepGetList( $_REQUEST ); + +$searchlib = new SearchLib(); +$results = $searchlib->find( $_REQUEST ); + +$out = []; +foreach( $results as $r ) { + $type = ''; + if( !empty( $r['content_type_guid'] ) ) { + $type = KernelTools::tra( $gLibertySystem->getContentTypeName( $r['content_type_guid'] ) ); + } + $out[] = [ + 'title' => $r['title'] ?? '', + 'href' => $r['href'] ?? '', + 'type' => $type, + ]; +} + +echo json_encode( $out, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); diff --git a/modules/mod_ajax_search.php b/modules/mod_ajax_search.php new file mode 100644 index 0000000..d83d3ce --- /dev/null +++ b/modules/mod_ajax_search.php @@ -0,0 +1,7 @@ +<?php +/** + * @package search + * @subpackage modules + */ +// No server-side setup needed — the template loads live_search.js which +// calls ajax_search.php directly via XHR. diff --git a/modules/mod_ajax_search.tpl b/modules/mod_ajax_search.tpl index 5ab1a4b..78869fa 100755 --- a/modules/mod_ajax_search.tpl +++ b/modules/mod_ajax_search.tpl @@ -1,16 +1,12 @@ -{* this needs to go in <head>, but we don't have a way of doing that from a module yet. *} -<script src="{$smarty.const.UTIL_PKG_URL}javascript/libs/prototype.js"></script> -<script src="{$smarty.const.UTIL_PKG_URL}javascript/libs/live_search.js"></script> -<script> - var search = new LiveSearch($('search_box'), $('search_results')); -</script> -{* end of <head> section *} - +{strip} {if $gBitSystem->isPackageActive( 'search' )} {bitmodule title="$moduleTitle" name="search_new"} - {form} - <input id="search_box" type="text" name="search" value="Search..." /> - <div id="search_results"></div> - {/form} + <input id="ajax_search_box" type="text" name="search" value="{tr}search{/tr}" /> + <div id="ajax_search_results"></div> {/bitmodule} + <script src="{$smarty.const.UTIL_PKG_URL}javascript/live_search.js"></script> + <script> + new LiveSearch('#ajax_search_box', '#ajax_search_results', '{$smarty.const.SEARCH_PKG_URL}ajax_search.php'); + </script> {/if} +{/strip} |
