tra('Custom Search'),
'documentation' => 'PluginCustomSearch',
'description' => tra('Create a custom search form for searching or listing items on the site'),
'prefs' => ['wikiplugin_customsearch', 'wikiplugin_list', 'feature_search'],
'body' => tra('LIST plugin configuration information'),
'filter' => 'wikicontent',
'profile_reference' => 'search_plugin_content',
'iconname' => 'search',
'introduced' => 8,
'tags' => ['advanced'],
'params' => [
'wiki' => [
'required' => false,
'name' => tra('Template wiki page'),
'description' => tra('Wiki page where search user interface template is found'),
'since' => '8.0',
'filter' => 'pagename',
'default' => '',
'profile_reference' => 'wiki_page',
],
'tpl' => [
'required' => false,
'name' => tra('Template file'),
'description' => tra('Smarty template (.tpl) file where search user interface template is found'),
'since' => '12.2',
'default' => '',
],
'id' => [
'required' => false,
'name' => tra('Search Id'),
'description' => tra('A unique identifier to distinguish custom searches for storing of previous search
criteria entered by users'),
'since' => '8.0',
'filter' => 'alnum',
'default' => 0,
],
'autosearchdelay' => [
'required' => false,
'name' => tra('Search Delay'),
'description' => tr('Delay in milliseconds before automatically triggering search after change
(%00%1 disables and is the default)', '', ''),
'since' => '8.0',
'filter' => 'digits',
'default' => 0,
],
'searchfadediv' => [
'required' => false,
'name' => tra('Fade DIV Id'),
'description' => tra('The specific ID of the specific div to fade out when AJAX search is in progress,
if not set will attempt to fade the whole area or if failing simply show the spinner'),
'since' => '8.0',
'filter' => 'text',
'default' => '',
],
'recalllastsearch' => [
'required' => false,
'name' => tra('Recall Last Search'),
'description' => tra('In the same session, return users to same search parameters on coming back to the
search page after leaving'),
'since' => '8.0',
'options' => [
['text' => tra(''), 'value' => ''],
['text' => tra('No'), 'value' => '0'],
['text' => tra('Yes'), 'value' => '1'],
],
'filter' => 'digits',
'default' => 0,
],
'callbackscript' => [
'required' => false,
'name' => tra('Custom JavaScript Page'),
'description' => tra('The wiki page on which custom JavaScript is to be executed on return of Ajax results'),
'since' => '8.0',
'filter' => 'pagename',
'default' => '',
],
'destdiv' => [
'required' => false,
'name' => tra('Destination Div'),
'description' => tra('The id of an existing div to contain the search results'),
'since' => '9.0',
'filter' => 'text',
'default' => '',
],
'searchonload' => [
'required' => false,
'name' => tra('Search On Load'),
'description' => tra('Execute the search when the page loads (default: Yes)'),
'since' => '9.0',
'options' => [
['text' => tra(''), 'value' => ''],
['text' => tra('No'), 'value' => '0'],
['text' => tra('Yes'), 'value' => '1'],
],
'filter' => 'digits',
'default' => 1,
],
'requireinput' => [
'required' => false,
'name' => tra('Require Input'),
'description' => tra('Require first input field to be filled for search to trigger'),
'since' => '12.0',
'options' => [
['text' => tra(''), 'value' => ''],
['text' => tra('No'), 'value' => '0'],
['text' => tra('Yes'), 'value' => '1'],
],
'filter' => 'digits',
'default' => 0,
],
'forcesortmode' => [
'required' => false,
'name' => tra('Force Sort'),
'description' => tra('Force the use of specified sort mode in place of search relevance even when there is a text search query'),
'since' => '13.0',
'options' => [
['text' => tra(''), 'value' => ''],
['text' => tra('No'), 'value' => '0'],
['text' => tra('Yes'), 'value' => '1'],
],
'filter' => 'digits',
'default' => 1,
],
'trimlinefeeds' => [
'required' => false,
'name' => tra('Trim Linefeeds'),
'description' => tra('Remove the linefeeds added after each input which casues the wiki parser to add extra paragraphs.'),
'since' => '14.1',
'options' => [
['text' => tra(''), 'value' => ''],
['text' => tra('No'), 'value' => '0'],
['text' => tra('Yes'), 'value' => '1'],
],
'filter' => 'digits',
'default' => 0,
],
'searchable_only' => [
'required' => false,
'name' => tra('Searchable Only Results'),
'description' => tra('Only include results marked as searchable in the index.'),
'since' => '14.1',
'options' => [
['text' => tra(''), 'value' => ''],
['text' => tra('No'), 'value' => '0'],
['text' => tra('Yes'), 'value' => '1'],
],
'filter' => 'digits',
'default' => 1,
],
'customsearchjs' => [
'required' => false,
'name' => tra('Use custom search JavaScript file'),
'description' => tra('Mainly keeps the search state on the URL hash, but also adds some helper functions like easier sorting and page size.'),
'since' => '14.1',
'options' => [
['text' => tra(''), 'value' => ''],
['text' => tra('No'), 'value' => '0'],
['text' => tra('Yes'), 'value' => '1'],
],
'filter' => 'digits',
'default' => 0,
],
'noajaxforbots' => [
'required' => false,
'name' => tra('No AJAX for bots'),
'description' => tra('Renders the default search results when being crawled by a bot.'),
'since' => '24.1',
'options' => [
['text' => tra(''), 'value' => ''],
['text' => tra('No'), 'value' => '0'],
['text' => tra('Yes'), 'value' => '1'],
],
'filter' => 'digits',
'default' => 0,
],
],
];
}
function wikiplugin_customsearch($data, $params)
{
global $prefs;
if ($prefs['javascript_enabled'] !== 'y') {
require_once('lib/wiki-plugins/wikiplugin_list.php');
$smarty = TikiLib::lib('smarty');
$smarty->loadPlugin('smarty_block_remarksbox');
$repeat = false;
$out = smarty_block_remarksbox(
[
'type' => 'warning',
'title' => tr('JavaScript disabled'),
],
tr('JavaScript is required for this search feature'),
$smarty,
$repeat
);
return '~np~' . $out . '~/np~' . wikiplugin_list($data, []);
}
static $instance_id = null;
if (empty($params['wiki']) && empty($params['tpl'])) {
$params['tpl'] = 'templates/search_customsearch/default_form.tpl';
} elseif (! empty($params['wiki']) && ! TikiLib::lib('tiki')->page_exists($params['wiki'])) {
$link = new WikiParser_OutputLink();
$link->setIdentifier($params['wiki']);
return tra('Template page not found') . ' ' . $link->getHtml();
}
if (isset($params['id'])) {
$id = TikiLib::remove_non_word_characters_and_accents($params['id']);
} else {
if ($instance_id === null) {
$instance_id = 0;
} else {
$instance_id++;
}
$id = (string) $instance_id;
}
if (isset($params['recalllastsearch']) && $params['recalllastsearch'] == 1 && (! isset($_REQUEST['forgetlastsearch']) || $_REQUEST['forgetlastsearch'] != 'y')) {
$recalllastsearch = 1;
} else {
$recalllastsearch = 0;
}
$defaults = [];
$plugininfo = wikiplugin_customsearch_info();
foreach ($plugininfo['params'] as $key => $param) {
$defaults["$key"] = $param['default'] ?? null;
}
$params = array_merge($defaults, $params);
if (! isset($_REQUEST["offset"])) {
$offset = 0;
} else {
$offset = (int) $_REQUEST["offset"];
}
if (isset($_REQUEST['maxRecords'])) {
$maxRecords = (int) $_REQUEST['maxRecords'];
} elseif ($recalllastsearch && ! empty($_SESSION["customsearch_$id"]['maxRecords'])) {
$maxRecords = (int) $_SESSION["customsearch_$id"]['maxRecords'];
} else {
$maxRecords = (int) $prefs['maxRecords'];
$maxDefault = true;
}
if (! empty($_REQUEST['sort_mode'])) {
$sort_mode = $_REQUEST['sort_mode'];
} elseif ($recalllastsearch && ! empty($_SESSION["customsearch_$id"]['sort_mode'])) {
$sort_mode = $_SESSION["customsearch_$id"]['sort_mode'];
} else {
$sort_mode = '';
}
$definitionKey = md5($data);
$matches = WikiParser_PluginMatcher::match($data);
$query = new Search_Query();
if (! isset($params['searchable_only']) || $params['searchable_only'] == 1) {
$query->filterIdentifier('y', 'searchable');
}
$builder = new Search_Query_WikiBuilder($query);
$builder->apply($matches);
$tsret = $builder->applyTablesorter($matches);
if (! empty($tsret['max']) || ! empty($_GET['numrows'])) {
$max = ! empty($_GET['numrows']) ? $_GET['numrows'] : $tsret['max'];
$builder->wpquery_pagination_max($query, $max);
}
$paginationArguments = $builder->getPaginationArguments();
// Use maxRecords set in LIST parameters rather then global default if set.
if (isset($maxDefault) && $maxDefault) {
if (! empty($paginationArguments['max'])) {
$maxRecords = $paginationArguments['max'];
}
}
// setup AJAX pagination
$paginationArguments['offset_jsvar'] = "customsearch_$id.offset";
$paginationArguments['sort_jsvar'] = "customsearch_$id.sort_mode";
$paginationArguments['_onclick'] = "$('#customsearch_$id').submit();return false;";
$builder = new Search_Formatter_Builder();
$builder->setId('wpcs-' . $id);
$builder->setPaginationArguments($paginationArguments);
$builder->setTsOn($tsret['tsOn']);
$facets = new Search_Query_FacetWikiBuilder();
$facets->apply($matches);
$cachelib = TikiLib::lib('cache');
$cachelib->cacheItem(
$definitionKey,
serialize(
[
'query' => $query,
'data' => $data,
'builder' => $builder,
'facets' => $facets,
'tsret' => $tsret,
]
),
'customsearch'
);
if (! empty($params['wiki'])) {
$wikitpl = "tplwiki:" . $params['wiki'];
} else {
$wikitpl = $params['tpl'];
}
$wikicontent = TikiLib::lib('smarty')->fetch($wikitpl);
TikiLib::lib('parser')->parse_wiki_argvariable($wikicontent);
$matches = WikiParser_PluginMatcher::match($wikicontent);
$fingerprint = md5($wikicontent);
$sessionprint = "customsearch_{$id}_$fingerprint";
if (isset($_SESSION[$sessionprint]) && $_SESSION[$sessionprint] != $fingerprint) {
unset($_SESSION["customsearch_$id"]);
}
$_SESSION[$sessionprint] = $fingerprint;
// important that offset from session is set after fingerprint check otherwise blank page might show
if ($recalllastsearch && ! isset($_REQUEST['offset']) && ! empty($_SESSION["customsearch_$id"]["offset"])) {
$offset = (int) $_SESSION["customsearch_$id"]["offset"];
}
$options = [
'searchfadetext' => tr('Loading...'),
'searchfadediv' => $params['searchfadediv'],
'results' => empty($params['destdiv']) ? "#customsearch_{$id}_results" : "#{$params['destdiv']}",
'autosearchdelay' => ! empty($params['autosearchdelay']) ? max(1500, (int) $params['autosearchdelay']) : 0,
'searchonload' => (int) $params['searchonload'],
'requireinput' => (bool) $params['requireinput'],
'origrequireinput' => (bool) $params['requireinput'],
'forcesortmode' => (bool) $params['forcesortmode'],
];
/**
* NOTES: Search Execution
*
* There is a global delay on execution of 1 second. This makes sure
* multiple submissions will never trigger multiple requests.
*
* There is an additional autosearchdelay configuration that can trigger the search
* on field change rather than explicit request. Explicit requests will still work.
*/
$script = "
var customsearch$id = {
options: " . json_encode($options) . ",
id: " . json_encode($id) . ",
offset: 0,
searchdata: {},
definition: " . json_encode((string) $definitionKey) . ",
autoTimeout: null,
add: function (fieldId, filter) {
this.searchdata[fieldId] = filter;
this.auto();
},
remove: function (fieldId) {
delete this.searchdata[fieldId];
this.auto();
},
load: function () {
this._executor(this);
},
auto: function () {
},
_executor: delayedExecutor(1000, function (cs) {
var selector = '#' + cs.options.searchfadediv;
if (cs.options.searchfadediv.length <= 1 && $(selector).length === 0) {
selector = '#customsearch_' + cs.id;
}
$(selector).tikiModal(cs.options.searchfadetext);
if ($(cs.options.results).length) {
var resultsTop = $(cs.options.results).offset().top;
if( resultsTop && $(window).scrollTop() > resultsTop ) {
$('html, body').animate({scrollTop: resultsTop + 'px'}, 'fast');
}
}
cs._load(function (data) {
$(selector).tikiModal();
$(cs.options.results).html(data);
$(document).trigger('pageSearchReady');
});
cs.store_query = '';
}),
init: function () {
var that = this;
if (that.options.searchonload) {
that.load();
}
if (that.options.autosearchdelay) {
that.auto = delayedExecutor(that.options.autosearchdelay, function () {
if (that.options.requireinput && (!$('#customsearch_$id').find(':text').val() || $('#customsearch_$id').find(':text').val().indexOf('...') > 0)) {
return false;
}
that.load();
});
}
}
};
$('#customsearch_$id').click(function() {
customsearch$id.offset = 0;
});
$('#customsearch_$id').submit(function() {
if (customsearch$id.options.requireinput && (!$(this).find(':text').val() || $(this).find(':text').val().indexOf('...') > 0)) {
alert(tr('Please enter a search query'));
return false;
}
if (customsearch$id.options.origrequireinput != customsearch$id.options.requireinput) {
customsearch$id.options.requireinput = customsearch$id.options.origrequireinput;
}
customsearch$id.load();
return false;
});
window.customsearch_$id = customsearch$id;
";
if (isset($_REQUEST['default']) && is_array($_REQUEST['default'])) {
$defaultRequest = $_REQUEST['default'];
} else {
$defaultRequest = [];
}
$parser = new WikiParser_PluginArgumentParser();
$dr = 0;
$configs = []; // to collect field data for noajaxforbots
foreach ($matches as $match) {
$name = $match->getName();
$arguments = $parser->parse($match->getArguments());
$key = $match->getInitialStart();
$fieldid = "customsearch_{$id}_$key";
if (isset($arguments['id'])) {
$fieldid = $arguments['id'];
}
if ($name == 'sort' && ! empty($arguments['mode']) && empty($sort_mode)) {
$sort_mode = $arguments['mode'];
$match->replaceWith('');
continue;
}
if ($defaultRequest) {
foreach ($defaultRequest as $key => $value) {
if (! empty($arguments['id']) && $key === $arguments['id']) {
$default = $value;
unset($defaultRequest[$key]);
break;
} elseif (! empty($arguments['_field']) && $key === $arguments['_field']) {
$default = $value;
unset($defaultRequest[$key]);
break;
} elseif (empty($arguments['id']) && empty($arguments['_field']) && $key === $arguments['_filter']) {
$default = $value;
unset($defaultRequest[$key]);
break;
} elseif (! empty($arguments['_group']) && $key === $arguments['_group'] && $arguments['type'] === 'radio' && $arguments['_value'] === $value) {
$default = $value;
unset($defaultRequest[$key]);
break;
} elseif (
! empty($arguments['id']) && (
! empty($defaultRequest["{$arguments['id']}_from"]) ||
! empty($defaultRequest["{$arguments['id']}_to"]) ||
! empty($defaultRequest["{$arguments['id']}_gap"])
)
) {
// defaults for date ranges with the id
$default = csGetRangeDefaults($defaultRequest, $arguments['id']);
} elseif (
! empty($arguments['_field']) && (
! empty($defaultRequest["{$arguments['_field']}_from"]) ||
! empty($defaultRequest["{$arguments['_field']}_to"]) ||
! empty($defaultRequest["{$arguments['_field']}_gap"])
)
) {
// defaults for date ranges with the field name
$default = csGetRangeDefaults($defaultRequest, $arguments['_field']);
}
}
} elseif ($recalllastsearch && isset($_SESSION["customsearch_$id"][$fieldid])) {
$default = $_SESSION["customsearch_$id"][$fieldid];
} elseif (! empty($arguments['_default'])) {
if (strpos($arguments['_default'], ',') !== false) {
$default = explode(',', $arguments['_default']);
} else {
$default = $arguments['_default'];
}
} else {
$default = '';
}
if ($name == 'categories') {
$parent = $arguments['_parent'];
if (! empty($_REQUEST['defaultcat'][$parent])) {
$default = $_REQUEST['defaultcat'][$parent];
}
}
$function = "cs_design_{$name}";
if (function_exists($function)) {
if (isset($arguments['_group'])) {
$fieldname = "customsearch_{$id}_gr" . $arguments['_group'];
} elseif (isset($arguments['_textrange'])) {
$fieldname = "customsearch_{$id}_textrange" . $arguments['_textrange'];
} elseif (isset($arguments['_daterange'])) {
$fieldname = "customsearch_{$id}_daterange" . $arguments['_daterange'];
} else {
$fieldname = $fieldid;
}
$html = $function($id, $fieldname, $fieldid, $arguments, $default, $script);
if ($params['trimlinefeeds']) {
$html = trim($html);
}
$match->replaceWith($html);
if (! empty($params['noajaxforbots'])) {
$configs[$fieldname] = [
'config' => $arguments,
'name' => $name,
];
}
}
if ($name == 'daterange') {
$dr++;
}
}
$callbackScript = null;
if (! empty($params['callbackscript']) && TikiLib::lib('tiki')->page_exists($params['callbackscript'])) {
$callbackscript_tpl = "wiki:" . $params['callbackscript'];
$callbackScript = TikiLib::lib('smarty')->fetch($callbackscript_tpl);
}
//get iconset icon if daterange is one of the fields
if ($dr) {
$smarty = TikiLib::lib('smarty');
$smarty->loadPlugin('smarty_function_js_insert_icon');
$iconinsert = smarty_function_js_insert_icon(['type' => 'jscalendar', 'return' => 'y'], $smarty->getEmptyInternalTemplate());
} else {
$iconinsert = '';
}
global $page;
$script .= "$('.icon-pdf').parent().click(function(){storeSortTable('#customsearch_" . $id . "_results',$('#customsearch_" . $id . "_results').html())});
customsearch$id._load = function (receive) {
var datamap = {
definition: this.definition,
adddata: $.toJSON(this.searchdata),
searchid: this.id,
offset: customsearch$id.offset,
maxRecords: this.maxRecords,
store_query: this.store_query,
page: " . json_encode($page) . ",
recalllastsearch: $recalllastsearch
};
if (!customsearch$id.options.forcesortmode && $('#customsearch_$id').find(':text').val() && $('#customsearch_$id').find(':text').val().indexOf('...') <= 0) {
customsearch$id.sort_mode = 'score_desc';
}
if (customsearch$id.sort_mode) {
// blank sort_mode is not allowed by Tiki input filter
datamap.sort_mode = customsearch$id.sort_mode;
}
$.ajax({
type: 'POST',
url: $.service('search_customsearch', 'customsearch'),
data: datamap,
dataType: 'html',
success: function(data) {
receive(data);
$('[data-bs-toggle=\'popover\']').attr('data-html', true);
$('[data-bs-toggle=\'popover\']').popover();
$callbackScript;
},
error: function ( jqXHR, textStatus, errorThrown ) {
var selector = '#' + customsearch$id.options.searchfadediv;
if (customsearch$id.options.searchfadediv.length <= 1 && $(selector).length === 0) {
selector = '#customsearch_$id';
}
$(selector).tikiModal();
$('#customsearch_$id').showError(jqXHR)
}
});
};
customsearch$id.sort_mode = " . json_encode($sort_mode) . ";
customsearch$id.offset = $offset;
customsearch$id.maxRecords = $maxRecords;
customsearch$id.store_query ='';
customsearch$id.init();
$iconinsert;
$(document).trigger('formSearchReady');
";
$out = '