diff --git a/orcinus/js/search.js b/orcinus/js/search.js index 383db28..71bfb2a 100644 --- a/orcinus/js/search.js +++ b/orcinus/js/search.js @@ -21,8 +21,22 @@ $('input.os_typeahead').attr('autocomplete', 'off').typeahead({ source: os_bloodhound, display: 'title' }).bind('typeahead:selected', function (obj, datum) { - if (typeof os_odata.jw_depth == 'string') - datum.url = datum.url.replace(/^\//, os_odata.jw_depth); - window.location.href = datum.url; + // We are offline + if (typeof os_odata != 'undefined' && typeof os_odata.jw_depth == 'string') { + window.location.href = datum.url.replace(/^\//, os_odata.jw_depth); + + // Else we are online + } else { + + // On user click of a search suggestion, add this search to the + // query log + fetch(new Request(window.location.origin + window.location.pathname), { + method: 'POST', + headers: { 'Content-type': 'application/json' }, + body: JSON.stringify({ q: datum.query, log: 'log' }) + }) + .then((response) => response.text()) + .then((data) => { window.location.href = datum.url; }); + } }); \ No newline at end of file diff --git a/orcinus/search.php b/orcinus/search.php index 586a2c0..5d71f0d 100644 --- a/orcinus/search.php +++ b/orcinus/search.php @@ -21,6 +21,19 @@ $_SDATA = array( ); +/* ***** Handle POST Requests ************************************** */ +unset($_REQUEST['log']); +if ($_SERVER['REQUEST_METHOD'] == 'POST') { + + // JSON POST request + // These are usually sent by javascript fetch() + if ($_SERVER['CONTENT_TYPE'] == 'application/json') { + $postBody = file_get_contents('php://input'); + $_REQUEST = json_decode($postBody, true); + } +} + + foreach ($_ODATA['s_weights'] as $key => $weight) $_ODATA['s_weights'][$key] = (float)$weight; @@ -656,6 +669,8 @@ if ($_RDATA['s_searchable_pages']) { $result['url'] = preg_replace($repStr, '', $result['url']); // Highlight the terms in the title, url and matchtext + $_RESULT->query = $_REQUEST['q']; + $_RESULT->title = $result['title']; $_RESULT->url = $result['url']; $_RESULT->matchtext = $result['matchtext']; @@ -833,9 +848,10 @@ if ($_ODATA['sp_interval'] && !$_ODATA['sp_crawling'] && } } - // Output JSON and exit if requested if (isset($_REQUEST['json'])) { header('Content-type: application/json; charset='.$_ODATA['s_charset']); die(json_encode($_SDATA['json'], JSON_INVALID_UTF8_IGNORE)); -} ?> \ No newline at end of file + +// Just log this query and do not post results +} else if (isset($_REQUEST['log'])) die(); ?> \ No newline at end of file